<?php

class object_view extends module {

    public $_loc_vars = [ 'templ', 'obj_name', 'sql_where', 'onPage'  ];
    public $_get_vars = [ 'act', 'id'  ];
    public $obj;
    public $templ  = 'object_view.htm';
    public $onPage = 20;


    public function _init() {
        $this->obj = new cobject($this->obj_name);
        if ($this->id && !$this->act) {
            $this->act = 'view';
        }
        return parent::_init();
    }

    public function _on_() {
        global $R, $db, $core_db;

        $tpl      = $R->getTpl($this->templ, 'body');
        $tpl_void = $R->getTpl($this->templ, 'body_void');
        $tplr     = $R->getTpl($this->templ, 'row');
        if ($this->obj->global_type == 1) {
            $curdb = $core_db;
        } else {
            $curdb = $db;
        }
        $db2 = $curdb->q($this->obj->getSelectEx('COUNT(*) as `cnt`', $this->sql_where));
        $db2->r();
        $size = $db2->f('cnt');

        if (!$size) {
            return $R->clear($tpl_void);
        }

        $pw     = new Walker($size, $this->onPage);
        $filter = new filter($this->obj_name);
        $filter->parse();
        $tpl = $R->set('filter', $filter->getHTML(), $tpl);

        $db2 = $curdb->q($this->obj->getSelect($filter->getSQL() . $this->sql_where . $pw->getSQL()));
        while ($db2->r()) {
            $this->obj->lRow($db2);
            $tt  = $this->obj->assign($tplr);
            $tpl = $R->parse('rows', $tt, $tpl);
        }

        $this->obj->byId($this->id);
        $tpl = $R->set('walker', $pw->getHTML(), $tpl);

        return $R->clear($tpl);
    }

    public function _on_view() {
        global $R, $cat;
        $this->obj->byId($this->id);
        $tpl = $R->getTpl($this->templ, 'card');
        $tpl = $this->obj->assign($tpl);

        if ($this->obj->get('title')) {
            $cat->addTitle($this->obj->get('title'));
        }
        if ($this->obj->get('page_keywords')) {
            $cat->setKeywords($this->obj->get('page_keywords'));
        }

        if ($this->obj->get('page_description')) {
            $cat->setDescription($this->obj->get('page_description'));
        }

        return $R->clear($tpl);
    }




}

?>