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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
<?php
/*
@author dhtmlx.com
@license GPL, see license.txt
*/
require_once("base_connector.php");
class CommonDataProcessor extends DataProcessor{
protected function get_post_values($ids){
if (isset($_GET['action'])){
$data = array();
if (isset($_POST["id"]))
$data[$_POST["id"]] = $_POST;
else
$data["dummy_id"] = $_POST;
return $data;
}
return parent::get_post_values($ids);
}
protected function get_ids(){
if (isset($_GET['action'])){
if (isset($_POST["id"]))
return array($_POST['id']);
else
return array("dummy_id");
}
return parent::get_ids();
}
protected function get_operation($rid){
if (isset($_GET['action']))
return $_GET['action'];
return parent::get_operation($rid);
}
public function output_as_xml($results){
if (isset($_GET['action'])){
LogMaster::log("Edit operation finished",$results);
ob_clean();
$type = $results[0]->get_status();
if ($type == "error" || $type == "invalid"){
echo "false";
} else if ($type=="insert"){
echo "true\n".$results[0]->get_new_id();
} else
echo "true";
} else
return parent::output_as_xml($results);
}
};
/*! DataItem class for DataView component
**/
class CommonDataItem extends DataItem{
/*! return self as XML string
*/
function to_xml(){
if ($this->skip) return "";
return $this->to_xml_start().$this->to_xml_end();
}
function to_xml_start(){
$str="<item id='".$this->get_id()."' ";
for ($i=0; $i < sizeof($this->config->text); $i++){
$name=$this->config->text[$i]["name"];
$str.=" ".$name."='".$this->xmlentities($this->data[$name])."'";
}
return $str.">";
}
}
/*! Connector class for DataView
**/
class DataConnector extends Connector{
/*! constructor
Here initilization of all Masters occurs, execution timer initialized
@param res
db connection resource
@param type
string , which hold type of database ( MySQL or Postgre ), optional, instead of short DB name, full name of DataWrapper-based class can be provided
@param item_type
name of class, which will be used for item rendering, optional, DataItem will be used by default
@param data_type
name of class which will be used for dataprocessor calls handling, optional, DataProcessor class will be used by default.
*/
public function __construct($res,$type=false,$item_type=false,$data_type=false){
if (!$item_type) $item_type="CommonDataItem";
if (!$data_type) $data_type="CommonDataProcessor";
parent::__construct($res,$type,$item_type,$data_type);
}
protected function parse_request_mode(){
//do nothing, at least for now
}
//parse GET scoope, all operations with incoming request must be done here
protected function parse_request(){
if (isset($_GET['action'])){
$action = $_GET['action'];
//simple request mode
if ($action == "get"){
//data request
if (isset($_GET['id'])){
//single entity data request
$this->request->set_filter($this->config->id["name"],$_GET['id'],"=");
} else {
//loading collection of items
}
} else {
//data saving
$this->editing = true;
}
} else {
if (isset($_GET['editing']) && isset($_POST['ids']))
$this->editing = true;
parent::parse_request();
}
if (isset($_GET["start"]) && isset($_GET["count"]))
$this->request->set_limit($_GET["start"],$_GET["count"]);
}
/*! renders self as xml, starting part
*/
protected function xml_start(){
return "<data>";
}
};
class JSONDataConnector extends DataConnector{
public function __construct($res,$type=false,$item_type=false,$data_type=false){
if (!$item_type) $item_type="JSONCommonDataItem";
if (!$data_type) $data_type="CommonDataProcessor";
$this->data_separator = ",\n";
parent::__construct($res,$type,$item_type,$data_type);
}
protected function output_as_xml($res){
$start = "[\n";
$end = substr($this->render_set($res),0,-2)."\n]";
if ($this->dload){
$start = "{ \"data\":".$start.$end;
if ($pos=$this->request->get_start())
$end = ", \"pos\":".$pos." }";
else
$end = ", \"pos\":0, \"total_count\":".$this->sql->get_size($this->request)." }";
}
$out = new OutputWriter($start, $end);
$out->set_type("json");
$this->event->trigger("beforeOutput", $this, $out);
$out->output("", true, $this->encoding);
}
}
class JSONCommonDataItem extends DataItem{
/*! return self as XML string
*/
function to_xml(){
if ($this->skip) return "";
$data = array( "id" => $this->get_id() );
for ($i=0; $i<sizeof($this->config->text); $i++){
$extra = $this->config->text[$i]["name"];
$data[$extra]=$this->data[$extra];
}
return json_encode($data);
}
}
class TreeCommonDataItem extends CommonDataItem{
protected $kids=-1;
function to_xml_start(){
$str="<item id='".$this->get_id()."' ";
for ($i=0; $i < sizeof($this->config->text); $i++){
$name=$this->config->text[$i]["name"];
$str.=" ".$name."='".$this->xmlentities($this->data[$name])."'";
}
if ($this->kids === true)
$str .=" dhx_kids='1'";
return $str.">";
}
function has_kids(){
return $this->kids;
}
function set_kids($value){
$this->kids=$value;
}
}
class TreeDataConnector extends DataConnector{
public function __construct($res,$type=false,$item_type=false,$data_type=false){
if (!$item_type) $item_type="TreeCommonDataItem";
if (!$data_type) $data_type="CommonDataProcessor";
parent::__construct($res,$type,$item_type,$data_type);
$this->event->attach("afterInsert",array($this,"parent_id_correction_a"));
$this->event->attach("beforeProcessing",array($this,"parent_id_correction_b"));
}
protected function xml_start(){
return "<data parent='".$this->request->get_relation()."'>";
}
/*! store info about ID changes during insert operation
@param dataAction
data action object during insert operation
*/
public function parent_id_correction_a($dataAction){
$this->id_swap[$dataAction->get_id()]=$dataAction->get_new_id();
}
/*! update ID if it was affected by previous operation
@param dataAction
data action object, before any processing operation
*/
public function parent_id_correction_b($dataAction){
$relation = $this->config->relation_id["db_name"];
$value = $dataAction->get_value($relation);
if (array_key_exists($value,$this->id_swap))
$dataAction->set_value($relation,$this->id_swap[$value]);
}
//parse GET scoope, all operations with incoming request must be done here
protected function parse_request(){
parent::parse_request();
if (isset($_GET["parent"]))
$this->request->set_relation($_GET["parent"]);
else
$this->request->set_relation("0");
}
protected function render_set($res){
$output="";
$index=0;
while ($data=$this->sql->get_next($res)){
$data = new $this->names["item_class"]($data,$this->config,$index);
$this->event->trigger("beforeRender",$data);
//there is no info about child elements,
//if we are using dyn. loading - assume that it has,
//in normal mode just exec sub-render routine
if ($data->has_kids()===-1 && $this->dload)
$data->set_kids(true);
$output.=$data->to_xml_start();
if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$this->dload)){
$sub_request = new DataRequestConfig($this->request);
$sub_request->set_relation($data->get_id());
$output.=$this->render_set($this->sql->select($sub_request));
}
$output.=$data->to_xml_end();
$index++;
}
return $output;
}
}
class JSONTreeDataConnector extends TreeDataConnector{
public function __construct($res,$type=false,$item_type=false,$data_type=false){
if (!$item_type) $item_type="JSONTreeCommonDataItem";
if (!$data_type) $data_type="CommonDataProcessor";
parent::__construct($res,$type,$item_type,$data_type);
$this->event->attach("afterInsert",array($this,"parent_id_correction_a"));
$this->event->attach("beforeProcessing",array($this,"parent_id_correction_b"));
}
protected function render_set($res){
$output=array();
$index=0;
while ($data=$this->sql->get_next($res)){
$data = new $this->names["item_class"]($data,$this->config,$index);
$this->event->trigger("beforeRender",$data);
//there is no info about child elements,
//if we are using dyn. loading - assume that it has,
//in normal mode just exec sub-render routine
if ($data->has_kids()===-1 && $this->dload)
$data->set_kids(true);
$record = &$data->to_xml_start();
if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$this->dload)){
$sub_request = new DataRequestConfig($this->request);
$sub_request->set_relation($data->get_id());
$temp = &$this->render_set($this->sql->select($sub_request));
if (sizeof($temp))
$record["data"] = $temp;
}
$output[] = $record;
$index++;
}
return $output;
}
protected function output_as_xml($res){
$data = array();
$data["parent"] = $this->request->get_relation();
$data["data"] = $this->render_set($res);
$out = new OutputWriter(json_encode($data), "");
$out->set_type("json");
$this->event->trigger("beforeOutput", $this, $out);
$out->output("", true, $this->encoding);
}
}
class JSONTreeCommonDataItem extends TreeCommonDataItem{
/*! return self as XML string
*/
function to_xml_start(){
if ($this->skip) return "";
$data = array( "id" => $this->get_id() );
for ($i=0; $i<sizeof($this->config->text); $i++){
$extra = $this->config->text[$i]["name"];
$data[$extra]=$this->data[$extra];
}
if ($this->kids === true)
$data["dhx_kids"] = 1;
return $data;
}
function to_xml_end(){
return "";
}
}
?>
|