This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | use CMSFactory\assetManager; |
||
4 | use core\src\CoreFactory; |
||
5 | |||
6 | if (!defined('BASEPATH')) { |
||
7 | exit('No direct script access allowed'); |
||
8 | } |
||
9 | |||
10 | /** |
||
11 | * Image CMS |
||
12 | * @param lib_category Lib_category |
||
13 | * @param menu Menu |
||
14 | */ |
||
15 | class Admin extends BaseAdminController |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private $root_menu = []; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $sub_menu = []; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $sub_menus = []; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var integer |
||
39 | */ |
||
40 | private $padding = 0; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $menu_result = []; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $for_delete = []; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $default_lang_id; |
||
59 | |||
60 | /** |
||
61 | * Admin constructor. |
||
62 | */ |
||
63 | public function __construct() { |
||
64 | |||
65 | parent::__construct(); |
||
66 | $lang = new MY_Lang(); |
||
67 | $lang->load('menu'); |
||
68 | |||
69 | // Only admin access |
||
70 | $this->load->library('DX_Auth'); |
||
71 | |||
72 | $this->cache->delete_all(); |
||
73 | $this->load->library('Form_validation'); |
||
74 | $this->load->library('lib_admin'); |
||
75 | $this->load->module('menu'); |
||
76 | $this->load->model('menu_model'); |
||
77 | |||
78 | $this->template->assign('langs', $this->_get_langs()); |
||
79 | |||
80 | $this->menu->select_hidden = TRUE; //select hidden items |
||
81 | |||
82 | $this->default_lang_id = CoreFactory::getModel()->getDefaultLanguage()['id']; |
||
83 | } |
||
84 | |||
85 | public function index() { |
||
86 | |||
87 | $root_menus = $this->db->get('menus')->result_array(); |
||
88 | assetManager::create() |
||
89 | ->setData('menus', $root_menus) |
||
90 | ->renderAdmin('menu_list'); |
||
91 | } |
||
92 | |||
93 | public function chose_hidden() { |
||
94 | |||
95 | $status = $this->input->post('status') === 'false' ? 0 : 1; |
||
96 | $id = $this->input->post('id'); |
||
97 | $this->db->query("update menus_data set hidden = '$status' where id = '$id'"); |
||
98 | $this->lib_admin->log(lang('Status menus item was changed', 'menu') . '. Id: ' . $id); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * List all menu items |
||
103 | * |
||
104 | * @param string $name |
||
105 | */ |
||
106 | public function menu_item($name = '') { |
||
107 | |||
108 | $this->menu->prepare_menu_array($name); |
||
109 | $this->root_menu = &$this->menu->menu_array; |
||
110 | $this->sub_menu = &$this->menu->sub_menu_array; |
||
111 | |||
112 | $this->process_root($this->root_menu); |
||
113 | $ins_id = $this->db->get_where('menus', ['name' => $name])->row_array(); |
||
114 | |||
115 | $this->template->assign('menu_result', $this->menu_result); |
||
116 | $this->template->assign('insert_id', $ins_id['id']); |
||
117 | $this->template->assign('menu_title', $ins_id['main_title']); |
||
118 | $this->template->assign('tree', $this->_printRecursiveMenuItems($this->root_menu)); |
||
119 | |||
120 | $this->display_tpl('main'); |
||
121 | } |
||
122 | |||
123 | public function list_menu_items($menu_id = 0) { |
||
124 | |||
125 | if ($menu_id > 0) { |
||
126 | $this->menu_item($this->get_name_by_id($menu_id)); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Display create_item.tpl |
||
132 | * @param null|int $id |
||
133 | */ |
||
134 | public function create_item($id = null) { |
||
135 | |||
136 | if (!$this->input->post()) { |
||
137 | $parents = $this->db |
||
138 | ->where('menu_id', $id) |
||
139 | ->select('menus_data.*, menu_translate.title') |
||
140 | ->join('menu_translate', 'menus_data.id = menu_translate.item_id') |
||
141 | ->where('lang_id', $this->default_lang_id) |
||
142 | ->get('menus_data')->result_array(); |
||
143 | |||
144 | $menu = $this->db->where('id', $id)->get('menus')->row_array(); |
||
145 | $cats = $this->lib_category->build(); |
||
146 | $pages = $this->get_pages(0, 0, 'controller'); |
||
147 | //$query = $this->db->get('shop_rbac_roles'); |
||
148 | $locale = MY_Controller::getCurrentLocale(); |
||
149 | $this->db->select('shop_rbac_roles.*', FALSE); |
||
150 | $this->db->select('shop_rbac_roles_i18n.alt_name', FALSE); |
||
151 | $this->db->where('locale', $locale); |
||
152 | $this->db->join('shop_rbac_roles_i18n', 'shop_rbac_roles_i18n.id = shop_rbac_roles.id'); |
||
153 | $role = $this->db->get('shop_rbac_roles')->result_array(); |
||
154 | |||
155 | $this->template->assign('roles', $role); |
||
156 | $this->template->assign('modules', $this->_load_module_list()); |
||
157 | $this->template->assign('cats', $cats); |
||
158 | $this->template->assign('menu', $menu); |
||
159 | $this->template->assign('parents', $parents); |
||
160 | $this->template->assign('pages', $pages); |
||
161 | $this->template->assign('insert_id', $id); |
||
162 | $this->display_tpl('create_item'); |
||
163 | } else { |
||
164 | $this->form_validation->set_rules('menu_id', 'Menu Id', 'required'); |
||
165 | $this->form_validation->set_rules('item_type', 'Item Type', 'required'); |
||
166 | $this->form_validation->set_rules('title', lang('Title', 'menu'), 'required'); |
||
167 | View Code Duplication | if ($this->input->post('item_type') == 'page') { |
|
168 | //$this->form_validation->set_rules('title', 'Заголовок', 'required'); |
||
169 | $this->form_validation->set_rules('item_id', lang('Page ID', 'menu'), 'required'); |
||
170 | } |
||
171 | View Code Duplication | if ($this->input->post('item_type') == 'category') { |
|
172 | $this->form_validation->set_rules('item_id', lang('category ID', 'menu'), 'required'); |
||
173 | } |
||
174 | View Code Duplication | if ($this->input->post('item_type') == 'module') { |
|
175 | $this->form_validation->set_rules('mod_name', lang('Module name', 'menu'), 'required'); |
||
176 | //$this->form_validation->set_rules('mod_method', 'Метод модуля', 'required'); |
||
177 | $this->form_validation->set_rules('item_id', lang('Module ID', 'menu'), 'required'); |
||
178 | } |
||
179 | if ($this->input->post('item_type') == 'url') { |
||
180 | $this->form_validation->set_rules('item_url', 'URL', 'required'); |
||
181 | } |
||
182 | |||
183 | if ($this->form_validation->run($this) == FALSE) { |
||
184 | showMessage(validation_errors(), '', 'r'); |
||
185 | } else { |
||
186 | |||
187 | View Code Duplication | if ($this->input->post('page_hidden')) { |
|
188 | $hidden = $this->input->post('page_hidden'); |
||
189 | } elseif ($this->input->post('cat_hidden')) { |
||
190 | $hidden = $this->input->post('cat_hidden'); |
||
191 | } elseif ($this->input->post('module_hidden')) { |
||
192 | $hidden = $this->input->post('module_hidden'); |
||
193 | } elseif ($this->input->post('url_hidden')) { |
||
194 | $hidden = $this->input->post('url_hidden'); |
||
195 | } |
||
196 | |||
197 | View Code Duplication | if ($this->input->post('page_item_image')) { |
|
198 | $image = $this->input->post('page_item_image'); |
||
199 | } elseif ($this->input->post('cat_item_image')) { |
||
200 | $image = $this->input->post('cat_item_image'); |
||
201 | } elseif ($this->input->post('module_item_image')) { |
||
202 | $image = $this->input->post('module_item_image'); |
||
203 | } elseif ($this->input->post('url_item_image')) { |
||
204 | $image = $this->input->post('url_item_image'); |
||
205 | } else { |
||
206 | $image = ''; |
||
207 | } |
||
208 | |||
209 | View Code Duplication | if ($this->input->post('page_newpage')) { |
|
210 | $newpage = $this->input->post('page_newpage'); |
||
211 | } elseif ($this->input->post('cat_newpage')) { |
||
212 | $newpage = $this->input->post('cat_newpage'); |
||
213 | } elseif ($this->input->post('module_newpage')) { |
||
214 | $newpage = $this->input->post('module_newpage'); |
||
215 | } elseif ($this->input->post('url_newpage')) { |
||
216 | $newpage = $this->input->post('url_newpage'); |
||
217 | } |
||
218 | |||
219 | //preparing roles |
||
220 | $roles = $this->input->post('item_roles'); |
||
221 | View Code Duplication | if ($roles == NULL) { |
|
222 | $roles = ''; |
||
223 | } else { |
||
224 | $roles = serialize($this->input->post('item_roles')); |
||
225 | } |
||
226 | |||
227 | //preparing main data |
||
228 | $item_data = [ |
||
229 | 'menu_id' => $this->input->post('menu_id'), |
||
230 | 'item_id' => $this->input->post('item_id'), |
||
231 | 'item_type' => $this->input->post('item_type'), |
||
232 | 'title' => htmlentities($this->input->post('title'), ENT_QUOTES, 'UTF-8'), |
||
233 | 'hidden' => (int) $hidden, |
||
234 | 'item_image' => $image, |
||
235 | 'roles' => $roles, |
||
236 | 'parent_id' => $this->input->post('parent_id'), |
||
237 | ]; |
||
238 | |||
239 | // $item_data['position'] = $all_menu_items_count + 1; |
||
240 | $last_item_position = $this->db->where('menu_id', $this->input->post('menu_id')) |
||
241 | ->where('parent_id', $this->input->post('parent_id')) |
||
242 | ->select_max('position') |
||
243 | ->get('menus_data')->result_array(); |
||
244 | $newItemPosition = $last_item_position[0]['position'] + 1; |
||
245 | $item_data['position'] = $newItemPosition; |
||
246 | |||
247 | if (!isset($item_data['add_data'])) { |
||
248 | if ($this->input->post('item_type') == 'module') { |
||
249 | $data['mod_name'] = $this->input->post('mod_name'); |
||
0 ignored issues
–
show
|
|||
250 | $data['method'] = $this->input->post('mod_method'); |
||
251 | } |
||
252 | if ($this->input->post('item_type') == 'url') { |
||
253 | $data['url'] = $this->input->post('item_url'); |
||
254 | } |
||
255 | $data['newpage'] = (int) $newpage; |
||
256 | $item_data['add_data'] = serialize($data); |
||
257 | } |
||
258 | // Error: wrong parent id |
||
259 | if ($this->input->post('item_type') != 'module' AND $this->input->post('item_type') != 'url') { |
||
260 | if ($this->input->post('item_id') == $this->input->post('parent_id')) { |
||
261 | $error = TRUE; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | if ($error == TRUE) { |
||
266 | showMessage(lang('Error', 'menu')); |
||
267 | //return FALSE; |
||
268 | } else { |
||
269 | $this->db->insert('menus_data', $item_data); |
||
270 | $lastId = $this->db->insert_id(); |
||
271 | $translate = [ |
||
272 | 'item_id' => $lastId, |
||
273 | 'title' => $item_data['title'], |
||
274 | 'lang_id' => $this->default_lang_id, |
||
275 | ]; |
||
276 | $this->db->insert('menu_translate', $translate); |
||
277 | $this->lib_admin->log(lang('The menu item was successfully created', 'menu') . '. Id: ' . $lastId); |
||
278 | showMessage(lang('The menu item was successfully created', 'menu')); |
||
279 | $row = $this->db->where('id', $this->input->post('menu_id'))->get('menus')->row_array(); |
||
280 | if ($this->input->post('action') == 'tomain') { |
||
281 | pjax('/admin/components/cp/menu/menu_item/' . $row['name']); |
||
282 | } else { |
||
283 | pjax('/admin/components/cp/menu/edit_item/' . $lastId . '/' . $row['name']); |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Display template to select/edit menu item |
||
292 | * @param int $id |
||
293 | * @param string $type |
||
294 | */ |
||
295 | public function display_selector($id, $type = 'page') { |
||
296 | |||
297 | $this->template->assign('insert_id', $id); |
||
298 | |||
299 | $this->menu->prepare_menu_array($this->get_name_by_id($id)); |
||
300 | $this->root_menu = &$this->menu->menu_array; |
||
301 | $this->sub_menu = &$this->menu->sub_menu_array; |
||
302 | $this->process_root($this->root_menu); |
||
303 | $this->template->assign('menu_result', $this->menu_result); |
||
304 | |||
305 | $item = $this->menu_model->get_item($id); |
||
306 | $parents = $this->db->where('menu_id', $item['menu_id'])->get('menus_data')->result_array(); |
||
307 | $this->template->assign('parents', $parents); |
||
308 | $this->template->add_array($item); |
||
309 | |||
310 | // roles |
||
311 | $query = $this->db->get('roles'); |
||
312 | $this->template->assign('roles', $query->result_array()); |
||
313 | // roles |
||
314 | |||
315 | $this->load->library('lib_category'); |
||
316 | |||
317 | $this->template->assign('tree', $this->lib_category->build()); |
||
318 | if ($type != 'category') { |
||
319 | $this->template->assign('cats', $this->fetch_tpl('cats')); |
||
320 | } |
||
321 | |||
322 | $this->template->assign('action', 'insert'); |
||
323 | $this->template->assign('update_id', '0'); |
||
324 | |||
325 | switch ($type) { |
||
326 | case 'page': |
||
327 | $this->display_tpl('pages_selector'); |
||
328 | break; |
||
329 | |||
330 | case 'category': |
||
331 | $this->template->assign('cats_list', $this->fetch_tpl('cats_list')); |
||
332 | $this->display_tpl('category_selector'); |
||
333 | break; |
||
334 | |||
335 | case 'module': |
||
336 | $this->template->assign('modules', $this->_load_module_list()); |
||
337 | $this->display_tpl('module_selector'); |
||
338 | break; |
||
339 | |||
340 | case 'url': |
||
341 | $this->display_tpl('url_selector'); |
||
342 | break; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Load all modules info |
||
348 | * |
||
349 | * @access private |
||
350 | * @return array |
||
351 | */ |
||
352 | private function _load_module_list() { |
||
353 | |||
354 | $this->load->module('admin/components'); |
||
355 | |||
356 | $modules = $this->db->get('components')->result_array(); |
||
357 | $id = $this->uri->segment(6); |
||
358 | $id = (int) $id; |
||
359 | $cnt = count($modules); |
||
360 | for ($i = 0; $i < $cnt; $i++) { |
||
361 | $info = $this->components->get_module_info($modules[$i]['name']); |
||
362 | if ($info) { |
||
363 | $modules[$i]['menu_name'] = $info['menu_name']; |
||
364 | $modules[$i]['description'] = $info['description']; |
||
365 | $modules[$i]['url_image'] = $this->db->where('id', $id) |
||
366 | ->where('title', $info['menu_name']) |
||
367 | ->select('item_image') |
||
368 | ->get('menus_data') |
||
369 | ->row() |
||
370 | ->item_image; |
||
371 | } else { |
||
372 | unset($modules[$i]); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | unset($info); |
||
377 | |||
378 | return $modules; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Get menu name by ID |
||
383 | * |
||
384 | * @param $id integer |
||
385 | * @access public |
||
386 | * @return array |
||
387 | */ |
||
388 | public function get_name_by_id($id) { |
||
389 | |||
390 | $query = $this->db->get_where('menus', ['id' => $id])->row_array(); |
||
391 | return $query['name']; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Delete menu item and its sub items |
||
396 | * |
||
397 | * @access public |
||
398 | * @return bool |
||
399 | */ |
||
400 | public function delete_item($id = null) { |
||
401 | |||
402 | if ($this->input->post('ids')) { |
||
403 | $id = $this->input->post('ids'); |
||
404 | foreach ($id as $i) { |
||
405 | $this->db->where('id', $i); |
||
406 | $this->db->limit(1); |
||
407 | $this->db->delete('menus_data'); |
||
408 | |||
409 | $this->_get_delete_items($i); |
||
410 | |||
411 | foreach ($this->for_delete as $item_id) { |
||
412 | $this->menu_model->delete_menu_item($item_id); |
||
413 | } |
||
414 | } |
||
415 | $this->lib_admin->log(lang('Menu item successfuly deleted', 'menu') . '. Ids ' . implode(', ', $id)); |
||
416 | showMessage(lang('Menu item successfuly deleted', 'menu'), ''); |
||
417 | } else { |
||
418 | if ($id > 0) { |
||
419 | $this->db->where('id', $id); |
||
420 | $this->db->limit(1); |
||
421 | $this->db->delete('menus_data'); |
||
422 | |||
423 | $this->_get_delete_items($id); |
||
424 | |||
425 | foreach ($this->for_delete as $item_id) { |
||
426 | $this->menu_model->delete_menu_item($item_id); |
||
427 | } |
||
428 | |||
429 | $this->lib_admin->log(lang('Menu item successfuly deleted', 'menu') . '. Id ' . $id); |
||
430 | showMessage(lang('Menu item successfuly deleted', 'menu'), ''); |
||
431 | return TRUE; |
||
432 | } else { |
||
433 | return FALSE; |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Find sub items for delete |
||
440 | * |
||
441 | * @param $id integer - item id |
||
442 | * @access private |
||
443 | * @return array |
||
444 | */ |
||
445 | View Code Duplication | private function _get_delete_items($id) { |
|
446 | |||
447 | $items = $this->menu_model->get_parent_items($id); |
||
448 | |||
449 | if ($items != FALSE) { |
||
450 | foreach ($items as $item) { |
||
451 | $this->for_delete[] = $item['id']; |
||
452 | $this->_get_delete_items($item['id']); |
||
453 | } |
||
454 | } |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Find all subitems and push in $this->sub_menus array |
||
459 | * |
||
460 | * @param $id integer - item id |
||
461 | * @access private |
||
462 | */ |
||
463 | View Code Duplication | private function _get_sub_items($id) { |
|
464 | |||
465 | $items = $this->menu_model->get_parent_items($id); |
||
466 | |||
467 | if ($items != FALSE) { |
||
468 | foreach ($items as $item) { |
||
469 | $this->sub_menus[] = $item['id']; |
||
470 | $this->_get_sub_items($item['id']); |
||
471 | } |
||
472 | } |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * ajax |
||
477 | */ |
||
478 | public function loadPathImg() { |
||
479 | |||
480 | $pathUrl = $this->db->like('add_data', $this->input->post('title'))->where('item_type', 'module')->get('menus_data')->row()->item_image; |
||
481 | if ($pathUrl) { |
||
482 | echo $pathUrl; |
||
483 | } |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Display edit item window |
||
488 | * @param int $item_id |
||
489 | */ |
||
490 | public function edit_item($item_id) { |
||
491 | |||
492 | if (!$this->input->post()) { |
||
493 | $item = $this->db |
||
494 | ->where('menus_data.id', $item_id) |
||
495 | ->select(['menus_data.*', 'menu_translate.title']) |
||
496 | ->join('menu_translate', 'menus_data.id = menu_translate.item_id', 'left') |
||
497 | ->where('lang_id', $this->default_lang_id) |
||
498 | ->get('menus_data')->row_array(); |
||
499 | |||
500 | if (empty($item)) { |
||
501 | $item = $this->db |
||
502 | ->where('menus_data.id', $item_id) |
||
503 | ->get('menus_data')->row_array(); |
||
504 | } |
||
505 | |||
506 | $parents = $this->db |
||
507 | ->select('menus_data.*, menu_translate.title') |
||
508 | ->where('menu_id', $item['menu_id']) |
||
509 | ->where('menus_data.id <>', $item['id']) |
||
510 | ->join('menu_translate', 'menus_data.id = menu_translate.item_id') |
||
511 | ->where('lang_id', $this->default_lang_id) |
||
512 | ->get('menus_data')->result_array(); |
||
513 | $menu = $this->db->where('id', $item['menu_id'])->get('menus')->row_array(); |
||
514 | |||
515 | $category_id = ($item['item_type'] === 'page') ? getPageCategoryId($item['item_id']) : 0; |
||
516 | |||
517 | $cats = $this->lib_category->build(); |
||
518 | $pages = $this->get_pages($category_id, 0, 'controller'); |
||
519 | $locale = MY_Controller::getCurrentLocale(); |
||
520 | $this->db->select('shop_rbac_roles.*', FALSE); |
||
521 | $this->db->select('shop_rbac_roles_i18n.alt_name', FALSE); |
||
522 | $this->db->where('locale', $locale); |
||
523 | $this->db->join('shop_rbac_roles_i18n', 'shop_rbac_roles_i18n.id = shop_rbac_roles.id'); |
||
524 | $role = $this->db->get('shop_rbac_roles')->result_array(); |
||
525 | |||
526 | //$query = $this->db->get('shop_rbac_roles'); |
||
527 | $this->template->assign('roles', $role); |
||
528 | $this->template->assign('selected_category_id', $category_id); |
||
529 | $this->template->assign('modules', $this->_load_module_list()); |
||
530 | $this->template->assign('cats', $cats); |
||
531 | $this->template->assign('menu', $menu); |
||
532 | $this->template->assign('parents', $parents); |
||
533 | $this->template->assign('item', $item); |
||
534 | $this->template->assign('pages', $pages); |
||
535 | $this->display_tpl('edit_item'); |
||
536 | } else { |
||
537 | if ($this->input->post('page_item_type')) { |
||
538 | $item_type = $this->input->post('page_item_type'); |
||
539 | } elseif ($this->input->post('cat_item_type')) { |
||
540 | $item_type = $this->input->post('cat_item_type'); |
||
541 | } elseif ($this->input->post('module_item_type')) { |
||
542 | $item_type = $this->input->post('module_item_type'); |
||
543 | } elseif ($this->input->post('url_item_type')) { |
||
544 | $item_type = $this->input->post('url_item_type'); |
||
545 | } |
||
546 | |||
547 | $this->form_validation->set_rules('menu_id', 'Menu Id', 'required'); |
||
548 | // $this->form_validation->set_rules('item_type', 'Item Type', 'required'); |
||
549 | $this->form_validation->set_rules('title', lang('Title', 'menu'), 'required'); |
||
550 | if ($item_type == 'page') { |
||
551 | $this->form_validation->set_rules('page_item_type', 'Item Type', 'required'); |
||
552 | $this->form_validation->set_rules('title', lang('Title', 'menu'), 'required'); |
||
553 | $this->form_validation->set_rules('item_id', lang('Page ID', 'menu'), 'required'); |
||
554 | } |
||
555 | if ($item_type == 'category') { |
||
556 | $this->form_validation->set_rules('cat_item_type', 'Item Type', 'required'); |
||
557 | $this->form_validation->set_rules('item_id', lang('Category ID', 'menu'), 'required'); |
||
558 | } |
||
559 | View Code Duplication | if ($item_type == 'module') { |
|
560 | $this->form_validation->set_rules('module_item_type', 'Item Type', 'required'); |
||
561 | $this->form_validation->set_rules('mod_name', lang('Module name', 'menu'), 'required'); |
||
562 | //$this->form_validation->set_rules('mod_method', 'Метод модуля', 'required'); |
||
563 | $this->form_validation->set_rules('item_id', lang('Page ID', 'menu'), 'required'); |
||
564 | } |
||
565 | if ($item_type == 'url') { |
||
566 | $this->form_validation->set_rules('url_item_type', 'Item Type', 'required'); |
||
567 | $this->form_validation->set_rules('item_url', 'URL', 'required'); |
||
568 | } |
||
569 | |||
570 | if ($this->form_validation->run($this) == FALSE) { |
||
571 | showMessage(validation_errors(), '', 'r'); |
||
572 | } else { |
||
573 | |||
574 | View Code Duplication | if ($this->input->post('page_hidden')) { |
|
575 | $hidden = $this->input->post('page_hidden'); |
||
576 | } elseif ($this->input->post('cat_hidden')) { |
||
577 | $hidden = $this->input->post('cat_hidden'); |
||
578 | } elseif ($this->input->post('module_hidden')) { |
||
579 | $hidden = $this->input->post('module_hidden'); |
||
580 | } elseif ($this->input->post('url_hidden')) { |
||
581 | $hidden = $this->input->post('url_hidden'); |
||
582 | } |
||
583 | View Code Duplication | if ($this->input->post('page_item_image')) { |
|
584 | $image = $this->input->post('page_item_image'); |
||
585 | } elseif ($this->input->post('cat_item_image')) { |
||
586 | $image = $this->input->post('cat_item_image'); |
||
587 | } elseif ($this->input->post('module_item_image')) { |
||
588 | $image = $this->input->post('module_item_image'); |
||
589 | } elseif ($this->input->post('url_item_image')) { |
||
590 | $image = $this->input->post('url_item_image'); |
||
591 | } |
||
592 | if ($this->input->post('page_parent_id')) { |
||
593 | $parent_id = $this->input->post('page_parent_id'); |
||
594 | } elseif ($this->input->post('cat_parent_id')) { |
||
595 | $parent_id = $this->input->post('cat_parent_id'); |
||
596 | } elseif ($this->input->post('module_parent_id')) { |
||
597 | $parent_id = $this->input->post('module_parent_id'); |
||
598 | } elseif ($this->input->post('url_parent_id')) { |
||
599 | $parent_id = $this->input->post('url_parent_id'); |
||
600 | } |
||
601 | View Code Duplication | if ($this->input->post('page_newpage')) { |
|
602 | $newpage = $this->input->post('page_newpage'); |
||
603 | } elseif ($this->input->post('cat_newpage')) { |
||
604 | $newpage = $this->input->post('cat_newpage'); |
||
605 | } elseif ($this->input->post('module_newpage')) { |
||
606 | $newpage = $this->input->post('module_newpage'); |
||
607 | } elseif ($this->input->post('url_newpage')) { |
||
608 | $newpage = $this->input->post('url_newpage'); |
||
609 | } |
||
610 | |||
611 | $roles = $this->input->post('item_roles'); |
||
612 | View Code Duplication | if ($roles == NULL) { |
|
613 | $roles = ''; |
||
614 | } else { |
||
615 | $roles = serialize($this->input->post('item_roles')); |
||
616 | } |
||
617 | |||
618 | $item_data = [ |
||
619 | 'menu_id' => $this->input->post('menu_id'), |
||
620 | 'item_id' => $this->input->post('item_id'), |
||
621 | 'item_type' => $item_type, |
||
622 | 'title' => htmlentities($this->input->post('title'), ENT_QUOTES, 'UTF-8'), |
||
623 | 'hidden' => (int) $hidden, |
||
624 | 'item_image' => $image, |
||
625 | 'roles' => $roles, |
||
626 | 'parent_id' => (int) $parent_id, |
||
627 | ]; |
||
628 | |||
629 | if ($item_data['item_type'] == 'module') { |
||
630 | $data['mod_name'] = $this->input->post('mod_name'); |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
631 | $data['method'] = $this->input->post('mod_method'); |
||
632 | $data['newpage'] = (int) $newpage; |
||
633 | } |
||
634 | |||
635 | View Code Duplication | if ($item_data['item_type'] == 'url') { |
|
636 | $item_data['item_id'] = 0; |
||
637 | $item_data['add_data'] = serialize(['url' => $this->input->post('item_url'), 'newpage' => (int) $newpage]); |
||
638 | } |
||
639 | View Code Duplication | if ($item_data['item_type'] == 'page') { |
|
640 | $item_data['add_data'] = serialize(['page' => $this->input->post('item_url'), 'newpage' => (int) $newpage]); |
||
641 | } |
||
642 | if (!isset($item_data['add_data'])) { |
||
643 | $item_data['add_data'] = serialize($data); |
||
644 | } |
||
645 | $errorMessage = Null; |
||
646 | // Error: wrong parent id |
||
647 | if ($this->input->post('item_id') != 0 && $parent_id != 0) { |
||
648 | if ($this->input->post('item_id') == $parent_id) { |
||
649 | $error = TRUE; |
||
650 | $errorMessage = 1; |
||
651 | } |
||
652 | } |
||
653 | |||
654 | // Error: don't place root menu in sub |
||
655 | if ($parent_id != 0) { |
||
656 | $this->_get_sub_items($item_id); |
||
657 | foreach ($this->sub_menus as $v) { |
||
658 | if ($v == $parent_id) { |
||
659 | $error = TRUE; |
||
660 | $errorMessage = 2; |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | |||
665 | if ($error == TRUE) { |
||
666 | if ($errorMessage == 1) { |
||
667 | showMessage(lang('Invalid parent identifier', 'menu'), '', 'r'); |
||
668 | } |
||
669 | if ($errorMessage == 2) { |
||
670 | showMessage(lang('Can not be root menu in subparagraph', 'menu'), '', 'r'); |
||
671 | } |
||
672 | exit(); |
||
673 | } else { |
||
674 | |||
675 | $item_data_translated = ['title' => $item_data['title']]; |
||
676 | $this->db->where('item_id', $item_id); |
||
677 | $this->db->where('lang_id', $this->default_lang_id); |
||
678 | $this->db->update('menu_translate', $item_data_translated); |
||
679 | |||
680 | $this->db->where('id', $item_id); |
||
681 | $this->db->update('menus_data', $item_data); |
||
682 | $this->lib_admin->log(lang('Menu item was edited', 'menu') . '. Id ' . $item_id); |
||
683 | showMessage(lang('Changes successfully saved', 'menu')); |
||
684 | $row = $this->db->where('id', $this->input->post('menu_id'))->get('menus')->row_array(); |
||
685 | if ($this->input->post('action') == 'tomain') { |
||
686 | pjax('/admin/components/cp/menu/menu_item/' . $row['name']); |
||
687 | } |
||
688 | } |
||
689 | } |
||
690 | } |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * @param array $items |
||
695 | * @return string |
||
696 | */ |
||
697 | private function _printRecursiveMenuItems($items) { |
||
698 | |||
699 | $html = ''; |
||
700 | foreach ($items as $item) { |
||
701 | $item['hasKids'] = false; |
||
702 | if ($submenus = $this->menu->_get_sub_menus($item['id'])) { |
||
703 | $item['hasKids'] = true; |
||
704 | } |
||
705 | |||
706 | $html .= '<div>'; |
||
707 | |||
708 | $this->template->assign('item', $item); |
||
709 | $html .= $this->fetch_tpl('_menulistitem'); |
||
710 | if ($item['hasKids']) { |
||
711 | $html .= '<div class="frame_level sortable ui-sortable">'; |
||
712 | $html .= $this->_printRecursiveMenuItems($submenus); |
||
713 | $html .= '</div>'; |
||
714 | } |
||
715 | |||
716 | $html .= '</div>'; |
||
717 | } |
||
718 | |||
719 | return $html; |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * @param array $array |
||
724 | */ |
||
725 | public function process_root($array) { |
||
726 | |||
727 | foreach ($array as $item) { |
||
728 | $sub_menus = $this->menu->_get_sub_menus($item['id']); |
||
729 | |||
730 | $item['padding'] = $this->padding; |
||
731 | $item['url'] = $item['link']; |
||
732 | $item['link'] = site_url($item['link']); |
||
733 | |||
734 | array_push($this->menu_result, $item); |
||
735 | |||
736 | if ($sub_menus != NULL) { |
||
737 | $this->padding += 1; |
||
738 | $this->process_root($sub_menus); |
||
739 | } |
||
740 | } |
||
741 | --$this->padding; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Insert link into menu |
||
746 | * Set positions |
||
747 | */ |
||
748 | public function insert_menu_item() { |
||
749 | |||
750 | $roles = $this->input->post('roles'); |
||
751 | View Code Duplication | if ($roles == NULL) { |
|
752 | $roles = ''; |
||
753 | } else { |
||
754 | $roles = serialize($this->input->post('roles')); |
||
755 | } |
||
756 | |||
757 | // Item position |
||
758 | if ($this->input->post('position_after') > 0) { |
||
759 | $after_pos = $this->menu_model->get_item_position($this->input->post('position_after')); |
||
760 | $after_pos = $after_pos['position']; |
||
761 | |||
762 | View Code Duplication | if ($after_pos != FALSE) { |
|
763 | $position = $after_pos + 1; |
||
764 | |||
765 | $sql = "UPDATE `menus_data` |
||
766 | SET `position`=`position` + 1 |
||
767 | WHERE `position` > '$after_pos' |
||
768 | AND `menu_id`='" . $this->input->post('menu_id') . "' |
||
769 | AND `parent_id`='" . $this->input->post('parent_id') . "' |
||
770 | "; |
||
771 | $this->db->query($sql); |
||
772 | } |
||
773 | } |
||
774 | |||
775 | View Code Duplication | if ($this->input->post('position_after') == 0) { |
|
776 | $this->db->select_max('position'); |
||
777 | $this->db->where('menu_id', $this->input->post('menu_id')); |
||
778 | $this->db->where('parent_id', $this->input->post('parent_id')); |
||
779 | $query = $this->db->get('menus_data')->row_array(); |
||
780 | |||
781 | if ($query['position'] == NULL) { |
||
782 | $position = 1; |
||
783 | } else { |
||
784 | $position = $query['position'] + 1; |
||
785 | } |
||
786 | } |
||
787 | |||
788 | View Code Duplication | if ($this->input->post('position_after') == 'first') { |
|
789 | $this->db->select_min('position'); |
||
790 | $this->db->where('menu_id', $this->input->post('menu_id')); |
||
791 | $this->db->where('parent_id', $this->input->post('parent_id')); |
||
792 | $query = $this->db->get('menus_data')->row_array(); |
||
793 | |||
794 | if ($query['position'] == NULL) { |
||
795 | $position = 1; |
||
796 | } else { |
||
797 | $position = $query['position'] - 1; |
||
798 | } |
||
799 | } |
||
800 | |||
801 | $item_data = [ |
||
802 | 'menu_id' => $this->input->post('menu_id'), |
||
803 | 'item_id' => $this->input->post('item_id'), |
||
804 | 'item_type' => $this->input->post('item_type'), |
||
805 | 'title' => htmlentities($this->input->post('title'), ENT_QUOTES, 'UTF-8'), |
||
806 | 'hidden' => $this->input->post('hidden'), |
||
807 | 'item_image' => $this->input->post('item_image'), |
||
808 | 'roles' => $roles, |
||
809 | 'parent_id' => $this->input->post('parent_id'), |
||
810 | 'position' => $position, |
||
811 | ]; |
||
812 | |||
813 | if ($item_data['item_type'] == 'module') { |
||
814 | $mod_info = [ |
||
815 | 'mod_name' => $this->input->post('item_id'), |
||
816 | 'method' => trim($this->input->post('method')), |
||
817 | 'newpage' => $this->input->post('newpage'), |
||
818 | ]; |
||
819 | |||
820 | $item_data['item_id'] = 0; |
||
821 | $item_data['add_data'] = serialize($mod_info); |
||
822 | } |
||
823 | |||
824 | View Code Duplication | if ($item_data['item_type'] == 'url') { |
|
825 | $item_data['item_id'] = 0; |
||
826 | $item_data['add_data'] = serialize(['url' => $this->input->post('url'), 'newpage' => $this->input->post('newpage')]); |
||
827 | } |
||
828 | |||
829 | if (!isset($item_data['add_data'])) { |
||
830 | $item_data['add_data'] = serialize(['newpage' => $this->input->post('newpage')]); |
||
831 | } |
||
832 | |||
833 | if ($this->input->post('update_id') == 0) { |
||
834 | // Insert new item |
||
835 | $this->menu_model->insert_item($item_data); |
||
836 | } else { |
||
837 | // Update item |
||
838 | $error = FALSE; |
||
839 | |||
840 | // Error: wrong parent id |
||
841 | if ($this->input->post('update_id') == $this->input->post('parent_id')) { |
||
842 | $error = TRUE; |
||
843 | } |
||
844 | |||
845 | // Error: don't place root menu in sub |
||
846 | $item = $this->menu_model->get_item($this->input->post('update_id')); |
||
847 | if ($item['parent_id'] == 0) { |
||
848 | $this->_get_sub_items($this->input->post('update_id')); |
||
849 | |||
850 | foreach ($this->sub_menus as $v) { |
||
851 | if ($v == $this->input->post('parent_id')) { |
||
852 | $error = TRUE; |
||
853 | } |
||
854 | } |
||
855 | } |
||
856 | |||
857 | if ($this->input->post('position_after') == 0) { |
||
858 | unset($item_data['position']); |
||
859 | } |
||
860 | |||
861 | if ($error == TRUE) { |
||
862 | return FALSE; |
||
863 | } else { |
||
864 | $this->db->where('id', $this->input->post('update_id')); |
||
865 | $this->db->update('menus_data', $item_data); |
||
866 | } |
||
867 | } |
||
868 | } |
||
869 | |||
870 | public function save_positions() { |
||
871 | |||
872 | //cp_check_perm('menu_edit'); |
||
873 | |||
874 | foreach ($this->input->post('positions') as $k => $v) { |
||
875 | $k = $k + 1; |
||
876 | $this->menu_model->set_item_position((int) $v, (int) $k); |
||
877 | } |
||
878 | showMessage(lang('Positions updated', 'menu')); |
||
879 | } |
||
880 | |||
881 | /** |
||
882 | * Create new menu |
||
883 | * |
||
884 | * @access public |
||
885 | */ |
||
886 | public function create_menu() { |
||
887 | |||
888 | //cp_check_perm('menu_create'); |
||
889 | View Code Duplication | if ($this->input->post('menu_name') == NULL) { |
|
890 | showMessage(lang('Name field sieve', 'menu'), '', 'r'); |
||
891 | |||
892 | exit; |
||
893 | } |
||
894 | $this->check_menu_data(); |
||
895 | |||
896 | $val = $this->form_validation; |
||
897 | $val->set_rules('menu_name', lang('Name', 'menu'), 'required|min_length[2]|max_length[25]|alpha_dash'); |
||
898 | $val->set_rules('main_title', lang('Name', 'menu'), 'required|max_length[100]'); |
||
899 | // $val->set_rules('menu_tpl', lang("Template folder", 'menu'), 'required|max_length[255]'); |
||
900 | $val->set_rules('menu_desc', lang('Description', 'menu'), 'max_length[500]'); |
||
901 | $val->set_rules('menu_expand_level', lang('Nesting level', 'menu'), 'numeric|max_length[2]'); |
||
902 | |||
903 | if ($this->form_validation->run($this) == FALSE) { |
||
904 | showMessage(validation_errors(), '', 'r'); |
||
905 | } else { |
||
906 | $data = [ |
||
907 | 'name' => $this->input->post('menu_name'), |
||
908 | 'main_title' => $this->input->post('main_title'), |
||
909 | 'description' => $this->input->post('menu_desc'), |
||
910 | 'tpl' => $this->input->post('menu_tpl'), |
||
911 | 'expand_level' => $this->input->post('menu_expand_level'), |
||
912 | 'created' => date('Y-m-d H:i:s'), |
||
913 | ]; |
||
914 | |||
915 | $menu_id = $this->menu_model->insert_menu($data); |
||
916 | |||
917 | $this->lib_admin->log(lang('Menu was created', 'menu') . '. Id: ' . $menu_id); |
||
918 | showMessage(lang('Menu created', 'menu')); |
||
919 | View Code Duplication | if ($this->input->post('action') == 'tomain') { |
|
920 | pjax('/admin/components/cp/menu'); |
||
921 | } else { |
||
922 | pjax('/admin/components/cp/menu/edit_menu/' . $menu_id); |
||
923 | } |
||
924 | } |
||
925 | } |
||
926 | |||
927 | /** |
||
928 | * @param int $id |
||
929 | */ |
||
930 | public function edit_menu($id) { |
||
931 | |||
932 | //cp_check_perm('menu_edit'); |
||
933 | $menu_data = $this->menu_model->get_menu($id); |
||
934 | $this->template->add_array($menu_data); |
||
935 | $this->display_tpl('edit_menu'); |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * @param int $id |
||
940 | */ |
||
941 | public function update_menu($id) { |
||
942 | |||
943 | $val = $this->form_validation; |
||
944 | $val->set_rules('menu_name', lang('Name', 'menu'), 'required|min_length[2]|max_length[25]|alpha_dash'); |
||
945 | $val->set_rules('main_title', lang('Title', 'menu'), 'required|max_length[100]'); |
||
946 | $val->set_rules('menu_desc', lang('Description', 'menu'), 'max_length[500]'); |
||
947 | $val->set_rules('menu_expand_level', lang('Nesting level', 'menu'), 'numeric|max_length[2]'); |
||
948 | |||
949 | if ($this->form_validation->run($this) == FALSE) { |
||
950 | showMessage(validation_errors(), '', 'r'); |
||
951 | } else { |
||
952 | |||
953 | $data = [ |
||
954 | 'name' => $this->input->post('menu_name'), |
||
955 | 'main_title' => $this->input->post('main_title'), |
||
956 | 'description' => $this->input->post('menu_desc'), |
||
957 | 'tpl' => $this->input->post('menu_tpl'), |
||
958 | 'expand_level' => $this->input->post('menu_expand_level'), |
||
959 | 'created' => date('Y-m-d H:i:s'), |
||
960 | ]; |
||
961 | |||
962 | $this->db->where('id', $id); |
||
963 | $this->db->update('menus', $data); |
||
964 | $this->lib_admin->log(lang('Menu was edited', 'menu') . '. Id: ' . $id); |
||
965 | showMessage(lang('Changes saved', 'menu')); |
||
966 | if ($this->input->post('action') == 'tomain') { |
||
967 | pjax('/admin/components/cp/menu'); |
||
968 | } |
||
969 | } |
||
970 | } |
||
971 | |||
972 | public function check_menu_data() { |
||
973 | |||
974 | View Code Duplication | if ($this->input->post('menu_name') == NULL) { |
|
975 | showMessage(lang('The field is required to be filled in'), false, 'r'); |
||
976 | exit; |
||
977 | } |
||
978 | |||
979 | View Code Duplication | if ($this->db->get_where('menus', ['name' => $this->input->post('menu_name')])->num_rows() > 0) { |
|
980 | showMessage(lang('The menu with the same name has been created yet'), false, 'r'); |
||
981 | exit; |
||
982 | } |
||
983 | } |
||
984 | |||
985 | /** |
||
986 | * @param null|string $name |
||
987 | */ |
||
988 | public function delete_menu($name = null) { |
||
989 | |||
990 | if ($name == null) { |
||
991 | $name = $this->input->post('ids'); |
||
992 | foreach ($name as $n) { |
||
993 | $this->menu->prepare_menu_array($n); |
||
994 | $this->root_menu = &$this->menu->menu_array; |
||
995 | $this->sub_menu = &$this->menu->sub_menu_array; |
||
996 | $this->process_root($this->root_menu); |
||
997 | //root menus array |
||
998 | foreach ($this->root_menu as $menu) { |
||
999 | $this->menu_model->delete_menu_item($menu['id']); |
||
1000 | } |
||
1001 | //sub menus array |
||
1002 | foreach ($this->sub_menu as $menu) { |
||
1003 | $this->menu_model->delete_menu_item($menu['id']); |
||
1004 | } |
||
1005 | //delete main menu |
||
1006 | $this->menu_model->delete_menu($n); |
||
1007 | } |
||
1008 | $this->lib_admin->log(lang('Menu removed', 'menu')); |
||
1009 | showMessage(lang('Menu removed', 'menu')); |
||
1010 | pjax('/admin/components/cp/menu'); |
||
1011 | } else { |
||
1012 | $this->menu->prepare_menu_array($name); |
||
1013 | $this->root_menu = &$this->menu->menu_array; |
||
1014 | $this->sub_menu = &$this->menu->sub_menu_array; |
||
1015 | $this->process_root($this->root_menu); |
||
1016 | //root menus array |
||
1017 | foreach ($this->root_menu as $menu) { |
||
1018 | $this->menu_model->delete_menu_item($menu['id']); |
||
1019 | } |
||
1020 | //sub menus array |
||
1021 | foreach ($this->sub_menu as $menu) { |
||
1022 | $this->menu_model->delete_menu_item($menu['id']); |
||
1023 | } |
||
1024 | //delete main menu |
||
1025 | $this->menu_model->delete_menu($name); |
||
1026 | $this->lib_admin->log(lang('Menu removed', 'menu') . '. Id: ' . $menu['id']); |
||
1027 | showMessage(lang('Menu removed', 'menu')); |
||
1028 | pjax('/admin/components/cp/menu'); |
||
1029 | } |
||
1030 | } |
||
1031 | |||
1032 | public function create_tpl() { |
||
1033 | |||
1034 | assetManager::create()->renderAdmin('create_menu'); |
||
1035 | } |
||
1036 | |||
1037 | /** |
||
1038 | * Get pages and return in JSON |
||
1039 | * @param int $cat_id |
||
1040 | * @param int $cur_page |
||
1041 | * @param null $referer |
||
1042 | */ |
||
1043 | public function get_pages($cat_id = 0, $cur_page = 0, $referer = null) { |
||
1044 | |||
1045 | $data['nav_count'] = []; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
1046 | $data['links'] = 0; |
||
1047 | $per_page = 10; |
||
1048 | if ($this->input->post('per_page')) { |
||
1049 | $per_page = (int) $this->input->post('per_page'); |
||
1050 | } |
||
1051 | $defaultLanguageId = CoreFactory::getConfiguration()->getDefaultLanguage()['id']; |
||
1052 | |||
1053 | //$per_page = (int) $this->input->post('per_page'); |
||
1054 | $this->db->select('content.id, content.title'); |
||
1055 | $this->db->select('route.url, route.parent_url as cat_url', false); |
||
1056 | $this->db->order_by('created', 'desc'); |
||
1057 | $this->db->join('route', 'route.id = content.route_id'); |
||
1058 | $this->db->where('lang', $defaultLanguageId); |
||
1059 | // $this->db->where('lang_alias', 0); |
||
1060 | $this->db->where('category', $cat_id); |
||
1061 | |||
1062 | View Code Duplication | if ($cur_page == 0) { |
|
1063 | $pages = $this->db->get('content', $per_page); |
||
1064 | } else { |
||
1065 | $pages = $this->db->get('content', $per_page, $per_page * $cur_page); |
||
1066 | } |
||
1067 | |||
1068 | if ($pages->num_rows() > 0) { |
||
1069 | $pages = $pages->result_array(); |
||
1070 | $data['pages_list'] = $pages; |
||
1071 | $total = $this->db->get_where('content', ['lang' => $this->default_lang_id, 'category' => $cat_id])->num_rows(); |
||
1072 | |||
1073 | $data['links'] = ceil($total / $per_page); |
||
1074 | if ($data['links'] == 1) { |
||
1075 | $data['links'] = 0; |
||
1076 | } |
||
1077 | |||
1078 | View Code Duplication | if ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') { |
|
1079 | if ($referer == 'controller') { |
||
1080 | return $data; |
||
1081 | } |
||
1082 | echo json_encode($data); |
||
1083 | } else { |
||
1084 | return $data; |
||
1085 | } |
||
1086 | } |
||
1087 | } |
||
1088 | |||
1089 | /** |
||
1090 | * Search pages |
||
1091 | * @param int $cur_page |
||
1092 | */ |
||
1093 | public function search_pages($cur_page = 0) { |
||
1094 | |||
1095 | $data['nav_count'] = []; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
1096 | $data['links'] = 0; |
||
1097 | |||
1098 | $per_page = (int) $this->input->post('per_page'); |
||
1099 | |||
1100 | $this->db->select('id, title, url, cat_url, category'); |
||
1101 | $this->db->order_by('created', 'desc'); |
||
1102 | // $this->db->where('lang_alias', 0); |
||
1103 | $this->db->where('lang', $this->default_lang_id); |
||
1104 | $this->db->like('title', $this->input->post('search')); |
||
1105 | |||
1106 | View Code Duplication | if ($cur_page == 0) { |
|
1107 | $pages = $this->db->get('content', $per_page); |
||
1108 | } else { |
||
1109 | $pages = $this->db->get('content', $per_page, $per_page * $cur_page); |
||
1110 | } |
||
1111 | |||
1112 | if ($pages->num_rows() > 0) { |
||
1113 | $pages = $pages->result_array(); |
||
1114 | |||
1115 | // Insert category names |
||
1116 | $this->load->library('lib_category'); |
||
1117 | $cnt = count($pages); |
||
1118 | for ($i = 0; $i < $cnt; $i++) { |
||
1119 | |||
1120 | $cat = $this->lib_category->get_category($pages[$i]['category']); |
||
1121 | |||
1122 | $name = ''; |
||
1123 | |||
1124 | if ($cat['parent_id'] != 0) { |
||
1125 | foreach ($cat['path'] as $path) { |
||
1126 | $c = $this->lib_category->get_category_by('url', $path); |
||
1127 | $name .= $c['name'] . ' → '; |
||
1128 | } |
||
1129 | } else { |
||
1130 | $name = $cat['name'] . ' → '; |
||
1131 | } |
||
1132 | |||
1133 | if ($pages[$i]['category'] == 0) { |
||
1134 | $pages[$i]['cat_name'] = lang('Without category', 'menu') . ' → '; |
||
1135 | } else { |
||
1136 | $pages[$i]['cat_name'] = $name; |
||
1137 | } |
||
1138 | } |
||
1139 | |||
1140 | $data['pages_list'] = $pages; |
||
1141 | |||
1142 | $this->db->select('id'); |
||
1143 | $this->db->where('lang', $this->default_lang_id); |
||
1144 | // $this->db->where('lang_alias', 0); |
||
1145 | $this->db->like('title', $this->input->post('search')); |
||
1146 | $total = $this->db->get('content')->num_rows(); |
||
1147 | |||
1148 | $data['links'] = ceil($total / $per_page); |
||
1149 | |||
1150 | if ($data['links'] == 1) { |
||
1151 | $data['links'] = 0; |
||
1152 | } |
||
1153 | |||
1154 | echo json_encode($data); |
||
1155 | } |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Ajax function |
||
1160 | * Load item data and return it in Json |
||
1161 | */ |
||
1162 | public function get_item() { |
||
1163 | |||
1164 | $item_id = (int) $this->input->post('item_id'); |
||
1165 | |||
1166 | $this->db->where('id', $item_id); |
||
1167 | $query = $this->db->get('menus_data'); |
||
1168 | |||
1169 | if ($query->num_rows() > 0) { |
||
1170 | $data = $query->row_array(); |
||
1171 | |||
1172 | if (!empty($data['add_data'])) { |
||
1173 | $data['add_data'] = unserialize($data['add_data']); |
||
1174 | } |
||
1175 | |||
1176 | $data['roles'] = unserialize($data['roles']); |
||
1177 | |||
1178 | if ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') { |
||
1179 | echo json_encode($data); |
||
1180 | } else { |
||
1181 | return $data; |
||
1182 | } |
||
1183 | } |
||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * |
||
1188 | * @param string $file |
||
1189 | */ |
||
1190 | public function display_tpl($file) { |
||
1191 | |||
1192 | $file = realpath(__DIR__) . '/templates/' . $file; |
||
1193 | $this->template->show('file:' . $file); |
||
1194 | } |
||
1195 | |||
1196 | public function fetch_tpl($file) { |
||
1197 | |||
1198 | $file = realpath(__DIR__) . '/templates/' . $file . '.tpl'; |
||
1199 | return $this->template->fetch('file:' . $file); |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * @param int $id |
||
1204 | */ |
||
1205 | public function translate_window($id) { |
||
1206 | |||
1207 | $langs = $this->_get_langs(); |
||
1208 | |||
1209 | $n = 0; |
||
1210 | foreach ($langs as $l) { |
||
1211 | $t = $this->db->get_where('menu_translate', ['item_id' => $id, 'lang_id' => $l['id']]); |
||
1212 | |||
1213 | if ($t->num_rows() == 1) { |
||
1214 | $t = $t->row_array(); |
||
1215 | $langs[$n]['curt'] = $t['title']; |
||
1216 | } |
||
1217 | |||
1218 | $n++; |
||
1219 | } |
||
1220 | |||
1221 | $menu_id = $this->db->where('id', $id)->get('menus_data')->row()->menu_id; |
||
1222 | $menu_url = $this->db->where('id', $menu_id)->get('menus')->row()->name; |
||
1223 | |||
1224 | assetManager::create() |
||
1225 | ->setData('langs', $langs) |
||
1226 | ->setData('id', $id) |
||
1227 | ->setData('menu_name', $menu_url) |
||
1228 | ->renderAdmin('translate_item'); |
||
1229 | } |
||
1230 | |||
1231 | /** |
||
1232 | * @param int $id |
||
1233 | */ |
||
1234 | public function translate_item($id) { |
||
1235 | |||
1236 | $langs = $this->_get_langs(); |
||
1237 | |||
1238 | $this->db->where('item_id', $id); |
||
1239 | $this->db->delete('menu_translate'); |
||
1240 | |||
1241 | foreach ($langs as $lang) { |
||
1242 | $postLang = trim($this->input->post("lang_{$lang['id']}")); |
||
1243 | |||
1244 | if (isset($postLang)) { |
||
1245 | $data = [ |
||
1246 | 'item_id' => (int) $id, |
||
1247 | 'lang_id' => $lang['id'], |
||
1248 | 'title' => $postLang, |
||
1249 | ]; |
||
1250 | $this->db->insert('menu_translate', $data); |
||
1251 | } |
||
1252 | } |
||
1253 | showMessage(lang('Changes saved', 'menu')); |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * |
||
1258 | * @return array |
||
1259 | */ |
||
1260 | public function _get_langs() { |
||
1261 | |||
1262 | $query = $this->db->get('languages'); |
||
1263 | |||
1264 | if ($query->num_rows() > 0) { |
||
1265 | return $query->result_array(); |
||
1266 | } else { |
||
1267 | return []; |
||
1268 | } |
||
1269 | } |
||
1270 | |||
1271 | public function change_hidden() { |
||
1272 | |||
1273 | $id = $this->input->post('id'); |
||
1274 | $hidden = $this->db->where('id', $id)->get('menus_data')->row(); |
||
1275 | $hidden = $hidden->hidden; |
||
1276 | if ($hidden == 1) { |
||
1277 | $hidden = 0; |
||
1278 | } else { |
||
1279 | $hidden = 1; |
||
1280 | } |
||
1281 | $data = ['hidden' => $hidden]; |
||
1282 | $this->menu_model->update_item($id, $data); |
||
1283 | } |
||
1284 | |||
1285 | /** |
||
1286 | * @param int $parent_id |
||
1287 | * @param int $menu_id |
||
1288 | */ |
||
1289 | public function get_children_items($parent_id, $menu_id) { |
||
1290 | |||
1291 | $result = $this->db->select('id, title')->where('parent_id', $parent_id)->where('menu_id', $menu_id)->get('menus_data')->result_array(); |
||
1292 | $html .= "<option value='0'> " . lang('No', 'menu') . ' </option>'; |
||
0 ignored issues
–
show
|
|||
1293 | $html .= "<option value='first'> " . lang('First', 'menu') . ' </option>'; |
||
1294 | if (count($result) > 0) { |
||
1295 | foreach ($result as $item) { |
||
1296 | $html .= "<option value='" . $item['id'] . "'> - " . $item['title'] . '</option>'; |
||
1297 | } |
||
1298 | } |
||
1299 | echo $html; |
||
1300 | } |
||
1301 | |||
1302 | } |
||
1303 | |||
1304 | /* End of file admin.php */ |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.