-
Notifications
You must be signed in to change notification settings - Fork 3
/
page_model.php
403 lines (341 loc) · 8.88 KB
/
page_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package CMS\Core\Models
* @author Daan Porru
* @author Jeroen de Graaf
* @copyright 2013-2015 Bitman
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Page model.
*
* This model is responsible for loading any
* page related information from the database.
*/
class Page_model extends CI_Model
{
/**
* @var trace
*/
protected $trace = array();
/**
* @var pages_by_id
*/
protected $pages_by_id = array();
/**
* Get Home
*
* Loads the default home page
* This is the first page in the DB
* that is visible and has no parent
*
* @param String $language
*
* @return object The row-data of the page
*/
public function get_home($language, $preview = FALSE)
{
$this->db->order_by('order', 'asc');
if ($preview !== TRUE)
{
$this->db->where('is_visible', 'yes');
}
$this->db->where('secondary_navigation IS NULL', NULL, FALSE);
$this->db->where('parent_id', 0);
$this->db->where('language', $language);
$this->db->limit(1);
$query = $this->db->get('page');
return $this->_cache_page($query->row_array());
}
/**
* Get page informatio by it's id
* The page has to be visible
*
* @param integer $id Identifier of the page
*
* @return object The row-data of the page
*/
public function get_page_by_id($id = 0, $preview = FALSE)
{
// check if this page already exists in cache
if (isset($this->pages_by_id[$id]) && !$preview)
{
$page = $this->pages_by_id[$id];
}
else
{
$page = $this->get_page_by_id_no_cache($id, $preview);
}
return $page;
}
/**
* Get page informatio by it's id
* without using the cache
*
* @param integer $id Identifier of the page
*
* @return object The row-data of the page
*/
public function get_page_by_id_no_cache($id = 0, $preview = FALSE)
{
$this->db->where('id', $id);
if ($preview !== TRUE)
{
$this->db->where('is_visible', 'yes');
}
$this->db->limit(1);
$query = $this->db->get('page');
$page = $query->row_array();
if (!$preview)
{
return $this->_cache_page($page);
}
else
{
// preview pages should not be cached
return $page;
}
}
/**
* Get Page By Slug
*
* Get page information by it's slug
* The page has to be visible
*
* @param string $slug The slug of the page
* @param String $language
*
* @return object The row-data of the page
*/
public function get_page_by_slug($slug = '', $language, $preview = FALSE)
{
$page = array();
// slug can not be empty
if ($slug !== '')
{
$this->db->where('slug', $slug);
if ($preview !== TRUE)
{
$this->db->where('is_visible', 'yes');
}
$this->db->where('language', $language);
$this->db->limit(1);
$query = $this->db->get('page');
$page = $query->row_array();
}
return $this->_cache_page($page);
}
/**
* Get page information corresponding to a controller/method.
*
* @param string $language Current language
* @param boolean $preview Preview modus
* @param string $class Class name
* @param string $method Method name
* @param array $params Optional array of extra controller parameters
* @return object The row-data of the page
*/
public function get_page_by_controller($language, $preview = FALSE, $class = NULL, $method = NULL, array $params = array())
{
// if not given as input - use current router class and method
$class = is_null($class) ? $this->router->class : $class;
$method = is_null($method) ? $this->router->method : $method;
$page = array();
if (!empty($params))
{
// Check on class/method + params (only when params are filled in)
$page = $this->_get_page_by_controller_and_parameters($language, $preview, $class . '/' . $method, $params);
}
if (empty($page))
{
// Check on class/method without params
$page = $this->_get_page_by_controller($language, $preview, $class . '/' . $method);
}
if (!empty($params) && empty($page))
{
// Check on class + params (only when params are filled in)
$page = $this->_get_page_by_controller_and_parameters($language, $preview, $class, $params);
}
if (empty($page))
{
// Check on class only without params
$page = $this->_get_page_by_controller($language, $preview, $class);
}
return $this->_cache_page($page);
}
protected function _get_page_by_controller_and_parameters($language, $preview, $controller, $params)
{
$page = array();
$this->db->where('controller', $controller);
$this->db->where('controller_params <>', '');
$this->db->where('language', $language);
if (!$preview)
{
$this->db->where('is_visible', 'yes');
}
$this->db->order_by('order');
$pages = $this->db->get('page')->result_array();
// Get page with all parameters present
foreach ($pages as $item)
{
parse_str($item['controller_params'], $item_params);
$equals = 0;
foreach ($params as $key => $value)
{
if (isset($item_params[$key]) && $item_params[$key] == $value)
{
$equals++;
}
}
if (count($params) === $equals)
{
$page = $item;
break;
}
}
return $page;
}
protected function _get_page_by_controller($language, $preview, $controller)
{
$this->db->where('controller', $controller);
$this->db->where('controller_params', '');
$this->db->where('language', $language);
if (!$preview)
{
$this->db->where('is_visible', 'yes');
}
$this->db->order_by('order');
$this->db->limit(1);
return $this->db->get('page')->row_array();
}
/**
* Get Navigation By Parent Id
*
* Get a (sub)navigation.
*
* @param Integer $parent_id The parent_id of the navigation to return (0 is main menu)
* @return mixed[] List of navigation items
*/
public function get_navigation_by_parent_id($parent_id, $language, $secondary_navigation = NULL, $preview = FALSE)
{
$this->db->order_by('order');
if (!$preview)
{
$this->db->where('is_visible', 'yes');
}
$this->db->where('in_menu', 'yes');
if(is_null($secondary_navigation))
{
$this->db->where('secondary_navigation IS NULL', NULL, FALSE);
}
else
{
$this->db->where('secondary_navigation', $secondary_navigation);
}
$this->db->where('parent_id', $parent_id);
$this->db->where('language', $language);
$query = $this->db->get('page');
return $query->result_array();
}
/**
* Get Relative Pages
*
* Get pages in other languages relative to this page
*
* @param mixed[] $page
*/
public function get_relative_pages($page)
{
$relative_pages = array();
$search_id = ($page['relative_page_id'] > 0) ? $page['relative_page_id'] : $page['id'];
$query = $this->db->query("SELECT * FROM page
WHERE is_visible = 'yes'
AND
(id = ? OR relative_page_id = ?)"
, array($search_id, $search_id));
foreach ($query->result_array() as $page)
{
$relative_pages[$page['language']] = $page['slug'];
}
return $relative_pages;
}
/**
* Get Trace
*
* Get the trace of the page requested
* meaning the path from the main parent
* the requested page
*
* @param Integer $page_id The requested page
*
* @return mixed[] List of pages in order from main parent to requested page
*/
public function get_trace($page_id)
{
// cache trace
if(!empty($this->trace))
{
return $this->trace;
}
$trace = array();
$current_page = $page_id;
// build the trace
while ($current_page != 0)
{
$trace[] = $current_page;
$current_page = $this->_get_parent_id($current_page);
}
//reverse the trace array to show the main parent first
$this->trace = array_reverse($trace);
return $this->trace;
}
protected function _get_parent_id($page_id)
{
$this->db->select('parent_id');
$this->db->where('id', $page_id);
$this->db->limit(1);
$query = $this->db->get('page');
$parent = $query->row_array();
return $parent['parent_id'];
}
/**
* Get the top page (with parent_id=0) in the ancestry of this page.
*
* @param integer $page_id
*
* @return mixed page-record
*/
public function get_ancestor($page_id)
{
$trace = $this->get_trace($page_id);
return $this->get_page_by_id($trace[0]);
}
/**
* Store a page in the $this->pages_by_id array
* so we can easily access for get_page_by_id()
*
* @param mixed $page Page-record
* @return mixed Page-record
*/
protected function _cache_page($page)
{
if (isset($page['id']))
{
$this->pages_by_id[$page['id']] = $page;
}
return $page;
}
}