1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
<?php
namespace Dhtmlx\Connector\Data;
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();
$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');
}
}
}
|