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