Completed
Push — development ( 67765c...7029e6 )
by Andrij
18:12
created

Cms_base::get_categories()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
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
        $this->db->order_by('position', 'ASC');
179
        $query = $this->db
180
            ->select('category.*, route.url')
181
            ->join('route', 'route.id = category.route_id')
182
            ->get('category');
183
184
        if ($query->num_rows() > 0) {
185
            $categories = $query->result_array();
186
187
            $n = 0;
188
            $ci = &get_instance();
189
            $ci->load->library('DX_Auth');
190
            foreach ($categories as $c) {
191
                $categories[$n] = $ci->load->module('cfcm')->connect_fields($c, 'category');
192
                $n++;
193
            }
194
195
            return $categories;
196
        }
197
198
        return FALSE;
199
    }
200
201
    /**
202
     * @param int $id
203
     * @return bool
204
     */
205 View Code Duplication
    public function get_category_by_id($id) {
206
207
        $query = $this->db
208
            ->select('category.*, route.url, route.parent_url')
209
            ->order_by('category.position', 'ASC')
210
            ->where('category.id', $id)
211
            ->join('route', 'category.route_id = route.id')
212
            ->get('category');
213
214
        if ($query->num_rows() > 0) {
215
            $categories = $query->row_array();
216
217
            return $categories;
218
        }
219
220
        return FALSE;
221
    }
222
223
    /**
224
     * @param int $id
225
     * @return string
226
     */
227
    public function get_category_full_path($id) {
228
229
        $cats = $this->get_category_by_id($id);
230
        $url = $cats['url'];
231
232
        if ($cats['parent_id']) {
233
            $url = $this->get_category_full_path($cats['parent_id']) . '/' . $url;
234
        } else {
235
            return $cats['url'];
236
        }
237
238
        return $url;
239
    }
240
241
    /**
242
     * @return array
243
     */
244
    public function getCategoriesPagesCounts() {
245
246
        // getting counts
247
        $result = $this->db
248
            ->select(['category.id', 'category.parent_id', 'count(content.id) as pages_count'])
249
            ->from('category')
250
            ->join('content', 'category.id=content.category')
251
            ->where('lang_alias', 0)
252
            ->group_by('category.id')
253
            ->get();
254
255
        if (!$result) {
256
            return [];
257
        }
258
259
        $result = $result->result_array();
260
261
        $categoriesPagesCounts = [];
262
        $count = count($result);
263
        for ($i = 0; $i < $count; $i++) {
264
            $categoriesPagesCounts[$result[$i]['id']] = [
265
                                                         'parent_id'   => $result[$i]['parent_id'],
266
                                                         'pages_count' => $result[$i]['pages_count'],
267
                                                        ];
268
        }
269
270
        return $categoriesPagesCounts;
271
    }
272
273
    /**
274
     * @return mixed
275
     */
276
    public function getLocaleId() {
277
278
        return $this->locale_id;
279
    }
280
281
    /**
282
     * @param mixed $locale_id
283
     */
284
    public function setLocaleId($locale_id) {
285
286
        if ($locale_id != MY_Controller::getDefaultLanguage()['id']) {
287
288
            $this->locale_id = $locale_id;
289
290
        }
291
292
    }
293
294
}
295
296
/* end of cms_base.php */