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

CoreModel::getPage()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 6
nop 1
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php namespace core\src;
2
3
use CI;
4
use CI_DB_active_record;
5
use core\models\Base\RouteQuery;
6
use core\models\Route;
7
use Doctrine\Common\Cache\Cache;
8
9
class CoreModel
10
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
11
12
    const CACHE_LANG = 'main_site_langs';
13
14
    /**
15
     * @var CI_DB_active_record
16
     */
17
    private $db;
18
19
    /**
20
     * @var CI
21
     */
22
    private $ci;
23
24
    /**
25
     * @var Cache
26
     */
27
    private $cache;
28
29
    /**
30
     * @var array
31
     */
32
    private $languages;
33
34
    /**
35
     * @var array
36
     */
37
    private $defaultLanguage;
38
39
    /**
40
     * FrontModel constructor.
41
     * @param CI $ci
42
     * @param Cache $cache
43
     */
44
    public function __construct(CI $ci, Cache $cache) {
45
        $this->ci = $ci;
46
        $this->db = $ci->db;
47
        $this->cache = $cache;
48
    }
49
50
    public function getPage($id) {
51
52
        // Select page permissions and page data
53
        $this->db->select('content.*');
54
        $this->db->select("IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url) as full_url", false);
55
        $this->db->select('content_permissions.data as roles', FALSE);
56
57
        if (CoreFactory::getConfiguration()->isDefaultLanguage()) {
58
            $this->db->where('content.id', $id);
59
        } else {
60
            $this->db->where('content.lang_alias', $id);
61
        }
62
63
        $this->db->where('post_status', 'publish');
64
        $this->db->where('publish_date <=', time());
65
        $this->db->where('lang', config_item('cur_lang'));
66
        $this->db->join('content_permissions', 'content_permissions.page_id = content.id', 'left');
67
        $this->db->join('route', 'route.id = content.route_id');
68
69
        $query = $this->db->get('content', 1);
70
71
        if ($query->num_rows() > 0) {
72
            $page = $query->row_array();
73
            $page['roles'] = unserialize($page['roles']);
74
            $page['full_text'] = $page['full_text'] ?: $page['prev_text'];
75
            $page = $this->ci->cfcm->connect_fields($page, 'page');
76
77
            return $page;
78
        }
79
    }
80
81
    public function iteratePageShowed($id) {
82
        $this->db->set('showed', 'showed + 1', FALSE);
83
        $this->db->where('id', $id);
84
        $this->db->limit(1);
85
        $this->db->update('content');
86
    }
87
88
    public function getCategory($id) {
89
90
        $defaultLanguage = $this->getDefaultLanguage();
91
        $this->db->select('category.*');
92
        if ($defaultLanguage['id'] != config_item('cur_lang')) {
93
94
            $this->db->select(
95
                'category_translate.name, category_translate.title, category_translate.short_desc,
96
category_translate.image, category_translate.keywords, category_translate.description, category_translate.lang'
97
            );
98
            $this->db->join('category_translate', 'category.id = category_translate.alias')
99
                ->where('category_translate.lang', config_item('cur_lang'));
100
        }
101
        $this->db->where('category.id', $id);
102
103
        $query = $this->db->get('category');
104
105
        if ($query->num_rows() > 0) {
106
            $category = $query->row_array();
107
            $category['fetch_pages'] = unserialize($category['fetch_pages']);
108
            $route = $this->getRoute($category['id'], Route::TYPE_CATEGORY);
109
            $category['path'] = $this->getRoutePath($route);
110
            $category['path_url'] = $route->getFullUrl();
111
            return $category;
112
        }
113
    }
114
115
    public function getRoutePath(Route $route) {
116
        $routes = $route->getPathRoutes();
117
118
        $path = [];
119
        foreach ($routes as $route) {
120
            $path[$route->getEntityId()] = $route->getUrl();
121
        }
122
123
        return $path;
124
125
    }
126
127
    public function getCategoryPages(array $category, $row_count = 0, $offset = 0) {
128
129
        $query = $this->createCategoryPagesQuery($category);
130
131
        $query->select('content .*');
132
        $query->select("IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url) as full_url", false);
133
        $query->join('route', 'route.id = content.route_id');
134
135
        $query->order_by($category['order_by'], $category['sort_order']);
136
        $query = $row_count > 0 ? $query->get('content', (int) $row_count, (int) $offset) : $query->get('content');
137
138
        if ($query->num_rows() > 0) {
139
            $pages = $query->result_array();
140
            foreach ($pages as $key => $page) {
141
                $pages[$key] = $this->ci->cfcm->connect_fields($page, 'page');
142
            }
143
144
            return $pages;
145
        }
146
    }
147
148
    public function countCategoryPages($category) {
149
150
        $query = $this->createCategoryPagesQuery($category);
151
        $query->from('content');
152
153
        return $query->count_all_results();
154
155
    }
156
157
    public function getRoute($id, $type) {
158
        $route = RouteQuery::create()->filterByEntityId($id)
159
            ->filterByType($type)
160
            ->findOne();
161
162
        return $route;
163
    }
164
165
    private function createCategoryPagesQuery($category) {
166
167
        $query = $this->db->where('post_status', 'publish')
168
            ->where('publish_date <= ', time())
169
            ->where('lang', config_item('cur_lang'));
170
171 View Code Duplication
        if (count($category['fetch_pages']) > 0) {
172
            $category['fetch_pages'][] = $category['id'];
173
            $query->where_in('category', $category['fetch_pages']);
174
        } else {
175
            $query->where('category', $category['id']);
176
        }
177
178
        return $query;
179
    }
180
181
    public function getDefaultLanguage() {
182
183
        if ($this->defaultLanguage === null) {
184
            foreach ($this->getLanguages() as $language) {
185
                if ($language['default'] == 1) {
186
                    $this->defaultLanguage = $language;
187
                }
188
            }
189
        }
190
        return $this->defaultLanguage;
191
    }
192
193
    public function getLanguages() {
194
195
        if ($this->languages === null) {
196
            if ($this->cache->contains(self::CACHE_LANG)) {
197
                $this->languages = $this->cache->fetch(self::CACHE_LANG);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cache->fetch(self::CACHE_LANG) of type * is incompatible with the declared type array of property $languages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
198
            } else {
199
                $languages = $this->ci->cms_base->get_langs(TRUE);
200
201
                foreach ($languages as $language) {
202
203
                    $language['name'] = $language['lang_name'];
204
                    unset($language['lang_name']);
205
                    $this->languages[$language['identif']] = $language;
206
207
                }
208
                $this->cache->save(self::CACHE_LANG, $this->languages);
209
210
            }
211
212
        }
213
214
        return $this->languages;
215
216
    }
217
218
    /**
219
     * Check user access for page
220
     * @param array $roles
221
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
222
     */
223
    public function checkPageAccess($roles) {
224
225
        if ($roles == FALSE OR count($roles) == 0) {
226
            return TRUE;
227
        }
228
229
        $access = FALSE;
230
        $logged = $this->ci->dx_auth->is_logged_in();
231
        $my_role = $this->ci->dx_auth->get_role_id();
232
233
        if ($this->ci->dx_auth->is_admin() === TRUE) {
234
            $access = TRUE;
235
        }
236
237
        // Check roles access
238
        if ($access != TRUE) {
239
            foreach ($roles as $role) {
240
                if ($role['role_id'] == $my_role) {
241
                    $access = TRUE;
242
                }
243
244
                if ($role['role_id'] == 1 AND $logged == TRUE) {
245
                    $access = TRUE;
246
                }
247
248
                if ($role['role_id'] == '0') {
249
                    $access = TRUE;
250
                }
251
            }
252
        }
253
254
        if ($access == FALSE) {
255
            $this->ci->dx_auth->deny_access('deny');
256
            exit;
257
        }
258
    }
259
260
}