source: trunk/mobile/inc/class.ui_mobilecalendar.inc.php @ 7655

Revision 7655, 14.8 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

  • Property svn:executable set to *
Line 
1<?php
2    class ui_mobilecalendar {
3               
4                var $bocalendar;
5                var $cat;
6                var $link_tpl;         
7                var $common;
8                var $template;         
9                var $daysOfWeek;
10                var $shortDaysOfWeek;
11               
12                var $public_functions = array(
13                        'index' => true
14                );
15               
16                function ui_mobilecalendar() {
17                        $this->template = CreateObject('phpgwapi.Template', PHPGW_SERVER_ROOT . '/mobile/templates/'.$GLOBALS['phpgw_info']['server']['template_set']);
18                        $this->common   = CreateObject('mobile.common_functions');
19                        $this->bocalendar = CreateObject('calendar.bocalendar',1);                     
20                        $this->cat = &$this->bo->cat;
21                        $this->daysOfWeek = array(lang('Sunday'), lang('Monday'),lang('Tuesday'),lang('Wednesday'),lang('Thursday'),lang('Friday'),lang('Saturday'));
22                        $this->shortDaysOfWeek = array(lang('short Sunday'), lang('short Monday'),lang('short Tuesday'),lang('short Wednesday'),lang('short Thursday'),lang('short Friday'),lang('short Saturday'));
23                       
24                        $GLOBALS['phpgw']->nextmatchs = CreateObject('phpgwapi.nextmatchs');
25                }
26               
27                function index($params) {
28                        $this->template->set_file(array(
29                                'calendar' => 'calendar.tpl',
30                                'home_search_bar' => 'search_bar.tpl'
31                        ));
32                        $actual_max_results = $_GET["results"]?$_GET["results"]:10;
33                        $this->template->set_block("calendar","page");
34                        $this->template->set_block('calendar','event_block');
35                        $this->template->set_block('calendar','day_event_block');
36                        $this->template->set_block('calendar','no_event_block');
37                        $this->template->set_block('calendar','type_option_block');
38                        $this->template->set_block('calendar','bar_block');
39                        $this->template->set_block('home_search_bar','search_bar');
40                        $this->template->set_var('lang_back',lang("back"));
41                        $this->template->set_var('href_back',$GLOBALS['phpgw_info']['mobiletemplate']->get_back_link());
42                        $this->template->set_var('lang_calendar',lang("Calendar"));
43                        $this->template->set_var('lang_search',lang("search"));
44                        $this->template->set_var('lang_more',lang("more"));
45                        $this->template->set_var('lang_events',lang("events"));
46                        $this->template->set_var('type',$params['type']);
47                        $this->template->set_var('dia',$params['dia']);
48                        $this->template->set_var('mes',$params['mes']);
49                        $this->template->set_var('ano',$params['ano']);
50                        $this->template->set_var('next_max_results',$actual_max_results+10);
51                       
52                        if($GLOBALS['phpgw']->session->appsession('mobile.layout','mobile')!="mini_desktop")
53                                $this->template->set_var('search',$this->template->fp('out','search_bar'));
54                       
55                        if(!function_exists("get_events")){
56                                function get_events($self, $accountId, $begin, $end) {
57                                        $self->bocalendar->so->owner = $accountId;
58                                        $self->bocalendar->so->open_box($accountId);
59                                        $self->bocalendar->store_to_cache(
60                                                array(
61                                                        'owner' => $accountId,
62                                                        'syear'  => date('Y',$begin),
63                                                        'smonth' => date('m',$begin),
64                                                        'sday'   => date('d',$begin),
65                                                        'eyear'  => date('Y',$end),
66                                                        'emonth' => date('m',$end),
67                                                        'eday'   => date('d',$end)
68                                                )
69                                        );
70                                       
71                                        $events = $self->bocalendar->cached_events;
72                                        ksort($events);
73                               
74                                        return $events;
75                                }
76                        }
77                       
78                        if(!function_exists("print_events")){   
79                                function print_events($self, $events) {
80                                        foreach($events as $index=>$event)
81                                        {
82                                                $self->template->set_var('dd_class', (($index%2==0) ? "dt-azul" : "dt-branco") );       
83                                                $vars = $self->bocalendar->event2array($event);
84                                                $data = array (
85                                                        "title_field"           => $vars['title']['field'],
86                                                        "title_data"            => $vars['title']['data'],
87                                                        "startdate_data"        => substr($vars['startdate']['data'],13, 17),
88                                                        "enddate_data"          => substr($vars['enddate']['data'],13, 17),
89                                                        "location_field"                => $vars['location']['field'],
90                                                        "location_data" => $vars['location']['data'] ? $vars['location']['data'] : "-",
91                                                        "description_field"             => $vars['description']['field'],
92                                                        "description_data"      => $vars['description']['data'] ? $vars['description']['data'] : "-"
93                                                );
94                       
95                                $self->template->set_var($data);
96                                                $self->template->parse('events_box','event_block',true);
97                                        }                               
98                                }
99                        }       
100                       
101                        if(!function_exists("print_events_header")){   
102                                function print_events_header($self, $day, $month, $year) {
103                                        $day_of_week = $self->daysOfWeek[$GLOBALS['phpgw']->datetime->day_of_week($year,$month,$day)];
104                                        $self->template->set_var("day",$day_of_week." - ".$day."/".$month."/".$year);
105                                        $self->template->parse('events_box','day_event_block',true);
106                                }
107                        }
108                               
109                        $type = $_GET["type"];
110                        if(!$type) $type = "dia";
111                        $types = array("dia" => lang("view day"),"semana" => lang("view week"),"calendario" => lang("view calendar"));
112                       
113                        foreach($types as $value=>$label)       {
114                                $this->template->set_var("value",$value);
115                                $this->template->set_var("label",$label);
116                               
117                                if($type == $value)
118                                        $this->template->set_var("selected", "selected");
119                                else
120                                        $this->template->set_var("selected", null);
121                                       
122                                $this->template->parse('type_option_box','type_option_block',true);
123                        }
124                       
125                        $accountId = $GLOBALS['phpgw_info']['user']['account_id'];
126
127                        $day = $_GET["dia"];
128                        if(!$day) $day = $this->bocalendar->day;
129
130                        $month = $_GET["mes"];
131                        if(!$month) $month = $this->bocalendar->month;
132                       
133                        $year = $_GET["ano"];
134                        if(!$year) $year = $this->bocalendar->year;
135                       
136                        $this->template->set_var("lang_today", lang("today"));
137                        $this->template->set_var("today_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=dia&dia");
138                        $show_more_button = false;
139                        if($type==="semana") {
140                                $tstart = $GLOBALS['phpgw']->datetime->get_weekday_start($year, $month, $day) + $GLOBALS['phpgw']->datetime->tz_offset;
141                                $tstart -= $GLOBALS['phpgw']->datetime->tz_offset;
142                                $tstop = $tstart + 604800;
143                                $events = get_events($this, $accountId, $tstart, $tstop);
144                                $current_day_of_week = $GLOBALS['phpgw']->datetime->day_of_week($year, $month, $day);
145                               
146                                //descobrindo a semana anterior e a pŽroxima
147                                $last_week = date("d-m-Y", strtotime("-7 day", mktime(0,0,0,$month,$day,$year) ) );
148                                $last_week = preg_split('/-/',$last_week);
149                                $next_week = date("d-m-Y", strtotime("+7 day", mktime(0,0,0,$month,$day,$year) ) );
150                                $next_week = preg_split('/-/',$next_week);
151                               
152                                //descobrind o primeiro dia da semana e o último
153                                $first_week_day = date("d-m-Y", strtotime("-".$current_day_of_week." day", mktime(0,0,0,$month,$day,$year) ) );
154                                $first_week_day = preg_split('/-/',$first_week_day);
155                                $last_week_day = date("d-m-Y", strtotime("+".(6-$current_day_of_week)." day", mktime(0,0,0,$month,$day,$year) ) );
156                                $last_week_day = preg_split('/-/',$last_week_day);
157                               
158                                //definindo a barra de navegação do calandário
159                                $this->template->set_var("before_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=semana&dia=".$last_week[0]."&mes=".$last_week[1]."&ano=".$last_week[2]);
160                                $this->template->set_var("current_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=semana&dia=".$day."&mes=".$month."&ano=".$year);
161                                $this->template->set_var("next_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=semana&dia=".$next_week[0]."&mes=".$next_week[1]."&ano=".$next_week[2]);
162                                $this->template->set_var("current_label", $first_week_day[0]."/".$first_week_day[1]."/".$first_week_day[2]." à ".$last_week_day[0]."/".$last_week_day[1]."/".$last_week_day[2]);
163                                $this->template->parse('events_box','bar_block',true);
164                               
165                                $total_events = 0;
166                                if(!empty($events)) {
167                                        foreach($events as $index=>$event)      {
168                                                if($total_events>=$actual_max_results) {
169                                                        $show_more_button = true;
170                                                        break;
171                                                }
172                                                $event_year  = (int) substr($index,0,4);
173                                                $event_month = (int) substr($index,4,2);
174                                                $event_day   = (int) substr($index,6,2);
175                                                $complete_day = $event_year.$this->common->complete_string($event_month,2,"R","0").$this->common->complete_string($event_day,2,"R","0");
176
177                                                //Se a quantidade de eventos for maior que o máximo pego nessa interação, o botão de mais deve aparecer
178                                                if(count($events[$complete_day])>$actual_max_results-$total_events)
179                                                        $show_more_button = true;
180                                               
181                                                //Pego os eventos até completar o máximo da paginação
182                                                $events_to_print = array_slice($events[$complete_day],0,$actual_max_results-$total_events);
183                                                $total_events += count($events_to_print);
184                                               
185                                                print_events_header($this, $event_day, $event_month, $event_year);
186                                                print_events($this, $events_to_print);
187                                        }               
188                                } else {
189                                        $this->template->set_var("msg_no_event", lang("Dont have event that week"));
190                                        $this->template->parse('events_box','no_event_block',true);
191                                }                               
192                                               
193                        } elseif($type==="calendario") {
194                                $day_of_week_first_day = $GLOBALS['phpgw']->datetime->day_of_week($year, $month, "01");
195                                $last_day_of_month = date('t', mktime(0,0,0,$month,"01",$year));
196                                $last_day_of_before_month = date('t', mktime(0,0,0,$month-1,"01",$year));
197                                $last_month_year = date("m-Y", strtotime("-1 month", mktime(0,0,0,$month,"01",$year) ) );
198                                $last_month_year = preg_split('/-/',$last_month_year);
199                                $next_month_year = date("m-Y", strtotime("+1 month", mktime(0,0,0,$month,"01",$year) ) );
200                                $next_month_year = preg_split('/-/',$next_month_year);
201                                $today = date("d-m-Y");
202                               
203                                $days_of_calendar = array();
204                               
205                                //identificando os dias do mês anterior
206                                for($i = 0; $i < $day_of_week_first_day; ++$i) {
207                                        $days_of_calendar[$i] = array("day" => ($last_day_of_before_month-$day_of_week_first_day+$i+1), "month" => $last_month_year[0], "year" => $last_month_year[1], "other_month" => true);                                 
208                                }
209                               
210                                //adicionando os dias do mês especificado
211                                for($i = 0; $i < ($last_day_of_month ); ++$i) {
212                                        $days_of_calendar[$i+$day_of_week_first_day] = array("day" => $i+1, "month" => $month, "year" => $year, "other_month" => false);
213                                }
214                               
215                                //pegando todos os eventos a partir o menor dia visível do mês anterior
216                                $tstart = mktime(0,0,0,$last_month_year[0],($last_day_of_before_month-$day_of_week_first_day),$last_month_year[1]);
217                                $tstart -= $GLOBALS['phpgw']->datetime->tz_offset;
218                                $tstop = $tstart + (86400*sizeof($days_of_calendar)); //(24horas*60min*60seg*total de dias exibidos)
219                                $events = get_events($this, $accountId, $tstart, $tstop);
220                               
221                                //definindo os blocos a serem utilizados
222                                $this->template->set_block('calendar','calendar_header_begin_block');
223                                $this->template->set_block('calendar','calendar_header_end_block');
224                                $this->template->set_block('calendar','calendar_header_block');
225                                $this->template->set_block('calendar','calendar_day_block');
226                                $this->template->set_block('calendar','calendar_day_begin_block');
227                                $this->template->set_block('calendar','calendar_day_end_block');
228                               
229                                //definindo a barra de navegação do calandário
230                                $this->template->set_var("before_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=calendario&mes=".$last_month_year[0]."&ano=".$last_month_year[1]);
231                                $this->template->set_var("current_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=calendario&mes=".$month."&ano=".$year);
232                                $this->template->set_var("next_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=calendario&mes=".$next_month_year[0]."&ano=".$next_month_year[1]);
233                                $this->template->set_var("current_label", $month." / ".$year);
234                                $this->template->parse('events_box','bar_block',true);
235                               
236                                for($i = 0; $i < 7; ++$i) {
237                                        $this->template->set_var("week_day", $this->shortDaysOfWeek[$i]);
238                                        $this->template->parse('week_day_box','calendar_header_block',true);
239                                }
240                                                               
241                                $this->template->parse('calendar_box','calendar_header_begin_block',true);
242                                $this->template->parse('calendar_box','calendar_day_begin_block',true);
243                               
244                                foreach($days_of_calendar as $index=>$value) {
245                                        $extra_class = "";
246                                        $calendar_day = $value["day"];
247
248                                        if($today === $value["day"]."-".$value["month"]."-".$value["year"])
249                                                $extra_class .= " hoje";
250
251                                        if($value["other_month"])
252                                                $extra_class .= " outro_mes";
253                                       
254                                        $key = $value["year"].$this->common->complete_string($value["month"],2,"R","0").$this->common->complete_string($value["day"],2,"R","0");
255                                       
256                                        if( array_key_exists($key, $events ) ) {
257                                                $this->template->set_var("qtd_commitment", sizeof($events[$key]));
258                                        } else {
259                                                $this->template->set_var("qtd_commitment", null);
260                                        }
261
262                                        $this->template->set_var("calendar_day", $calendar_day);
263                                       
264                                        $this->template->set_var("calendar_day_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=dia&dia=".$value["day"]."&mes=".$value["month"]."&ano=".$value["year"]);
265                                        $this->template->set_var("extra_class", $extra_class);                                 
266                                        $this->template->parse('calendar_box','calendar_day_block',true);
267                                       
268                                        if(($index+1)%7==0) {
269                                                $this->template->parse('calendar_box','calendar_day_end_block',true);
270                                                $this->template->parse('calendar_box','calendar_day_begin_block',true);
271                                        }
272                                }
273                                $this->template->parse('calendar_box','calendar_day_end_block',true);
274                                $this->template->parse('calendar_box','calendar_header_end_block',true);
275                       
276                        } else  {
277                                $tstart = mktime(0,0,0,$month,$day,$year);
278                                $tstart -= $GLOBALS['phpgw']->datetime->tz_offset;
279                                $tstop = $tstart + 86400; //(24horas*60min*60seg*1dia)
280                                $events = get_events($this, $accountId, $tstart, $tstop);
281                               
282                                $day = $this->common->complete_string($day,2,"R","0");
283                                $month = $this->common->complete_string($month,2,"R","0");
284                               
285                                //descobrind o primeiro dia da semana e o último
286                                $before_day = date("d-m-Y", strtotime("-1 day", mktime(0,0,0,$month,$day,$year) ) );
287                                $before_day = preg_split('/-/',$before_day);
288                                $next_day = date("d-m-Y", strtotime("+1 day", mktime(0,0,0,$month,$day,$year) ) );
289                                $next_day = preg_split('/-/',$next_day);                               
290                               
291                                //definindo a barra de navegação do calandário
292                                $this->template->set_var("before_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=dia&dia=".$before_day[0]."&mes=".$before_day[1]."&ano=".$before_day[2]);
293                                $this->template->set_var("current_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=dia&dia=".$day."&mes=".$month."&ano=".$year);
294                                $this->template->set_var("next_link", "index.php?menuaction=mobile.ui_mobilecalendar.index&type=dia&dia=".$next_day[0]."&mes=".$next_day[1]."&ano=".$next_day[2]);
295                                $this->template->set_var("current_label", $this->daysOfWeek[$GLOBALS['phpgw']->datetime->day_of_week($year,$month,$day)]." - ".$day."/".$month."/".$year);
296                                $this->template->parse('events_box','bar_block',true);                         
297
298                                if(!empty($events[$year.$month.$day])) {
299                                        print_events($this, array_slice($events[$year.$month.$day],0,$actual_max_results,true));
300                                        if(count($events[$year.$month.$day])>$actual_max_results)
301                                                        $show_more_button = true;
302                                }
303                                else {
304                                        $this->template->set_var("msg_no_event", lang("Dont have event that day"));
305                                        $this->template->parse('events_box','no_event_block',true);
306                                }
307                        }
308                        if($show_more_button)
309                                $this->template->set_var('show_more','block');
310                        else
311                                $this->template->set_var('show_more','none');
312                        $GLOBALS['phpgw_info']['mobiletemplate']->set_content($this->template->fp('out','page'));
313                }
314        }
315       
316?>
Note: See TracBrowser for help on using the repository browser.