diff options
author | Dmitry <dmitry@dhtmlx.com> | 2012-05-10 13:23:30 +0200 |
---|---|---|
committer | Dmitry <dmitry@dhtmlx.com> | 2012-05-10 13:23:30 +0200 |
commit | fbdaeeb0158c38937782ee8a19a84fa077a2f224 (patch) | |
tree | 656f7c12d94e028a89322695dd31b540e117060c /codebase | |
parent | 92c66f4e9362beb467720fd4e15d269b405e04bc (diff) | |
download | connector-php-fbdaeeb0158c38937782ee8a19a84fa077a2f224.zip connector-php-fbdaeeb0158c38937782ee8a19a84fa077a2f224.tar.gz connector-php-fbdaeeb0158c38937782ee8a19a84fa077a2f224.tar.bz2 |
refactor tree/treegrid multitable connectors
Diffstat (limited to 'codebase')
-rw-r--r-- | codebase/strategy.php | 85 | ||||
-rw-r--r-- | codebase/treegridmultitable_connector.php | 121 | ||||
-rw-r--r-- | codebase/treemultitable_connector.php | 128 |
3 files changed, 119 insertions, 215 deletions
diff --git a/codebase/strategy.php b/codebase/strategy.php index 302e34a..da1f68e 100644 --- a/codebase/strategy.php +++ b/codebase/strategy.php @@ -119,16 +119,19 @@ class JSONTreeRenderStrategy extends RenderStrategy { class MultitableRenderStrategy extends RenderStrategy { + private $level = 0; + private $max_level = null; + 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']]); + $data[$config->id['name']] = $this->level_id($data[$config->id['name']]); $data = new $name($data,$config,$index); $conn->event->trigger("beforeRender",$data); - if (($conn->getMaxLevel() !== null)&&($conn->get_level() == $conn->getMaxLevel())) { + if (($this->max_level !== null)&&($conn->get_level() == $this->max_level)) { $data->set_kids(false); } else { if ($data->has_kids()===-1) @@ -141,6 +144,82 @@ class MultitableRenderStrategy extends RenderStrategy { return $output; } + + public function level_id($id, $level = null) { + return ($level === null ? $this->level : $level).'%23'.$id; + } + + + /*! remove level prefix from id, parent id and set new id before processing + @param action + DataAction object + */ + public function id_translate_before($action) { + $id = $action->get_id(); + $id = $this->parse_id($id, false); + $action->set_id($id); + $action->set_value('tr_id', $id); + $action->set_new_id($id); + $pid = $action->get_value($this->conn->get_config()->relation_id['db_name']); + $pid = $this->parse_id($pid, false); + $action->set_value($this->conn->get_config()->relation_id['db_name'], $pid); + } + + + /*! add level prefix in id and new id after processing + @param action + DataAction object + */ + public function id_translate_after($action) { + $id = $action->get_id(); + $action->set_id($this->level_id($id)); + $id = $action->get_new_id(); + $action->success($this->level_id($id)); + } + + + public function get_level() { + if ($this->level) return $this->level; + if (!isset($_GET['id'])) { + if (isset($_POST['ids'])) { + $ids = explode(",",$_POST["ids"]); + $id = $this->parse_id($ids[0]); + $this->level--; + } + $this->conn->get_request()->set_relation(false); + } else { + $id = $this->parse_id($_GET['id']); + $_GET['id'] = $id; + } + return $this->level; + } + + + public function is_max_level() { + if (($this->max_level !== null) && ($this->level() >= $this->max_level)) + return true; + return false; + } + + public function set_max_level($max_level) { + $this->max_level = $max_level; + } + + public function parse_id($id, $set_level = true) { + $result = Array(); + preg_match('/^(.+)((#)|(%23))/', $id, $result); + if ($set_level === true) { + $this->level = isset($result[1]) ? $result[1] + 1 : 0; + } + preg_match('/^(.+)(#|%23)(.*)$/', $id, $result); + if (isset($result[3])) { + $id = $result[3]; + } else { + $id = ''; + } + return $id; + } + } @@ -156,7 +235,7 @@ class JSONMultitableRenderStrategy extends MultitableRenderStrategy { $data = new $name($data,$config,$index); $conn->event->trigger("beforeRender",$data); - if ($conn->is_max_level()) { + if ($this->is_max_level()) { $data->set_kids(false); } else { if ($data->has_kids()===-1) diff --git a/codebase/treegridmultitable_connector.php b/codebase/treegridmultitable_connector.php index 4e7f38a..ec75fcd 100644 --- a/codebase/treegridmultitable_connector.php +++ b/codebase/treegridmultitable_connector.php @@ -7,136 +7,52 @@ require_once("treegrid_connector.php"); class TreeGridMultitableConnector extends TreeGridConnector{ - private $level = 0; private $max_level = null; - public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ $data_type="TreeGridMultitableDataProcessor"; if (!$render_type) $render_type="MultitableRenderStrategy"; parent::__construct($res,$type,$item_type,$data_type,$render_type); - $this->event->attach("beforeProcessing", Array($this, 'id_translate_before')); - $this->event->attach("afterProcessing", Array($this, 'id_translate_after')); + $this->event->attach("beforeProcessing", Array($this->render, 'id_translate_before')); + $this->event->attach("afterProcessing", Array($this->render, 'id_translate_after')); } public function render(){ - $this->parse_request(); $this->dload = true; - if ((isset($_GET["editing"]))||(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(); + return parent::render(); + } - if (isset($_GET['id'])) { - $this->output_as_xml( $this->sql->select($this->request) ); - } else { - $this->request->set_relation(false); - $this->output_as_xml( $this->sql->select($this->request) ); - } - } - $this->end_run(); + /*! sets relation for rendering */ + protected function set_relation() { + if (!isset($_GET['id'])) + $this->request->set_relation(false); } + /*! gets resource for rendering */ + protected function get_resource() { + return $this->sql->select($this->request); + } public function xml_start(){ if (isset($_GET['id'])) { - return "<rows parent='".($this->level - 1).'%23'.$_GET['id']."'>"; + return "<rows parent='".$this->render->level_id($_GET['id'], $this->render->get_level() - 1)."'>"; } else { return "<rows parent=''>"; } } - - public function get_level() { - if ($this->level) return $this->level; - if (!isset($_GET['id'])) { - if (isset($_POST['ids'])) { - $ids = explode(",",$_POST["ids"]); - $id = $this->parse_id($ids[0]); - $this->level--; - } - $this->request->set_relation(false); - } else { - $id = $this->parse_id($_GET['id']); - $_GET['id'] = $id; - } - return $this->level; - } - - - public function parse_id($id, $set_level = true) { - $result = Array(); - preg_match('/^(.+)((#)|(%23))/', $id, $result); - if ($set_level === true) { - $this->level = $result[1] + 1; - } - preg_match('/^(.+)((#)|(%23))(.*)$/', $id, $result); - $id = $result[5]; - return $id; - } - - - public function level_id($id) { - return $this->level.'%23'.$id; - } - - /*! set maximum level of tree @param max_level maximum level */ public function setMaxLevel($max_level) { - $this->max_level = $max_level; + $this->render->set_max_level($max_level); } - - /*! gets maximum level of tree - */ - public function getMaxLevel() { - return $this->max_level; - } - - - /*! remove level prefix from id, parent id and set new id before processing - @param action - DataAction object - */ - public function id_translate_before($action) { - $this->request->set_relation(false); - $id = $action->get_id(); - $id = $this->parse_id($id, false); - $action->set_id($id); - $action->set_value('gr_id', $id); - $action->set_new_id($id); - $pid = $action->get_value($this->config->relation_id['db_name']); - $pid = $this->parse_id($pid, false); - $action->set_value($this->config->relation_id['db_name'], $pid); + public function get_level() { + return $this->render->get_level(); } - /*! add level prefix in id and new id after processing - @param action - DataAction object - */ - public function id_translate_after($action) { - $id = $action->get_id(); - $action->set_id($this->level_id($id)); - $id = $action->get_new_id(); - $action->success($this->level_id($id)); - } - } @@ -145,7 +61,10 @@ class TreeGridMultitableDataProcessor extends DataProcessor { function name_data($data){ if ($data=="gr_pid") return $this->config->relation_id["name"]; - preg_match('/^c(\d+)$/', $data, $data_num); + if ($data=="gr_id") + return $this->config->id["name"]; + preg_match('/^c([%\d]+)$/', $data, $data_num); + if (!isset($data_num[1])) return $data; $data_num = $data_num[1]; if (isset($this->config->data[$data_num]["db_name"])) { return $this->config->data[$data_num]["db_name"]; diff --git a/codebase/treemultitable_connector.php b/codebase/treemultitable_connector.php index 8e96a83..a26d2cf 100644 --- a/codebase/treemultitable_connector.php +++ b/codebase/treemultitable_connector.php @@ -7,145 +7,51 @@ require_once("tree_connector.php"); class TreeMultitableConnector extends TreeConnector{ - private $level = 0; private $max_level = null; public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ if (!$data_type) $data_type="TreeDataProcessor"; if (!$render_type) $render_type="MultitableRenderStrategy"; parent::__construct($res,$type,$item_type,$data_type,$render_type); - $this->event->attach("beforeProcessing", Array($this, 'id_translate_before')); - $this->event->attach("afterProcessing", Array($this, 'id_translate_after')); + $this->event->attach("beforeProcessing", Array($this->render, 'id_translate_before')); + $this->event->attach("afterProcessing", Array($this->render, 'id_translate_after')); } - public function render(){ - $this->parse_request(); $this->dload = true; - if ((isset($_GET["editing"]))||(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(); + return parent::render(); + } - $wrap = new FilterInterface($this->request); - $this->event->trigger("beforeFilter",$wrap); - $wrap->store(); + /*! sets relation for rendering */ + protected function set_relation() { + if (!isset($_GET['id'])) + $this->request->set_relation(false); + } - if (isset($_GET['id'])) { - $this->output_as_xml( $this->sql->select($this->request) ); - } else { - $this->request->set_relation(false); - $this->output_as_xml( $this->sql->select($this->request) ); - } - } - $this->end_run(); + /*! gets resource for rendering */ + protected function get_resource() { + return $this->sql->select($this->request); } public function xml_start(){ if (isset($_GET['id'])) { - return "<tree id='".($this->level - 1).'#'.$_GET['id']."'>"; + return "<tree id='".($this->render->level_id($_GET['id'], $this->render->get_level() - 1))."'>"; } else { return "<tree id='0'>"; } } - - public function get_level() { - if ($this->level) return $this->level; - if (!isset($_GET['id'])) { - if (isset($_POST['ids'])) { - $ids = explode(",",$_POST["ids"]); - $id = $this->parse_id($ids[0]); - $this->level--; - } - $this->request->set_relation(false); - } else { - $id = $this->parse_id($_GET['id']); - $_GET['id'] = $id; - } - return $this->level; - } - - - public function parse_id($id, $set_level = true) { - $result = Array(); - preg_match('/^(.+)#/', $id, $result); - if ($set_level === true) { - $this->level = $result[1] + 1; - } - preg_match('/^(.+)#(.*)$/', $id, $result); - if (isset($result[2])) { - $id = $result[2]; - } else { - $id = ''; - } - return $id; - } - - - public function level_id($id) { - return $this->level.'#'.$id; - } - - /*! set maximum level of tree @param max_level maximum level */ public function setMaxLevel($max_level) { - $this->max_level = $max_level; - } - - - /*! gets maximum level of tree - */ - public function getMaxLevel() { - return $this->max_level; - } - - - public function is_max_level() { - if (($this->max_level !== null) && ($this->level >= $this->max_level)) - return true; - return false; - } - - - /*! remove level prefix from id, parent id and set new id before processing - @param action - DataAction object - */ - public function id_translate_before($action) { - $id = $action->get_id(); - $id = $this->parse_id($id, false); - $action->set_id($id); - $action->set_value('tr_id', $id); - $action->set_new_id($id); - $pid = $action->get_value($this->config->relation_id['db_name']); - $pid = $this->parse_id($pid, false); - $action->set_value($this->config->relation_id['db_name'], $pid); + $this->render->set_max_level($max_level); } - - /*! add level prefix in id and new id after processing - @param action - DataAction object - */ - public function id_translate_after($action) { - $id = $action->get_id(); - $action->set_id($this->level_id($id)); - $id = $action->get_new_id(); - $action->success($this->level_id($id)); + public function get_level() { + return $this->render->get_level(); } - + } ?>
\ No newline at end of file |