source: branches/2.2.0.1/calendar/js/dhtmlx/codebase/connector/dataprocessor.php @ 4001

Revision 4001, 11.4 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1615 - Componente novo para agenda......................................

Line 
1<?php
2/*! Base DataProcessor handling
3**/
4class DataProcessor{
5        protected $connector;//!< Connector instance
6        protected $config;//!< DataConfig instance
7        protected $request;//!< DataRequestConfig instance
8       
9        /*! constructor
10               
11                @param connector
12                        Connector object
13                @param config
14                        DataConfig object
15                @param request
16                        DataRequestConfig object
17        */
18        function __construct($connector,$config,$request){
19                $this->connector= $connector;
20                $this->config=$config;
21                $this->request=$request;
22        }
23       
24        /*! convert incoming data name to valid db name
25                redirect to Connector->name_data by default
26                @param data
27                        data name from incoming request
28                @return
29                        related db_name
30        */
31        function name_data($data){
32                return $data;
33        }
34        /*! retrieve data from incoming request and normalize it
35               
36                @param ids
37                        array of extected IDs
38                @return
39                        hash of data
40        */
41        function get_post_values($ids){
42                $data=array();
43                for ($i=0; $i < sizeof($ids); $i++)
44                        $data[$ids[$i]]=array();
45               
46                foreach ($_POST as $key => $value) {
47                        $details=explode("_",$key,2);
48                        if (sizeof($details)==1) continue;
49                       
50                        $name=$this->name_data($details[1]);
51                        $data[$details[0]][$name]=$value;
52                }
53                       
54                return $data;
55        }
56        /*! process incoming request ( save|update|delete )
57        */
58        function process(){
59                LogMaster::log("DataProcessor object initialized",$_POST);
60               
61                $results=array();
62
63                if (!isset($_POST["ids"]))
64                        throw new Exception("Incorrect incoming data, ID of incoming records not recognized");
65                       
66                $ids=explode(",",$_POST["ids"]);
67                $rows_data=$this->get_post_values($ids);
68                $failed=false;
69               
70                try{
71                        if ($this->connector->sql->is_global_transaction())
72                                $this->connector->sql->begin_transaction();
73                       
74                        for ($i=0; $i < sizeof($ids); $i++) {
75                                $rid = $ids[$i];
76                                LogMaster::log("Row data [{$rid}]",$rows_data[$rid]);
77                               
78                                if (!isset($_POST[$rid."_!nativeeditor_status"]))
79                                        throw new Exception("Status of record [{$rid}] not found in incoming request");
80                                $status = $_POST[$rid."_!nativeeditor_status"];
81                               
82                                $action=new DataAction($status,$rid,$rows_data[$rid]);
83                                $results[]=$action;
84                                $this->inner_process($action);
85                        }
86                       
87                } catch(Exception $e){
88                        $failed=true;
89                }
90               
91                if ($this->connector->sql->is_global_transaction()){
92                        if (!$failed)
93                                for ($i=0; $i < sizeof($results); $i++)
94                                        if ($results[$i]->get_status()=="error" || $results[$i]->get_status()=="invalid"){
95                                                $failed=true;
96                                                break;
97                                        }
98                        if ($failed){
99                                for ($i=0; $i < sizeof($results); $i++)
100                                        $results[$i]->error();
101                                $this->connector->sql->rollback_transaction();
102                        }
103                        else
104                                $this->connector->sql->commit_transaction();
105                }
106               
107                $this->output_as_xml($results);
108        }       
109       
110        /*! converts status string to the inner mode name
111               
112                @param status
113                        external status string
114                @return
115                        inner mode name
116        */
117        protected function status_to_mode($status){
118                switch($status){
119                        case "updated":
120                                return "update";
121                                break;
122                        case "inserted":
123                                return "insert";
124                                break;
125                        case "deleted":
126                                return "delete";
127                                break;
128                        default:
129                                return $status;
130                                break;
131                }
132        }
133        /*! process data updated request received
134               
135                @param action
136                        DataAction object
137                @return
138                        DataAction object with details of processing
139        */
140        protected function inner_process($action){
141               
142                if ($this->connector->sql->is_record_transaction())
143                                $this->connector->sql->begin_transaction();             
144               
145                try{
146                               
147                        $mode = $this->status_to_mode($action->get_status());
148                        if (!$this->connector->access->check($mode)){
149                                LogMaster::log("Access control: {$operation} operation blocked");
150                                $action->error();
151                        } else {
152                                $check = $this->connector->event->trigger("beforeProcessing",$action);
153                                if (!$action->is_ready())
154                                        $this->check_exts($action,$mode);
155                                $check = $this->connector->event->trigger("afterProcessing",$action);
156                        }
157               
158                } catch (Exception $e){
159                        $action->set_status("error");
160                }
161               
162                if ($this->connector->sql->is_record_transaction()){
163                        if ($action->get_status()=="error" || $action->get_status()=="invalid")
164                                $this->connector->sql->rollback_transaction();         
165                        else
166                                $this->connector->sql->commit_transaction();           
167                }
168                               
169                return $action;
170        }
171        /*! check if some event intercepts processing, send data to DataWrapper in other case
172
173                @param action
174                        DataAction object
175                @param mode
176                        name of inner mode ( will be used to generate event names )
177        */
178        function check_exts($action,$mode){
179                $old_config = new DataConfig($this->config);
180               
181                $this->connector->event->trigger("before".$mode,$action);
182                if ($action->is_ready())
183                        LogMaster::log("Event code for ".$mode." processed");
184                else {
185                        //check if custom sql defined
186                        $sql = $this->connector->sql->get_sql($mode,$action);
187                        if ($sql)
188                                $this->connector->sql->query($sql);
189                        else{
190                                $action->sync_config($this->config);
191                                $method=array($this->connector->sql,$mode);
192                                if (!is_callable($method))
193                                        throw new Exception("Unknown dataprocessing action: ".$mode);
194                                call_user_func($method,$action,$this->request);
195                        }
196                }
197                $this->connector->event->trigger("after".$mode,$action);
198               
199                $this->config = $old_config;
200        }
201       
202        /*! output xml response for dataprocessor
203
204                @param  results
205                        array of DataAction objects
206        */
207        function output_as_xml($results){
208                LogMaster::log("Edit operation finished",$results);
209                ob_clean();
210                header("Content-type:text/xml");
211                echo "<?xml version='1.0' ?>";
212                echo "<data>";
213                for ($i=0; $i < sizeof($results); $i++)
214                        echo $results[$i]->to_xml();
215                echo "</data>";
216        }               
217       
218}
219
220/*! contain all info related to action and controls customizaton
221**/
222class DataAction{
223        private $status; //!< cuurent status of record
224        private $id;//!< id of record
225        private $data;//!< data hash of record
226        private $userdata;//!< hash of extra data , attached to record
227        private $nid;//!< new id value , after operation executed
228        private $output;//!< custom output to client side code
229        private $attrs;//!< hash of custtom attributes
230        private $ready;//!< flag of operation's execution
231        private $addf;//!< array of added fields
232        private $delf;//!< array of deleted fields
233       
234       
235        /*! constructor
236               
237                @param status
238                        current operation status
239                @param id
240                        record id
241                @param data
242                        hash of data
243        */
244        function __construct($status,$id,$data){
245                $this->status=$status;
246                $this->id=$id;
247                $this->data=$data;     
248                $this->nid=$id;
249               
250                $this->output="";
251                $this->attrs=array();
252                $this->ready=false;
253               
254                $this->addf=array();
255                $this->delf=array();
256        }
257
258       
259        /*! add custom field and value to DB operation
260               
261                @param name
262                        name of field which will be added to DB operation
263                @param value
264                        value which will be used for related field in DB operation
265        */
266        function add_field($name,$value){
267                LogMaster::log("adding field: ".$name.", with value: ".$value);
268                $this->data[$name]=$value;
269                $this->addf[]=$name;
270        }
271        /*! remove field from DB operation
272               
273                @param name
274                        name of field which will be removed from DB operation
275        */
276        function remove_field($name){
277                LogMaster::log("removing field: ".$name);
278                $this->delf[]=$name;
279        }
280       
281        /*! sync field configuration with external object
282               
283                @param slave
284                        SQLMaster object
285                @todo
286                        check , if all fields removed then cancel action
287        */
288        function sync_config($slave){
289                foreach ($this->addf as $k => $v)
290                        $slave->add_field($v);
291                foreach ($this->delf as $k => $v)
292                        $slave->remove_field($v);
293        }
294        /*! get value of some record's propery
295               
296                @param name
297                        name of record's property ( name of db field or alias )
298                @return
299                        value of related property
300        */
301        function get_value($name){
302                if (!array_key_exists($name,$this->data)){
303                        LogMaster::log("Incorrect field name used: ".$name);
304                        LogMaster::log("data",$this->data);
305                        return "";
306                }
307                return $this->data[$name];
308        }
309        /*! set value of some record's propery
310               
311                @param name
312                        name of record's property ( name of db field or alias )
313                @param value
314                        value of related property
315        */
316        function set_value($name,$value){
317                LogMaster::log("change value of: ".$name." as: ".$value);
318                $this->data[$name]=$value;
319        }
320        /*! get hash of data properties
321               
322                @return
323                        hash of data properties
324        */
325        function get_data(){
326                return $this->data;
327        }
328        /*! get some extra info attached to record
329                deprecated, exists just for backward compatibility, you can use set_value instead of it
330                @param name
331                        name of userdata property
332                @return
333                        value of related userdata property
334        */
335        function get_userdata_value($name){
336                return $this->get_value($name);
337        }
338        /*! set some extra info attached to record
339                deprecated, exists just for backward compatibility, you can use get_value instead of it
340                @param name
341                        name of userdata property
342                @param value
343                        value of userdata property
344        */
345        function set_userdata_value($name,$value){
346                return $this->set_value($name,$value);
347        }
348        /*! get current status of record
349               
350                @return
351                        string with status value
352        */
353        function get_status(){
354                return $this->status;
355        }
356        /*! assign new status to the record
357               
358                @param status
359                        new status value
360        */
361        function set_status($status){
362                $this->status=$status;
363        }
364        /*! get id of current record
365               
366                @return
367                        id of record
368        */
369        function get_id(){
370                return $this->id;
371        }
372        /*! sets custom response text
373               
374                can be accessed through defineAction on client side. Text wrapped in CDATA, so no extra escaping necessary
375                @param text
376                        custom response text
377        */
378        function set_response_text($text){
379                $this->set_response_xml("<![CDATA[".$text."]]>");
380        }
381        /*! sets custom response xml
382               
383                can be accessed through defineAction on client side
384                @param text
385                        string with XML data
386        */
387        function set_response_xml($text){
388                $this->output=$text;
389        }
390        /*! sets custom response attributes
391               
392                can be accessed through defineAction on client side
393                @param name
394                        name of custom attribute
395                @param value
396                        value of custom attribute
397        */
398        function set_response_attribute($name,$value){
399                $this->attrs[$name]=$value;
400        }
401        /*! check if action finished
402               
403                @return
404                        true if action finished, false otherwise
405        */
406        function is_ready(){
407                return $this->ready;
408        }       
409        /*! return new id value
410       
411                equal to original ID normally, after insert operation - value assigned for new DB record       
412                @return
413                        new id value
414        */
415        function get_new_id(){
416                return $this->nid;
417        }
418       
419        /*! set result of operation as error
420        */
421        function error(){
422                $this->status="error";
423                $this->ready=true;
424        }
425        /*! set result of operation as invalid
426        */
427        function invalid(){
428                $this->status="invalid";
429                $this->ready=true;
430        }
431        /*! confirm successful opeation execution
432                @param  id
433                        new id value, optional
434        */
435        function success($id=false){
436                if ($id!==false)
437                        $this->nid = $id;
438                $this->ready=true;
439        }
440        /*! convert DataAction to xml format compatible with client side dataProcessor
441                @return
442                        DataAction operation report as XML string
443        */
444        function to_xml(){
445                $str="<action type='{$this->status}' sid='{$this->id}' tid='{$this->nid}' ";
446                foreach ($this->attrs as $k => $v) {
447                        $str.=$k."='".$v."' ";
448                }
449                $str.=">{$this->output}</action>";     
450                return $str;
451        }
452        /*! convert self to string ( for logs )
453               
454                @return
455                        DataAction operation report as plain string
456        */
457        function __toString(){
458                return "action:{$this->status}; sid:{$this->id}; tid:{$this->nid};";
459        }
460       
461
462}
463
464
465?>
Note: See TracBrowser for help on using the repository browser.