Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

Seoexpert_model   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 209
Duplicated Lines 19.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 40
loc 209
rs 10
wmc 19
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deinstall() 0 7 1
A getBaseSettings() 0 15 4
A getCategoriesByIdName() 0 18 2
A getLangIdByLocale() 13 13 3
A getSettings() 10 10 2
B install() 0 35 1
B setBaseSettings() 0 33 3
A setSettings() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @property CI_DB_active_record $db
5
 * @property DX_Auth $dx_auth
6
 */
7
class Seoexpert_model extends CI_Model
8
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
9
10
    /**
11
     * Seoexpert_model constructor.
12
     */
13
    public function __construct() {
14
15
        parent::__construct();
16
    }
17
18
    /**
19
     * Deinstall
20
     * @return boolean|null
0 ignored issues
show
introduced by
@return doc comment specified, but function has no return statement
Loading history...
21
     */
22
    public function deinstall() {
23
24
        $this->load->dbforge();
25
        $this->dbforge->drop_table('mod_seo');
26
        $this->dbforge->drop_table('mod_seo_products');
27
        $this->dbforge->drop_table('mod_seo_inflect');
28
    }
29
30
    /**
31
     * Get base settings
32
     * @param bool|int $langId
33
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
34
     */
35
    public function getBaseSettings($langId = false) {
36
37
        if (!$langId || is_numeric($langId) === false) {
38
            return false;
39
        }
40
        $settings = $this->db->select('add_site_name, add_site_name_to_cat, delimiter, create_keywords, create_description')->where('s_name', 'main')->get('settings')->row_array();
41
        $settingsIn = $this->db->where('lang_ident', $langId)->get('settings_i18n')->row_array();
42
43
        $res = array_merge($settings, $settingsIn);
44
45
        if ($res) {
46
            return $res;
47
        }
48
        return false;
49
    }
50
51
    /**
52
     * Get categories by ID, NAME
53
     * @param string  $term  id,name
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
54
     * @param integer $limit limit for results
55
     * @return boolean|array
56
     */
57
    public function getCategoriesByIdName($term, $limit = 7) {
58
59
        $locale = MY_Controller::defaultLocale();
60
61
        $sql = "SELECT *
62
                FROM  `shop_category_i18n`
63
                WHERE  (`locale` =  '$locale'
64
                AND  `name` LIKE  '%$term%')
65
                OR (`id` LIKE '%$term%' AND `locale` =  '$locale')
66
                LIMIT 0 , $limit";
67
        $query = $this->db->query($sql);
68
69
        if ($query) {
70
            return $query->result_array();
71
        } else {
72
            return false;
73
        }
74
    }
75
76
     /**
77
     * Get language id
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
78
     * @param bool|string $locale
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
79
     * @return bool|int
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
80
     */
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
81 View Code Duplication
    public function getLangIdByLocale($locale = false) {
82
83
        if (!$locale) {
84
            $locale = \MY_Controller::getCurrentLocale();
85
        }
86
87
        $res = $this->db->select('id')->where('identif', $locale)->get('languages')->row_array();
88
        if ($res) {
89
            return $res['id'];
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * Get module settings
97
     * @param string $locale
98
     * @return array
99
     */
100 View Code Duplication
    public function getSettings($locale = 'ru') {
101
102
        $settings = $this->db->select('settings')
103
            ->where('locale', $locale)
104
            ->get('mod_seo')
105
            ->row_array();
106
107
        $settings = unserialize($settings['settings']);
108
        return $settings ?: false;
109
    }
110
111
    /**
112
     * Install
113
     * @return boolean
114
     */
115
    public function install() {
116
117
        $sql = 'CREATE TABLE IF NOT EXISTS `mod_seo` (
118
                    `id` int(11) NOT NULL AUTO_INCREMENT,
119
                    `locale` varchar(5) DEFAULT NULL,
120
                    `settings` text DEFAULT NULL,
121
                    PRIMARY KEY (`id`)
122
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;';
123
124
        $this->db->query($sql);
125
126
        $sql = 'CREATE TABLE IF NOT EXISTS `mod_seo_products` (
127
                    `id` int(11) NOT NULL AUTO_INCREMENT,
128
                    `cat_id` int(11) NOT NULL,
129
                    `locale` varchar(5) DEFAULT NULL,
130
                    `settings` text DEFAULT NULL,
131
                    `active` tinyint(4) DEFAULT NULL,
132
                    `empty_meta` int(11) DEFAULT NULL,
133
                    PRIMARY KEY (`id`)
134
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;';
135
136
        $this->db->query($sql);
137
138
        $sql = 'CREATE TABLE IF NOT EXISTS `mod_seo_inflect` (
139
                `id` int(11) NOT NULL AUTO_INCREMENT,
140
                `original` varchar(250) NOT NULL,
141
                `inflection_id` int(11) NOT NULL,
142
                `inflected` varchar(250) NOT NULL,
143
                PRIMARY KEY (`id`)
144
              ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;';
145
        $this->db->query($sql);
146
147
        $this->db->where('name', 'mod_seo')->update('components', ['autoload' => 1, 'in_menu' => 1]);
148
        return true;
149
    }
150
151
    /**
152
     * Set Base settings
153
     * @param bool|int $langId
154
     * @param array    $settings
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
155
     * @return bool
156
     */
157
    public function setBaseSettings($langId = false, $settings) {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
158
159
        if (!$langId) {
160
            return false;
161
        }
162
        // Update shop settings table
163
        $mainSettings = [
164
            'add_site_name' => $settings['add_site_name'],
165
            'add_site_name_to_cat' => $settings['add_site_name_to_cat'],
166
            'delimiter' => $settings['delimiter'],
167
            'create_keywords' => $settings['create_keywords'],
168
            'create_description' => $settings['create_description']
169
        ];
170
        $this->db->where('s_name', 'main')->update('settings', $mainSettings);
171
172
        //Check exists settings with current langId
173
        $checkLangSettings = $this->db->where('lang_ident', $langId)->get('settings_i18n')->row_array();
174
175
        $mainSettingsIn = [
176
            'name' => $settings['name'],
177
            'short_name' => $settings['short_name'],
178
            'description' => $settings['description'],
179
            'keywords' => $settings['keywords']
180
        ];
181
182
        // Update or insert shop settings I18n table
183
        if ($checkLangSettings) {
184
            return $this->db->where('lang_ident', $langId)->update('settings_i18n', $mainSettingsIn);
185
        } else {
186
            $mainSettingsIn['lang_ident'] = $langId;
187
            return $this->db->insert('settings_i18n', $mainSettingsIn);
188
        }
189
    }
190
191
    /**
192
     * Save settings
193
     * @param array $settings
194
     * @param string $locale
195
     * @return bool
196
     */
197 View Code Duplication
    public function setSettings($settings, $locale = 'ru') {
198
199
        $data = $this->db->select('locale')
200
            ->where('locale', $locale)
201
            ->get('mod_seo')
202
            ->row_array();
203
        if (empty($data)) {
204
            return $this->db->insert('mod_seo', ['locale' => $locale, 'settings' => serialize($settings)]);
205
        }
206
207
        return $this->db->where('locale', $locale)
208
            ->update(
209
                'mod_seo',
210
                ['settings' => serialize($settings)
211
                ]
212
            );
213
    }
214
215
}