diff options
Diffstat (limited to 'codebase/db_common.php')
-rw-r--r-- | codebase/db_common.php | 92 |
1 files changed, 87 insertions, 5 deletions
diff --git a/codebase/db_common.php b/codebase/db_common.php index 19365f3..e2f521e 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; } @@ -195,7 +202,9 @@ class DataRequestConfig{ name of source table */ public function set_source($value){ - $this->source=trim($value); + if (is_string($value)) + $value = trim($value); + $this->source = $value; if (!$this->source) throw new Exception("Source of data can't be empty"); } /*! sets data limits @@ -640,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 dhx_maxvalue FROM $table"); + $dhx_maxvalue = $max["dhx_maxvalue"] + 1; + + $this->query("UPDATE $table SET $order = $dhx_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); @@ -724,8 +800,14 @@ 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) - array_push($sql,$this->escape_name($this->config->relation_id["db_name"])." = '".$this->escape($relation)."'"); + + if ($relation !== false && $relation !== ""){ + $relsql = $this->escape_name($this->config->relation_id["db_name"])." = '".$this->escape($relation)."'"; + if ($relation == "0") + $relsql = "( ".$relsql." OR ".$this->escape_name($this->config->relation_id["db_name"])." IS NULL )"; + + array_push($sql,$relsql); + } return implode(" AND ",$sql); } /*! convert sorting rules to sql string @@ -800,7 +882,7 @@ abstract class DBDataWrapper extends DataWrapper{ $sql.=implode(",",$temp)." WHERE ".$this->escape_name($this->config->id["db_name"])."='".$this->escape($data->get_id())."'"; //if we have limited set - set constraints - $where=$this->build_where($request->get_filters(),$request->get_relation()); + $where=$this->build_where($request->get_filters()); if ($where) $sql.=" AND (".$where.")"; return $sql; @@ -820,7 +902,7 @@ abstract class DBDataWrapper extends DataWrapper{ $sql.=" WHERE ".$this->escape_name($this->config->id["db_name"])."='".$this->escape($data->get_id())."'"; //if we have limited set - set constraints - $where=$this->build_where($request->get_filters(),$request->get_relation()); + $where=$this->build_where($request->get_filters()); if ($where) $sql.=" AND (".$where.")"; return $sql; |