summaryrefslogtreecommitdiffstats
path: root/codebase/Dhtmlx/Connector/Output
diff options
context:
space:
mode:
Diffstat (limited to 'codebase/Dhtmlx/Connector/Output')
-rw-r--r--codebase/Dhtmlx/Connector/Output/JSONRenderStrategy.php33
-rw-r--r--codebase/Dhtmlx/Connector/Output/JSONTreeRenderStrategy.php39
-rw-r--r--codebase/Dhtmlx/Connector/Output/OutputWriter.php40
-rw-r--r--codebase/Dhtmlx/Connector/Output/RenderStrategy.php115
-rw-r--r--codebase/Dhtmlx/Connector/Output/TreeRenderStrategy.php64
5 files changed, 291 insertions, 0 deletions
diff --git a/codebase/Dhtmlx/Connector/Output/JSONRenderStrategy.php b/codebase/Dhtmlx/Connector/Output/JSONRenderStrategy.php
new file mode 100644
index 0000000..fddfd3d
--- /dev/null
+++ b/codebase/Dhtmlx/Connector/Output/JSONRenderStrategy.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace DHTMLX\Connector\Output;
+
+class JSONRenderStrategy extends RenderStrategy {
+
+ /*! render from DB resultset
+ @param res
+ DB resultset
+ process commands, output requested data as json
+ */
+ public function render_set($res, $name, $dload, $sep, $config, $mix){
+ $output=array();
+ $index=0;
+ $conn = $this->conn;
+ $this->mix($config, $mix);
+ $conn->event->trigger("beforeRenderSet",$conn,$res,$config);
+ while ($data=$conn->sql->get_next($res)){
+ $data = $this->complex_mix($mix, $data);
+ $data = new $name($data,$config,$index);
+ if ($data->get_id()===false)
+ $data->set_id($conn->uuid());
+ $conn->event->trigger("beforeRender",$data);
+ $item = $data->to_xml();
+ if ($item !== false)
+ $output[]=$item;
+ $index++;
+ }
+ $this->unmix($config, $mix);
+ return $output;
+ }
+
+}
diff --git a/codebase/Dhtmlx/Connector/Output/JSONTreeRenderStrategy.php b/codebase/Dhtmlx/Connector/Output/JSONTreeRenderStrategy.php
new file mode 100644
index 0000000..04a8672
--- /dev/null
+++ b/codebase/Dhtmlx/Connector/Output/JSONTreeRenderStrategy.php
@@ -0,0 +1,39 @@
+<?php
+namespace DHTMLX\Connector\Output;
+
+class JSONTreeRenderStrategy extends TreeRenderStrategy {
+
+ public function render_set($res, $name, $dload, $sep, $config,$mix){
+ $output=array();
+ $index=0;
+ $conn = $this->conn;
+ $config_copy = new DataConfig($config);
+ $this->mix($config, $mix);
+ while ($data=$conn->sql->get_next($res)){
+ $data = $this->complex_mix($mix, $data);
+ $data = new $name($data,$config,$index);
+ $conn->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 && $dload)
+ $data->set_kids(true);
+ $record = $data->to_xml_start();
+ if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload)){
+ $sub_request = new DataRequestConfig($conn->get_request());
+ //$sub_request->set_fieldset(implode(",",$config_copy->db_names_list($conn->sql)));
+ $sub_request->set_relation($data->get_id());
+ //$sub_request->set_filters(array());
+ $temp = $this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config_copy, $mix);
+ if (sizeof($temp))
+ $record["data"] = $temp;
+ }
+ if ($record !== false)
+ $output[] = $record;
+ $index++;
+ }
+ $this->unmix($config, $mix);
+ return $output;
+ }
+
+} \ No newline at end of file
diff --git a/codebase/Dhtmlx/Connector/Output/OutputWriter.php b/codebase/Dhtmlx/Connector/Output/OutputWriter.php
new file mode 100644
index 0000000..96bb49a
--- /dev/null
+++ b/codebase/Dhtmlx/Connector/Output/OutputWriter.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace DHTMLX\Connector\Output;
+
+class OutputWriter{
+ private $start;
+ private $end;
+ private $type;
+
+ public function __construct($start, $end = ""){
+ $this->start = $start;
+ $this->end = $end;
+ $this->type = "xml";
+ }
+ public function add($add){
+ $this->start.=$add;
+ }
+ public function reset(){
+ $this->start="";
+ $this->end="";
+ }
+ public function set_type($add){
+ $this->type=$add;
+ }
+ public function output($name="", $inline=true, $encoding=""){
+ ob_clean();
+
+ if ($this->type == "xml"){
+ $header = "Content-type: text/xml";
+ if ("" != $encoding)
+ $header.="; charset=".$encoding;
+ header($header);
+ }
+
+ echo $this->__toString();
+ }
+ public function __toString(){
+ return $this->start.$this->end;
+ }
+} \ No newline at end of file
diff --git a/codebase/Dhtmlx/Connector/Output/RenderStrategy.php b/codebase/Dhtmlx/Connector/Output/RenderStrategy.php
new file mode 100644
index 0000000..9431d0d
--- /dev/null
+++ b/codebase/Dhtmlx/Connector/Output/RenderStrategy.php
@@ -0,0 +1,115 @@
+<?php
+namespace DHTMLX\Connector\Output;
+
+use DHTMLX\Connector\Data\DataItem;
+use DHTMLX\Connector\GridDataItem;
+class RenderStrategy {
+
+ protected $conn = null;
+
+ public function __construct($conn) {
+ $this->conn = $conn;
+ }
+
+ /*! adds mix fields into DataConfig
+ * @param config
+ * DataConfig object
+ * @param mix
+ * mix structure
+ */
+ protected function mix($config, $mix) {
+ for ($i = 0; $i < count($mix); $i++) {
+ if ($config->is_field($mix[$i]['name'])===-1) {
+ $config->add_field($mix[$i]['name']);
+ }
+ }
+ }
+
+ /*! remove mix fields from DataConfig
+ * @param config
+ * DataConfig object
+ * @param mix
+ * mix structure
+ */
+ protected function unmix($config, $mix) {
+ for ($i = 0; $i < count($mix); $i++) {
+ if ($config->is_field($mix[$i]['name'])!==-1) {
+ $config->remove_field_full($mix[$i]['name']);
+ }
+ }
+ }
+
+ /*! adds mix fields in item
+ * simple mix adds only strings specified by user
+ * @param mix
+ * mix structure
+ * @param data
+ * array of selected data
+ */
+ protected function simple_mix($mix, $data) {
+ // get mix details
+ for ($i = 0; $i < count($mix); $i++)
+ $data[$mix[$i]["name"]] = is_object($mix[$i]["value"]) ? "" : $mix[$i]["value"];
+ return $data;
+ }
+
+ /*! adds mix fields in item
+ * complex mix adds strings specified by user and results of subrequests
+ * @param mix
+ * mix structure
+ * @param data
+ * array of selected data
+ */
+ protected function complex_mix($mix, $data) {
+ // get mix details
+ for ($i = 0; $i < count($mix); $i++) {
+ $mixname = $mix[$i]["name"];
+ if ($mix[$i]['filter'] !== false) {
+ $subconn = $mix[$i]["value"];
+ $filter = $mix[$i]["filter"];
+
+ // setting relationships
+ $subconn->clear_filter();
+ foreach ($filter as $k => $v)
+ if (isset($data[$v]))
+ $subconn->filter($k, $data[$v], "=");
+ else
+ throw new Exception('There was no such data field registered as: '.$k);
+
+ $subconn->asString(true);
+ $data[$mixname]=$subconn->simple_render();
+ if (is_array($data[$mixname]) && count($data[$mixname]) == 1)
+ $data[$mixname] = $data[$mixname][0];
+ } else {
+ $data[$mixname] = $mix[$i]["value"];
+ }
+ }
+ return $data;
+ }
+
+ /*! render from DB resultset
+ @param res
+ DB resultset
+ process commands, output requested data as XML
+ */
+ public function render_set($res, $name, $dload, $sep, $config, $mix){
+ $output="";
+ $index=0;
+ $conn = $this->conn;
+ $this->mix($config, $mix);
+ $conn->event->trigger("beforeRenderSet",$conn,$res,$config);
+ while ($data=$conn->sql->get_next($res)){
+ $data = $this->simple_mix($mix, $data);
+
+ $data = new $name($data,$config,$index);
+ if ($data->get_id()===false)
+ $data->set_id($conn->uuid());
+ $conn->event->trigger("beforeRender",$data);
+ $output.=$data->to_xml().$sep;
+ $index++;
+ }
+ $this->unmix($config, $mix);
+ return $output;
+ }
+
+} \ No newline at end of file
diff --git a/codebase/Dhtmlx/Connector/Output/TreeRenderStrategy.php b/codebase/Dhtmlx/Connector/Output/TreeRenderStrategy.php
new file mode 100644
index 0000000..d66e58e
--- /dev/null
+++ b/codebase/Dhtmlx/Connector/Output/TreeRenderStrategy.php
@@ -0,0 +1,64 @@
+<?php
+namespace DHTMLX\Connector\Output;
+
+use DHTMLX\Connector\DataStorage\DataConfig;
+
+class TreeRenderStrategy extends RenderStrategy {
+
+ protected $id_swap = array();
+
+ public function __construct($conn) {
+ parent::__construct($conn);
+ $conn->event->attach("afterInsert",array($this,"parent_id_correction_a"));
+ $conn->event->attach("beforeProcessing",array($this,"parent_id_correction_b"));
+ }
+
+ public function render_set($res, $name, $dload, $sep, $config, $mix){
+ $output="";
+ $index=0;
+ $conn = $this->conn;
+ $config_copy = new DataConfig($config);
+ $this->mix($config, $mix);
+ while ($data=$conn->sql->get_next($res)){
+ $data = $this->simple_mix($mix, $data);
+ $data = new $name($data,$config,$index);
+ $conn->event->trigger("beforeRender",$data);
+ //there is no info about child elements,
+ //if we are using dyn. loading - assume that it has,
+ //in normal mode juse exec sub-render routine
+ if ($data->has_kids()===-1 && $dload)
+ $data->set_kids(true);
+ $output.=$data->to_xml_start();
+ if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload)){
+ $sub_request = new DataRequestConfig($conn->get_request());
+ //$sub_request->set_fieldset(implode(",",$config_copy->db_names_list($conn->sql)));
+ $sub_request->set_relation($data->get_id());
+ $output.=$this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config_copy, $mix);
+ }
+ $output.=$data->to_xml_end();
+ $index++;
+ }
+ $this->unmix($config, $mix);
+ return $output;
+ }
+
+ /*! 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->conn->get_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]);
+ }
+} \ No newline at end of file