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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
41 ********************************************************************************/
50 class Controller extends SugarBean {
53 var $type; //defines id this is a new list order or existing, or delete
56 function Controller() {
59 $this->disable_row_level_security =true;
63 function init(& $seed_object, $type){
65 $this->focus = & $seed_object;
68 //end function Controller
71 function change_component_order($magnitude, $direction, $parent_id=""){
73 if(!empty($this->type) && $this->type=="Save"){
76 $wall_test = $this->check_wall($magnitude, $direction, $parent_id);
78 if($wall_test==false){
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;
91 if($this->focus->controller_def['list_y']=="Y"){
93 $new_y = $this->focus->list_order_y;
94 $affected_y = $this->focus->list_order_y;
100 $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
101 //end if direction Left
103 if($direction=="Right"){
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;
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;
119 $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
120 //end if direction Right
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;
131 if($this->focus->controller_def['list_y']=="Y"){
133 $new_y = $this->focus->list_order_y - 1;
134 $affected_y = $this->focus->list_order_y;
140 $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
142 //end if direction Up
144 if($direction=="Down"){
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;
153 if($this->focus->controller_def['list_y']=="Y"){
155 $new_y = $this->focus->list_order_y + 1;
156 $affected_y = $this->focus->list_order_y;
161 $affected_id = $this->get_affected_id($parent_id, $new_x, $new_y);
162 //end if direction Down
165 //This takes care of the component being pushed out of its position
166 $this->update_affected_order($affected_id, $affected_x, $affected_y);
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;
173 if($this->focus->controller_def['list_y']=="Y"){
174 $this->focus->list_order_y = $new_y;
178 //this is a new component, set the x or y value to the max + 1
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'
184 $result = $this->db->query($query,true," Error capturing max start order: ");
185 $row = $this->db->fetchByAssoc($result);
187 if(!is_null($row['max_start'])){
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;
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;
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;
204 //end if else to check if this is first entry
206 //end if else on whether this is a new entry
208 //end function change_component_order
211 function update_affected_order($affected_id, $affected_new_x="", $affected_new_y=""){
213 $query = "UPDATE ".$this->focus->table_name." SET";
215 if($this->focus->controller_def['list_x']=="Y"){
216 $query .= " list_order_x='$affected_new_x'";
218 if($this->focus->controller_def['list_y']=="Y"){
220 if($this->focus->controller_def['list_x']=="Y"){
223 $query .= " list_order_y='$affected_new_y'";
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
232 function get_affected_id($parent_id, $list_order_x="", $list_order_y=""){
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'
239 if($this->focus->controller_def['list_x']=="Y"){
240 $query .= " AND list_order_x='$list_order_x' ";
243 if($this->focus->controller_def['list_y']=="Y"){
244 $query .= " AND list_order_y='$list_order_y' ";
247 //echo $query."<BR>";
248 $result = $this->db->query($query,true," Error capturing affected id: ");
249 $row = $this->db->fetchByAssoc($result);
253 //end function get_affected_id
257 /////////////Wall Functions////////////////////
260 function check_wall($magnitude, $direction, $parent_id){
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
266 //If down or Right, then check max list_order value
267 if($direction=="Down" || $direction =="Right"){
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'
273 $result = $this->db->query($query,true," Error capturing max start order: ");
274 $row = $this->db->fetchByAssoc($result);
276 if($this->focus->controller_def['start_axis']=="x") {
277 if($row['max_start'] == $this->focus->list_order_x){
281 if($this->focus->controller_def['start_axis']=="y") {
282 if($row['max_start'] == $this->focus->list_order_y){
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){
296 if($this->focus->controller_def['start_axis']=="y") {
297 if($this->focus->list_order_y==0){
302 //end if down or left
305 //If you get here, then you are not at the wall and can change order
308 //end function check_wall
311 //End Wall Functions/////////////////////////
313 //Delete adjust functions////////////////////
316 function delete_adjust_order($parent_id){
319 //Currently handles single axis motion only!!!!!!!!!
320 //TODO: jgreen - Add dual axis motion
322 //adjust along start_axis
323 $variable_name = $this->focus->controller_def['start_var'];
324 $current_position = $this->focus->$variable_name;
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."'";
331 $result = $this->db->query($query,true," Error updating the delete_adjust_order: ");
332 //end delete_adjust_order
334 //End Delete Functions/////////////////////////
335 //end class Controller