summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry <dmitry@dhtmlx.com>2012-07-10 15:52:52 +0200
committerDmitry <dmitry@dhtmlx.com>2012-07-10 15:53:23 +0200
commite185ba6f5e28429e209774ca21715b1342fb73eb (patch)
treeb8225632c6a6f93eaf8a8b5afb15cbfc9a658f90
parent43e205b14de20ebbb814ffd5cc821bd70a8df8f4 (diff)
downloadconnector-php-e185ba6f5e28429e209774ca21715b1342fb73eb.zip
connector-php-e185ba6f5e28429e209774ca21715b1342fb73eb.tar.gz
connector-php-e185ba6f5e28429e209774ca21715b1342fb73eb.tar.bz2
implements MixedConnector
-rw-r--r--codebase/base_connector.php13
-rw-r--r--codebase/data_connector.php48
-rw-r--r--codebase/mixed_connector.php28
-rw-r--r--codebase/scheduler_connector.php2
4 files changed, 71 insertions, 20 deletions
diff --git a/codebase/base_connector.php b/codebase/base_connector.php
index ac81c9c..8cf3625 100644
--- a/codebase/base_connector.php
+++ b/codebase/base_connector.php
@@ -301,6 +301,7 @@ class Connector {
protected $live_update = false; // actions table name for autoupdating
protected $extra_output="";//!< extra info which need to be sent to client side
protected $options=array();//!< hash of OptionsConnector
+ protected $as_string = false;
/*! constructor
@@ -488,9 +489,11 @@ class Connector {
if ($this->model && method_exists($this->model, "get")){
$this->sql = new ArrayDBDataWrapper();
$result = new ArrayQueryWrapper(call_user_func(array($this->model, "get"), $this->request));
- $this->output_as_xml($result);
+ $out = $this->output_as_xml($result);
} else {
- $this->output_as_xml($this->get_resource());
+ $out = $this->output_as_xml($this->get_resource());
+
+ if ($out !== null) return $out;
}
}
@@ -788,6 +791,12 @@ class Connector {
$this->event->attach("beforeProcessing", Array($this->live_update, "check_collision"));
$this->event->attach("afterProcessing", Array($this->live_update, "log_operations"));
}
+
+ /*! render() returns result as string or send to response
+ */
+ public function asString($as_string) {
+ $this->as_string = $as_string;
+ }
}
diff --git a/codebase/data_connector.php b/codebase/data_connector.php
index b77d06b..b7cf4a5 100644
--- a/codebase/data_connector.php
+++ b/codebase/data_connector.php
@@ -213,42 +213,48 @@ class JSONDataConnector extends DataConnector{
}
protected function output_as_xml($res){
- $start = "";
- $end = "{ \"data\":[\n".substr($this->render_set($res),0,-2)."\n]";
-
- $collections = $this->fill_collections();
- if (!empty($this->extra_output))
- $end .= ', "collections": {'.$this->extra_output.'}';
-
+ $result = "[\n".substr($this->render_set($res),0,-2)."\n]";
+ $this->fill_collections();
$is_sections = sizeof($this->sections) && $this->is_first_call();
- if ($this->dload || $is_sections || sizeof($this->attributes)){
- $start = $start.$end;
- $end="";
+ if ($this->dload || $is_sections || sizeof($this->attributes) || !empty($this->extra_data)){
$attributes = "";
foreach($this->attributes as $k=>$v)
- $end .= ", ".$k.":\"".$v."\"";
+ $attributes .= ", ".$k.":\"".$v."\"";
+
+ if (!empty($this->extra_output))
+ $extra .= ', "collections": {'.$this->extra_output.'}';
+ $sections = "";
if ($is_sections){
//extra sections
foreach($this->sections as $k=>$v)
- $end.= ", ".$k.":".$v;
+ $sections .= ", ".$k.":".$v;
}
+ $dyn = "";
if ($this->dload){
//info for dyn. loadin
if ($pos=$this->request->get_start())
- $end .= ", \"pos\":".$pos;
+ $dyn .= ", \"pos\":".$pos;
else
- $end .= ", \"pos\":0, \"total_count\":".$this->sql->get_size($this->request);
+ $dyn .= ", \"pos\":0, \"total_count\":".$this->sql->get_size($this->request);
+ }
+ if ($attributes || $sections || $this->extra_output || $dyn) {
+ $result = "{ \"data\":".$result.$attributes.$this->extra_output.$sections.$dyn."}";
}
}
- $end .= " }";
- $out = new OutputWriter($start, $end);
+
+ // return as string
+ if ($this->as_string) return $result;
+
+ // output direct to response
+ $out = new OutputWriter($result, "");
$out->set_type("json");
$this->event->trigger("beforeOutput", $this, $out);
$out->output("", true, $this->encoding);
+ return null;
}
}
@@ -394,7 +400,7 @@ class TreeDataConnector extends DataConnector{
class JSONTreeDataConnector extends TreeDataConnector{
- public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type){
+ public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){
if (!$item_type) $item_type="JSONTreeCommonDataItem";
if (!$data_type) $data_type="CommonDataProcessor";
if (!$render_type) $render_type="JSONTreeRenderStrategy";
@@ -405,7 +411,13 @@ class JSONTreeDataConnector extends TreeDataConnector{
$data = array();
$data["parent"] = $this->request->get_relation();
$data["data"] = $this->render_set($res);
- $out = new OutputWriter(json_encode($data), "");
+ $data = json_encode($data);
+
+ // return as string
+ if ($this->as_string) return $data;
+
+ // output direct to response
+ $out = new OutputWriter($data, "");
$out->set_type("json");
$this->event->trigger("beforeOutput", $this, $out);
$out->output("", true, $this->encoding);
diff --git a/codebase/mixed_connector.php b/codebase/mixed_connector.php
new file mode 100644
index 0000000..461d6ec
--- /dev/null
+++ b/codebase/mixed_connector.php
@@ -0,0 +1,28 @@
+<?php
+/*
+ @author dhtmlx.com
+ @license GPL, see license.txt
+*/
+require_once("base_connector.php");
+
+class MixedConnector extends Connector {
+
+ protected $connectors = array();
+
+ public function add($name, $conn) {
+ $this->connectors[$name] = $conn;
+ }
+
+ public function render() {
+ $result = "{";
+ $parts = array();
+ foreach($this->connectors as $name => $conn) {
+ $conn->asString(true);
+ $parts[] = "\"".$name."\":".($conn->render())."\n";
+ }
+ $result .= implode(",\n", $parts)."}";
+ echo $result;
+ }
+}
+
+?> \ No newline at end of file
diff --git a/codebase/scheduler_connector.php b/codebase/scheduler_connector.php
index 11572b2..8ebe875 100644
--- a/codebase/scheduler_connector.php
+++ b/codebase/scheduler_connector.php
@@ -217,6 +217,8 @@ class JSONSchedulerConnector extends SchedulerConnector {
$data.=$this->render_set($res);
$data.=$this->xml_end();
+ if ($this->as_string) return $data;
+
$out = new OutputWriter($data, "");
$out->set_type("json");
$this->event->trigger("beforeOutput", $this, $out);