diff options
author | Dmitry <dmitry@dhtmlx.com> | 2012-03-27 12:03:37 +0200 |
---|---|---|
committer | Dmitry <dmitry@dhtmlx.com> | 2012-03-27 12:03:37 +0200 |
commit | 42a33ffe00650aa354e9ea7752c7e57bf9eb8a9f (patch) | |
tree | 42cc2bd66615d6b841afc22bb904737802e8eb91 /codebase | |
parent | 97ab467fd6c5671eb81b2110053676b555b97df6 (diff) | |
parent | 98542f4bab255fc1ec846c7e22688bf1d55aa716 (diff) | |
download | connector-php-42a33ffe00650aa354e9ea7752c7e57bf9eb8a9f.zip connector-php-42a33ffe00650aa354e9ea7752c7e57bf9eb8a9f.tar.gz connector-php-42a33ffe00650aa354e9ea7752c7e57bf9eb8a9f.tar.bz2 |
Merge branch 'master' of 192.168.1.251:connector-php
Diffstat (limited to 'codebase')
-rw-r--r-- | codebase/data_connector.php | 197 |
1 files changed, 190 insertions, 7 deletions
diff --git a/codebase/data_connector.php b/codebase/data_connector.php index 042a20b..08ae891 100644 --- a/codebase/data_connector.php +++ b/codebase/data_connector.php @@ -57,13 +57,16 @@ class CommonDataItem extends DataItem{ */ function to_xml(){ if ($this->skip) return ""; - - $str="<item id='".$this->get_id()."' >"; - for ($i=0; $i<sizeof($this->config->text); $i++){ - $extra = $this->config->text[$i]["name"]; - $str.="<".$extra."><![CDATA[".$this->data[$extra]."]]></".$extra.">"; + return $this->to_xml_start().$this->to_xml_end(); + } + + function to_xml_start(){ + $str="<item id='".$this->get_id()."' "; + for ($i=0; $i < sizeof($this->config->text); $i++){ + $name=$this->config->text[$i]["name"]; + $str.=" ".$name."='".$this->xmlentities($this->data[$name])."'"; } - return $str."</item>"; + return $str.">"; } } @@ -117,6 +120,9 @@ class DataConnector extends Connector{ parent::parse_request(); } + + if (isset($_GET["start"]) && isset($_GET["count"])) + $this->request->set_limit($_GET["start"],$_GET["count"]); } /*! renders self as xml, starting part @@ -138,6 +144,13 @@ class JSONDataConnector extends DataConnector{ $start = "[\n"; $end = substr($this->render_set($res),0,-2)."\n]"; + if ($this->dload){ + $start = "{ \"data\":".$start.$end; + if ($pos=$this->request->get_start()) + $end = ", \"pos\":".$pos." }"; + else + $end = ", \"pos\":0, \"total_count\":".$this->sql->get_size($this->request)." }"; + } $out = new OutputWriter($start, $end); $out->set_type("json"); $this->event->trigger("beforeOutput", $this, $out); @@ -160,4 +173,174 @@ class JSONCommonDataItem extends DataItem{ } } -?>
\ No newline at end of file +class TreeCommonDataItem extends CommonDataItem{ + protected $kids=-1; + + function to_xml_start(){ + $str="<item id='".$this->get_id()."' "; + for ($i=0; $i < sizeof($this->config->text); $i++){ + $name=$this->config->text[$i]["name"]; + $str.=" ".$name."='".$this->xmlentities($this->data[$name])."'"; + } + + if ($this->kids === true) + $str .=" dhx_kids='1'"; + + return $str.">"; + } + + function has_kids(){ + return $this->kids; + } + + function set_kids($value){ + $this->kids=$value; + } +} + + +class TreeDataConnector extends DataConnector{ + public function __construct($res,$type=false,$item_type=false,$data_type=false){ + if (!$item_type) $item_type="TreeCommonDataItem"; + if (!$data_type) $data_type="CommonDataProcessor"; + parent::__construct($res,$type,$item_type,$data_type); + + $this->event->attach("afterInsert",array($this,"parent_id_correction_a")); + $this->event->attach("beforeProcessing",array($this,"parent_id_correction_b")); + } + + protected function xml_start(){ + return "<data parent='".$this->request->get_relation()."'>"; + } + + /*! store info about ID changes during insert operation + @param dataAction + data action object during insert operation + */ + public function parent_id_correction_a($dataAction){ + $this->id_swap[$dataAction->get_id()]=$dataAction->get_new_id(); + } + /*! update ID if it was affected by previous operation + @param dataAction + data action object, before any processing operation + */ + public function parent_id_correction_b($dataAction){ + $relation = $this->config->relation_id["db_name"]; + $value = $dataAction->get_value($relation); + + if (array_key_exists($value,$this->id_swap)) + $dataAction->set_value($relation,$this->id_swap[$value]); + } + + //parse GET scoope, all operations with incoming request must be done here + protected function parse_request(){ + parent::parse_request(); + + if (isset($_GET["parent"])) + $this->request->set_relation($_GET["parent"]); + else + $this->request->set_relation("0"); + } + + protected function render_set($res){ + $output=""; + $index=0; + while ($data=$this->sql->get_next($res)){ + $data = new $this->names["item_class"]($data,$this->config,$index); + $this->event->trigger("beforeRender",$data); + //there is no info about child elements, + //if we are using dyn. loading - assume that it has, + //in normal mode just exec sub-render routine + if ($data->has_kids()===-1 && $this->dload) + $data->set_kids(true); + $output.=$data->to_xml_start(); + if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$this->dload)){ + $sub_request = new DataRequestConfig($this->request); + $sub_request->set_relation($data->get_id()); + $output.=$this->render_set($this->sql->select($sub_request)); + } + $output.=$data->to_xml_end(); + $index++; + } + return $output; + } + +} + + +class JSONTreeDataConnector extends TreeDataConnector{ + + public function __construct($res,$type=false,$item_type=false,$data_type=false){ + if (!$item_type) $item_type="JSONTreeCommonDataItem"; + if (!$data_type) $data_type="CommonDataProcessor"; + parent::__construct($res,$type,$item_type,$data_type); + + $this->event->attach("afterInsert",array($this,"parent_id_correction_a")); + $this->event->attach("beforeProcessing",array($this,"parent_id_correction_b")); + } + + protected function render_set($res){ + $output=array(); + $index=0; + while ($data=$this->sql->get_next($res)){ + $data = new $this->names["item_class"]($data,$this->config,$index); + $this->event->trigger("beforeRender",$data); + //there is no info about child elements, + //if we are using dyn. loading - assume that it has, + //in normal mode just exec sub-render routine + if ($data->has_kids()===-1 && $this->dload) + $data->set_kids(true); + $record = &$data->to_xml_start(); + if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$this->dload)){ + $sub_request = new DataRequestConfig($this->request); + $sub_request->set_relation($data->get_id()); + $temp = &$this->render_set($this->sql->select($sub_request)); + if (sizeof($temp)) + $record["data"] = $temp; + } + $output[] = $record; + $index++; + } + return $output; + } + + protected function output_as_xml($res){ + $data = array(); + $data["parent"] = $this->request->get_relation(); + $data["data"] = $this->render_set($res); + + $out = new OutputWriter(json_encode($data), ""); + $out->set_type("json"); + $this->event->trigger("beforeOutput", $this, $out); + $out->output("", true, $this->encoding); + } + +} + + +class JSONTreeCommonDataItem extends TreeCommonDataItem{ + /*! return self as XML string + */ + function to_xml_start(){ + if ($this->skip) return ""; + + $data = array( "id" => $this->get_id() ); + for ($i=0; $i<sizeof($this->config->text); $i++){ + $extra = $this->config->text[$i]["name"]; + $data[$extra]=$this->data[$extra]; + } + + if ($this->kids === true) + $data["dhx_kids"] = 1; + + return $data; + } + + function to_xml_end(){ + return ""; + } +} + + +?> + |