diff options
author | Dmitry <dmitry@dhtmlx.com> | 2012-07-12 13:31:01 +0200 |
---|---|---|
committer | Dmitry <dmitry@dhtmlx.com> | 2012-07-12 13:31:01 +0200 |
commit | 292bdb8b3b444dc480c6039a3b7acd0d5f60aa2a (patch) | |
tree | 5cb0efb2bb6518766294dd55394f448249921eb9 /codebase | |
parent | 2bc0d779055d2d3f31ba1dfcaec6777ab22d2c82 (diff) | |
download | connector-php-292bdb8b3b444dc480c6039a3b7acd0d5f60aa2a.zip connector-php-292bdb8b3b444dc480c6039a3b7acd0d5f60aa2a.tar.gz connector-php-292bdb8b3b444dc480c6039a3b7acd0d5f60aa2a.tar.bz2 |
implement complex mix using
Diffstat (limited to 'codebase')
-rw-r--r-- | codebase/base_connector.php | 35 | ||||
-rw-r--r-- | codebase/data_connector.php | 24 | ||||
-rw-r--r-- | codebase/db_common.php | 16 | ||||
-rw-r--r-- | codebase/scheduler_connector.php | 7 | ||||
-rw-r--r-- | codebase/strategy.php | 120 | ||||
-rw-r--r-- | codebase/treedatamultitable_connector.php | 10 | ||||
-rw-r--r-- | codebase/xss_filter.php | 2 |
7 files changed, 181 insertions, 33 deletions
diff --git a/codebase/base_connector.php b/codebase/base_connector.php index b3dddb9..b845e81 100644 --- a/codebase/base_connector.php +++ b/codebase/base_connector.php @@ -304,9 +304,11 @@ 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; + protected $as_string = false; // render() returns string, don't send result in response + protected $simple = false; // render only data without any other info protected $filters; protected $sorts; + protected $mix; /*! constructor @@ -338,6 +340,7 @@ class Connector { $this->attributes = array(); $this->filters = array(); $this->sorts = array(); + $this->mix = array(); $this->config = new DataConfig(); $this->request = new DataRequestConfig(); @@ -493,7 +496,6 @@ class Connector { $this->apply_filters($wrap); $this->event->trigger("beforeFilter",$wrap); $wrap->store(); - if ($this->model && method_exists($this->model, "get")){ $this->sql = new ArrayDBDataWrapper(); @@ -634,7 +636,7 @@ class Connector { process commands, output requested data as XML */ protected function render_set($res){ - return $this->render->render_set($res, $this->names["item_class"], $this->dload, $this->data_separator, $this->config); + return $this->render->render_set($res, $this->names["item_class"], $this->dload, $this->data_separator, $this->config, $this->mix); } /*! output fetched data as XML @@ -642,9 +644,14 @@ class Connector { DB resultset */ protected function output_as_xml($res){ + $result = $this->render_set($res); + if ($this->simple) return $result; + $start="<?xml version='1.0' encoding='".$this->encoding."' ?>".$this->xml_start(); - $end=$this->render_set($res).$this->xml_end(); - + $end=$result.$this->xml_end(); + + if ($this->as_string) return $start.$end; + $out = new OutputWriter($start, $end); $this->event->trigger("beforeOutput", $this, $out); $out->output("", true, $this->encoding); @@ -806,18 +813,28 @@ class Connector { public function asString($as_string) { $this->as_string = $as_string; } - + + public function simple_render() { + $this->simple = true; + return $this->render(); + } + public function filter($name, $value = false, $operation = '=') { $this->filters[] = array('name' => $name, 'value' => $value, 'operation' => $operation); } + public function clear_filter() { + $this->filters = array(); + $this->request->set_filters(array()); + } + protected function apply_filters($wrap) { for ($i = 0; $i < count($this->filters); $i++) { $f = $this->filters[$i]; $wrap->add($f['name'], $f['value'], $f['operation']); } } - + public function sort($name, $direction = false) { $this->sorts[] = array('name' => $name, 'direction' => $direction); } @@ -828,6 +845,10 @@ class Connector { $wrap->add($s['name'], $s['direction']); } } + + public function mix($name, $value, $filter=false) { + $this->mix[] = Array('name'=>$name, 'value'=>$value, 'filter'=>$filter); + } } diff --git a/codebase/data_connector.php b/codebase/data_connector.php index b7cf4a5..dc02551 100644 --- a/codebase/data_connector.php +++ b/codebase/data_connector.php @@ -162,11 +162,12 @@ class DataConnector extends Connector{ class JSONDataConnector extends DataConnector{ - public function __construct($res,$type=false,$item_type=false,$data_type=false){ + public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ if (!$item_type) $item_type="JSONCommonDataItem"; if (!$data_type) $data_type="CommonDataProcessor"; + if (!$render_type) $render_type="JSONRenderStrategy"; $this->data_separator = ",\n"; - parent::__construct($res,$type,$item_type,$data_type); + parent::__construct($res,$type,$item_type,$data_type,$render_type); } /*! assign options collection to the column @@ -213,7 +214,9 @@ class JSONDataConnector extends DataConnector{ } protected function output_as_xml($res){ - $result = "[\n".substr($this->render_set($res),0,-2)."\n]"; + $json = $this->render_set($res); + if ($this->simple) return $json; + $result = json_encode($json); $this->fill_collections(); $is_sections = sizeof($this->sections) && $this->is_first_call(); @@ -223,6 +226,7 @@ class JSONDataConnector extends DataConnector{ foreach($this->attributes as $k=>$v) $attributes .= ", ".$k.":\"".$v."\""; + $extra = ""; if (!empty($this->extra_output)) $extra .= ', "collections": {'.$this->extra_output.'}'; @@ -242,7 +246,7 @@ class JSONDataConnector extends DataConnector{ $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."}"; + $result = "{ \"data\":".$result.$attributes.$extra.$sections.$dyn."}"; } } @@ -276,7 +280,7 @@ class JSONCommonDataItem extends DataItem{ foreach ($this->userdata as $key => $value) $data[$key]=$value; - return json_encode($data); + return $data; } } @@ -408,9 +412,12 @@ class JSONTreeDataConnector extends TreeDataConnector{ } protected function output_as_xml($res){ + $result = $this->render_set($res); + if ($this->simple) return $result; + $data = array(); $data["parent"] = $this->request->get_relation(); - $data["data"] = $this->render_set($res); + $data["data"] = $result; $data = json_encode($data); // return as string @@ -431,11 +438,12 @@ class JSONTreeCommonDataItem extends TreeCommonDataItem{ */ 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 (isset($this->data[$extra])) + $data[$extra]=$this->data[$extra]; } if ($this->userdata !== false) diff --git a/codebase/db_common.php b/codebase/db_common.php index 2ae9b22..8d8762e 100644 --- a/codebase/db_common.php +++ b/codebase/db_common.php @@ -465,6 +465,22 @@ class DataConfig{ //we not deleting field from $data collection, so it will not be included in data operation, but its data still available } + /*! remove field from dataset config ($text and $data collections) + + removed field will be excluded from all auto-generated queries + @param name + name of field, or aliase of field + */ + public function remove_field_full($name){ + $ind = $this->is_field($name); + if ($ind==-1) throw new Exception('There was no such data field registered as: '.$name); + array_splice($this->text,$ind,1); + + $ind = $this->is_field($name, $this->data); + if ($ind==-1) throw new Exception('There was no such data field registered as: '.$name); + array_splice($this->data,$ind,1); + } + /*! check if field is a part of dataset @param name diff --git a/codebase/scheduler_connector.php b/codebase/scheduler_connector.php index 8ebe875..e2a9773 100644 --- a/codebase/scheduler_connector.php +++ b/codebase/scheduler_connector.php @@ -213,9 +213,10 @@ class JSONSchedulerConnector extends SchedulerConnector { DB resultset */ protected function output_as_xml($res){ - $data=$this->xml_start(); - $data.=$this->render_set($res); - $data.=$this->xml_end(); + $result = $this->render_set($res); + if ($this->simple) return $result; + + $data=$this->xml_start().json_encode($result).$this->xml_end(); if ($this->as_string) return $data; diff --git a/codebase/strategy.php b/codebase/strategy.php index 837e394..15328e9 100644 --- a/codebase/strategy.php +++ b/codebase/strategy.php @@ -8,17 +8,91 @@ class RenderStrategy { $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++) + $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++) { + $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_string($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){ + 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($config, $data); + $data = new $name($data,$config,$index); if ($data->get_id()===false) $data->set_id($conn->uuid()); @@ -26,6 +100,7 @@ class RenderStrategy { $output.=$data->to_xml().$sep; $index++; } + $this->unmix($config, $mix); return $output; } @@ -38,12 +113,14 @@ class JSONRenderStrategy extends RenderStrategy { DB resultset process commands, output requested data as json */ - public function render_set($res, $name, $dload, $sep, $config){ + 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()); @@ -51,7 +128,8 @@ class JSONRenderStrategy extends RenderStrategy { $output[]=$data->to_xml(); $index++; } - return json_encode($output); + $this->unmix($config, $mix); + return $output; } } @@ -66,11 +144,13 @@ class TreeRenderStrategy extends RenderStrategy { $conn->event->attach("beforeProcessing",array($this,"parent_id_correction_b")); } - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config, $mix){ $output=""; $index=0; $conn = $this->conn; + $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, @@ -87,6 +167,7 @@ class TreeRenderStrategy extends RenderStrategy { $output.=$data->to_xml_end(); $index++; } + $this->unmix($config, $mix); return $output; } @@ -115,11 +196,13 @@ class TreeRenderStrategy extends RenderStrategy { class JSONTreeRenderStrategy extends TreeRenderStrategy { - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config,$mix){ $output=array(); $index=0; $conn = $this->conn; + $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, @@ -138,6 +221,7 @@ class JSONTreeRenderStrategy extends TreeRenderStrategy { $output[] = $record; $index++; } + $this->unmix($config, $mix); return $output; } @@ -160,11 +244,13 @@ class MultitableTreeRenderStrategy extends TreeRenderStrategy { $this->sep = $sep; } - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config, $mix){ $output=""; $index=0; $conn = $this->conn; + $this->mix($config, $mix); while ($data=$conn->sql->get_next($res)){ + $data = $this->simple_mix($mix, $data); $data[$config->id['name']] = $this->level_id($data[$config->id['name']]); $data = new $name($data,$config,$index); $conn->event->trigger("beforeRender",$data); @@ -178,6 +264,7 @@ class MultitableTreeRenderStrategy extends TreeRenderStrategy { $output.=$data->to_xml_end(); $index++; } + $this->unmix($config, $mix); return $output; } @@ -258,11 +345,13 @@ class MultitableTreeRenderStrategy extends TreeRenderStrategy { class JSONMultitableTreeRenderStrategy extends MultitableTreeRenderStrategy { - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config, $mix){ $output=array(); $index=0; $conn = $this->conn; + $this->mix($config, $mix); while ($data=$conn->sql->get_next($res)){ + $data = $this->complex_mix($mix, $data); $data[$config->id['name']] = $this->level_id($data[$config->id['name']]); $data = new $name($data,$config,$index); $conn->event->trigger("beforeRender",$data); @@ -277,6 +366,7 @@ class JSONMultitableTreeRenderStrategy extends MultitableTreeRenderStrategy { $output[] = $record; $index++; } + $this->unmix($config, $mix); return $output; } @@ -285,7 +375,7 @@ class JSONMultitableTreeRenderStrategy extends MultitableTreeRenderStrategy { class GroupRenderStrategy extends RenderStrategy { - private $id_postfix = '__{group_param}'; + protected $id_postfix = '__{group_param}'; public function __construct($conn) { parent::__construct($conn); @@ -293,12 +383,14 @@ class GroupRenderStrategy extends RenderStrategy { $conn->event->attach("onInit", Array($this, 'replace_postfix')); } - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config, $mix, $usemix = false){ $output=""; $index=0; $conn = $this->conn; + if ($usemix) $this->mix($config, $mix); while ($data=$conn->sql->get_next($res)){ if (isset($data[$config->id['name']])) { + $this->simple_mix($mix, $data); $has_kids = false; } else { $data[$config->id['name']] = $data['value'].$this->id_postfix; @@ -317,11 +409,12 @@ class GroupRenderStrategy extends RenderStrategy { if (($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload))&&($has_kids == true)){ $sub_request = new DataRequestConfig($conn->get_request()); $sub_request->set_relation(str_replace($this->id_postfix, "", $data->get_id())); - $output.=$this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config); + $output.=$this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config, $mix, true); } $output.=$data->to_xml_end(); $index++; } + if ($usemix) $this->unmix($config, $mix); return $output; } @@ -359,12 +452,14 @@ class GroupRenderStrategy extends RenderStrategy { class JSONGroupRenderStrategy extends GroupRenderStrategy { - public function render_set($res, $name, $dload, $sep, $config){ + public function render_set($res, $name, $dload, $sep, $config, $mix, $usemix = false){ $output=array(); $index=0; $conn = $this->conn; + if ($usemix) $this->mix($config, $mix); while ($data=$conn->sql->get_next($res)){ if (isset($data[$config->id['name']])) { + $data = $this->complex_mix($mix, $data); $has_kids = false; } else { $data[$config->id['name']] = $data['value'].$this->id_postfix; @@ -383,13 +478,14 @@ class JSONGroupRenderStrategy extends GroupRenderStrategy { if (($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload))&&($has_kids == true)){ $sub_request = new DataRequestConfig($conn->get_request()); $sub_request->set_relation(str_replace($this->id_postfix, "", $data->get_id())); - $temp = $this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config); + $temp = $this->render_set($conn->sql->select($sub_request), $name, $dload, $sep, $config, $mix, true); if (sizeof($temp)) $record["data"] = $temp; } $output[] = $record; $index++; } + if ($usemix) $this->unmix($config, $mix); return $output; } diff --git a/codebase/treedatamultitable_connector.php b/codebase/treedatamultitable_connector.php index 79cc9a4..8dba8c6 100644 --- a/codebase/treedatamultitable_connector.php +++ b/codebase/treedatamultitable_connector.php @@ -63,14 +63,20 @@ class JSONTreeDataMultitableConnector extends TreeDataMultitableConnector{ } protected function output_as_xml($res){ + $result = $this->render_set($res); + if ($this->simple) return $result; + $data = array(); if (isset($_GET['parent'])) $data["parent"] = $this->render->level_id($_GET[$this->parent_name], $this->render->get_level() - 1); else $data["parent"] = "0"; - $data["data"] = $this->render_set($res); + $data["data"] = $result; + + $result = json_encode($data); + if ($this->as_string) return $result; - $out = new OutputWriter(json_encode($data), ""); + $out = new OutputWriter($result, ""); $out->set_type("json"); $this->event->trigger("beforeOutput", $this, $out); $out->output("", true, $this->encoding); diff --git a/codebase/xss_filter.php b/codebase/xss_filter.php index b68cb56..51bea56 100644 --- a/codebase/xss_filter.php +++ b/codebase/xss_filter.php @@ -170,7 +170,7 @@ class ConnectorSecurity{ die(); } static function checkCSRF($edit){ - @session_start(); + if(isset($_SESSION)) @session_start(); if (ConnectorSecurity::$security_key){ if ($edit=== true){ |