Completed
Push — development ( ef9e73...b2c3e4 )
by Andrij
20:27
created

Cms_base::setLocaleId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * @property CI_DB_active_record $db
9
 */
10
class Cms_base 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
    public $locale_id;
14
15
    /**
16
     * @var array
17
     */
18
    private static $settings;
19
20
    /**
21
     * @var array
22
     */
23
    private static $language;
24
25
    public function __construct() {
26
27
        parent::__construct();
28
    }
29
30
    /**
31
     * Select main settings
32
     *
33
     * @access public
34
     * @param string|null $key
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
35
     * @return array
36
     */
37
    public function get_settings($key = null) {
38
39
        if (self::$settings) {
40
            return $key === null ? self::$settings : self::$settings[$key];
41
        }
42
43
        $this->db->where('s_name', 'main');
44
        $query = $this->db->get('settings', 1);
45
46
        if ($query and $query->num_rows() == 1) {
47
            $settings = $query->row_array();
48
            $lang_arr = get_main_lang();
49
            $meta = $this->db
50
                ->where('lang_ident', $lang_arr['id'])
51
                ->limit(1)
52
                ->get('settings_i18n')
53
                ->result_array();
54
55
            $settings['site_short_title'] = $meta[0]['short_name'];
56
            $settings['site_title'] = $meta[0]['name'];
57
            $settings['site_description'] = $meta[0]['description'];
58
            $settings['site_keywords'] = $meta[0]['keywords'];
59
            $this->db->cache_off();
60
            self::$settings = $settings;
61
            return $key === null ? $settings : $settings[$key];
62
        } else {
63
            show_error($this->db->_error_message());
64
        }
65
66
        return FALSE;
67
    }
68
69
    /**
70
     * Select site languages
71
     * @access public
72
     * @param bool $active
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
73
     * @return array
74
     */
75
    public function get_langs($active = FALSE) {
76
77
        if (self::$language[(int) $active]) {
78
            return self::$language[(int) $active];
79
        }
80
        if ($active) {
81
            $this->db->where('active', 1);
82
        }
83
84
        $query = $this->db->get('languages');
85
86
        if ($query) {
87
            $query = $query->result_array();
88
            self::$language[(int) $active] = $query;
89
        } else {
90
            show_error($this->db->_error_message());
91
        }
92
93
        return $query;
94
    }
95
96
    /**
97
     * Load modules
98
     */
99
    public function get_modules() {
100
101
        $this->db->cache_on();
102
        $query = $this->db
103
            ->select('id, name, identif, autoload, enabled')
104
            ->order_by('position')
105
            ->get('components');
106
        $this->db->cache_off();
107
108
        return $query;
109
    }
110
111
    /**
112
     * @param int $cat_id
113
     * @return bool|object
114
     */
115
    public function get_category_pages($cat_id) {
116
117
        $this->db->cache_on();
118
        $this->db->where('category', $cat_id);
119
        $this->db->where('post_status', 'publish');
120
        $this->db->where('publish_date <=', time());
121
        $this->db->where('lang_alias', 0);
122
        $this->db->where('lang', $this->config->item('cur_lang'));
123
        $this->db->order_by('created', 'desc');
124
        $query = $this->db->get('content');
125
126
        if ($query->num_rows() > 0) {
127
            $query = $query->result_array();
128
            $this->db->cache_off();
129
            return $query;
130
        } else {
131
            $this->db->cache_off();
132
            return FALSE;
133
        }
134
    }
135
136
    /**
137
     * @param bool|int $page_id
138
     * @return bool|object
139
     */
140
    public function get_page_by_id($page_id = FALSE) {
141
142
        if ($page_id != FALSE) {
143
            $this->db->cache_on();
144
            $this->db->where('post_status', 'publish');
145
            $this->db->where('publish_date <=', time());
146
            $this->db->where('id', $page_id);
147
148
            $query = $this->db->get('content', 1);
149
150
            if ($query->num_rows() == 1) {
151
                $query = $query->row_array();
152
                $this->db->cache_off();
153
                return $query;
154
            }
155
            $this->db->cache_off();
156
        }
157
158
        return FALSE;
159
    }
160
161
    /**
162
     * @param bool|int $page_id
163
     * @return bool|object
164
     */
165
    public function get_page($page_id = FALSE) {
166
167
        return $this->get_page_by_id($page_id);
168
    }
169
170
    /**
171
     * Select all categories
172
     *
173
     * @access public
174
     * @return array
175
     */
176
    public function get_categories() {
177
178
        /** Сетаеться только в pages->edit */
179
        if ($this->getLocaleId()) {
180
181
            $query = $this->db->select('*')
182
                ->from('category')
183
                ->join('category_translate', 'category.id = category_translate.alias', 'left')
184
                ->where('lang', $this->getLocaleId())
185
                ->order_by('position', 'ASC')
186
                ->get();
187
        } else {
188
189
            $this->db->order_by('position', 'ASC');
190
            $query = $this->db->get('category');
191
192
        }
193
194
        if ($query->num_rows() > 0) {
195
            $categories = $query->result_array();
196
197
            $n = 0;
198
            $ci = &get_instance();
199
            $ci->load->library('DX_Auth');
200
            foreach ($categories as $c) {
201
                $categories[$n] = $ci->load->module('cfcm')->connect_fields($c, 'category');
202
                $n++;
203
            }
204
205
            return $categories;
206
        }
207
208
        return FALSE;
209
    }
210
211
    /**
212
     * @param int $id
213
     * @return bool
214
     */
215
    public function get_category_by_id($id) {
216
217
        $query = $this->db
218
            ->order_by('position', 'ASC')
219
            ->where('id', $id)
220
            ->get('category');
221
222
        if ($query->num_rows() > 0) {
223
            $categories = $query->row_array();
224
225
            return $categories;
226
        }
227
228
        return FALSE;
229
    }
230
231
    /**
232
     * @param int $id
233
     * @return string
234
     */
235
    public function get_category_full_path($id) {
236
237
        $cats = $this->get_category_by_id($id);
238
        $url = $cats['url'];
239
240
        if ($cats['parent_id']) {
241
            $url = $this->get_category_full_path($cats['parent_id']) . '/' . $url;
242
        } else {
243
            return $cats['url'];
244
        }
245
246
        return $url;
247
    }
248
249
    /**
250
     * @return array
251
     */
252
    public function getCategoriesPagesCounts() {
253
254
        // getting counts
255
        $result = $this->db
256
            ->select(['category.id', 'category.parent_id', 'count(content.id) as pages_count'])
257
            ->from('category')
258
            ->join('content', 'category.id=content.category')
259
            ->where('lang_alias', 0)
260
            ->group_by('category.id')
261
            ->get();
262
263
        if (!$result) {
264
            return [];
265
        }
266
267
        $result = $result->result_array();
268
269
        $categoriesPagesCounts = [];
270
        $count = count($result);
271
        for ($i = 0; $i < $count; $i++) {
272
            $categoriesPagesCounts[$result[$i]['id']] = [
273
                                                         'parent_id'   => $result[$i]['parent_id'],
274
                                                         'pages_count' => $result[$i]['pages_count'],
275
                                                        ];
276
        }
277
278
        return $categoriesPagesCounts;
279
    }
280
281
    /**
282
     * @return mixed
283
     */
284
    public function getLocaleId() {
285
286
        return $this->locale_id;
287
    }
288
289
    /**
290
     * @param mixed $locale_id
291
     */
292
    public function setLocaleId($locale_id) {
293
294
        if ($locale_id != MY_Controller::getDefaultLanguage()['id']) {
295
296
            $this->locale_id = $locale_id;
297
298
        }
299
300
    }
301
302
}
303
304
/* end of cms_base.php */