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
|
<?php
namespace Dhtmlx\Connector\Data;
use Dhtmlx\Connector\Tools\LogMaster;
use \Exception;
/*! manager of data configuration
**/
class DataConfig {
public $id;////!< name of ID field
public $relation_id;//!< name or relation ID field
public $text;//!< array of text fields
public $data;//!< array of all known fields , fields which exists only in this collection will not be included in dataprocessor's operations
/*! converts self to the string, for logging purposes
**/
public function __toString(){
$str="ID:{$this->id['db_name']}(ID:{$this->id['name']})\n";
$str.="Relation ID:{$this->relation_id['db_name']}({$this->relation_id['name']})\n";
$str.="Data:";
for ($i=0; $i<sizeof($this->text); $i++)
$str.="{$this->text[$i]['db_name']}({$this->text[$i]['name']}),";
$str.="\nExtra:";
for ($i=0; $i<sizeof($this->data); $i++)
$str.="{$this->data[$i]['db_name']}({$this->data[$i]['name']}),";
return $str;
}
/*! removes un-used fields from configuration
@param name
name of field , which need to be preserved
*/
public function minimize($name){
for ($i=0; $i < sizeof($this->text); $i++){
if ($this->text[$i]["db_name"]==$name || $this->text[$i]["name"]==$name){
$this->text[$i]["name"]="value";
$this->data=array($this->text[$i]);
$this->text=array($this->text[$i]);
return;
}
}
throw new Exception("Incorrect dataset minimization, master field not found.");
}
public function limit_fields($data){
if (isset($this->full_field_list))
$this->restore_fields();
$this->full_field_list = $this->text;
$this->text = array();
for ($i=0; $i < sizeof($this->full_field_list); $i++) {
if (array_key_exists($this->full_field_list[$i]["name"],$data))
$this->text[] = $this->full_field_list[$i];
}
}
public function restore_fields(){
if (isset($this->full_field_list))
$this->text = $this->full_field_list;
}
/*! initialize inner state by parsing configuration parameters
@param id
name of id field
@param fields
name of data field(s)
@param extra
name of extra field(s)
@param relation
name of relation field
*/
public function init($id,$fields,$extra,$relation){
$this->id = $this->parse($id,false);
$this->text = $this->parse($fields,true);
$this->data = array_merge($this->text,$this->parse($extra,true));
$this->relation_id = $this->parse($relation,false);
}
/*! parse configuration string
@param key
key string from configuration
@param mode
multi names flag
@return
parsed field name object
*/
private function parse($key,$mode){
if ($mode){
if (!$key) return array();
$key=explode(",",$key);
for ($i=0; $i < sizeof($key); $i++)
$key[$i]=$this->parse($key[$i],false);
return $key;
}
$key=explode("(",$key);
$data=array("db_name"=>trim($key[0]), "name"=>trim($key[0]));
if (sizeof($key)>1)
$data["name"]=substr(trim($key[1]),0,-1);
return $data;
}
/*! constructor
init public collectons
@param proto
DataConfig object used as prototype for new one, optional
*/
public function __construct($proto=false){
if ($proto!==false)
$this->copy($proto);
else {
$this->text=array();
$this->data=array();
$this->id=array("name"=>"dhx_auto_id", "db_name"=>"dhx_auto_id");
$this->relation_id=array("name"=>"", "db_name"=>"");
}
}
/*! copy properties from source object
@param proto
source object
*/
public function copy($proto){
$this->id = $proto->id;
$this->relation_id = $proto->relation_id;
$this->text = $proto->text;
$this->data = $proto->data;
}
/*! returns list of data fields (db_names)
@return
list of data fields ( ready to be used in SQL query )
*/
public function db_names_list($db){
$out=array();
if ($this->id["db_name"])
array_push($out,$db->escape_name($this->id["db_name"]));
if ($this->relation_id["db_name"])
array_push($out,$db->escape_name($this->relation_id["db_name"]));
for ($i=0; $i < sizeof($this->data); $i++){
if ($this->data[$i]["db_name"]!=$this->data[$i]["name"])
$out[]=$db->escape_name($this->data[$i]["db_name"])." as ".$this->data[$i]["name"];
else
$out[]=$db->escape_name($this->data[$i]["db_name"]);
}
return $out;
}
/*! add field to dataset config ($text collection)
added field will be used in all auto-generated queries
@param name
name of field
@param aliase
aliase of field, optional
*/
public function add_field($name,$aliase=false){
if ($aliase===false) $aliase=$name;
//adding to list of data-active fields
if ($this->id["db_name"]==$name || $this->relation_id["db_name"] == $name){
LogMaster::log("Field name already used as ID, be sure that it is really necessary.");
}
if ($this->is_field($name,$this->text)!=-1)
throw new Exception('Data field already registered: '.$name);
array_push($this->text,array("db_name"=>$name,"name"=>$aliase));
//adding to list of all fields as well
if ($this->is_field($name,$this->data)==-1)
array_push($this->data,array("db_name"=>$name,"name"=>$aliase));
}
/*! remove field from dataset config ($text collection)
removed field will be excluded from all auto-generated queries
@param name
name of field, or aliase of field
*/
public function remove_field($name){
$ind = $this->is_field($name);
if ($ind==-1) throw new Exception('There was no such data field registered as: '.$name);
array_splice($this->text,$ind,1);
//we not deleting field from $data collection, so it will not be included in data operation, but its data still available
}
/*! remove field from dataset config ($text and $data collections)
removed field will be excluded from all auto-generated queries
@param name
name of field, or aliase of field
*/
public function remove_field_full($name){
$ind = $this->is_field($name);
if ($ind==-1) throw new Exception('There was no such data field registered as: '.$name);
array_splice($this->text,$ind,1);
$ind = $this->is_field($name, $this->data);
if ($ind==-1) throw new Exception('There was no such data field registered as: '.$name);
array_splice($this->data,$ind,1);
}
/*! check if field is a part of dataset
@param name
name of field
@param collection
collection, against which check will be done, $text collection by default
@return
returns true if field already a part of dataset, otherwise returns true
*/
public function is_field($name,$collection = false){
if (!$collection)
$collection=$this->text;
for ($i=0; $i<sizeof($collection); $i++)
if ($collection[$i]["name"] == $name || $collection[$i]["db_name"] == $name) return $i;
return -1;
}
}
|