diff options
author | Stanislau Wolski <stanislau.wolski@gmail.com> | 2013-09-04 13:07:43 +0300 |
---|---|---|
committer | Stanislau Wolski <stanislau.wolski@gmail.com> | 2013-09-04 13:07:43 +0300 |
commit | 394f114ab531f4a89555a3bd347a8cd3591364e6 (patch) | |
tree | 0dc788b72471d24e6fbade229853553ea5d2e525 /codebase | |
parent | 3aa4511823e830d895c56ae1ca002d20ff9eb1bf (diff) | |
download | connector-php-394f114ab531f4a89555a3bd347a8cd3591364e6.zip connector-php-394f114ab531f4a89555a3bd347a8cd3591364e6.tar.gz connector-php-394f114ab531f4a89555a3bd347a8cd3591364e6.tar.bz2 |
[add] reordering for the gantt connector
Diffstat (limited to 'codebase')
-rw-r--r-- | codebase/base_connector.php | 26 | ||||
-rw-r--r-- | codebase/data_connector.php | 25 | ||||
-rw-r--r-- | codebase/dataprocessor.php | 10 | ||||
-rw-r--r-- | codebase/db_common.php | 76 | ||||
-rw-r--r-- | codebase/gantt_connector.php | 32 |
5 files changed, 149 insertions, 20 deletions
diff --git a/codebase/base_connector.php b/codebase/base_connector.php index ac25b00..39ac6eb 100644 --- a/codebase/base_connector.php +++ b/codebase/base_connector.php @@ -312,7 +312,8 @@ class Connector { protected $filters; protected $sorts; protected $mix; - + protected $order = false; + /*! constructor Here initilization of all Masters occurs, execution timer initialized @@ -605,10 +606,13 @@ class Connector { $this->request->set_filter($this->resolve_parameter($k),$v); } + $this->check_csrf(); + } + + protected function check_csrf(){ $key = ConnectorSecurity::checkCSRF($this->editing); if ($key !== "") $this->add_top_attribute(ConnectorSecurity::$security_var, $key); - } /*! convert incoming request name to the actual DB name @@ -704,7 +708,23 @@ class Connector { */ public function dynamic_loading($count){ $this->dload=$count; - } + } + + /*! enable or disable data reordering + + @param name + name of field, which will be used for order storing, optional + by default 'sortorder' field will be used + */ + public function enable_order($name = true){ + if ($name === true) + $name = "sortorder"; + + $this->sort($name); + $this->access->allow("order"); + $this->request->set_order($name); + $this->order = $name; + } /*! enable logging diff --git a/codebase/data_connector.php b/codebase/data_connector.php index caa5369..fe8cc82 100644 --- a/codebase/data_connector.php +++ b/codebase/data_connector.php @@ -113,6 +113,12 @@ class DataConnector extends Connector{ $this->sections[$name] = $string; } + protected function parse_request_mode(){ + if (isset($_GET['action']) && $_GET["action"] != "get") + $this->editing = true; + else + parent::parse_request_mode(); + } //parse GET scoope, all operations with incoming request must be done here protected function parse_request(){ @@ -131,10 +137,10 @@ class DataConnector extends Connector{ //data saving $this->editing = true; } + parent::check_csrf(); } else { if (isset($_GET['editing']) && isset($_POST['ids'])) $this->editing = true; - parent::parse_request(); } @@ -146,7 +152,10 @@ class DataConnector extends Connector{ /*! renders self as xml, starting part */ protected function xml_start(){ - $start = parent::xml_start(); + $start = "<data"; + foreach($this->attributes as $k=>$v) + $start .= " ".$k."='".$v."'"; + $start.= ">"; foreach($this->sections as $k=>$v) $start .= "<".$k.">".$v."</".$k.">\n"; @@ -192,7 +201,7 @@ class JSONDataConnector extends DataConnector{ $name = $k; $option="\"{$name}\":["; if (!is_string($this->options[$name])) - $option.=substr($this->options[$name]->render(),0,-2); + $option.=substr(json_encode($this->options[$name]->render()),1,-1); else $option.=$this->options[$name]; $option.="]"; @@ -391,7 +400,11 @@ class TreeDataConnector extends DataConnector{ /*! renders self as xml, starting part */ protected function xml_start(){ - return "<data parent='".$this->request->get_relation()."'>"; + $attributes = " parent='".$this->request->get_relation()."' "; + foreach($this->attributes as $k=>$v) + $attributes .= " ".$k."='".$v."'"; + + return "<data".$attributes.">"; } } @@ -417,6 +430,10 @@ class JSONTreeDataConnector extends TreeDataConnector{ if (!empty($this->options)) $data["collections"] = $this->options; + + foreach($this->attributes as $k=>$v) + $data[$k] = $v; + $data = json_encode($data); // return as string diff --git a/codebase/dataprocessor.php b/codebase/dataprocessor.php index be18eba..459d5a1 100644 --- a/codebase/dataprocessor.php +++ b/codebase/dataprocessor.php @@ -166,6 +166,9 @@ class DataProcessor{ $check = $this->connector->event->trigger("beforeProcessing",$action);
if (!$action->is_ready())
$this->check_exts($action,$mode);
+ if ($mode == "insert" && $action->get_status() != "error" && $action->get_status() != "invalid")
+ $this->connector->sql->new_record_order($action, $this->request);
+
$check = $this->connector->event->trigger("afterProcessing",$action);
}
@@ -178,13 +181,14 @@ class DataProcessor{ if ($this->connector->sql->is_record_transaction()){
if ($action->get_status()=="error" || $action->get_status()=="invalid")
- $this->connector->sql->rollback_transaction();
+ $this->connector->sql->rollback_transaction();
else
- $this->connector->sql->commit_transaction();
+ $this->connector->sql->commit_transaction();
}
-
+
return $action;
}
+
/*! check if some event intercepts processing, send data to DataWrapper in other case
@param action
diff --git a/codebase/db_common.php b/codebase/db_common.php index 76748e7..76ac179 100644 --- a/codebase/db_common.php +++ b/codebase/db_common.php @@ -14,6 +14,7 @@ class DataRequestConfig{ private $start; //!< start of requested data private $count; //!< length of requested data + private $order = false; private $user; private $version; @@ -83,6 +84,12 @@ class DataRequestConfig{ } + public function get_order(){ + return $this->order; + } + public function set_order($order){ + $this->order = $order; + } public function get_user(){ return $this->user; } @@ -642,6 +649,73 @@ abstract class DBDataWrapper extends DataWrapper{ return $str; } + public function new_record_order($action, $source){ + $order = $source->get_order(); + if ($order){ + $table = $source->get_source(); + $id = $this->config->id["db_name"]; + $idvalue = $action->get_new_id(); + + $max = $this->queryOne("SELECT MAX($order) as maxvalue FROM $table"); + $maxvalue = $max["maxvalue"] + 1; + + $this->query("UPDATE $table SET $order = $maxvalue WHERE $id = $idvalue"); + } + } + + public function order($data, $source){ + //id of moved item + $id1 = $this->escape($data->get_value("id")); + //id of target item + $target = $data->get_value("target"); + if (strpos($target, "next:") !== false){ + $dropnext = true; + $id2 = str_replace("next:", "", $target); + } else { + $id2 = $target; + } + $id2 = $this->escape($id2); + + + //for tree like components we need to limit out queries to the affected branch only + $relation_select = $relation_update = $relation_sql_out = $relation_sql = ""; + if ($this->config->relation_id["name"]){ + $relation = $data->get_value($this->config->relation_id["name"]); + if ($relation !== false && $relation !== ""){ + $relation_sql = " ".$this->config->relation_id["db_name"]." = '".$this->escape($relation)."' AND "; + $relation_select = $this->config->relation_id["db_name"]." as dhx_parent, "; + $relation_update = " ".$this->config->relation_id["db_name"]." = '".$this->escape($relation)."', "; + } + } + + + $name = $source->get_order(); + $table = $source->get_source(); + $idkey = $this->config->id["db_name"]; + + $source = $this->queryOne("select $relation_select $name as dhx_index from $table where $idkey = '$id1'"); + $source_index = $source["dhx_index"] ? $source["dhx_index"] : 0; + if ($relation_sql) + $relation_sql_out = " ".$this->config->relation_id["db_name"]." = '".$this->escape($source["dhx_parent"])."' AND "; + + $this->query("update $table set $name = $name - 1 where $relation_sql_out $name >= $source_index"); + + if ($id2 !== ""){ + $target = $this->queryOne("select $name as dhx_index from $table where $idkey = '$id2'"); + $target_index = $target["dhx_index"]; + if (!$target_index) + $target_index = 0; + if ($dropnext) + $target_index += 1; + $this->query("update $table set $name = $name + 1 where $relation_sql $name >= $target_index"); + } else { + $target = $this->queryOne("select max($name) as dhx_index from $table"); + $target_index = ($target["dhx_index"] ? $target["dhx_index"] : 0)+1; + } + + $this->query("update $table set $relation_update $name = $target_index where $idkey = '$id1'"); + } + public function insert($data,$source){ $sql=$this->insert_query($data,$source); $this->query($sql); @@ -726,7 +800,7 @@ abstract class DBDataWrapper extends DataWrapper{ else array_push($sql,$this->escape_name($rules[$i]["name"])." ".$rules[$i]["operation"]." '".$this->escape($rules[$i]["value"])."'"); } - if ($relation!==false) + if ($relation !== false && $relation !== "") array_push($sql,$this->escape_name($this->config->relation_id["db_name"])." = '".$this->escape($relation)."'"); return implode(" AND ",$sql); } diff --git a/codebase/gantt_connector.php b/codebase/gantt_connector.php index 46c580b..74b8636 100644 --- a/codebase/gantt_connector.php +++ b/codebase/gantt_connector.php @@ -10,8 +10,6 @@ require_once("data_connector.php"); **/ class GanttDataItem extends DataItem{ - public static $open = null; - /*! return self as XML string */ function to_xml(){ @@ -28,8 +26,6 @@ class GanttDataItem extends DataItem{ if ($this->userdata !== false) foreach ($this->userdata as $key => $value) $str.="<".$key."><![CDATA[".$value."]]></".$key.">"; - if (GanttDataItem::$open !== null) - $str.="<open>".GanttDataItem::$open."</open>"; return $str."</task>"; } @@ -82,6 +78,9 @@ class GanttConnector extends Connector{ if (!$data_type) $data_type="GanttDataProcessor"; if (!$render_type) $render_type="RenderStrategy"; parent::__construct($res,$type,$item_type,$data_type,$render_type); + + $this->event->attach("afterDelete", array($this, "delete_related_links")); + $this->event->attach("afterOrder", array($this, "order_set_parent")); } //parse GET scoope, all operations with incoming request must be done here @@ -99,8 +98,25 @@ class GanttConnector extends Connector{ } } - public function openAll($mode = true) { - GanttDataItem::$open = $mode; + function order_set_parent($action){ + $value = $action->get_id(); + $parent = $action->get_value("parent"); + + $table = $this->request->get_source(); + $id = $this->config->id["db_name"]; + + $this->sql->query("UPDATE $table SET parent = $parent WHERE $id = $value"); + } + + function delete_related_links($action){ + if (isset($this->options["links"])){ + $links = $this->options["links"]; + $value = $this->sql->escape($action->get_new_id()); + $table = $links->get_request()->get_source(); + + $this->sql->query("DELETE FROM $table WHERE source = '$value'"); + $this->sql->query("DELETE FROM $table WHERE target = '$value'"); + } } public function render_links($table,$id="",$fields=false,$extra=false,$relation_id=false) { @@ -145,9 +161,7 @@ class JSONGanttDataItem extends GanttDataItem{ $extra = $this->config->text[$i]["name"]; $obj[$extra]=$this->data[$extra]; } - if (GanttDataItem::$open !== null) - $obj['open'] = GanttDataItem::$open; - + if ($this->userdata !== false) foreach ($this->userdata as $key => $value) $obj[$key]=$value; |