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