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

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

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

Line 
1<?php
2require_once("base_connector.php");
3require_once("grid_config.php");
4
5//require_once("grid_dataprocessor.php");
6
7/*! DataItem class for Grid component
8**/
9
10class GridDataItem extends DataItem{
11        protected $row_attrs;//!< hash of row attributes
12        protected $cell_attrs;//!< hash of cell attributes
13        protected $userdata;
14       
15        function __construct($data,$name,$index=0){
16                parent::__construct($data,$name,$index);
17               
18                $this->row_attrs=array();
19                $this->cell_attrs=array();
20                $this->userdata=array();
21        }
22        /*! set color of row
23               
24                @param color
25                        color of row
26        */
27        function set_row_color($color){
28                $this->row_attrs["bgColor"]=$color;
29        }
30        /*! set style of row
31               
32                @param color
33                        color of row
34        */
35        function set_row_style($color){
36                $this->row_attrs["style"]=$color;
37        }
38        /*! assign custom style to the cell
39               
40                @param name
41                        name of column
42                @param value
43                        css style string
44        */
45        function set_cell_style($name,$value){
46                $this->set_cell_attribute($name,"style",$value);
47        }
48        /*! assign custom class to specific cell
49               
50                @param name
51                        name of column
52                @param value
53                        css class name
54        */
55        function set_cell_class($name,$value){
56                $this->set_cell_attribute($name,"class",$value);
57        }
58        /*! set custom cell attribute
59               
60                @param name
61                        name of column
62                @param attr
63                        name of attribute
64                @param value
65                        value of attribute
66        */
67        function set_cell_attribute($name,$attr,$value){
68                if (!$this->cell_attrs[$name]) $this->cell_attrs[$name]=array();
69                $this->cell_attrs[$name][$attr]=$value;
70        }
71       
72        /*! set userdata section for the item
73               
74                @param name
75                        name of userdata
76                @param value
77                        value of userdata
78        */
79        function set_userdata($name, $value){
80                $this->userdata[$name]=$value;
81        }
82               
83        /*! set custom row attribute
84               
85                @param attr
86                        name of attribute
87                @param value
88                        value of attribute
89        */
90        function set_row_attribute($attr,$value){
91                $this->row_attrs[$attr]=$value;
92        }       
93       
94        /*! return self as XML string, starting part
95        */
96        public function to_xml_start(){
97                if ($this->skip) return "";
98               
99                $str="<row id='".$this->get_id()."'";
100                foreach ($this->row_attrs as $k=>$v)
101                        $str.=" ".$k."='".$v."'";
102                $str.=">";
103                for ($i=0; $i < sizeof($this->config->text); $i++){
104                        $str.="<cell";
105                        $name=$this->config->text[$i]["name"];
106                        if (isset($this->cell_attrs[$name])){
107                                $cattrs=$this->cell_attrs[$name];
108                                foreach ($cattrs as $k => $v)
109                                        $str.=" ".$k."='".$this->xmlentities($v)."'";
110                        }
111                        $str.="><![CDATA[".$this->data[$name]."]]></cell>";
112                }
113                foreach ($this->userdata as $key => $value)
114                        $str.="<userdata name='".$key."'><![CDATA[".$value."]]></userdata>";
115                       
116                return $str;
117        }
118        /*! return self as XML string, ending part
119        */
120        public function to_xml_end(){
121                if ($this->skip) return "";
122               
123                return "</row>";
124        }
125}
126/*! Connector for the dhtmlxgrid
127**/
128class GridConnector extends Connector{
129        protected $extra_output="";//!< extra info which need to be sent to client side
130        private $options=array();//!< hash of OptionsConnector
131       
132        /*! constructor
133               
134                Here initilization of all Masters occurs, execution timer initialized
135                @param res
136                        db connection resource
137                @param type
138                        string , which hold type of database ( MySQL or Postgre ), optional, instead of short DB name, full name of DataWrapper-based class can be provided
139                @param item_type
140                        name of class, which will be used for item rendering, optional, DataItem will be used by default
141                @param data_type
142                        name of class which will be used for dataprocessor calls handling, optional, DataProcessor class will be used by default.
143        */             
144        public function __construct($res,$type=false,$item_type=false,$data_type=false){
145                if (!$item_type) $item_type="GridDataItem";
146                if (!$data_type) $data_type="GridDataProcessor";
147                parent::__construct($res,$type,$item_type,$data_type);
148        }
149
150
151        protected function parse_request(){
152                parent::parse_request();
153               
154                if (isset($_GET["dhx_colls"]))
155                        $this->fill_collections($_GET["dhx_colls"]);   
156               
157                if (isset($_GET["posStart"]) && isset($_GET["count"]))
158                        $this->request->set_limit($_GET["posStart"],$_GET["count"]);
159        }
160        protected function resolve_parameter($name){
161                if (intval($name).""==$name)
162                        return $this->config->text[intval($name)]["db_name"];
163                return $name;
164        }
165       
166        /*! replace xml unsafe characters
167               
168                @param string
169                        string to be escaped
170                @return
171                        escaped string
172        */     
173        private function xmlentities($string) {
174                return str_replace( array( '&', '"', "'", '<', '>', '’' ), array( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string);
175        }
176               
177        /*! assign options collection to the column
178               
179                @param name
180                        name of the column
181                @param options
182                        array or connector object
183        */
184        public function set_options($name,$options){
185                if (is_array($options)){
186                        $str="";
187                        foreach($options as $k => $v)
188                                $str.="<item value='".$this->xmlentities($k)."' label='".$this->xmlentities($v)."' />";
189                        $options=$str;
190                }
191                $this->options[$name]=$options;
192        }
193        /*! generates xml description for options collections
194               
195                @param list
196                        comma separated list of column names, for which options need to be generated
197        */
198        protected function fill_collections($list){
199                $names=explode(",",$list);
200                for ($i=0; $i < sizeof($names); $i++) {
201                        $name = $this->resolve_parameter($names[$i]);
202                        if (!array_key_exists($name,$this->options)){
203                                $this->options[$name] = new DistinctOptionsConnector($this->get_connection(),$this->names["db_class"]);
204                                $c = new DataConfig($this->config);
205                                $r = new DataRequestConfig($this->request);
206                                $c->minimize($name);
207                               
208                                $this->options[$name]->render_connector($c,$r);
209                        }
210                       
211                        $this->extra_output.="<coll_options for='{$names[$i]}'>";
212                        if (!is_string($this->options[$name]))
213                                $this->extra_output.=$this->options[$name]->render();
214                        else
215                                $this->extra_output.=$this->options[$name];
216                        $this->extra_output.="</coll_options>";
217                }
218        }
219       
220        /*! renders self as  xml, starting part
221        */
222        protected function xml_start(){
223                if ($this->dload){
224                        if ($pos=$this->request->get_start())
225                                return "<rows pos='".$pos."'>";
226                        else
227                                return "<rows total_count='".$this->sql->get_size($this->request)."'>";
228                }
229                else
230                        return "<rows>";
231        }
232       
233       
234        /*! renders self as  xml, ending part
235        */
236        protected function xml_end(){
237                return $this->extra_output."</rows>";
238        }
239
240        public function set_config($config = false){
241                if (gettype($config) == 'boolean')
242                        $config = new GridConfiguration($config);
243                       
244                $this->event->attach("beforeOutput", Array($config, "attachHeaderToXML"));
245        }
246}
247
248/*! DataProcessor class for Grid component
249**/
250class GridDataProcessor extends DataProcessor{
251       
252        /*! convert incoming data name to valid db name
253                converts c0..cN to valid field names
254                @param data
255                        data name from incoming request
256                @return
257                        related db_name
258        */
259        function name_data($data){
260                if ($data == "gr_id") return $this->config->id["name"];
261                $parts=explode("c",$data);
262                if ($parts[0]=="" && intval($parts[1])==$parts[1])
263                        return $this->config->text[intval($parts[1])]["name"];
264                return $data;
265        }
266}
267
268?>
Note: See TracBrowser for help on using the repository browser.