/**
  * The constructor of DeleteModel
  */
 public function __construct()
 {
     try {
         DeleteModel::init();
     } catch (Exception $e) {
         echo $e->getMessage();
     }
 }
 public function deleteAction()
 {
     if (Session::hasUser('admin')) {
         $deleteModel = new DeleteModel(Router::getId());
         $deleteModel->delete();
         $this->rewrite_file_alias();
         $this->redirect('/' . Router::get_alis_by_id(94, 'uk'));
     } else {
         throw new Exception('Access  denied', 403);
     }
 }
 public function edit($id, $with_without_menu = null)
 {
     $lang = Router::getLanguage();
     $placeholders = array('id' => $id);
     $dbc = Connect::getConnection();
     $sql = "SELECT bp_{$lang}.id_{$this->material_type} AS id FROM  {$this->material_type}_{$lang} bp_{$lang} JOIN {$this->material_type} bp ON bp.id = bp_{$lang}.id_{$this->material_type}\n        AND bp.id_page = :id";
     $date = $dbc->getDate($sql, $placeholders);
     $id_[$this->material_type] = $date[0]['id'];
     $placeholders = array('title' => $this->title, 'alias' => $this->new_alias, 'id_' . $this->material_type => $id_[$this->material_type]);
     $sql = "UPDATE `{$this->material_type}_{$lang}` SET `title`= :title,`alias`= :alias {$this->additional_fields_key_value} WHERE id_{$this->material_type} = :id_{$this->material_type} ";
     $sth = $dbc->getPDO()->prepare($sql);
     $sth->execute($placeholders);
     $publish = $this->publication ? 1 : 0;
     $placeholders = array('id' => $id, 'publish' => $publish);
     $sql = "UPDATE `pages` SET `status`= :publish WHERE id = :id";
     $sth = $dbc->getPDO()->prepare($sql);
     $sth->execute($placeholders);
     if ($this->img_name) {
         $placeholders = array('id' => $id, 'img' => $this->img);
         $sql = "UPDATE `{$this->material_type}` SET `img`= :img WHERE id_page = :id";
         $sth = $dbc->getPDO()->prepare($sql);
         $sth->execute($placeholders);
     }
     // Проверка - есть ли для этого докумета пункт меню
     $placeholders = array('id' => $id);
     $sql = "SELECT id FROM `main_menu` WHERE id_page = :id ";
     $date = $dbc->getDate($sql, $placeholders);
     $isInMenu = empty($date);
     //Если полученный массив пустой добавляем инф. в меню
     if ($isInMenu) {
         $placeholders = array('id_new_page' => $id, 'id_parent' => $this->id_parent);
         $sql = "INSERT INTO `main_menu`(`id_page`, `id_parent_page`, `status`) VALUES (:id_new_page,:id_parent,1)";
         $sth = $dbc->getPDO()->prepare($sql);
         $sth->execute($placeholders);
         $sql = "SELECT MAX(id) AS max_id FROM main_menu";
         $placeholders = array();
         $date = $dbc->getDate($sql, $placeholders);
         $id_new_menu = $date[0]['max_id'];
         $placeholders = array('id_new_menu' => $id_new_menu, 'title' => $this->title_or_menu_name, 'alias' => $this->new_alias);
         $lang = Config::get('default_language');
         $sql = "INSERT INTO `main_menu_{$lang}`(`id_main_menu`, `name`, `alias_menu`) VALUES (:id_new_menu,:title,:alias)";
         $sth = $dbc->getPDO()->prepare($sql);
         $sth->execute($placeholders);
         foreach (Config::get('languages') as $v) {
             if ($v != $lang) {
                 $placeholders = array();
                 $sql = "SELECT `alias_menu` FROM main_menu_{$v} WHERE id_main_menu =\n                (SELECT id FROM main_menu WHERE id_page = (SELECT id_parent_page FROM main_menu WHERE id_page = {$id}) )";
                 $data = $dbc->getDate($sql, $placeholders);
                 $parent_alias = $data[0]['alias_menu'];
                 $sql = "SELECT n_{$v}.title FROM {$this->material_type}_{$v} n_{$v} JOIN {$this->material_type} n ON n_{$v}.id_{$this->material_type} = n.id AND n.id_page = {$id}";
                 $data = $dbc->getDate($sql, $placeholders);
                 $title = $data[0]['title'];
                 $translitClass = new Translit($title);
                 $translit = $translitClass->translit;
                 $alias_arr = array($parent_alias, $translit);
                 $new_alias = implode('/', $alias_arr);
                 $placeholders = array('id_new_menu' => $id_new_menu, 'title' => $title, 'alias' => $new_alias);
                 $sql = "INSERT INTO `main_menu_{$v}`(`id_main_menu`, `name`, `alias_menu`) VALUES (:id_new_menu,:title,:alias)";
                 $sth = $dbc->getPDO()->prepare($sql);
                 $sth->execute($placeholders);
             }
         }
     }
     if (!isset($with_without_menu)) {
         $lang = Config::get('default_language');
         $this->edit_menu($id, $lang, $this->new_alias);
         foreach (Config::get('languages') as $v) {
             if ($v != $lang) {
                 $sql = "SELECT `alias_menu` FROM main_menu_{$v} WHERE id_main_menu =\n                (SELECT id FROM main_menu WHERE id_page = (SELECT id_parent_page FROM main_menu WHERE id_page = {$id}) )";
                 $data = $dbc->getDate($sql, $placeholders);
                 $parent_alias = $data ? $data[0]['alias_menu'] : null;
                 // Debugger::PrintR($data);
                 $sql = "SELECT mm_{$v}.alias_menu, mm_{$v}.name  FROM main_menu_{$v} mm_{$v} JOIN main_menu mm ON mm_{$v}.id_main_menu = mm.id AND mm.id_page = {$id}";
                 $data = $dbc->getDate($sql, $placeholders);
                 $old_alias = $data[0]['alias_menu'];
                 $alias_arr = explode('/', $old_alias);
                 $last_element = array_pop($alias_arr);
                 $alias_arr = array($parent_alias, $last_element);
                 $new_alias = $alias_arr[0] ? implode('/', $alias_arr) : $alias_arr[1];
                 $title = $data[0]['name'];
                 $sql = "UPDATE {$this->material_type}_{$v} bp_{$v} JOIN {$this->material_type} bp SET `alias`= " . '"' . $new_alias . '"' . " WHERE bp_{$v}.id_{$this->material_type} = bp.id AND bp.id_page = {$id} ";
                 $placeholders = array();
                 $sth = $dbc->getPDO()->prepare($sql);
                 $sth->execute($placeholders);
                 $this->edit_menu($id, $v, $new_alias, $title);
             }
         }
     } else {
         $edit = 1;
         $deleteModel = new DeleteModel($id, $edit);
         $deleteModel->delete_from_menu();
     }
 }