<?php
/**
 * Class represents records from table pages
 * {autogenerated}
 * @property int $page_id PK
 * @property string $title
 * @property string|null $desc
 * @property string|null $path
 * @property int|null $use_layout
 * @property string|null $html
 * @property datetime|null $dattm
 * @property int|null $onmenu
 * @property int|null $hide
 * @property string|null $tpl
 * @property string|null $meta_title
 * @property string|null $meta_keywords
 * @property string|null $meta_description
 * @property mixed|null $meta_robots
 * @property string|null $no_access_url
 * {/autogenerated}
 */
class Page extends ResourceAbstract
{
    public function getUrl()
    {
        return $this->path
            ? $this->getDi()->url("page/" . urlencode($this->path), false)
            : $this->getDi()->url("content/p/id/$this->page_id/", false);
    }

    public function render(Am_View $view, $user = null, $use_layout = true, $lang = null)
    {
        $l10n = null;

        if ($lang) {
            [$lang] = explode('_', $lang);
            $l10n = $this->getDi()->l10nPageTable->findFirstBy([
                'page_id' => $this->pk(),
                'lang' => $lang,
            ]);
        }

        $html = $l10n ? $l10n->html : $this->html;
        $meta_keywords = $l10n ? $l10n->meta_keywords : $this->meta_keywords;
        $meta_description = $l10n ? $l10n->meta_description : $this->meta_description;
        $meta_robots = $this->meta_robots;
        $meta_title = $l10n ? $l10n->meta_title : ___($this->meta_title);
        $title = $l10n ? $l10n->title : ___($this->title);
        $this_use_layout = $l10n ? $l10n->use_layout : $this->use_layout;
        $tpl = $l10n ? $l10n->tpl : $this->tpl;

        $t = new Am_SimpleTemplate();
        if ($user)
            $t->assign('user', $user);
        $t->assignStdVars();

        $e = new Am_Event_PageBeforeRender(Am_Event::PAGE_BEFORE_RENDER, [
            'template' => $t,
            'user' => $user,
            'page' => $this,
            'html' => $html,
        ]);
        $this->getDi()->hook->call($e);

        $html = $t->render($e->getHtml());

        if ($use_layout && $this_use_layout) {
            $view->page = $this;
            $view->content = '<div class="am-content-page">' . $html . '</div>';
            $view->title = $title;
            $view->meta_title = $meta_title ?: $title;
            if ($meta_keywords)
                $view->headMeta()->setName('keywords', $meta_keywords);
            if ($meta_description)
                $view->headMeta()->setName('description', $meta_description);
            if ($this->meta_robots)
                $view->headMeta()->setName('robots', $meta_robots);

            return $view->render($tpl ?: (is_bool($use_layout) ? 'layout.phtml' : $use_layout));
        } else {
            return $html;
        }
    }

    function delete()
    {
        foreach ($this->getDi()->l10nPageTable->findByPageId($this->pk()) as $t) {
            $t->delete();
        }
        return parent::delete();
    }
}

/**
 * @extends ResourceAbstractTable<Page>
 * @method Page[] findByOnmenu($value, $start = null, $count = null, $orderBy = null)
 * @method Page|null findFirstByPath($value)
 */
class PageTable extends ResourceAbstractTable
{
    protected $_key = 'page_id';
    protected $_table = '?_page';
    protected $_recordClass = 'Page';

    public function getAccessType()
    {
        return ResourceAccess::PAGE;
    }

    public function getAccessTitle()
    {
        return ___('Pages');
    }

    public function getPageId()
    {
        return 'pages';
    }
}