summaryrefslogtreecommitdiffstats
path: root/codebase
diff options
context:
space:
mode:
authorStanislav-Wolski <stan@dhtmlx.com>2012-03-23 19:11:25 +0300
committerStanislav-Wolski <stan@dhtmlx.com>2012-03-23 19:11:25 +0300
commit279209c3bf193a73eff49b34a1208b1698f2d7e0 (patch)
treef5bcea2a158bdb6ca6d1b2a6406bcc757528853d /codebase
parent2413df85344f0d6f7d8da8179979557f0d78511d (diff)
downloadconnector-php-279209c3bf193a73eff49b34a1208b1698f2d7e0.zip
connector-php-279209c3bf193a73eff49b34a1208b1698f2d7e0.tar.gz
connector-php-279209c3bf193a73eff49b34a1208b1698f2d7e0.tar.bz2
[add] common-tree connectors
Diffstat (limited to 'codebase')
-rw-r--r--codebase/data_connector.php186
1 files changed, 179 insertions, 7 deletions
diff --git a/codebase/data_connector.php b/codebase/data_connector.php
index e6b5420..2789224 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.">";
}
}
@@ -170,4 +173,173 @@ class JSONCommonDataItem extends DataItem{
}
}
-?> \ No newline at end of file
+class TreeCommonDataItem extends CommonDataItem{
+ private $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 "";
+ }
+}
+
+
+?>
+