diff options
author | Stanislau Wolski <stanislau.wolski@gmail.com> | 2015-08-24 20:21:29 +0300 |
---|---|---|
committer | Stanislau Wolski <stanislau.wolski@gmail.com> | 2015-08-24 20:21:29 +0300 |
commit | dc3bc4895f8e18ee7ec1da5b67ec7781b8c49fe6 (patch) | |
tree | d1152f58c63a4ee15028313ed29a097f1cc130d3 /codebase | |
parent | 582b5b5355dafaf4fe427fe7bf98e2b38f07fba3 (diff) | |
parent | 6078b93f3f3b2f2b4a1c8471c9d43e0af9fd21f3 (diff) | |
download | connector-php-dc3bc4895f8e18ee7ec1da5b67ec7781b8c49fe6.zip connector-php-dc3bc4895f8e18ee7ec1da5b67ec7781b8c49fe6.tar.gz connector-php-dc3bc4895f8e18ee7ec1da5b67ec7781b8c49fe6.tar.bz2 |
Merge branch 'master' of github.com:DHTMLX/connector-php
Diffstat (limited to 'codebase')
-rwxr-xr-x | codebase/base_connector.php | 50 | ||||
-rwxr-xr-x[-rw-r--r--] | codebase/db_common.php | 12 | ||||
-rwxr-xr-x[-rw-r--r--] | codebase/gantt_connector.php | 90 | ||||
-rw-r--r-- | codebase/mixed_connector.php | 63 | ||||
-rwxr-xr-x[-rw-r--r--] | codebase/scheduler_connector.php | 1 | ||||
-rwxr-xr-x[-rw-r--r--] | codebase/update.php | 610 |
6 files changed, 514 insertions, 312 deletions
diff --git a/codebase/base_connector.php b/codebase/base_connector.php index 05a885e..4e033b2 100755 --- a/codebase/base_connector.php +++ b/codebase/base_connector.php @@ -282,38 +282,38 @@ class DataItem{ Can be used on its own to provide raw data. **/ class Connector { + + private $id_seed=0; //!< default value, used to generate auto-IDs + private $db; //!< db connection resource + protected $config;//DataConfig instance protected $request;//DataRequestConfig instance protected $names;//!< hash of names for used classes protected $encoding="utf-8";//!< assigned encoding (UTF-8 by default) protected $editing=false;//!< flag of edit mode ( response for dataprocessor ) + protected $updating=false;//!< flag of update mode ( response for data-update ) + protected $dload;//!< flag of dyn. loading mode + protected $data_separator = "\n"; + protected $live_update = false; // actions table name for autoupdating + protected $live_update_data_type = "DataUpdate"; + protected $extra_output="";//!< extra info which need to be sent to client side + protected $options=array();//!< hash of OptionsConnector + 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; + protected $order = false; public static $filter_var="dhx_filter"; public static $sort_var="dhx_sort"; public static $kids_var="dhx_kids"; - public $model=false; - - private $updating=false;//!< flag of update mode ( response for data-update ) - private $db; //!< db connection resource - protected $dload;//!< flag of dyn. loading mode - public $access; //!< AccessMaster instance - protected $data_separator = "\n"; - - public $sql; //DataWrapper instance - public $event; //EventMaster instance - public $limit=false; - - private $id_seed=0; //!< default value, used to generate auto-IDs - 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; // 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; - protected $order = false; + public $model=false; + public $access; //!< AccessMaster instance + public $sql; //DataWrapper instance + public $event; //EventMaster instance + public $limit=false; /*! constructor @@ -810,6 +810,10 @@ class Connector { $this->options[$name]=$options; } + public function get_options() { + return $this->options; + } + public function insert($data) { $action = new DataAction('inserted', false, $data); @@ -851,7 +855,7 @@ class Connector { url used for update notifications */ public function enable_live_update($table, $url=false){ - $this->live_update = new DataUpdate($this->sql, $this->config, $this->request, $table,$url); + $this->live_update = new $this->live_update_data_type($this->sql, $this->config, $this->request, $table,$url, array("connector" => $this)); $this->live_update->set_event($this->event,$this->names["item_class"]); $this->event->attach("beforeOutput", Array($this->live_update, "version_output")); $this->event->attach("beforeFiltering", Array($this->live_update, "get_updates")); diff --git a/codebase/db_common.php b/codebase/db_common.php index f6391d2..67c14c0 100644..100755 --- a/codebase/db_common.php +++ b/codebase/db_common.php @@ -8,6 +8,8 @@ require_once("tools.php"); /*! manager of data request **/ class DataRequestConfig{ + + private $action_mode = ""; private $filters; //!< array of filtering rules private $relation=false; //!< ID or other element used for linking hierarchy private $sort_by; //!< sorting field @@ -52,6 +54,7 @@ class DataRequestConfig{ $this->relation =$proto->get_relation(); $this->user = $proto->user; $this->version = $proto->version; + $this->action_mode = $proto->action_mode; } /*! convert self to string ( for logs ) @@ -69,6 +72,15 @@ class DataRequestConfig{ return $str; } + public function set_action_mode($action_mode) { + $this->action_mode = $action_mode; + return $this; + } + + public function get_action_mode() { + return $this->action_mode; + } + /*! returns set of filtering rules @return set of filtering rules diff --git a/codebase/gantt_connector.php b/codebase/gantt_connector.php index f8c62cf..9bd9422 100644..100755 --- a/codebase/gantt_connector.php +++ b/codebase/gantt_connector.php @@ -55,10 +55,12 @@ class GanttLinkDataItem extends DataItem { **/ class GanttConnector extends Connector{ + private $action_mode = ""; + public $links_table = ""; + + protected $live_update_data_type = "GanttDataUpdate"; protected $extra_output="";//!< extra info which need to be sent to client side protected $options=array();//!< hash of OptionsConnector - protected $links_mode = false; - /*! assign options collection to the column @@ -106,8 +108,14 @@ class GanttConnector extends Connector{ function parse_request(){ parent::parse_request(); - if (isset($_GET["gantt_mode"]) && $_GET["gantt_mode"] == "links") - $this->links_mode = true; + $action_links = "links"; + if(isset($_GET["gantt_mode"]) && $_GET["gantt_mode"] == $action_links) { + $this->action_mode = $action_links; + $this->request->set_action_mode($action_links); + $this->options[$action_links]->request->set_action_mode($action_links); + $this->options[$action_links]->request->set_user($this->request->get_user()); + } + if (count($this->config->text)){ if (isset($_GET["to"])) @@ -138,10 +146,15 @@ class GanttConnector extends Connector{ } } - public function render_links($table,$id="",$fields=false,$extra=false,$relation_id=false) { + public function render_links($table,$id="",$fields=false,$extra=false) { $links = new GanttLinksConnector($this->get_connection(),$this->names["db_class"]); + + if($this->live_update) + $links->enable_live_update($this->live_update->get_table()); + $links->render_table($table,$id,$fields,$extra); $this->set_options("links", $links); + $this->links_table = $table; } @@ -157,10 +170,10 @@ class GanttConnector extends Connector{ $this->set_relation(); if ($this->live_update !== false && $this->updating!==false) { - $this->live_update->get_updates(); + $this->live_update->get_updates(); } else { if ($this->editing){ - if ($this->links_mode && isset($this->options["links"])) { + if (($this->action_mode == "links") && isset($this->options["links"])) { $this->options["links"]->save(); } else { $dp = new $this->names["data_class"]($this,$this->config,$this->request); @@ -247,6 +260,7 @@ class JSONGanttDataItem extends GanttDataItem{ class JSONGanttConnector extends GanttConnector { protected $data_separator = ","; + protected $live_update_data_type = "JSONGanttDataUpdate"; /*! constructor @@ -340,6 +354,10 @@ class JSONGanttConnector extends GanttConnector { public function render_links($table,$id="",$fields=false,$extra=false,$relation_id=false) { $links = new JSONGanttLinksConnector($this->get_connection(),$this->names["db_class"]); + + if($this->live_update) + $links->enable_live_update($this->live_update->get_table()); + $links->render_table($table,$id,$fields,$extra); $this->set_options("links", $links); } @@ -348,6 +366,7 @@ class JSONGanttConnector extends GanttConnector { class GanttLinksConnector extends OptionsConnector { + protected $live_update_data_type = "GanttDataUpdate"; public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){ if (!$item_type) $item_type="GanttLinkDataItem"; @@ -372,6 +391,7 @@ class GanttLinksConnector extends OptionsConnector { class JSONGanttLinksConnector extends JSONOptionsConnector { + protected $live_update_data_type = "JSONGanttDataUpdate"; public function render(){ if (!$this->init_flag){ $this->init_flag=true; @@ -388,4 +408,60 @@ class JSONGanttLinksConnector extends JSONOptionsConnector { } } +class JSONGanttDataUpdate extends JSONDataUpdate { + + public function get_updates() { + $updates = $this->get_data_updates(); + //ToDo: Add rendering for data. + } + + + private function get_data_updates() { + $actions_table = $this->table; + $version = $this->request->get_version(); + $user = $this->request->get_user(); + + $select_actions = "SELECT DATAID, TYPE, USER FROM {$actions_table}"; + $select_actions .= " WHERE {$actions_table}.ID > '{$version}' AND {$actions_table}.USER <> '{$user}'"; + + + $output = array(); + $index = 0; + $actions_query = $this->sql->query($select_actions); + while($action_data=$this->sql->get_next($actions_query)){ + $action_type = $action_data["TYPE"]; + $type_parts = explode("#", $action_type); + $action_mode = $type_parts[1]; + if($action_mode == "links") { + $data = $this->select_links_for_action($action_data["DATAID"]); + $data = new DataItemUpdate($data, $this->config, $index, $this->item_class); + } + else { + $data = $this->select_task_for_action($action_data["DATAID"]); + $data = new DataItemUpdate($data, $this->config, $index, $this->item_class); + } + + array_push($output, $data); + $index++; + } + + return $output; + } + + protected function select_task_for_action($taskId) { + $tasks_table = $this->request->get_source(); + $field_task_id = $this->config->id['db_name']; + $select_actions_tasks = "SELECT * FROM {$tasks_table} WHERE {$taskId} = {$tasks_table}.{$field_task_id}"; + return $this->sql->get_next($this->sql->query($select_actions_tasks)); + } + + protected function select_links_for_action($taskId) { + $links_connector_options = $this->options["connector"]->get_options(); + $links_table = $links_connector_options["links"]->get_request()->get_source(); + $field_task_id = $this->config->id['db_name']; + $select_actions_tasks = "SELECT * FROM {$links_table} WHERE {$taskId} = {$links_table}.{$field_task_id}"; + return $this->sql->get_next($this->sql->query($select_actions_tasks)); + } +} + ?>
\ No newline at end of file diff --git a/codebase/mixed_connector.php b/codebase/mixed_connector.php index 461d6ec..bbef8d4 100644 --- a/codebase/mixed_connector.php +++ b/codebase/mixed_connector.php @@ -7,22 +7,53 @@ require_once("base_connector.php"); class MixedConnector extends Connector { - protected $connectors = array(); - - public function add($name, $conn) { - $this->connectors[$name] = $conn; - } - - public function render() { - $result = "{"; - $parts = array(); - foreach($this->connectors as $name => $conn) { - $conn->asString(true); - $parts[] = "\"".$name."\":".($conn->render())."\n"; - } - $result .= implode(",\n", $parts)."}"; - echo $result; - } + private $_data_type = null; + + function __construct($dataType = "json") { + $this->_data_type = $dataType; + } + + protected $attributes = array(); + protected $connectors = array(); + + public function add($name, $conn) { + $this->connectors[$name] = $conn; + $conn->simple = true; + } + + public function render() { + if($this->_data_type == "json") + $this->render_json(); + else + $this->render_xml(); + } + + private function render_json() { + $result = "{"; + $parts = array(); + foreach($this->connectors as $name => $conn) { + $conn->asString(true); + $parts[] = "\"".$name."\":".($conn->render())."\n"; + } + $result .= implode(",\n", $parts)."}"; + echo $result; + } + + private function render_xml() { + $result = ""; + $parts = array(); + + foreach($this->connectors as $name => $conn) { + $conn->asString(true); + $parts[] = "<".$name.">".($conn->render())."</".$name.">\n"; + } + $result .= implode("", $parts); + $this->output_as_xml($result); + } + + protected function output_as_xml($res) { + echo "<?xml version='1.0' encoding='".$this->encoding."' ?>".$this->xml_start().$res.$this->xml_end(); + } } ?>
\ No newline at end of file diff --git a/codebase/scheduler_connector.php b/codebase/scheduler_connector.php index ee0cd20..b7049db 100644..100755 --- a/codebase/scheduler_connector.php +++ b/codebase/scheduler_connector.php @@ -136,6 +136,7 @@ class JSONSchedulerDataItem extends SchedulerDataItem{ class JSONSchedulerConnector extends SchedulerConnector { protected $data_separator = ","; + protected $live_update_data_type = "JSONDataUpdate"; /*! constructor diff --git a/codebase/update.php b/codebase/update.php index 45a2837..9ef12ac 100644..100755 --- a/codebase/update.php +++ b/codebase/update.php @@ -1,267 +1,345 @@ -<?php -/* - @author dhtmlx.com - @license GPL, see license.txt -*/ - -/*! DataItemUpdate class for realization Optimistic concurrency control - Wrapper for DataItem object - It's used during outputing updates instead of DataItem object - Create wrapper for every data item with update information. -*/ -class DataItemUpdate extends DataItem { - - - /*! constructor - @param data - hash of data - @param config - DataConfig object - @param index - index of element - */ - public function __construct($data,$config,$index,$type){ - $this->config=$config; - $this->data=$data; - $this->index=$index; - $this->skip=false; - $this->child = new $type($data, $config, $index); - } - - /*! returns parent_id (for Tree and TreeGrid components) - */ - public function get_parent_id(){ - if (method_exists($this->child, 'get_parent_id')) { - return $this->child->get_parent_id(); - } else { - return ''; - } - } - - - /*! generate XML on the data hash base - */ - public function to_xml(){ - $str= "<update "; - $str .= 'status="'.$this->data['type'].'" '; - $str .= 'id="'.$this->data['dataId'].'" '; - $str .= 'parent="'.$this->get_parent_id().'"'; - $str .= '>'; - $str .= $this->child->to_xml(); - $str .= '</update>'; - return $str; - } - - /*! return starting tag for XML string - */ - public function to_xml_start(){ - $str="<update "; - $str .= 'status="'.$this->data['type'].'" '; - $str .= 'id="'.$this->data['dataId'].'" '; - $str .= 'parent="'.$this->get_parent_id().'"'; - $str .= '>'; - $str .= $this->child->to_xml_start(); - return $str; - } - - /*! return ending tag for XML string - */ - public function to_xml_end(){ - $str = $this->child->to_xml_end(); - $str .= '</update>'; - return $str; - } - - /*! returns false for outputing only current item without child items - */ - public function has_kids(){ - return false; - } - - /*! sets count of child items - @param value - count of child items - */ - public function set_kids($value){ - if (method_exists($this->child, 'set_kids')) { - $this->child->set_kids($value); - } - } - - /*! sets attribute for item - */ - public function set_attribute($name, $value){ - if (method_exists($this->child, 'set_attribute')) { - LogMaster::log("setting attribute: \nname = {$name}\nvalue = {$value}"); - $this->child->set_attribute($name, $value); - } else { - LogMaster::log("set_attribute method doesn't exists"); - } - } -} - - -class DataUpdate{ - - protected $table; //!< table , where actions are stored - protected $url; //!< url for notification service, optional - protected $sql; //!< DB wrapper object - protected $config; //!< DBConfig object - protected $request; //!< DBRequestConfig object - protected $event; - protected $item_class; - protected $demu; - - //protected $config;//!< DataConfig instance - //protected $request;//!< DataRequestConfig instance - - /*! constructor - - @param connector - Connector object - @param config - DataConfig object - @param request - DataRequestConfig object - */ - function __construct($sql, $config, $request, $table, $url){ - $this->config= $config; - $this->request= $request; - $this->sql = $sql; - $this->table=$table; - $this->url=$url; - $this->demu = false; - } - - public function set_demultiplexor($path){ - $this->demu = $path; - } - - public function set_event($master, $name){ - $this->event = $master; - $this->item_class = $name; - } - - private function select_update($actions_table, $join_table, $id_field_name, $version, $user) { - $sql = "SELECT * FROM {$actions_table}"; - $sql .= " LEFT OUTER JOIN {$join_table} ON "; - $sql .= "{$actions_table}.DATAID = {$join_table}.{$id_field_name} "; - $sql .= "WHERE {$actions_table}.ID > '{$version}' AND {$actions_table}.USER <> '{$user}'"; - return $sql; - } - - private function get_update_max_version() { - $sql = "SELECT MAX(id) as VERSION FROM {$this->table}"; - $res = $this->sql->query($sql); - $data = $this->sql->get_next($res); - - if ($data == false || $data['VERSION'] == false) - return 1; - else - return $data['VERSION']; - } - - private function log_update_action($actions_table, $dataId, $status, $user) { - $sql = "INSERT INTO {$actions_table} (DATAID, TYPE, USER) VALUES ('{$dataId}', '{$status}', '{$user}')"; - $this->sql->query($sql); - if ($this->demu) - file_get_contents($this->demu); - } - - - - - /*! records operations in actions_table - @param action - DataAction object - */ - public function log_operations($action) { - $type = $this->sql->escape($action->get_status()); - $dataId = $this->sql->escape($action->get_new_id()); - $user = $this->sql->escape($this->request->get_user()); - if ($type!="error" && $type!="invalid" && $type !="collision") { - $this->log_update_action($this->table, $dataId, $type, $user); - } - } - - - /*! return action version in XMl format - */ - public function get_version() { - $version = $this->get_update_max_version(); - return "<userdata name='version'>".$version."</userdata>"; - } - - - /*! adds action version in output XML as userdata - */ - public function version_output($conn, $out) { - $out->add($this->get_version()); - } - - - /*! create update actions in XML-format and sends it to output - */ - public function get_updates() { - $sub_request = new DataRequestConfig($this->request); - $version = $this->request->get_version(); - $user = $this->request->get_user(); - - $table = explode(" ", $this->request->get_source()); - $sub_request->parse_sql($this->select_update($this->table, $table[0], $this->config->id['db_name'], $version, $user)); - $sub_request->set_relation(false); - - $output = $this->render_set($this->sql->select($sub_request), $this->item_class); - - ob_clean(); - header("Content-type:text/xml"); - - echo $this->updates_start(); - echo $this->get_version(); - echo $output; - echo $this->updates_end(); - } - - - protected function render_set($res, $name){ - $output=""; - $index=0; - while ($data=$this->sql->get_next($res)){ - $data = new DataItemUpdate($data,$this->config,$index, $name); - $this->event->trigger("beforeRender",$data); - $output.=$data->to_xml(); - $index++; - } - return $output; - } - - /*! returns update start string - */ - protected function updates_start() { - $start = '<updates>'; - return $start; - } - - /*! returns update end string - */ - protected function updates_end() { - $start = '</updates>'; - return $start; - } - - /*! checks if action version given by client is deprecated - @param action - DataAction object - */ - public function check_collision($action) { - $version = $this->sql->escape($this->request->get_version()); - //$user = $this->sql->escape($this->request->get_user()); - $last_version = $this->get_update_max_version(); - if (($last_version > $version)&&($action->get_status() == 'update')) { - $action->error(); - $action->set_status('collision'); - } - } -} - +<?php
+/*
+ @author dhtmlx.com
+ @license GPL, see license.txt
+*/
+
+/*! DataItemUpdate class for realization Optimistic concurrency control
+ Wrapper for DataItem object
+ It's used during outputing updates instead of DataItem object
+ Create wrapper for every data item with update information.
+*/
+class DataItemUpdate extends DataItem {
+
+
+ /*! constructor
+ @param data
+ hash of data
+ @param config
+ DataConfig object
+ @param index
+ index of element
+ */
+ public function __construct($data,$config,$index,$type){
+ $this->config=$config;
+ $this->data=$data;
+ $this->index=$index;
+ $this->skip=false;
+ $this->child = new $type($data, $config, $index);
+ }
+
+ /*! returns parent_id (for Tree and TreeGrid components)
+ */
+ public function get_parent_id(){
+ if (method_exists($this->child, 'get_parent_id')) {
+ return $this->child->get_parent_id();
+ } else {
+ return '';
+ }
+ }
+
+
+ /*! generate XML on the data hash base
+ */
+ public function to_xml(){
+ $str= "<update ";
+ $str .= 'status="'.$this->data['action_table_type'].'" ';
+ $str .= 'id="'.$this->data['dataId'].'" ';
+ $str .= 'parent="'.$this->get_parent_id().'"';
+ $str .= '>';
+ $str .= $this->child->to_xml();
+ $str .= '</update>';
+ return $str;
+ }
+
+ /*! return starting tag for XML string
+ */
+ public function to_xml_start(){
+ $str="<update ";
+ $str .= 'status="'.$this->data['action_table_type'].'" ';
+ $str .= 'id="'.$this->data['dataId'].'" ';
+ $str .= 'parent="'.$this->get_parent_id().'"';
+ $str .= '>';
+ $str .= $this->child->to_xml_start();
+ return $str;
+ }
+
+ /*! return ending tag for XML string
+ */
+ public function to_xml_end(){
+ $str = $this->child->to_xml_end();
+ $str .= '</update>';
+ return $str;
+ }
+
+ /*! returns false for outputing only current item without child items
+ */
+ public function has_kids(){
+ return false;
+ }
+
+ /*! sets count of child items
+ @param value
+ count of child items
+ */
+ public function set_kids($value){
+ if (method_exists($this->child, 'set_kids')) {
+ $this->child->set_kids($value);
+ }
+ }
+
+ /*! sets attribute for item
+ */
+ public function set_attribute($name, $value){
+ if (method_exists($this->child, 'set_attribute')) {
+ LogMaster::log("setting attribute: \nname = {$name}\nvalue = {$value}");
+ $this->child->set_attribute($name, $value);
+ } else {
+ LogMaster::log("set_attribute method doesn't exists");
+ }
+ }
+}
+
+class DataUpdate{
+
+ protected $table; //!< table , where actions are stored
+ protected $url; //!< url for notification service, optional
+ protected $sql; //!< DB wrapper object
+ protected $config; //!< DBConfig object
+ protected $request; //!< DBRequestConfig object
+ protected $event;
+ protected $item_class;
+ protected $demu;
+
+ //protected $config;//!< DataConfig instance
+ //protected $request;//!< DataRequestConfig instance
+
+ /*! constructor
+
+ @param connector
+ Connector object
+ @param config
+ DataConfig object
+ @param request
+ DataRequestConfig object
+ */
+ function __construct($sql, $config, $request, $table, $url, $options){
+ $this->config= $config;
+ $this->request= $request;
+ $this->sql = $sql;
+ $this->table=$table;
+ $this->url=$url;
+ $this->demu = false;
+ $this->options = $options;
+ }
+
+ public function set_demultiplexor($path){
+ $this->demu = $path;
+ }
+
+ public function set_event($master, $name){
+ $this->event = $master;
+ $this->item_class = $name;
+ }
+
+ protected function select_update($actions_table, $join_table, $id_field_name, $version, $user) {
+ $sql = "SELECT $join_table.*, {$actions_table}.id, {$actions_table}.dataId, {$actions_table}.type as action_table_type, {$actions_table}.user FROM {$actions_table}";
+ $sql .= " LEFT OUTER JOIN {$join_table} ON ";
+ $sql .= "{$actions_table}.DATAID = {$join_table}.{$id_field_name} ";
+ $sql .= "WHERE {$actions_table}.ID > '{$version}' AND {$actions_table}.USER <> '{$user}'";
+ return $sql;
+ }
+
+ protected function get_update_max_version() {
+ $sql = "SELECT MAX(id) as VERSION FROM {$this->table}";
+ $res = $this->sql->query($sql);
+ $data = $this->sql->get_next($res);
+
+ if ($data == false || $data['VERSION'] == false)
+ return 1;
+ else
+ return $data['VERSION'];
+ }
+
+ private function log_update_action($actions_table, $dataId, $status, $user) {
+ $sql = "INSERT INTO {$actions_table} (DATAID, TYPE, USER) VALUES ('{$dataId}', '{$status}', '{$user}')";
+ $this->sql->query($sql);
+ if ($this->demu)
+ file_get_contents($this->demu);
+ }
+
+ public function get_table() {
+ return $this->table;
+ }
+
+ /*! records operations in actions_table
+ @param action
+ DataAction object
+ */
+ public function log_operations($action) {
+ $type = $this->sql->escape($action->get_status());
+ $dataId = $this->sql->escape($action->get_new_id());
+ $user = $this->sql->escape($this->request->get_user());
+ if ($type!="error" && $type!="invalid" && $type !="collision") {
+ $action_mode = $this->request->get_action_mode();
+ if(!empty($action_mode))
+ $type .= "#".$action_mode;
+
+ $this->log_update_action($this->table, $dataId, $type, $user);
+ }
+ }
+
+
+ /*! return action version in XMl format
+ */
+ public function get_version() {
+ $version = $this->get_update_max_version();
+ return "<userdata name='version'>".$version."</userdata>";
+ }
+
+
+ /*! adds action version in output XML as userdata
+ */
+ public function version_output($conn, $out) {
+ $out->add($this->get_version());
+ }
+
+
+ /*! create update actions in XML-format and sends it to output
+ */
+ public function get_updates() {
+ $sub_request = new DataRequestConfig($this->request);
+ $version = $this->request->get_version();
+ $user = $this->request->get_user();
+
+ $sub_request->parse_sql($this->select_update($this->table, $this->request->get_source(), $this->config->id['db_name'], $version, $user));
+ $sub_request->set_relation(false);
+
+ $output = $this->render_set($this->sql->select($sub_request), $this->item_class);
+
+ ob_clean();
+ header("Content-type:text/xml");
+
+ echo $this->updates_start();
+ echo $this->get_version();
+ echo $output;
+ echo $this->updates_end();
+ }
+
+
+ protected function render_set($res, $name){
+ $output="";
+ $index=0;
+ while ($data=$this->sql->get_next($res)){
+ $data = new DataItemUpdate($data,$this->config,$index, $name);
+ $this->event->trigger("beforeRender",$data);
+ $output.=$data->to_xml();
+ $index++;
+ }
+ return $output;
+ }
+
+ /*! returns update start string
+ */
+ protected function updates_start() {
+ $start = '<updates>';
+ return $start;
+ }
+
+ /*! returns update end string
+ */
+ protected function updates_end() {
+ $start = '</updates>';
+ return $start;
+ }
+
+ /*! checks if action version given by client is deprecated
+ @param action
+ DataAction object
+ */
+ public function check_collision($action) {
+ $version = $this->sql->escape($this->request->get_version());
+ //$user = $this->sql->escape($this->request->get_user());
+ $last_version = $this->get_update_max_version();
+ if (($last_version > $version)&&($action->get_status() == 'update')) {
+ $action->error();
+ $action->set_status('collision');
+ }
+ }
+}
+
+
+class JSONDataItemUpdate extends DataItemUpdate {
+
+ public function to_xml() {
+ return array(
+ "status" => $this->data["action_table_type"],
+ "id" => $this->data["dataId"],
+ "parent" => $this->get_parent_id(),
+ "data" => $this->child->to_xml()
+ );
+ }
+
+}
+
+class JSONDataUpdate extends DataUpdate {
+
+ /*! adds action version in output XML as userdata
+*/
+ public function version_output($conn, $out) {
+ $outJson = json_decode($out->__toString(), true);
+ if(!isset($outJson["userdata"]))
+ $outJson["userdata"] = array();
+
+ $outJson["userdata"] = array_merge($outJson["userdata"], $this->get_version());
+ $out->reset();
+ $out->add(json_encode($outJson));
+ }
+
+ /*! return action version in XMl format
+ */
+ public function get_version() {
+ $version = array("version" => $this->get_update_max_version());
+ return $version;
+ }
+
+ public function get_updates() {
+ $sub_request = new DataRequestConfig($this->request);
+ $version = $this->request->get_version();
+ $user = $this->request->get_user();
+
+ $sub_request->parse_sql($this->select_update($this->table, $this->request->get_source(), $this->config->id['db_name'], $version, $user));
+ $sub_request->set_relation(false);
+
+ $output = $this->render_set($this->sql->select($sub_request), $this->item_class);
+
+ if(!isset($output["userdata"]))
+ $output["userdata"] = array();
+
+ $output["userdata"] = array_merge($output["userdata"], $this->get_version());
+ $this->output(json_encode($output));
+ }
+
+ protected function render_set($res, $name){
+ $output = array();
+ $index = 0;
+ while($data = $this->sql->get_next($res)) {
+ $data = new JSONDataItemUpdate($data, $this->config, $index, $name);
+ $this->event->trigger("beforeRender", $data);
+ array_push($output, $data->to_xml());
+ $index++;
+ }
+
+ return array("updates" => $output);
+ }
+
+ protected function output($res){
+ $out = new OutputWriter($res, "");
+ $out->set_type("json");
+ $this->event->trigger("beforeOutput", $this, $out);
+ $out->output("", true, $this->encoding);
+ }
+
+}
+
?>
\ No newline at end of file |