]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/controller.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Calendar / 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 require_once("modules/Calendar/CalendarUtils.php");
40
41 class CalendarController extends SugarController
42 {
43
44     /**
45      * Bean that is being handled by the Calendar's current action.
46      * @var SugarBean $currentBean 
47      */
48     protected $currentBean = null;
49
50
51     /**
52      * Action SaveActivity
53      */
54     protected function action_saveactivity()
55     {    
56         $this->view = 'json';        
57         
58         if (!$this->retrieveCurrentBean('Save')) {
59             return;
60         }
61         
62         $module = $this->currentBean->module_dir;
63         $bean = $this->currentBean;
64         
65         if (empty($_REQUEST['edit_all_recurrences'])) {
66         
67             $repeat_fields = array('type', 'interval', 'count', 'until', 'dow', 'parent_id');
68             foreach ($repeat_fields as $suffix) {
69                 unset($_POST['repeat_' . $suffix]);
70             }    
71                         
72         }else if (!empty($_REQUEST['repeat_type']) && !empty($_REQUEST['date_start'])) {
73         
74             $params = array(
75                     'type' => $_REQUEST['repeat_type'],
76                     'interval' => $_REQUEST['repeat_interval'],
77                     'count' => $_REQUEST['repeat_count'],    
78                     'until' => $_REQUEST['repeat_until'],    
79                     'dow' => $_REQUEST['repeat_dow'],            
80             );
81                 
82             $repeatArr = CalendarUtils::build_repeat_sequence($_REQUEST['date_start'], $params);            
83             $limit = SugarConfig::getInstance()->get('calendar.max_repeat_count', 1000);
84             
85             if (count($repeatArr) > ($limit - 1)) {
86                 ob_clean();
87                 $jsonData = array(
88                     'access' => 'yes',
89                     'limit_error' => 'true',
90                     'limit' => $limit,                    
91                 );
92                 $this->view_object_map['jsonData'] = $jsonData;
93                 return;    
94             }                                    
95         }
96         
97         
98         
99         $path = "modules/{$bean->module_dir}/{$bean->object_name}FormBase.php";
100         if (!file_exists($path)) {
101             $GLOBALS['log']->fatal("File {$bean->object_name}FormBase.php doesn't exist");
102             sugar_cleanup(true);
103         }
104                
105         require_once($path);
106         
107         $FBObjectName = "{$bean->object_name}FormBase";
108         
109         if (!class_exists($FBObjectName)) {
110             $GLOBALS['log']->fatal("Class {$bean->object_name}FormBase doesn't exist");
111             sugar_cleanup(true);
112         }
113         
114         $formBase = new $FBObjectName();
115         $bean = $formBase->handleSave('', false, false);
116         unset($_REQUEST['send_invites'], $_POST['send_invites']); // prevent invites sending for recurring activities
117
118         if ($record = $bean->id) {    
119                         
120             if ($module == "Meetings" || $module == "Calls") {
121                 if (!empty($_REQUEST['edit_all_recurrences'])) {
122                     CalendarUtils::markRepeatDeleted($bean);
123                 }
124                 if (isset($repeatArr) && is_array($repeatArr) && count($repeatArr) > 0) {
125                     $repeatCreated = CalendarUtils::save_repeat_activities($bean, $repeatArr);
126                 }
127             }    
128                     
129             $bean->retrieve($record);                                
130             $jsonData = CalendarUtils::get_sendback_array($bean);    
131                     
132             if (isset($repeatCreated) && is_array($repeatCreated)) {
133                 $jsonData = array_merge($jsonData, array('repeat' => $repeatCreated));
134             }
135             
136             if (!empty($_REQUEST['edit_all_recurrences'])) {
137                 $jsonData['edit_all_recurrences'] = 'true';
138             }
139                 
140         } else {
141             $jsonData = array(
142                 'access' => 'no',
143             );
144         }
145
146         $this->view_object_map['jsonData'] = $jsonData;
147     }
148     
149     
150     /**
151      * Action QuickEdit
152      */
153     protected function action_quickedit()
154     {
155         $this->view = 'quickedit';
156         
157         if (!$this->retrieveCurrentBean('Detail')) {
158             return;
159         }
160
161         $this->view_object_map['currentModule'] = $this->currentBean->module_dir;
162         $this->view_object_map['currentBean'] = $this->currentBean;    
163
164     }
165     
166     /**
167      * Action Reschedule
168      * Used for drag & drop
169      */
170     protected function action_reschedule()
171     {
172         $this->view = 'json';
173         
174         $commit = true;                
175         
176         if (!$this->retrieveCurrentBean('Save')) {
177             return;
178         }        
179         
180         $_REQUEST['parent_name'] = $this->currentBean->parent_name;
181         
182         $dateField = "date_start";
183         if ($this->currentBean->module_dir == "Tasks") {
184             $dateField = "date_due";
185         }            
186
187         if (!empty($_REQUEST['calendar_style']) && $_REQUEST['calendar_style'] == "basic") {
188             list($tmp, $time) = explode(" ", $this->currentBean->$dateField);            
189             list($date, $tmp) = explode(" ", $_REQUEST['datetime']);
190             $_REQUEST['datetime'] = $date . " " . $time;            
191         }
192         $_POST[$dateField] = $_REQUEST['datetime'];
193         
194         if ($this->currentBean->module_dir == "Tasks" && !empty($this->currentBean->date_start)) {
195             if ($GLOBALS['timedate']->fromUser($_POST['date_due'])->ts < $GLOBALS['timedate']->fromUser($this->currentBean->date_start)->ts) {
196                 $this->view_object_map['jsonData'] = array(
197                     'access' => 'no',
198                     'errorMessage' => $GLOBALS['mod_strings']['LBL_DATE_END_ERROR'],
199                 );
200                 $commit = false; 
201             }   
202         }
203         
204         if ($commit) {            
205             require_once('include/formbase.php');
206             $this->currentBean = populateFromPost("", $this->currentBean);                
207             $this->currentBean->save();        
208             $this->currentBean->retrieve($_REQUEST['record']);        
209                 
210             $this->view_object_map['jsonData'] = CalendarUtils::get_sendback_array($this->currentBean);
211         }    
212     }
213     
214     /**
215      * Action Remove
216      */
217     protected function action_remove()
218     {
219         $this->view = 'json';        
220         
221         if (!$this->retrieveCurrentBean('Delete')) {
222             return;
223         }
224                 
225         if ($this->currentBean->module_dir == "Meetings" || $this->currentBean->module_dir == "Calls") {
226             if (!empty($_REQUEST['remove_all_recurrences']) && $_REQUEST['remove_all_recurrences']) {
227                 CalendarUtils::markRepeatDeleted($this->currentBean);
228             }         
229         }
230
231         $this->currentBean->mark_deleted($_REQUEST['record']);
232
233         $this->view_object_map['jsonData'] = array(
234             'access' => 'yes',
235         );
236     
237     }
238     
239     /**
240      * Action Resize
241      * Used for drag & drop resizing
242      */
243     protected function action_resize()
244     {
245         $this->view = 'json';        
246         
247         if (!$this->retrieveCurrentBean('Save')) {
248             return;
249         }
250         
251         require_once('include/formbase.php');
252         $this->currentBean = populateFromPost("", $this->currentBean);
253         $this->currentBean->save();
254         
255         $this->view_object_map['jsonData'] = array(
256             'access' => 'yes',
257         );
258     }
259     
260     
261     /**
262      * Retrieves current activity bean and checks access to action
263      * 
264      * @param string $actionToCheck
265      * @return bool Result of check
266      */
267     protected function retrieveCurrentBean($actionToCheck = false)
268     {    
269         $module = $_REQUEST['current_module'];        
270         $record = null;
271         if (!empty($_REQUEST['record'])) {
272             $record = $_REQUEST['record'];
273         }
274         
275         require_once("data/BeanFactory.php");        
276         $this->currentBean = BeanFactory::getBean($module, $record);        
277
278         if (!empty($actionToCheck)) {    
279             if (!$this->currentBean->ACLAccess($actionToCheck)) {
280                 $this->view = 'json';
281                 $jsonData = array(
282                     'access' => 'no',
283                 );
284                 $this->view_object_map['jsonData'] = $jsonData;
285                 return false;    
286             }
287         }
288         
289         return true;
290     }
291     
292     protected function action_getActivities()
293     {
294         $this->view = 'json';
295         
296         if(!ACLController::checkAccess('Calendar', 'list', true)){
297             ACLController::displayNoAccess(true);
298         }
299     
300         require_once('modules/Calendar/Calendar.php');
301         $cal = new Calendar($_REQUEST['view']);
302         
303         if (in_array($cal->view, array('day', 'week', 'month'))){
304             $cal->add_activities($GLOBALS['current_user']);    
305        
306         } else if ($cal->view == 'shared') {
307             $cal->init_shared();
308             $sharedUser = new User();    
309             foreach ($cal->shared_ids as $member) {
310                 $sharedUser->retrieve($member);
311                 $cal->add_activities($sharedUser);
312             }
313         }
314         $cal->load_activities();
315         $this->view_object_map['jsonData'] = $cal->items;   
316     }
317
318 }