]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/controller/Controller.php
Release 6.5.0
[Github/sugarcrm.git] / include / controller / Controller.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:
41  ********************************************************************************/
42
43
44
45
46
47
48
49
50 class Controller extends SugarBean {
51         
52         var $focus;
53         var $type;  //defines id this is a new list order or existing, or delete
54                                 // New, Save, Delete
55         
56         function Controller() {
57                 parent::SugarBean();
58
59                 $this->disable_row_level_security =true;
60
61         }
62         
63         function init(& $seed_object, $type){
64         
65                 $this->focus = & $seed_object;
66                 $this->type = $type;
67         
68         //end function Controller
69         }       
70
71         function change_component_order($magnitude, $direction, $parent_id=""){
72                 
73                 if(!empty($this->type) && $this->type=="Save"){
74                         
75                         //safety check
76                         $wall_test = $this->check_wall($magnitude, $direction, $parent_id);
77
78                         if($wall_test==false){
79                                 return;
80                         }
81                                 
82                                 
83                                 if($direction=="Left"){
84                                         if($this->focus->controller_def['list_x']=="Y"){
85                                                 $new_x = $this->focus->list_order_x -1;
86                                                 $affected_x = $this->focus->list_order_x;
87                                         } else {
88                                                 $new_x = "";
89                                                 $affected_x = "";
90                                         }       
91                                         if($this->focus->controller_def['list_y']=="Y"){        
92                                                 
93                                                 $new_y = $this->focus->list_order_y;
94                                                 $affected_y = $this->focus->list_order_y;
95                                         } else {
96                                                 $new_y = "";
97                                                 $affected_y = "";       
98                                         }       
99                                         
100                                         $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
101                                 //end if direction Left
102                                 }       
103                                 if($direction=="Right"){
104                                         
105                                         if($this->focus->controller_def['list_x']=="Y"){
106                                                 $new_x = $this->focus->list_order_x + 1;
107                                                 $affected_x = $this->focus->list_order_x;                                               
108                                         } else {
109                                                 $new_x = "";
110                                                 $affected_x = "";
111                                         }
112                                         if($this->focus->controller_def['list_y']=="Y"){
113                                                 $new_y = $this->focus->list_order_y;
114                                                 $affected_y = $this->focus->list_order_y;
115                                         } else {
116                                                 $new_y = "";
117                                                 $affected_y = "";
118                                         }                                               
119                                         $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);              
120                                 //end if direction Right
121                                 }
122                         
123                                 if($direction=="Up"){
124                                         if($this->focus->controller_def['list_x']=="Y"){
125                                                 $new_x = $this->focus->list_order_x;
126                                                 $affected_x = $this->focus->list_order_x;
127                                         } else {
128                                                 $new_x = "";
129                                                 $affected_x = "";
130                                         }       
131                                         if($this->focus->controller_def['list_y']=="Y"){        
132                                                 
133                                         $new_y = $this->focus->list_order_y - 1;
134                                         $affected_y = $this->focus->list_order_y;
135                                         } else {
136                                                 $new_y = "";
137                                                 $affected_y = "";       
138                                         }       
139                                         
140                                         $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
141                                         
142                                 //end if direction Up
143                                 }       
144                                 if($direction=="Down"){
145                                         
146                                         if($this->focus->controller_def['list_x']=="Y"){
147                                                 $new_x = $this->focus->list_order_x;
148                                                 $affected_x = $this->focus->list_order_x;
149                                         } else {
150                                                 $new_x = "";
151                                                 $affected_x = "";
152                                         }
153                                         if($this->focus->controller_def['list_y']=="Y"){
154
155                                                 $new_y = $this->focus->list_order_y + 1;
156                                                 $affected_y = $this->focus->list_order_y;
157                                         } else {
158                                                 $new_y = "";
159                                                 $affected_y = "";
160                                         }                                               
161                                         $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);              
162                                 //end if direction Down
163                                 }
164
165                         //This takes care of the component being pushed out of its position
166                         $this->update_affected_order($affected_id, $affected_x, $affected_y);
167                         
168                                 //This takes care of the new positions for itself
169                         if($this->focus->controller_def['list_x']=="Y"){
170                                 $this->focus->list_order_x = $new_x;
171                         }
172                                 
173                         if($this->focus->controller_def['list_y']=="Y"){        
174                                 $this->focus->list_order_y = $new_y;
175                         }
176                         
177                 } else {
178                 //this is a new component, set the x or y value to the max + 1
179
180                                 $query = "SELECT MAX(".$this->focus->controller_def['start_var'].") max_start from ".$this->focus->table_name."
181                                                   WHERE ".$this->focus->controller_def['parent_var']."='$parent_id'
182                                                   AND ".$this->focus->table_name.".deleted='0'
183                                                  ";
184                                 $result = $this->db->query($query,true," Error capturing max start order: ");
185                                 $row = $this->db->fetchByAssoc($result);
186                 
187                         if(!is_null($row['max_start'])){                
188                                 
189                                 if($this->focus->controller_def['start_axis']=="x")     {
190                                         $this->focus->list_order_x = $row['max_start'] + 1;
191                                         if($this->focus->controller_def['list_y']=="Y") $this->focus->list_order_y = 0;
192                                 }
193                                 
194                                 if($this->focus->controller_def['start_axis']=="y")     {
195                                         $this->focus->list_order_y = $row['max_start'] + 1;
196                                         if($this->focus->controller_def['list_x']=="Y") $this->focus->list_order_x = 0;
197                                 }
198                                 
199                         } else {
200                                 //first row
201                                 if($this->focus->controller_def['list_x']=="Y") $this->focus->list_order_x = 0;
202                                 if($this->focus->controller_def['list_y']=="Y") $this->focus->list_order_y = 0;
203
204                         //end if else to check if this is first entry
205                         }
206                 //end if else on whether this is a new entry
207                 }       
208         //end function change_component_order   
209         }
210         
211         function update_affected_order($affected_id, $affected_new_x="", $affected_new_y=""){
212                  
213                 $query =        "UPDATE ".$this->focus->table_name." SET";
214                 
215                 if($this->focus->controller_def['list_x']=="Y"){                                
216                                 $query .=       "        list_order_x='$affected_new_x'";
217                 }
218                 if($this->focus->controller_def['list_y']=="Y"){        
219
220                         if($this->focus->controller_def['list_x']=="Y"){
221                                 $query .=       "       , ";
222                         }                
223                         $query .=       "       list_order_y='$affected_new_y'";
224                 }                                               
225                 
226                 $query .=       "       WHERE id='$affected_id'";
227                 $query .=   "   AND ".$this->focus->table_name.".deleted='0'";
228                 $result = $this->db->query($query,true," Error updating affected order id: ");
229         //end function update_affected_order
230         }
231         
232         function get_affected_id($parent_id, $list_order_x="", $list_order_y=""){       
233                 
234                 $query = "      SELECT id from ".$this->focus->table_name."
235                                         WHERE ".$this->focus->controller_def['parent_var']."='$parent_id'
236                                         AND ".$this->focus->table_name.".deleted='0'
237                                         ";
238                 
239                 if($this->focus->controller_def['list_x']=="Y"){                
240                         $query .= "     AND list_order_x='$list_order_x' ";
241                 }
242                 
243                 if($this->focus->controller_def['list_y']=="Y"){                        
244                         $query .= "     AND list_order_y='$list_order_y' ";
245                 }                       
246
247         //echo $query."<BR>";           
248                 $result = $this->db->query($query,true," Error capturing affected id: ");
249                 $row = $this->db->fetchByAssoc($result);
250
251                 return $row['id'];
252                 
253         //end function get_affected_id
254         }       
255         
256
257 /////////////Wall Functions////////////////////
258
259
260 function check_wall($magnitude, $direction, $parent_id){
261         
262 //TODO: jgreen - this is only single axis check_wall mechanism, will need to upgrade this to double axis        
263 //This function determines if you can't move the direction you want, because you are at the edge
264
265         
266 //If down or Right, then check max list_order value
267         if($direction=="Down" || $direction =="Right"){
268
269                 $query = "SELECT MAX(".$this->focus->controller_def['start_var'].") max_start from ".$this->focus->table_name."
270                                   WHERE ".$this->focus->controller_def['parent_var']."='$parent_id'
271                                   AND ".$this->focus->table_name.".deleted='0'
272                                                  ";
273                 $result = $this->db->query($query,true," Error capturing max start order: ");
274                 $row = $this->db->fetchByAssoc($result);
275                 
276                         if($this->focus->controller_def['start_axis']=="x")     {
277                                 if($row['max_start'] == $this->focus->list_order_x){
278                                         return false;   
279                                 }       
280                         }
281                         if($this->focus->controller_def['start_axis']=="y")     {
282                                 if($row['max_start'] == $this->focus->list_order_y){
283                                         return false;   
284                                 }       
285                         }
286         //end if up or right
287         }
288
289 //If up or left, then simply check the 0 value  
290         if($direction=="Up" || $direction =="Left"){
291                         if($this->focus->controller_def['start_axis']=="x")     {
292                                 if($this->focus->list_order_x==0){
293                                         return false;   
294                                 }       
295                         }
296                         if($this->focus->controller_def['start_axis']=="y")     {
297                                 if($this->focus->list_order_y==0){
298                                         return false;   
299                                 }       
300                         }
301                 
302         //end if down or left
303         }       
304         
305         //If you get here, then you are not at the wall and can change order
306         return true;
307                 
308 //end function check_wall       
309 }       
310         
311 //End Wall Functions/////////////////////////
312
313 //Delete adjust functions////////////////////
314
315
316 function delete_adjust_order($parent_id){
317         
318         
319         //Currently handles single axis motion only!!!!!!!!!
320         //TODO: jgreen - Add dual axis motion 
321         
322         //adjust along start_axis
323         $variable_name = $this->focus->controller_def['start_var'];
324         $current_position = $this->focus->$variable_name;
325
326         $query =  "UPDATE ".$this->focus->table_name." ";
327         $query .= "SET ".$this->focus->controller_def['start_var']." = ".$this->focus->controller_def['start_var']." - 1 ";
328         $query .= "WHERE ".$this->focus->controller_def['start_var']." > ".$current_position." AND deleted=0 ";
329         $query .= "AND ".$this->focus->controller_def['parent_var']." = '".$parent_id."'";
330
331         $result = $this->db->query($query,true," Error updating the delete_adjust_order: ");
332 //end delete_adjust_order       
333 }       
334 //End Delete Functions/////////////////////////
335 //end class Controller
336 }       
337
338 ?>