<?php

class object_polygon_view extends module {

    public $_loc_vars = [ 'templ', 'obj_name' ];
    public $_get_vars = [ 'act', 'id'  ];
    public $obj;
    public $templ    = 'object_polygon_view.htm';
    public $onPage   = 20;
    public $nofilter = 0;

    public function _init() {
        $this->obj = new cobject($this->obj_name);
        return parent::_init();
    }

    public function _on_() {
        global $R, $db, $core_db;
        $tpl  = $R->getTpl($this->templ, 'body');
        $tplr = $R->getTpl($this->templ, 'row');

        $db2 = $this->obj->getDB()->q($this->obj->getSelectEx('id, title, color, ASTEXT(`geom`) as `geom` '));
        while($db2->nr()) {
            $tt = $tplr;

            $coords = $this->mysql2json($db2->f('geom'));
            $tt     = $R->set('coords', $coords, $tt);
            $tt     = $R->set('id', $db2->f('id'), $tt);
            $tt     = $R->set('title', htmlspecialchars_decode($db2->f('title')), $tt);
            $tt     = $R->set('color', $db2->f('color') ? $db2->f('color') : '#00FF00', $tt);

            $tpl = $R->parse('rows', $tt, $tpl);

        }




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



    public function mysql2json($str) {
        // POLYGON((59.94778267548529 30.047386140625,60.12503918773836 30.468986482421876,59.92986664045532 30.963371248046883,59.84083181279909 30.456626863281247,59.960180395813275 30.38933560351563,59.94778267548529 30.047386140625))
        preg_match_all('/POLYGON\(\((.*?)\)\)/', $str, $arr);
        $points = explode(',', $arr[1][0]);

        foreach($points as $point) {
            $ret[0][] = explode(' ', $point);
        }

        return json_encode($ret);
    }

}

?>