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

Cms_admin::delete_lang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * @property Cms_base $cms_base
9
 */
10
class Cms_admin extends CI_Model
11
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
12
13
    /*     * ***********************************************************
14
     * 	Pages
15
     * ********************************************************** */
16
17
    /**
18
     * Add page into content table
19
     *
20
     * @param array $data
21
     * @return integer
22
     */
23
    public function add_page($data) {
24
25
        $this->db->limit(1);
26
        if (!$data['comments_count']) {
27
            $data['comments_count'] = 0;
28
        }
29
        if (!$data['position']) {
30
            $data['position'] = 0;
31
        }
32
        if (!$data['updated']) {
33
            $data['updated'] = 0;
34
        }
35
        if (!$data['showed']) {
36
            $data['showed'] = 0;
37
        }
38
        if (!$data['lang_alias']) {
39
            $data['lang_alias'] = 0;
40
        }
41
        $this->db->insert('content', $data);
42
43
        return $this->db->insert_id();
44
    }
45
46
    /**
47
     * Select page by id and lang_id
48
     *
49
     * @param int $id
50
     * @param int $lang
51
     * @return array
52
     */
53
    public function get_page_by_lang($id, $lang = 0) {
54
55
        $this->db->where('id', $id);
56
        $this->db->where('lang', $lang);
57
        $query = $this->db->get('content', 1);
58
59
        if ($query->num_rows == 1) {
60
            return $query->row_array();
61
        }
62
63
        return FALSE;
64
    }
65
66
    /**
67
     * Select page by id
68
     *
69
     * @param integer $id
70
     * @return array
71
     */
72
    public function get_page($id) {
73
74
        $this->db->where('id', $id);
75
        $query = $this->db->get('content', 1);
76
77
        if ($query->num_rows > 0) {
78
            return $query->row_array();
79
        }
80
81
        return FALSE;
82
    }
83
84
    /**
85
     * @param integer $id
86
     * @return bool
87
     */
88
    public function page_exists($id) {
89
90
        $this->db->select('id');
91
        $this->db->where('id', $id);
92
        $query = $this->db->get('content', 1);
93
94
        return $query->num_rows == 1;
95
    }
96
97
    /**
98
     * @param $id
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
99
     * @param $data
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
100
     * @param bool $exists
101
     * @return bool
102
     */
103
    public function update_page($id, $data , $exists = false) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between argument "$data" and comma; 1 found
Loading history...
104
105
        $lang_id = $this->input->post('lang_id');
106
        $pageExists = (int) $this->input->post('pageExists');
107
108
        if (!$pageExists && $exists == false) {
109
            unset($data['id']);
110
            $data['lang_alias'] = $id;
111
            $data['lang'] = $lang_id;
112
            $id = $this->add_page($data);
113
            $inserted = $id ? true : false;
114
115
        }
116
117
        $page = $this->get_page($id);
118
        $alias = $page['lang_alias'];
119
120
        if ($alias == 0) {
121
            $this->db->where('lang_alias', $page['id']);
122
            $this->db->update('content', ['post_status' => $data['post_status'], 'category' => $data['category'], 'cat_url' => $data['cat_url'], 'url' => $data['url']]);
123
        } else {
124
            $page = $this->get_page($alias);
125
            $this->db->where('lang_alias', $page['id']);
126
            $this->db->update('content', ['post_status' => $data['post_status'], 'category' => $data['category'], 'cat_url' => $data['cat_url']]);
127
128
            $this->db->where('id', $alias);
129
            $this->db->update('content', ['post_status' => $data['post_status'], 'category' => $data['category'], 'cat_url' => $data['cat_url']]);
130
131
            $data['url'] = $page['url'];
132
        }
133
134
        $this->db->where('id', $id);
135
        $this->db->update('content', $data);
136
137
        $affectedRows = $this->db->affected_rows();
138
        return ($affectedRows || $inserted) ? $id : false;
139
    }
140
141
    /**
142
     * Creates new category
143
     *
144
     * @param array $data
145
     * @return int
146
     */
147
    public function create_category($data) {
148
149
        $this->db->insert('category', $data);
150
151
        return $this->db->insert_id();
152
    }
153
154
    /**
155
     * Update category data
156
     *
157
     * @access public
158
     * @param array $data
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
159
     * @param int $id
160
     */
161
    public function update_category($data, $id) {
162
163
        $this->db->where('id', $id);
164
        $this->db->update('category', $data);
165
    }
166
167
    /**
168
     * Select all categories
169
     *
170
     * @access public
171
     * @return array
172
     */
173
    public function get_categories() {
174
175
        return $this->cms_base->get_categories();
176
    }
177
178
    /**
179
     * Get category by id
180
     * @param int $id
181
     * @return bool|array
182
     */
183 View Code Duplication
    public function get_category($id) {
184
185
        $this->db->where('id', $id);
186
        $query = $this->db->get('category', 1);
187
188
        if ($query->num_rows() > 0) {
189
            return $query->row_array();
190
        }
191
192
        return FALSE;
193
    }
194
195
    /**
196
     * Check if category is created
197
     *
198
     * @access public
199
     * @param string $url
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
200
     * @return bool
201
     */
202
    public function is_category($url) {
203
204
        $this->db->where('url', $url);
205
        $query = $this->db->get('category', 1);
206
207
        return $query->num_rows == 1;
208
    }
209
210
    /*     * ***********************************************************
211
     * 	Settings
212
     * ********************************************************** */
213
214
    /**
215
     *    Save settings
0 ignored issues
show
introduced by
Function comment short description must start with exactly one space
Loading history...
216
     *
217
     * @settings array
218
     * @access public
219
     * @param array $settings
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
220
     */
221
    public function save_settings($settings) {
222
223
        $this->db->where('s_name', 'main');
224
        $this->db->update('settings', $settings);
225
    }
226
227
    /**
228
     * Selecting main settings
229
     *
230
     * @access public
231
     * @return array
232
     */
233
    public function get_settings() {
234
235
        return $this->cms_base->get_settings();
236
    }
237
238
    /**
239
     * Get editor theme
240
     *
241
     * @access public
242
     * @return string
243
     */
244
    public function editor_theme() {
245
246
        $this->db->select('editor_theme');
247
        $this->db->where('s_name', 'main');
248
        $query = $this->db->get('settings', 1);
249
250
        return $query->row_array();
251
    }
252
253
    /*     * ***********************************************************
254
     * 	Languages
255
     * ********************************************************** */
256
257
    /**
258
     * Add page into content table
259
     *
260
     * @param array $data
261
     * @return int
262
     */
263
    public function insert_lang($data) {
264
265
        $this->db->insert('languages', $data);
266
267
        return $this->db->insert_id();
268
    }
269
270
    /**
271
     * @param bool|false $forShop
272
     * @return array
273
     */
274
    public function get_langs($forShop = false) {
275
276
        if ($forShop) {
277
            if (strpos(getCMSNumber(), 'Pro')) {
278
                return $this->db
279
                    ->where('default', true)
280
                    ->get('languages')
281
                    ->result_array();
282
            }
283
        }
284
285
        $query = $this->db->get('languages');
286
287
        return $query->result_array();
288
    }
289
290
    /**
291
     * @param $id
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
292
     * @return bool|array
293
     */
294 View Code Duplication
    public function get_lang($id) {
295
296
        $this->db->where('id', $id);
297
        $query = $this->db->get('languages', 1);
298
299
        if ($query->num_rows() == 1) {
300
            return $query->row_array();
301
        }
302
303
        return FALSE;
304
    }
305
306
    /**
307
     * @param array $data
308
     * @param int $id
309
     */
310
    public function update_lang($data, $id) {
311
312
        $this->db->where('id', $id);
313
        $this->db->update('languages', $data);
314
    }
315
316
    /**
317
     * @param integer $id
318
     */
319
    public function delete_lang($id) {
320
321
        $this->db->where('id', $id);
322
        $this->db->limit(1);
323
        $this->db->delete('languages');
324
    }
325
326
    /**
327
     * @param integer $id
328
     */
329
    public function set_default_lang($id) {
330
331
        $this->db->update('languages', ['default' => 0]);
332
333
        $this->db->where('id', $id);
334
        $this->db->limit(1);
335
        $this->db->update('languages', ['default' => 1, 'active' => 1]);
336
    }
337
338
    /**
339
     * @return array
340
     */
341
    public function get_default_lang() {
342
343
        if ($this->db) {
344
            $this->db->where('default', 1);
345
            $query = $this->db->get('languages', 1);
346
            return $query->row_array();
347
        }
348
    }
349
350
}