<?php
class PageAdmin extends Buyers
{
    //файлы и папки которые исключены из списка
    private $excludes = array('..', '.', 'system', 'components', 'modal', 'notify', 'index.tpl', 'breadcrumbs.tpl', 'blog.tpl', 'post.tpl', 'pagination.tpl', 'banners.tpl');

    function fetch()
    {
        $related_pages = array();
        $properties = array();
        if($this->request->method('post'))
        {
            $page = new stdClass;
            $page->id = $this->request->post('id', 'integer');
            $page->parent_id = $this->request->post('parent_id', 'integer');
            $page->name = $this->request->post('name');
            $page->header = $this->request->post('header');

            $page->url = $this->request->post('url');
            $page->meta_title = $this->request->post('meta_title');
            $page->meta_description = $this->request->post('meta_description');

            $page->annotation = $this->request->post('annotation');
            $page->body = $this->request->post('body');

            $page->template = $this->request->post('template');

            $page->visible = $this->request->post('visible', 'integer');
            $page->is_menu = $this->request->post('is_menu', 'integer');
            $page->visible_submenu = $this->request->post('visible_submenu', 'integer');

            // Связанные
            if(!empty($this->request->post('related_pages')))
            {
                foreach($this->request->post('related_pages') as $p)
                {
                    $related_pages[$p] = new stdClass;
                    $related_pages[$p]->page_id = &$page->id;
                    $related_pages[$p]->related_id = $p;
                }
            }

            // получим переменные
            if(!empty($this->request->post('properties')))
            {
                foreach($this->request->post('properties') AS $n => $nv)
                {
                    foreach($nv as $i=>$v)
                    {
                        if(empty($properties[$i]))
                        {
                            $properties[$i] = new stdClass;
                            $properties[$i]->page_id = &$page->id;
                            $properties[$i]->template = $page->template;
                        }
                        $properties[$i]->$n = $v;
                    }
                }
            }

            // Не допустить одинаковые URL разделов.
            if(($c = $this->pages->get_page($page->url)) && $c->id!=$page->id)
            {
                $this->design->assign('message_error', 'url_exists');
            }
            elseif(empty($page->name))
            {
                $this->design->assign('message_error', 'name_empty');
            }
            else
            {
                if(empty($page->id))
                {
                    $status = "added";
                    $page->id = $this->pages->add_page($page);
                }
                else
                {
                    $status = "updated";
                    $this->pages->update_page($page->id, $page);
                }

                //обновляем файлы
                $this->files->update_files($page->id);

                //обновляем фотки
                $this->images->update_images($page->id);

                // Обновим связанные товары
                $this->pages->delete_related_pages($page->id);
                $this->pages->add_related_pages($page->id, $related_pages);

                //удалим старые переменные и добавим заново
                $this->db->query("DELETE FROM properties WHERE page_id = ?", $page->id);
                $this->pages->add_properties($properties);

                $this->request->redirect(array('module'=>'PageAdmin', 'id'=>$page->id, 'message_success'=>$status));
            }
        }

        $id = $this->request->get('id', 'integer');

        //для автоматическго заполнения родителя и шаблона при создании страницы
        $this->db->query("SELECT parent_id, template FROM pages ORDER BY id DESC LIMIT 1");
        $last_page = $this->db->result();

        if(!$page = $this->pages->get_page($id))
            $page = (object) array("template" => $last_page->template, "parent_id"=>$last_page->parent_id, "visible"=> 1, "files"=>[], "related_pages"=>[]);

        $page->related_pages = $this->pages->get_related_pages($id);
        $page->template_properties = $this->pages->get_properties($page->template);

        $this->design->assign('page', $page);
        $this->design->assign('pages', $this->pages->get_pages_tree());
        $this->design->assign('templates', $this->get_templates());

        return  $this->design->fetch('page.tpl');
    }

    //список доступных шаблонов
    protected function get_templates()
    {
        $dir = "design/".$this->settings->theme."/html/";
        return array_diff(scandir($dir), $this->excludes);
    }
}
