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