<?php



class site_index extends module {

    public $apiKey = '660e8ed3-a21d-4a59-9403-f315dc8aa211';
    public $suggestKey = '1749711d-e092-4206-ae0a-a332c44d8cb0';
	public $templBlade = 'site_index';

    public $_get_vars = ['act'];
    public $_post_vars = ['act', 'coords', 'polygon_id'];


	function _on_() {
		global $blade;
        $data = [];
        $data['apikey'] = $this->apiKey;
        $data['suggest_apikey'] = $this->suggestKey;


        $obj = new cobject('shop_delivery_zones');

        // полигоны
        $db2 = $obj->getDB()->q($obj->getSelectEx('id, title, ASTEXT(`geom`) as `geom`, color '));
        while($db2->nr()) {

            $coords = $this->mysql2json($db2->f('geom'));
            $row = [];
            $row['coords'] = $coords;
            $row['id'] = $db2->f('id');
            $row['title'] = $db2->f('title');
            $row['color'] = $db2->f('color');

            $data['rows'][] = $row;
        }

		return $blade->run($this->getBladeTempl('main'), $data);
	}

    function _on_getInfo()
    {
        global $blade;

        $data = [];
        return $blade->run($this->getBladeTempl('data'), $data);
    }

    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);
    }

}
	



?>