1 | <?php
|
---|
2 | require_once("base_connector.php");
|
---|
3 |
|
---|
4 | /*! DataItem class for Tree component
|
---|
5 | **/
|
---|
6 |
|
---|
7 | class TreeDataItem extends DataItem{
|
---|
8 | private $im0;//!< image of closed folder
|
---|
9 | private $im1;//!< image of opened folder
|
---|
10 | private $im2;//!< image of leaf item
|
---|
11 | private $check;//!< checked state
|
---|
12 | private $kids=-1;//!< checked state
|
---|
13 | private $attrs;//!< collection of custom attributes
|
---|
14 |
|
---|
15 | function __construct($data,$config,$index){
|
---|
16 | parent::__construct($data,$config,$index);
|
---|
17 |
|
---|
18 | $this->im0=false;
|
---|
19 | $this->im1=false;
|
---|
20 | $this->im2=false;
|
---|
21 | $this->check=false;
|
---|
22 | $this->attrs = array();
|
---|
23 | $this->userdata = array();
|
---|
24 | }
|
---|
25 | /*! get id of parent record
|
---|
26 |
|
---|
27 | @return
|
---|
28 | id of parent record
|
---|
29 | */
|
---|
30 | function get_parent_id(){
|
---|
31 | return $this->data[$this->config->relation_id["name"]];
|
---|
32 | }
|
---|
33 | /*! get state of items checkbox
|
---|
34 |
|
---|
35 | @return
|
---|
36 | state of item's checkbox as int value, false if state was not defined
|
---|
37 | */
|
---|
38 | function get_check_state(){
|
---|
39 | return $this->check;
|
---|
40 | }
|
---|
41 | /*! set state of item's checkbox
|
---|
42 |
|
---|
43 | @param value
|
---|
44 | int value, 1 - checked, 0 - unchecked, -1 - third state
|
---|
45 | */
|
---|
46 | function set_check_state($value){
|
---|
47 | $this->check=$value;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /*! return count of child items
|
---|
51 | -1 if there is no info about childs
|
---|
52 | @return
|
---|
53 | count of child items
|
---|
54 | */
|
---|
55 | function has_kids(){
|
---|
56 | return $this->kids;
|
---|
57 | }
|
---|
58 | /*! sets count of child items
|
---|
59 | @param value
|
---|
60 | count of child items
|
---|
61 | */
|
---|
62 | function set_kids($value){
|
---|
63 | $this->kids=$value;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*! set custom attribute
|
---|
67 |
|
---|
68 | @param name
|
---|
69 | name of the attribute
|
---|
70 | @param value
|
---|
71 | new value of the attribute
|
---|
72 | */
|
---|
73 | function set_attribute($name, $value){
|
---|
74 | switch($name){
|
---|
75 | case "id":
|
---|
76 | $this->set_id($value);
|
---|
77 | break;
|
---|
78 | case "text":
|
---|
79 | $this->data[$this->config->text[0]["name"]]=$value;
|
---|
80 | break;
|
---|
81 | case "checked":
|
---|
82 | $this->set_check_state($value);
|
---|
83 | break;
|
---|
84 | case "im0":
|
---|
85 | $this->im0=$value;
|
---|
86 | break;
|
---|
87 | case "im1":
|
---|
88 | $this->im1=$value;
|
---|
89 | break;
|
---|
90 | case "im2":
|
---|
91 | $this->im2=$value;
|
---|
92 | break;
|
---|
93 | case "child":
|
---|
94 | $this->set_kids($value);
|
---|
95 | break;
|
---|
96 | default:
|
---|
97 | $this->attrs[$name]=$value;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*! set userdata section for the item
|
---|
102 |
|
---|
103 | @param name
|
---|
104 | name of userdata
|
---|
105 | @param value
|
---|
106 | value of userdata
|
---|
107 | */
|
---|
108 | function set_userdata($name, $value){
|
---|
109 | $this->userdata[$name]=$value;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*! assign image for tree's item
|
---|
113 |
|
---|
114 | @param img_folder_closed
|
---|
115 | image for item, which represents folder in closed state
|
---|
116 | @param img_folder_open
|
---|
117 | image for item, which represents folder in opened state, optional
|
---|
118 | @param img_leaf
|
---|
119 | image for item, which represents leaf item, optional
|
---|
120 | */
|
---|
121 | function set_image($img_folder_closed,$img_folder_open=false,$img_leaf=false){
|
---|
122 | $this->im0=$img_folder_closed;
|
---|
123 | $this->im1=$img_folder_open?$img_folder_open:$img_folder_closed;
|
---|
124 | $this->im2=$img_leaf?$img_leaf:$img_folder_closed;
|
---|
125 | }
|
---|
126 | /*! return self as XML string, starting part
|
---|
127 | */
|
---|
128 | function to_xml_start(){
|
---|
129 | if ($this->skip) return "";
|
---|
130 |
|
---|
131 | $str1="<item id='".$this->get_id()."' text='".$this->xmlentities($this->data[$this->config->text[0]["name"]])."' ";
|
---|
132 | if ($this->has_kids()==true) $str1.="child='".$this->has_kids()."' ";
|
---|
133 | if ($this->im0) $str1.="im0='".$this->im0."' ";
|
---|
134 | if ($this->im1) $str1.="im1='".$this->im0."' ";
|
---|
135 | if ($this->im2) $str1.="im2='".$this->im0."' ";
|
---|
136 | if ($this->check) $str1.="checked='".$this->check."' ";
|
---|
137 | foreach ($this->attrs as $key => $value)
|
---|
138 | $str1.=$key."='".$this->xmlentities($value)."' ";
|
---|
139 | $str1.=">";
|
---|
140 | foreach ($this->userdata as $key => $value)
|
---|
141 | $str1.="<userdata name='".$key."'><![CDATA[".$value."]]></userdata>";
|
---|
142 |
|
---|
143 | return $str1;
|
---|
144 | }
|
---|
145 | /*! return self as XML string, ending part
|
---|
146 | */
|
---|
147 | function to_xml_end(){
|
---|
148 | if ($this->skip) return "";
|
---|
149 | return "</item>";
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | require_once("filesystem_item.php");
|
---|
155 |
|
---|
156 | /*! Connector for the dhtmlxtree
|
---|
157 | **/
|
---|
158 | class TreeConnector extends Connector{
|
---|
159 | private $id_swap = array();
|
---|
160 |
|
---|
161 | /*! constructor
|
---|
162 |
|
---|
163 | Here initilization of all Masters occurs, execution timer initialized
|
---|
164 | @param res
|
---|
165 | db connection resource
|
---|
166 | @param type
|
---|
167 | string , which hold type of database ( MySQL or Postgre ), optional, instead of short DB name, full name of DataWrapper-based class can be provided
|
---|
168 | @param item_type
|
---|
169 | name of class, which will be used for item rendering, optional, DataItem will be used by default
|
---|
170 | @param data_type
|
---|
171 | name of class which will be used for dataprocessor calls handling, optional, DataProcessor class will be used by default.
|
---|
172 | */
|
---|
173 | public function __construct($res,$type=false,$item_type=false,$data_type=false){
|
---|
174 | if (!$item_type) $item_type="TreeDataItem";
|
---|
175 | if (!$data_type) $data_type="TreeDataProcessor";
|
---|
176 | parent::__construct($res,$type,$item_type,$data_type);
|
---|
177 |
|
---|
178 | $this->event->attach("afterInsert",array($this,"parent_id_correction_a"));
|
---|
179 | $this->event->attach("beforeProcessing",array($this,"parent_id_correction_b"));
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*! store info about ID changes during insert operation
|
---|
183 | @param dataAction
|
---|
184 | data action object during insert operation
|
---|
185 | */
|
---|
186 | public function parent_id_correction_a($dataAction){
|
---|
187 | $this->id_swap[$dataAction->get_id()]=$dataAction->get_new_id();
|
---|
188 | }
|
---|
189 | /*! update ID if it was affected by previous operation
|
---|
190 | @param dataAction
|
---|
191 | data action object, before any processing operation
|
---|
192 | */
|
---|
193 | public function parent_id_correction_b($dataAction){
|
---|
194 | $relation = $this->config->relation_id["db_name"];
|
---|
195 | $value = $dataAction->get_value($relation);
|
---|
196 |
|
---|
197 | if (array_key_exists($value,$this->id_swap))
|
---|
198 | $dataAction->set_value($relation,$this->id_swap[$value]);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | public function parse_request(){
|
---|
203 | parent::parse_request();
|
---|
204 |
|
---|
205 | if (isset($_GET["id"]))
|
---|
206 | $this->request->set_relation($_GET["id"]);
|
---|
207 | else
|
---|
208 | $this->request->set_relation("0");
|
---|
209 |
|
---|
210 | $this->request->set_limit(0,0); //netralize default reaction on dyn. loading mode
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 |
|
---|
215 | protected function render_set($res){
|
---|
216 | $output="";
|
---|
217 | $index=0;
|
---|
218 | while ($data=$this->sql->get_next($res)){
|
---|
219 | $data = new $this->names["item_class"]($data,$this->config,$index);
|
---|
220 | $this->event->trigger("beforeRender",$data);
|
---|
221 | //there is no info about child elements,
|
---|
222 | //if we are using dyn. loading - assume that it has,
|
---|
223 | //in normal mode juse exec sub-render routine
|
---|
224 | if ($data->has_kids()===-1 && $this->dload)
|
---|
225 | $data->set_kids(true);
|
---|
226 | $output.=$data->to_xml_start();
|
---|
227 | if ($data->has_kids()===-1 || ( $data->has_kids()==true && !$this->dload)){
|
---|
228 | $sub_request = new DataRequestConfig($this->request);
|
---|
229 | $sub_request->set_relation($data->get_id());
|
---|
230 | $output.=$this->render_set($this->sql->select($sub_request));
|
---|
231 | }
|
---|
232 | $output.=$data->to_xml_end();
|
---|
233 | $index++;
|
---|
234 | }
|
---|
235 | return $output;
|
---|
236 | }
|
---|
237 | /*! renders self as xml, starting part
|
---|
238 | */
|
---|
239 | public function xml_start(){
|
---|
240 | return "<tree id='".$this->request->get_relation()."'>";
|
---|
241 | }
|
---|
242 |
|
---|
243 | /*! renders self as xml, ending part
|
---|
244 | */
|
---|
245 | public function xml_end(){
|
---|
246 | return "</tree>";
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | class TreeDataProcessor extends DataProcessor{
|
---|
252 |
|
---|
253 | function __construct($connector,$config,$request){
|
---|
254 | parent::__construct($connector,$config,$request);
|
---|
255 | $request->set_relation(false);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*! convert incoming data name to valid db name
|
---|
259 | converts c0..cN to valid field names
|
---|
260 | @param data
|
---|
261 | data name from incoming request
|
---|
262 | @return
|
---|
263 | related db_name
|
---|
264 | */
|
---|
265 | function name_data($data){
|
---|
266 | if ($data=="tr_pid")
|
---|
267 | return $this->config->relation_id["db_name"];
|
---|
268 | if ($data=="tr_text")
|
---|
269 | return $this->config->text[0]["db_name"];
|
---|
270 | return $data;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | ?> |
---|