diff options
author | Dmitry <dmitry@dhtmlx.com> | 2012-05-08 16:54:28 +0200 |
---|---|---|
committer | Dmitry <dmitry@dhtmlx.com> | 2012-05-08 16:54:28 +0200 |
commit | c5783990b8d84d764584dd8893397ddc181022ad (patch) | |
tree | eb73a0748d3ba1439b7924abc0a62dd3d4f4ea6d /codebase | |
parent | c0efba59747a13bd79af22b70d9c0d055118598c (diff) | |
download | connector-php-c5783990b8d84d764584dd8893397ddc181022ad.zip connector-php-c5783990b8d84d764584dd8893397ddc181022ad.tar.gz connector-php-c5783990b8d84d764584dd8893397ddc181022ad.tar.bz2 |
minimize code duplicating for GroupConnectors
Diffstat (limited to 'codebase')
-rw-r--r-- | codebase/base_connector.php | 25 | ||||
-rw-r--r-- | codebase/grid_connector.php | 3 | ||||
-rw-r--r-- | codebase/strategy.php | 68 | ||||
-rw-r--r-- | codebase/treegridgroup_connector.php | 84 | ||||
-rw-r--r-- | codebase/treegroup_connector.php | 83 |
5 files changed, 114 insertions, 149 deletions
diff --git a/codebase/base_connector.php b/codebase/base_connector.php index fb3152f..48039a6 100644 --- a/codebase/base_connector.php +++ b/codebase/base_connector.php @@ -315,7 +315,7 @@ class Connector { if (!class_exists($this->names["db_class"],false)) throw new Exception("DB class not found: ".$this->names["db_class"]); $this->sql = new $this->names["db_class"]($db,$this->config); - $this->render = new $this->names["render_class"](); + $this->render = new $this->names["render_class"]($this); $this->db=$db;//saved for options connectors, if any @@ -419,6 +419,7 @@ class Connector { EventMaster::trigger_static("connectorInit",$this); $this->parse_request(); + $this->set_relation(); if ($this->live_update !== false && $this->updating!==false) { $this->live_update->get_updates(); @@ -426,8 +427,7 @@ class Connector { if ($this->editing){ $dp = new $this->names["data_class"]($this,$this->config,$this->request); $dp->process($this->config,$this->request); - } - else { + } else { if (!$this->access->check("read")){ LogMaster::log("Access control: read operation blocked"); echo "Access denied"; @@ -441,12 +441,25 @@ class Connector { $this->event->trigger("beforeFilter",$wrap); $wrap->store(); - $this->output_as_xml( $this->sql->select($this->request) ); + $this->output_as_xml($this->get_resource()); } } $this->end_run(); } - + + + /*! empty call which used for tree-logic + * to prevent code duplicating + */ + protected function set_relation() {} + + /*! gets resource for rendering + */ + protected function get_resource() { + return $this->sql->select($this->request); + } + + /*! prevent SQL injection through column names replace dangerous chars in field names @param str @@ -555,7 +568,7 @@ class Connector { process commands, output requested data as XML */ protected function render_set($res){ - return $this->render->render_set($res, $this, $this->names["item_class"], $this->dload, $this->data_separator); + return $this->render->render_set($res, $this->names["item_class"], $this->dload, $this->data_separator); } /*! output fetched data as XML diff --git a/codebase/grid_connector.php b/codebase/grid_connector.php index 229e9ff..cc56cec 100644 --- a/codebase/grid_connector.php +++ b/codebase/grid_connector.php @@ -112,7 +112,8 @@ class GridDataItem extends DataItem{ foreach ($cattrs as $k => $v) $str.=" ".$k."='".$this->xmlentities($v)."'"; } - $str.="><![CDATA[".$this->data[$name]."]]></cell>"; + $value = isset($this->data[$name]) ? $this->data[$name] : ''; + $str.="><![CDATA[".$value."]]></cell>"; } foreach ($this->userdata as $key => $value) $str.="<userdata name='".$key."'><![CDATA[".$value."]]></userdata>"; diff --git a/codebase/strategy.php b/codebase/strategy.php index 506a5e6..302e34a 100644 --- a/codebase/strategy.php +++ b/codebase/strategy.php @@ -2,14 +2,21 @@ class RenderStrategy { + protected $conn = null; + + public function __construct($conn) { + $this->conn = $conn; + } + /*! render from DB resultset @param res DB resultset process commands, output requested data as XML */ - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=""; $index=0; + $conn = $this->conn; $conn->event->trigger("beforeRenderSet",$conn,$res,$conn->get_config()); while ($data=$conn->sql->get_next($res)){ $data = new $name($data,$conn->get_config(),$index); @@ -31,9 +38,10 @@ class JSONRenderStrategy { DB resultset process commands, output requested data as json */ - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=array(); $index=0; + $conn = $this->conn; $conn->event->trigger("beforeRenderSet",$conn,$res,$conn->get_config()); while ($data=$conn->sql->get_next($res)){ $data = new $name($data,$conn->get_config(),$index); @@ -50,9 +58,10 @@ class JSONRenderStrategy { class TreeRenderStrategy extends RenderStrategy { - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=""; $index=0; + $conn = $this->conn; while ($data=$conn->sql->get_next($res)){ $data = new $name($data,$conn->get_config(),$index); $conn->event->trigger("beforeRender",$data); @@ -65,7 +74,7 @@ class TreeRenderStrategy extends RenderStrategy { if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload)){ $sub_request = new DataRequestConfig($conn->get_request()); $sub_request->set_relation($data->get_id()); - $output.=$this->render_set($conn->sql->select($sub_request), $conn, $name, $dload, $sep); + $output.=$this->render_set($conn->sql->select($sub_request), $name, $dload, $sep); } $output.=$data->to_xml_end(); $index++; @@ -79,9 +88,10 @@ class TreeRenderStrategy extends RenderStrategy { class JSONTreeRenderStrategy extends RenderStrategy { - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=array(); $index=0; + $conn = $this->conn; while ($data=$conn->sql->get_next($res)){ $data = new $name($data,$conn->get_config(),$index); $conn->event->trigger("beforeRender",$data); @@ -94,7 +104,7 @@ class JSONTreeRenderStrategy extends RenderStrategy { if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$dload)){ $sub_request = new DataRequestConfig($conn->get_request()); $sub_request->set_relation($data->get_id()); - $temp = $this->render_set($conn->sql->select($sub_request), $conn, $name, $dload, $sep); + $temp = $this->render_set($conn->sql->select($sub_request), $name, $dload, $sep); if (sizeof($temp)) $record["data"] = $temp; } @@ -109,9 +119,10 @@ class JSONTreeRenderStrategy extends RenderStrategy { class MultitableRenderStrategy extends RenderStrategy { - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=""; $index=0; + $conn = $this->conn; $config = $conn->get_config(); while ($data=$conn->sql->get_next($res)){ $data[$config->id['name']] = $conn->level_id($data[$config->id['name']]); @@ -135,9 +146,10 @@ class MultitableRenderStrategy extends RenderStrategy { class JSONMultitableRenderStrategy extends MultitableRenderStrategy { - public function render_set($res, $conn, $name, $dload, $sep){ + public function render_set($res, $name, $dload, $sep){ $output=array(); $index=0; + $conn = $this->conn; $config = $conn->get_config(); while ($data=$conn->sql->get_next($res)){ $data[$config->id['name']] = $conn->level_id($data[$config->id['name']]); @@ -162,15 +174,18 @@ class JSONMultitableRenderStrategy extends MultitableRenderStrategy { class GroupRenderStrategy extends RenderStrategy { - public function render_set($res, $conn, $name, $dload, $sep){ + private $id_postfix = '__{group_param}'; + + public function render_set($res, $name, $dload, $sep){ $output=""; $index=0; + $conn = $this->conn; $config = $conn->get_config(); while ($data=$conn->sql->get_next($res)){ if (isset($data[$config->id['name']])) { $has_kids = false; } else { - $data[$config->id['name']] = $data['value'].$conn->get_id_postfix(); + $data[$config->id['name']] = $data['value'].$this->id_postfix; $data[$config->text[0]['name']] = $data['value']; $has_kids = true; } @@ -185,8 +200,8 @@ class GroupRenderStrategy extends RenderStrategy { $output.=$data->to_xml_start(); 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($conn->get_id_postfix(), "", $data->get_id())); - $output.=$this->render_set($conn->sql->select($sub_request), $conn, $name, $dload, $sep); + $sub_request->set_relation(str_replace($this->id_postfix, "", $data->get_id())); + $output.=$this->render_set($conn->sql->select($sub_request), $name, $dload, $sep); } $output.=$data->to_xml_end(); $index++; @@ -194,6 +209,35 @@ class GroupRenderStrategy extends RenderStrategy { return $output; } + public function check_id($action) { + if (isset($_GET['editing'])) { + $config = $this->conn->get_config(); + $id = $action->get_id(); + $pid = $action->get_value($config->relation_id['name']); + $pid = str_replace($this->id_postfix, "", $pid); + $action->set_value($config->relation_id['name'], $pid); + if (!empty($pid)) { + return $action; + } else { + $action->error(); + $action->set_response_text("This record can't be updated!"); + return $action; + } + } else { + return $action; + } + } + + public function replace_postfix() { + if (isset($_GET['id'])) { + $_GET['id'] = str_replace($this->id_postfix, "", $_GET['id']); + } + } + + public function get_postfix() { + return $this->id_postfix; + } + } diff --git a/codebase/treegridgroup_connector.php b/codebase/treegridgroup_connector.php index 4140c2c..9900bba 100644 --- a/codebase/treegridgroup_connector.php +++ b/codebase/treegridgroup_connector.php @@ -7,88 +7,42 @@ require_once("treegrid_connector.php"); class TreeGridGroupConnector extends TreeGridConnector{ - private $id_postfix = '__{group_param}'; - public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ if (!$render_type) $render_type="GroupRenderStrategy"; parent::__construct($res,$type,$item_type,$data_type,$render_type); - $this->event->attach("beforeProcessing", Array($this, 'check_id')); + $this->event->attach("beforeProcessing", Array($this->render, 'check_id')); + $this->event->attach("onInit", Array($this->render, 'replace_postfix')); } - - public function get_id_postfix() { - return $this->id_postfix; + /*! if not isset $_GET[id] then it's top level + */ + protected function set_relation() { + if (!isset($_GET['id'])) $this->request->set_relation(false); } - - public function render(){ - if (isset($_GET['id'])) { - $_GET['id'] = str_replace($this->id_postfix, "", $_GET['id']); - } - $this->parse_request(); - if (!isset($_GET['id'])) { - $this->request->set_relation(false); - } - - if (isset($_GET["editing"])) - $this->editing=true; - else if (isset($_POST["ids"])){ - $this->editing=true; - } else { - $this->editing = false; - } - - if ($this->editing){ - $dp = new $this->names["data_class"]($this,$this->config,$this->request); - $dp->process($this->config,$this->request); - } - else { - $wrap = new SortInterface($this->request); - $this->event->trigger("beforeSort",$wrap); - $wrap->store(); - - $wrap = new FilterInterface($this->request); - $this->event->trigger("beforeFilter",$wrap); - $wrap->store(); - - if (isset($_GET['id'])) { - $this->output_as_xml( $this->sql->select($this->request) ); - } else { - $relation_id = $this->config->relation_id['name']; - $this->output_as_xml( $this->sql->get_variants($this->config->relation_id['name'], $this->request)); - } - } - $this->end_run(); + /*! if it's first level then distinct level + * else select by parent + */ + protected function get_resource() { + $resource = null; + if (isset($_GET['id'])) + $resource = $this->sql->select($this->request); + else + $resource = $this->sql->get_variants($this->config->relation_id['name'], $this->request); + return $resource; } + /*! renders self as xml, starting part */ protected function xml_start(){ if (isset($_GET['id'])) { - return "<rows parent='".$_GET['id'].$this->id_postfix."'>"; + return "<rows parent='".$_GET['id'].$this->render->get_postfix()."'>"; } else { return "<rows parent='0'>"; } } - - - public function check_id($action) { - if (isset($_GET['editing'])) { - $id = $action->get_id(); - $pid = $action->get_value($this->config->relation_id['name']); - $pid = str_replace($this->id_postfix, "", $pid); - $action->set_value($this->config->relation_id['name'], $pid); - if (strpos($id, $this->id_postfix) == false) { - return $action; - } else { - $action->error(); - $action->set_response_text("This record can't be updated!"); - return $action; - } - } else { - return $action; - } - } + } ?>
\ No newline at end of file diff --git a/codebase/treegroup_connector.php b/codebase/treegroup_connector.php index 80a15f1..09638fa 100644 --- a/codebase/treegroup_connector.php +++ b/codebase/treegroup_connector.php @@ -7,89 +7,42 @@ require_once("tree_connector.php"); class TreeGroupConnector extends TreeConnector{ - private $id_postfix = '__{group_param}'; - public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ if (!$render_type) $render_type="GroupRenderStrategy"; parent::__construct($res,$type,$item_type,$data_type,$render_type); - $this->event->attach("beforeProcessing", Array($this, 'check_id')); + $this->event->attach("beforeProcessing", Array($this->render, 'check_id')); + $this->event->attach("onInit", Array($this->render, 'replace_postfix')); } - - public function get_id_postfix() { - return $this->id_postfix; + /*! if not isset $_GET[id] then it's top level + */ + protected function set_relation() { + if (!isset($_GET['id'])) $this->request->set_relation(false); } - - public function render(){ - if (isset($_GET['id'])) { - $_GET['id'] = str_replace($this->id_postfix, "", $_GET['id']); - } - $this->parse_request(); - if (!isset($_GET['id'])) { - $this->request->set_relation(false); - } - - if (isset($_GET["editing"])) - $this->editing=true; - else if (isset($_POST["ids"])){ - $this->editing=true; - } else { - $this->editing = false; - } - - if ($this->editing){ - $dp = new $this->names["data_class"]($this,$this->config,$this->request); - $dp->process($this->config,$this->request); - } - else { - $wrap = new SortInterface($this->request); - $this->event->trigger("beforeSort",$wrap); - $wrap->store(); - - $wrap = new FilterInterface($this->request); - $this->event->trigger("beforeFilter",$wrap); - $wrap->store(); - - if (isset($_GET['id'])) { - $this->output_as_xml( $this->sql->select($this->request) ); - } else { - $relation_id = $this->config->relation_id['name']; - $this->output_as_xml( $this->sql->get_variants($this->config->relation_id['name'], $this->request)); - } - } - $this->end_run(); + /*! if it's first level then distinct level + * else select by parent + */ + protected function get_resource() { + $resource = null; + if (isset($_GET['id'])) + $resource = $this->sql->select($this->request); + else + $resource = $this->sql->get_variants($this->config->relation_id['name'], $this->request); + return $resource; } - /*! renders self as xml, starting part + /*! renders self as xml, starting part */ public function xml_start(){ if (isset($_GET['id'])) { - return "<tree id='".$_GET['id'].$this->id_postfix."'>"; + return "<tree id='".$_GET['id'].$this->render->get_postfix()."'>"; } else { return "<tree id='0'>"; } } - - public function check_id($action) { - if (isset($_GET['editing'])) { - $id = $action->get_id(); - $pid = $action->get_value($this->config->relation_id['name']); - $pid = str_replace($this->id_postfix, "", $pid); - $action->set_value($this->config->relation_id['name'], $pid); - if (strpos($id, $this->id_postfix) == false) { - return $action; - } else { - $action->error(); - $action->set_response_text("This record can't be updated!"); - return $action; - } - } else { - return $action; - } - } } ?>
\ No newline at end of file |