]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/CalendarGrid.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Calendar / CalendarGrid.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-2011 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 global $timedate;
41
42 class CalendarGrid {
43
44         protected $cal; // Calendar object
45         protected $today_ts; // timestamp of today
46         protected $weekday_names; // string array of names of week days
47         protected $startday; // first day of week
48         protected $scrollable; // srolling in calendar
49         protected $time_step = 30; // time step
50         protected $time_format; // user time format
51         protected $date_time_format; // user date time format
52         protected $scroll_height; // height of scrollable div
53         
54         /**
55          * constructor
56          * @param Calendar $cal
57          */
58         function __construct(&$cal){
59                 global $current_user;
60                 $this->cal = &$cal;             
61                 $today = $GLOBALS['timedate']->getNow(true)->get_day_begin();
62                 $this->today_ts = $today->format('U') + $today->getOffset();            
63                 $this->startday = $current_user->get_first_day_of_week();
64                 
65                 $weekday_names = array();
66                 for($i = 0; $i < 7; $i++){
67                         $j = $i + $this->startday;
68                         if($j >= 7)
69                                 $j = $j - 7;
70                         $weekday_names[$i] = $GLOBALS['app_list_strings']['dom_cal_day_short'][$j+1];
71                 }               
72                         
73                 $this->weekday_names = $weekday_names;  
74                 
75                 $this->scrollable = false;              
76                 if(in_array($this->cal->view,array('day','week'))){
77                         $this->scrollable = true;
78                         if($this->cal->time_step < 30)
79                                 $this->scroll_height = 480;
80                         else
81                                 $this->scroll_height = $this->cal->celcount * 15 + 1;
82                 }
83                 
84                 $this->time_step = $this->cal->time_step;
85                 $this->time_format = $GLOBALS['timedate']->get_time_format();
86                 $this->date_time_format = $GLOBALS['timedate']->get_date_time_format(); 
87         
88         }
89         
90         
91         /** Get html of calendar grid
92          * @return string
93          */
94         public function display(){
95                 $action = "display_".strtolower($this->cal->view);              
96                 return $this->$action();
97         }
98         
99         /** Get html of time column
100          * @param integer $start timestamp       
101          * @return string
102          */
103         protected function get_time_column($start){                     
104                 $str = "";                      
105                 $head_content = "&nbsp;";       
106                 if($this->cal->view == 'month'){
107                         if($this->startday == 0)
108                                 $wf = 1;
109                         else
110                                 $wf = 0;                                
111                         $head_content = "<a href='".ajaxLink("index.php?module=Calendar&action=index&view=week&hour=0&day=".$GLOBALS['timedate']->fromTimestamp($start)->format('j')."&month=".$GLOBALS['timedate']->fromTimestamp($start)->format('n')."&year=".$GLOBALS['timedate']->fromTimestamp($start)->format('Y'))."'>".$GLOBALS['timedate']->fromTimestamp($start + $wf*3600*24)->format('W')."</a>";
112                 }                       
113                 $str .= "<div class='left_time_col'>";
114                         if(!$this->scrollable)                  
115                                 $str .= "<div class='day_head'>".$head_content."</div>";
116                         for($i = 0; $i < 24; $i++){
117                                 for($j = 0; $j < 60; $j += $this->time_step){   
118                                                         
119                                         if($j == 0){ 
120                                                 $innerText = $GLOBALS['timedate']->fromTimestamp($start + $i * 3600)->format($this->time_format);                                               
121                                         }else{
122                                                 $innerText = "&nbsp;";
123                                         }                                               
124                                         if($j == 60 - $this->time_step && $this->time_step < 60){
125                                                 $class = " odd_border";
126                                         }else{
127                                                 $class = "";
128                                         }                                                                               
129                                         if($this->scrollable || !$this->check_owt($i,$j,$this->cal->d_start_minutes,$this->cal->d_end_minutes))                                                                                 
130                                                 $str .= "<div class='left_cell".$class."'>".$innerText."</div>";
131                                 }
132                         }       
133                 $str .= "</div>";               
134                 return $str;
135         }
136         
137         /** 
138          * Get html of day slots column
139          * @param integer $start timestamp
140          * @param integer $day number of day in week
141          * @param string $prefix prefix for id of timeslot used in shared view   
142          * @return string
143          */
144         protected function get_day_column($start,$day = 0,$prefix = ""){        
145                 $curr_time = $start;
146                 $str = "";
147                 $str .= "<div class='day_col'>";
148                 $str .= $this->get_day_head($start,$day);
149                 for($i = 0; $i < 24; $i++){                     
150                         for($j = 0; $j < 60; $j += $this->time_step){
151                                 $timestr = $GLOBALS['timedate']->fromTimestamp($curr_time)->format($this->time_format); 
152                                 if($j == 60 - $this->time_step && $this->time_step < 60){
153                                         $class = " odd_border";
154                                 }else{
155                                         $class = "";    
156                                 }                               
157                                 if($this->scrollable || !$this->check_owt($i,$j,$this->cal->d_start_minutes,$this->cal->d_end_minutes))
158                                         $str .= "<div id='t_".$curr_time.$prefix."' class='slot".$class."' time='".$timestr."' datetime='".$GLOBALS['timedate']->fromTimestamp($curr_time)->format($this->date_time_format)."'></div>";
159                                 $curr_time += $this->time_step*60;
160                         }
161                 }
162                 $str .= "</div>";               
163                 return $str;    
164         }
165         
166         /** 
167          * Get html of day head
168          * @param integer $start timestamp
169          * @param integer $day number of day in week 
170          * @param bulean $force force display header 
171          * @return string
172          */     
173         protected function get_day_head($start,$day = 0,$force = false){
174                 $str = "";
175                 if(!$this->scrollable || $force){
176                         $headstyle = ""; 
177                         if($this->today_ts == $start)
178                                 $headstyle = " today";
179                         $str .= "<div class='day_head".$headstyle."'><a href='".ajaxLink("index.php?module=Calendar&action=index&view=day&hour=0&day=".$GLOBALS['timedate']->fromTimestamp($start)->format('j')."&month=".$GLOBALS['timedate']->fromTimestamp($start)->format('n')."&year=".$GLOBALS['timedate']->fromTimestamp($start)->format('Y'))."'>".$this->weekday_names[$day]." ".$GLOBALS['timedate']->fromTimestamp($start)->format('d')."</a></div>";
180                 }
181                 return $str;
182         }       
183         
184         /**
185          * Get true if out of working day
186          * @param integer $i hours
187          * @param integer $j minutes
188          * @param integer $r_start start of working day in minutes
189          * @param integer $r_end end of working day in minutes
190          * @return boolean
191          */
192         protected function check_owt($i,$j,$r_start,$r_end){
193                 if($i*60+$j < $r_start || $i*60+$j >= $r_end)
194                         return true;
195         }
196         
197         /** 
198          * Get html of week calendar grid
199          * @return string       
200          */     
201         protected function display_week(){
202                 
203                 $current_date = $this->cal->date_time;
204                 $week_start = CalendarUtils::get_first_day_of_week($current_date);
205                 $week_start_ts = $week_start->format('U') + $week_start->getOffset(); // convert to timestamp, ignore tz
206         
207                 $str = "";
208                 $str .= "<div id='cal-grid' style='visibility: hidden;'>";
209                                 
210                         $str .= "<div style='overflow-y: hidden;'>";                                            
211                                 $str .= "<div class='left_time_col'>";
212                                         $str .= "<div class='day_head'>&nbsp;</div>";           
213                                 $str .= "</div>";
214                                 $str .= "<div class='week_block'>";
215                                 for($d = 0; $d < 7; $d++){
216                                         $curr_time = $week_start_ts + $d*86400;
217                                         $str .= "<div class='day_col'>";
218                                         $str .= $this->get_day_head($curr_time,$d,true);                                        
219                                         $str .= "</div>";                       
220                                 }
221                                 $str .= "</div>";               
222                         $str .= "</div>";               
223                         
224                         $str .= "<div id='cal-scrollable' style='clear: both; height: ".$this->scroll_height ."px;'>";                  
225                                 $str .= $this->get_time_column($week_start_ts);                 
226                                 $str .= "<div class='week_block'>";
227                                 for($d = 0; $d < 7; $d++){
228                                         $curr_time = $week_start_ts + $d*86400;                         
229                                         $str .= $this->get_day_column($curr_time);
230                                 }       
231                                 $str .= "</div>";               
232                         $str .= "</div>";
233                                 
234                 $str .= "</div>";
235                 
236                 return $str;
237         }               
238         
239         /** 
240          * Get html of day calendar grid
241          * @return string       
242          */
243         protected function display_day(){       
244
245                 $current_date = $this->cal->date_time;
246                 $day_start_ts = $current_date->format('U') + $current_date->getOffset(); // convert to timestamp, ignore tz
247                 
248                 
249                 $str = "";
250                 $str .= "<div id='cal-grid' style=' min-width: 300px; visibility: hidden;'>";
251                         $str .= "<div id='cal-scrollable' style='height: ".$this->scroll_height ."px;'>";                       
252                                 $str .= $this->get_time_column($day_start_ts);
253                                 $d = 0;
254                                 $curr_time = $day_start_ts + $d*86400;
255                                 $str .= "<div class='week_block'>";                             
256                                 $str .= $this->get_day_column($curr_time);
257                                 $str .= "</div>";
258                         $str .= "</div>";               
259                 $str .= "</div>";
260                 
261                 return $str;    
262         }       
263         
264         /** 
265          * Get html of month calendar grid
266          * @return string       
267          */
268         protected function display_month(){
269                         
270                 $current_date = $this->cal->date_time;
271                 $month_start = $current_date->get_day_by_index_this_month(0);   
272                 $month_end = $month_start->get("+".$month_start->format('t')." days");                  
273                 $week_start = CalendarUtils::get_first_day_of_week($month_start);
274                 $week_start_ts = $week_start->format('U') + $week_start->getOffset(); // convert to timestamp, ignore tz
275                 $month_end_ts = $month_end->format('U') + $month_end->getOffset();                                      
276
277                 $str = "";
278                 $str .= "<div id='cal-grid' style='visibility: hidden;'>";
279                         $curr_time_global = $week_start_ts;
280                         $w = 0;
281                         while($curr_time_global < $month_end_ts){
282                                 $str .= $this->get_time_column($curr_time_global);                              
283                                 $str .= "<div class='week_block'>";     
284                                 for($d = 0; $d < 7; $d++){
285                                         $curr_time = $week_start_ts + $d*86400 + $w*60*60*24*7;
286                                         $str .= $this->get_day_column($curr_time,$d);           
287                                 }
288                                 $str .= "</div>";
289                                 $str .= "<div style='clear: left;'></div>";
290                                 $curr_time_global += 60*60*24*7;
291                                 $w++;
292                         }
293                 $str .= "</div>";
294                 
295                 return $str;
296         }
297         
298         /** 
299          * Get html of week shared grid
300          * @return string       
301          */
302         protected function display_shared(){
303         
304                 $current_date = $this->cal->date_time;
305                 $week_start = CalendarUtils::get_first_day_of_week($current_date);
306                 $week_start_ts = $week_start->format('U') + $week_start->getOffset(); // convert to timestamp, ignore tz
307
308                 $str = "";
309                 $str .= "<div id='cal-grid' style='visibility: hidden;'>";
310                 $user_number = 0;
311                 
312                 $shared_user = new User();
313                 foreach($this->cal->shared_ids as $member_id){
314
315                         $user_number_str = "_".$user_number;
316                 
317                         $shared_user->retrieve($member_id);
318                         $str .= "<div style='clear: both;'></div>";                     
319                         $str .= "<div class='monthCalBody'><h5 class='calSharedUser'>".$shared_user->full_name."</h5></div>";   
320                         $str .= "<div user_id='".$member_id."' user_name='".$shared_user->user_name."'>";                       
321                         
322                         $str .= $this->get_time_column($week_start_ts);
323                                 $str .= "<div class='week_block'>";
324                                 for($d = 0; $d < 7; $d++){
325                                         $curr_time = $week_start_ts + $d*86400;
326                                         $str .= $this->get_day_column($curr_time,$d,$user_number_str);
327                                 }
328                                 $str .= "</div>";               
329                         $str .= "</div>";
330                         $user_number++;
331                 }
332                 $str .= "</div>";
333                 
334                 return $str;
335         }       
336         
337         /** 
338          * Get html of year calendar
339          * @return string       
340          */
341         protected function display_year(){      
342
343                 $weekEnd1 = 0 - $this->startday; 
344                 $weekEnd2 = -1 - $this->startday; 
345                 if($weekEnd1 < 0)
346                         $weekEnd1 += 7;         
347                 if($weekEnd2 < 0)
348                         $weekEnd2 += 7; 
349
350                 $year_start = $GLOBALS['timedate']->fromString($this->cal->date_time->year.'-01-01');
351
352                 $str = "";
353                 $str .= '<table id="daily_cal_table" cellspacing="1" cellpadding="0" border="0" width="100%">';
354
355                 for($m = 0; $m < 12; $m++){
356         
357                         $month_start = $year_start->get("+".$m." months");                      
358                         $month_start_ts = $month_start->format('U') + $month_start->getOffset();
359                         $month_end = $month_start->get("+".$month_start->format('t')." days");                  
360                         $week_start = CalendarUtils::get_first_day_of_week($month_start);
361                         $week_start_ts = $week_start->format('U') + $week_start->getOffset(); // convert to timestamp, ignore tz
362                         $month_end_ts = $month_end->format('U') + $month_end->getOffset();
363                         $table_id = "daily_cal_table".$m; //bug 47471   
364                                                 
365                         if($m % 3 == 0)
366                                 $str .= "<tr>";         
367                                         $str .= '<td class="yearCalBodyMonth" align="center" valign="top" scope="row">';
368                                                 $str .= '<a class="yearCalBodyMonthLink" href="'.ajaxLink('index.php?module=Calendar&action=index&view=month&&hour=0&day=1&month='.($m+1).'&year='.$GLOBALS['timedate']->fromTimestamp($month_start_ts)->format('Y')).'">'.$GLOBALS['app_list_strings']['dom_cal_month_long'][$m+1].'</a>';
369                                                 $str .= '<table id="'. $table_id. '" cellspacing="1" cellpadding="0" border="0" width="100%">'; 
370                                                         $str .= '<tr class="monthCalBodyTH">';
371                                                                 for($d = 0; $d < 7; $d++)
372                                                                         $str .= '<th width="14%">'.$this->weekday_names[$d].'</th>';                    
373                                                         $str .= '</tr>';                                
374                                                         $curr_time_global = $week_start_ts;
375                                                         $w = 0;
376                                                         while($curr_time_global < $month_end_ts){
377                                                                 $str .= '<tr class="monthViewDayHeight yearViewDayHeight">';
378                                                                         for($d = 0; $d < 7; $d++){
379                                                                                 $curr_time = $week_start_ts + $d*86400 + $w*60*60*24*7;
380
381                                                                                 if($curr_time < $month_start_ts || $curr_time >= $month_end_ts)
382                                                                                         $monC = "";
383                                                                                 else
384                                                                                         $monC = '<a href="'.ajaxLink('index.php?module=Calendar&action=index&view=day&hour=0&day='.$GLOBALS['timedate']->fromTimestamp($curr_time)->format('j').'&month='.$GLOBALS['timedate']->fromTimestamp($curr_time)->format('n').'&year='.$GLOBALS['timedate']->fromTimestamp($curr_time)->format('Y')) .'">'.$GLOBALS['timedate']->fromTimestamp($curr_time)->format('j').'</a>';
385                                                                 
386                                                                                 if($d == $weekEnd1 || $d == $weekEnd2)  
387                                                                                         $str .= "<td class='weekEnd monthCalBodyWeekEnd'>"; 
388                                                                                 else
389                                                                                         $str .= "<td class='monthCalBodyWeekDay'>";
390                                                                                                 $str .= $monC;
391                                                                                         $str .= "</td>";
392                                                                         }
393                                                                 $str .= "</tr>";
394                                                                 $curr_time_global += 60*60*24*7;
395                                                                 $w++;
396                                                         }                               
397                                                 $str .= '</table>';
398                                         $str .= '</td>';
399                         if(($m - 2) % 3 == 0)
400                                 $str .= "</tr>";        
401                 }
402                 $str .= "</table>";
403                 
404                 return $str;            
405         }
406 }
407
408 ?>