File: /home/temp/yarusvl.ru/adminx/core/LinksAdmin.php
<?php
require_once('api/Mgc.php');
class LinksAdmin extends Mgc {
public function fetch() {
$links = array();
/*Принимаем данные о меню*/
if($this->request->method('POST')) {
if ($this->request->post('menu_items')) {
foreach ($this->request->post('menu_items') as $field => $values) {
foreach ($values as $i => $v) {
if (empty($menu_items[$i])) {
$menu_items[$i] = new stdClass();
$menu_items[$i]->i_tm = $i;
}
$menu_items[$i]->$field = $v;
}
}
// сортируем по родителю
usort($menu_items, function ($item1, $item2) {
if ($item1->parent_index == $item2->parent_index) {
return $item1->i_tm - $item2->i_tm;
}
return strcmp($item1->parent_index, $item2->parent_index);
});
$tm = array();
$local = array(trim($this->config->root_url, "/"), trim(preg_replace("~^https?://~", "", $this->config->root_url), "/"));
foreach ($menu_items as $key => $item) {
foreach ($local as $l) {
$item->url = preg_replace("~^$l/?~", "", $item->url);
}
$tm[$item->index] = $item;
}
$menu_items = $tm;
}
/*Добавляем/обновляем меню*/
$menu_items_ids = array();
if(is_array($menu_items)) {
foreach ($menu_items as $i=>$item) {
if ($item->parent_index > 0) {
if (!isset($menu_items[$item->parent_index]->id)) {
unset($menu_items[$i]);
continue;
}
$item->parent_id = $menu_items[$item->parent_index]->id;
} else {
$item->parent_id = 0;
}
unset($item->index);
unset($item->parent_index);
unset($item->i_tm);
if (empty($item->id)) {
$item->id = $this->links->add_link_item($item);
} else {
$this->links->update_link_item($item->id, $item);
}
if ($item->id) {
$menu_items_ids[] = $item->id;
}
}
}
$this->db->query("SELECT id FROM __links");
foreach ($this->db->results('id') as $current_id) {
if (!in_array($current_id, $menu_items_ids)) {
$this->links->delete_link_item($current_id);
}
}
// Отсортировать элементы меню
asort($menu_items_ids);
$i = 0;
foreach($menu_items_ids as $menu_item_id) {
$this->links->update_link_item($menu_items_ids[$i], array('position'=>$menu_item_id));
$i++;
}
$links = $this->links->get_links_items_tree();
} else {
/*Отображение меню*/
$links = $this->links->get_links_items_tree();
}
$this->design->assign('menu_items', $links[0]);
return $this->design->fetch('links.tpl');
}
}