]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/Calendar.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Calendar / Calendar.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
41
42 require_once('include/utils/activity_utils.php');
43 require_once('modules/Calendar/CalendarUtils.php');
44 require_once('modules/Calendar/CalendarActivity.php');
45
46
47 class Calendar {
48         
49         public $view = 'week'; // current view
50         public $style; // calendar style (basic or advanced)
51         public $dashlet = false; // if is displayed in dashlet  
52         public $date_time; // current date
53         
54         public $show_tasks = true;
55         public $show_calls = true;
56         public $enable_repeat = true;   
57
58         public $time_step = 60; // time step of each slot in minutes
59                 
60         public $acts_arr = array(); // Array of activities objects      
61         public $items = array(); // Array of activities data to be displayed    
62         public $shared_ids = array(); // ids of users for shared view
63         
64         
65         public $cells_per_day; // entire 24h day count of slots         
66         public $grid_start_ts; // start timestamp of calendar grid
67         
68         public $day_start_time; // working day start time in format '11:00'
69         public $day_end_time; // working day end time in format '11:00'
70         public $scroll_slot; // first slot of working day
71         public $celcount; // count of slots in a working day    
72
73     /**
74      * @var bool $print Whether is print mode.
75      */
76     private $print = false;
77                 
78         /**
79          * constructor
80          * @param string $view 
81          * @param array $time_arr 
82          */     
83         function __construct($view = "day", $time_arr = array()){
84                 global $current_user, $timedate;        
85                 
86                 $this->view = $view;            
87
88                 if(!in_array($this->view,array('day','week','month','year','shared')))
89                         $this->view = 'week';
90                 
91                 $date_arr = array();
92                 if(!empty($_REQUEST['day']))
93                         $_REQUEST['day'] = intval($_REQUEST['day']);
94                 if(!empty($_REQUEST['month']))
95                         $_REQUEST['month'] = intval($_REQUEST['month']);
96
97                 if (!empty($_REQUEST['day']))
98                         $date_arr['day'] = $_REQUEST['day'];
99                 if (!empty($_REQUEST['month']))
100                         $date_arr['month'] = $_REQUEST['month'];
101                 if (!empty($_REQUEST['week']))
102                         $date_arr['week'] = $_REQUEST['week'];
103
104                 if (!empty($_REQUEST['year'])){
105                         if ($_REQUEST['year'] > 2037 || $_REQUEST['year'] < 1970){
106                                 print("Sorry, calendar cannot handle the year you requested");
107                                 print("<br>Year must be between 1970 and 2037");
108                                 exit;
109                         }
110                         $date_arr['year'] = $_REQUEST['year'];
111                 }
112
113                 if(empty($_REQUEST['day']))
114                         $_REQUEST['day'] = "";
115                 if(empty($_REQUEST['week']))
116                         $_REQUEST['week'] = "";
117                 if(empty($_REQUEST['month']))
118                         $_REQUEST['month'] = "";
119                 if(empty($_REQUEST['year']))
120                         $_REQUEST['year'] = "";
121
122                 // if date is not set in request use current date
123                 if(empty($date_arr) || !isset($date_arr['year']) || !isset($date_arr['month']) || !isset($date_arr['day']) ){   
124                         $today = $timedate->getNow(true);
125                         $date_arr = array(
126                               'year' => $today->year,
127                               'month' => $today->month,
128                               'day' => $today->day,
129                         );
130                 }
131                 
132                 $current_date_db = $date_arr['year']."-".str_pad($date_arr['month'],2,"0",STR_PAD_LEFT)."-".str_pad($date_arr['day'],2,"0",STR_PAD_LEFT);
133                 $this->date_time = $GLOBALS['timedate']->fromString($current_date_db);  
134                                 
135                 $this->show_tasks = $current_user->getPreference('show_tasks');
136                 if(is_null($this->show_tasks))
137                         $this->show_tasks = SugarConfig::getInstance()->get('calendar.show_tasks_by_default',true);             
138                 $this->show_calls = $current_user->getPreference('show_calls');
139                 if(is_null($this->show_calls))
140                         $this->show_calls = SugarConfig::getInstance()->get('calendar.show_calls_by_default',true);
141                         
142                 $this->enable_repeat = SugarConfig::getInstance()->get('calendar.enable_repeat',true);  
143
144                 if(in_array($this->view,array('month','shared','year'))){
145                         $this->style = "basic"; 
146                 }else{
147                         $displayTimeslots = $GLOBALS['current_user']->getPreference('calendar_display_timeslots');
148                         if (is_null($displayTimeslots)) {
149                                 $displayTimeslots = SugarConfig::getInstance()->get('calendar.display_timeslots', true);
150                         }
151                         if ($displayTimeslots) {
152                                 $this->style = "advanced";
153                         } else {
154                                 $this->style = "basic";
155                         }
156                 }
157                 
158                 $this->day_start_time = $current_user->getPreference('day_start_time');
159                 if(is_null($this->day_start_time))
160                         $this->day_start_time = SugarConfig::getInstance()->get('calendar.default_day_start',"08:00");
161                 $this->day_end_time = $current_user->getPreference('day_end_time');
162                 if(is_null($this->day_end_time))
163                         $this->day_end_time = SugarConfig::getInstance()->get('calendar.default_day_end',"19:00");
164                         
165                 if($this->view == "day"){
166                         $this->time_step = SugarConfig::getInstance()->get('calendar.day_timestep',15);
167                 }else if($this->view == "week" || $this->view == "shared"){
168                         $this->time_step = SugarConfig::getInstance()->get('calendar.week_timestep',30);
169                 }else if($this->view == "month"){
170                         $this->time_step = SugarConfig::getInstance()->get('calendar.month_timestep',60);
171                 }else{
172                         $this->time_step = 60;
173                 }
174                 $this->cells_per_day = 24 * (60 / $this->time_step);            
175                 $this->calculate_grid_start_ts();
176                 $this->calculate_day_range();           
177         }
178         
179         /**
180          * Load activities data to array
181          */             
182         public function load_activities(){
183                 $field_list = CalendarUtils::get_fields();
184                 
185                 foreach($this->acts_arr as $user_id => $acts){  
186                         foreach($acts as $act){
187                                                                                         
188                                         $item = array();
189                                         $item['user_id'] = $user_id;
190                                         $item['module_name'] = $act->sugar_bean->module_dir;
191                                         $item['type'] = strtolower($act->sugar_bean->object_name);
192                                         $item['assigned_user_id'] = $act->sugar_bean->assigned_user_id;
193                                         $item['record'] = $act->sugar_bean->id;         
194                                         $item['name'] = $act->sugar_bean->name;
195                                         
196                                         if(isset($act->sugar_bean->duration_hours)){
197                                                 $item['duration_hours'] = $act->sugar_bean->duration_hours;
198                                                 $item['duration_minutes'] = $act->sugar_bean->duration_minutes;
199                                         }                               
200                                                                 
201                                         $item['detail'] = 0;
202                                         $item['edit'] = 0;
203                                         
204                                         if($act->sugar_bean->ACLAccess('DetailView'))
205                                                 $item['detail'] = 1;                                            
206                                         if($act->sugar_bean->ACLAccess('Save'))
207                                                 $item['edit'] = 1;                                      
208                                                 
209                                         if(empty($act->sugar_bean->id)){
210                                                 $item['detail'] = 0;
211                                                 $item['edit'] = 0;
212                                         }
213                                         
214                                         if(!empty($act->sugar_bean->repeat_parent_id))
215                                                 $item['repeat_parent_id'] = $act->sugar_bean->repeat_parent_id;                                 
216                                         
217                                         if($item['detail'] == 1){
218                                                 if(isset($field_list[$item['module_name']])){
219                                                         foreach($field_list[$item['module_name']] as $field){
220                                                                 if(!isset($item[$field])){
221                                                                         $item[$field] = $act->sugar_bean->$field;
222                                                                         if(empty($item[$field]))
223                                                                                 $item[$field] = "";
224                                                                 }
225                                                         }                                       
226                                                 }                               
227                                         }
228                                         
229                                         if(!isset($item['duration_hours']) || empty($item['duration_hours']))
230                                                 $item['duration_hours'] = 0;
231                                         if(!isset($item['duration_minutes']) || empty($item['duration_minutes']))
232                                                 $item['duration_minutes'] = 0;  
233                                                 
234                                         $item = array_merge($item,CalendarUtils::get_time_data($act->sugar_bean));                      
235                         
236                                         $this->items[] = $item;
237                         }
238                 }
239         }       
240         
241         /**
242          * initialize ids of shared users
243          */     
244         public function init_shared(){
245                 global $current_user;
246                 
247                 
248                 $user_ids = $current_user->getPreference('shared_ids');
249                 if(!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['shared_ids'])) {
250                         $this->shared_ids = $user_ids;
251                 }else if(isset($_REQUEST['shared_ids']) && count($_REQUEST['shared_ids']) > 0){
252                         $this->shared_ids = $_REQUEST['shared_ids'];
253                         $current_user->setPreference('shared_ids', $_REQUEST['shared_ids']);
254                 }else{
255                         $this->shared_ids = array($current_user->id);                           
256                 }
257         }
258         
259         /**
260          * Calculate timestamp the calendar grid should be started from 
261          */
262         protected function calculate_grid_start_ts(){
263         
264                 if($this->view == "week" || $this->view == "shared"){
265                         $week_start = CalendarUtils::get_first_day_of_week($this->date_time);
266                         $this->grid_start_ts = $week_start->format('U') + $week_start->getOffset(); 
267                 }else if($this->view == "month"){
268                         $month_start = $this->date_time->get_day_by_index_this_month(0);
269                         $week_start = CalendarUtils::get_first_day_of_week($month_start);
270                         $this->grid_start_ts = $week_start->format('U') + $week_start->getOffset(); // convert to timestamp, ignore tz
271                 }else if($this->view == "day"){
272                         $this->grid_start_ts = $this->date_time->format('U') + $this->date_time->getOffset();
273                 }       
274         }
275         
276         /**
277          * calculate count of timeslots per visible day, calculates day start and day end in minutes 
278          */     
279         function calculate_day_range(){ 
280                 
281                 list($hour_start,$minute_start) =  explode(":",$this->day_start_time);          
282                 list($hour_end,$minute_end) =  explode(":",$this->day_end_time);
283                 $this->scroll_slot = intval($hour_start * (60 / $this->time_step) + ($minute_start / $this->time_step));
284                 $this->celcount = (($hour_end * 60 + $minute_end) - ($hour_start * 60 + $minute_start)) / $this->time_step;
285         }       
286         
287         /**
288          * loads array of objects
289          * @param User $user user object
290          * @param string $type
291          */     
292         public function add_activities($user,$type='sugar'){
293                 global $timedate;
294                 $start_date_time = $this->date_time;
295                 if($this->view == 'week' || $this->view == 'shared'){           
296                         $start_date_time = CalendarUtils::get_first_day_of_week($this->date_time);
297                         $end_date_time = $start_date_time->get("+7 days");
298                 }else if($this->view == 'month'){
299                         $start_date_time = $this->date_time->get_day_by_index_this_month(0);    
300                         $end_date_time = $start_date_time->get("+".$start_date_time->format('t')." days");
301                         $start_date_time = CalendarUtils::get_first_day_of_week($start_date_time);
302                         $end_date_time = CalendarUtils::get_first_day_of_week($end_date_time)->get("+7 days");
303                 }else{
304                         $end_date_time = $this->date_time->get("+1 day");
305                 }
306                 
307                 $start_date_time = $start_date_time->get("-5 days"); // 5 days step back to fetch multi-day activities that
308
309                 $acts_arr = array();
310                 if($type == 'vfb'){
311                                 $acts_arr = CalendarActivity::get_freebusy_activities($user, $start_date_time, $end_date_time);
312                 }else{
313                                 $acts_arr = CalendarActivity::get_activities($user->id, $this->show_tasks, $start_date_time, $end_date_time, $this->view,$this->show_calls);
314                 }
315                 
316                 $this->acts_arr[$user->id] = $acts_arr;  
317         }
318
319         /**
320          * Get date string of next or previous calendar grid
321          * @param string $direction next or previous
322          * @return string
323          */
324         public function get_neighbor_date_str($direction){
325                 if($direction == "previous")
326                         $sign = "-";
327                 else 
328                         $sign = "+";
329                         
330                 if($this->view == 'month'){
331             $day = $this->date_time->get_day_by_index_this_month(0)->get($sign."1 month")->get_day_begin(1);
332                 }else if($this->view == 'week' || $this->view == 'shared'){
333                         $day = CalendarUtils::get_first_day_of_week($this->date_time);
334                         $day = $day->get($sign."7 days");
335                 }else if($this->view == 'day'){
336                         $day = $this->date_time->get($sign."1 day")->get_day_begin();
337                 }else if($this->view == 'year'){
338                         $day = $this->date_time->get($sign."1 year")->get_day_begin();
339                 }else{
340                         return "get_neighbor_date_str: notdefined for this view";
341                 }
342                 return $day->get_date_str();
343         }
344
345     public function setPrint($print)
346     {
347         $this->print = $print;
348     }
349     
350     public function isPrint()
351     {
352         return $this->print;
353     }
354
355 }
356
357 ?>