source: branches/2.2/calendar/inc/class.boholiday.inc.php @ 3914

Revision 3914, 12.4 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1671 - Feriados não são destacados na visão diária e mensal

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Holiday                                                     *
4  * http://www.egroupware.org                                                *
5  * Written by Mark Peters <skeeter@phpgroupware.org>                        *
6  * --------------------------------------------                             *
7  *  This program is free software; you can redistribute it and/or modify it *
8  *  under the terms of the GNU General Public License as published by the   *
9  *  Free Software Foundation; either version 2 of the License, or (at your  *
10  *  option) any later version.                                              *
11  \**************************************************************************/
12
13
14        class boholiday
15        {
16                var $public_functions = Array(
17                        'add'           => True,
18                        'delete_holiday'        => True,
19                        'delete_locale' => True,
20                        'accept_holiday'        => True,
21                       
22                        'read_entries'  => True,
23                        'read_entry'    => True,
24                        'add_entry'     => True,
25                        'update_entry'  => True
26                );
27
28                var $debug = False;
29                var $base_url = '/index.php';
30
31                var $ui;
32                var $so;
33                var $owner;
34                var $year;
35
36                var $id;
37                var $total;
38                var $start;
39                var $query;
40                var $sort;
41
42                var $locales = Array();
43                var $holidays;
44                var $cached_holidays;
45
46                function boholiday()
47                {
48                        $this->so = CreateObject('calendar.soholiday');
49
50                        $this->start  = (int)get_var('start',array('POST','GET'));
51                        $this->query  = get_var('query',array('POST','GET'));
52                        $this->sort   = get_var('sort',array('POST','GET'));
53                        $this->order  = get_var('order',array('POST','GET'));
54                        $this->id     = get_var('id',array('POST','GET'));
55                        $this->year   = get_var('year',array('POST','GET'),date('Y'));
56                        $this->locale = get_var('locale',array('POST','GET'));
57                        if ($this->locale)
58                        {
59                                $this->locales[] = $this->locale;
60                        }
61
62                        if($this->debug)
63                        {
64                                echo '<-- Locale = '.$this->locales[0].' -->'."\n";
65                        }
66
67                        $this->total = $this->so->holiday_total($this->locales[0],$this->query,$this->year);
68                }
69
70                /* Begin Calendar functions */
71                function read_entry($id=0)
72                {
73                        if($this->debug)
74                        {
75                                echo "BO : Reading Holiday ID : ".$id."<br>\n";
76                        }
77
78                        if(!$id)
79                        {
80                                if(!$this->id)
81                                {
82                                        return Array();
83                                }
84                                else
85                                {
86                                        $id = $this->id;
87                                }
88                        }
89
90                        return $this->so->read_holiday($id);
91                }
92
93                function delete_holiday($id=0)
94                {
95                        if(!$id)
96                        {
97                                if($this->id)
98                                {
99                                        $id = $this->id;
100                                }
101                        }
102
103                        $this->ui = CreateObject('calendar.uiholiday');
104                        if($id)
105                        {
106                                $this->so->delete_holiday($id);
107                                $this->ui->edit_locale();
108                        }
109                        else
110                        {
111                                $this->ui->admin();
112                        }
113                }
114
115                function delete_locale($locale='')
116                {
117                        if(!$locale)
118                        {
119                                if($this->locales[0])
120                                {
121                                        $locale = $this->locales[0];
122                                }
123                        }
124
125                        if($locale)
126                        {
127                                $this->so->delete_locale($locale);
128                        }
129                        $this->ui = CreateObject('calendar.uiholiday');
130                        $this->ui->admin();
131                }
132
133                function accept_holiday()
134                {
135                        $send_back_to = str_replace('submitlocale','holiday_admin',$_SERVER['HTTP_REFERER']);
136                        if(!@$this->locales[0])
137                        {
138                                Header('Location: '.$send_back_to);
139                        }
140
141                        $send_back_to = str_replace('&locale='.$this->locales[0],'',$send_back_to);
142                        $file = './holidays.'.$this->locales[0];
143                        if(!file_exists($file) && count($_POST['name']))
144                        {
145                                $c_holidays = count($_POST['name']);
146                                $fp = fopen($file,'w');
147                                for($i=0;$i<$c_holidays;$i++)
148                                {
149                                        fwrite($fp,$this->locales[0]."\t".$_POST['name'][$i]."\t".$_POST['day'][$i]."\t".$_POST['month'][$i]."\t".$_POST['occurence'][$i]."\t".$_POST['dow'][$i]."\t".$_POST['observance'][$i]."\n");
150                                }
151                                fclose($fp);
152                        }
153                        Header('Location: '.$send_back_to);
154                }
155
156                function get_holiday_list($locale='', $sort='', $order='', $query='', $total='', $year=0)
157                {
158                        $locale = ($locale?$locale:$this->locales[0]);
159                        $sort = ($sort?$sort:$this->sort);
160                        $order = ($order?$order:$this->order);
161                        $query = ($query?$query:$this->query);
162                        $year = ($$year?$$year:$this->year);
163                        return $this->so->read_holidays($locale,$query,$order,$year);
164                }
165
166                function get_locale_list($sort='', $order='', $query='')
167                {
168                        $sort = ($sort?$sort:$this->sort);
169                        $order = ($order?$order:$this->order);
170                        $query = ($query?$query:$this->query);
171                        return $this->so->get_locale_list($sort,$order,$query);
172                }
173
174                function prepare_read_holidays($year=0,$owner=0)
175                {
176                        $this->year = (isset($year) && $year > 0?$year:$GLOBALS['phpgw']->common->show_date(time() - $GLOBALS['phpgw']->datetime->tz_offset,'Y'));
177                        $this->owner = ($owner?$owner:$GLOBALS['phpgw_info']['user']['account_id']);
178
179                        if($this->debug)
180                        {
181                                echo 'Setting Year to : '.$this->year.'<br>'."\n";
182                        }
183
184                        if(@$GLOBALS['phpgw_info']['user']['preferences']['common']['country'])
185                        {
186                                $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['common']['country'];
187                        }
188                        elseif(@$GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale'])
189                        {
190                                $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale'];
191                        }
192                        else
193                        {
194                                $this->locales[] = 'US';
195                        }
196
197                        if($this->owner != $GLOBALS['phpgw_info']['user']['account_id'])
198                        {
199                                $owner_pref = CreateObject('phpgwapi.preferences',$owner);
200                                $owner_prefs = $owner_pref->read_repository();
201                                if(@$owner_prefs['common']['country'])
202                                {
203                                        $this->locales[] = $owner_prefs['common']['country'];
204                                }
205                                elseif(@$owner_prefs['calendar']['locale'])
206                                {
207                                        $this->locales[] = $owner_prefs['calendar']['locale'];
208                                }
209                                unset($owner_pref);
210                        }
211
212                        @reset($this->locales);
213                        if($GLOBALS['phpgw_info']['server']['auto_load_holidays'] == True)
214                        {
215                                while(list($key,$value) = each($this->locales))
216                                {
217                                        $this->auto_load_holidays($value);
218                                }
219                        }
220                }
221
222                function auto_load_holidays($locale)
223                {
224                        if($this->so->holiday_total($locale) == 0)
225                        {
226                                @set_time_limit(0);
227
228                                /* get the file that contains the calendar events for your locale */
229                                /* "http://www.egroupware.org/cal/holidays.US.csv";                 */
230                                $network = CreateObject('phpgwapi.network');
231                                if(isset($GLOBALS['phpgw_info']['server']['holidays_url_path']) && $GLOBALS['phpgw_info']['server']['holidays_url_path'] != 'localhost')
232                                {
233                                        $load_from = $GLOBALS['phpgw_info']['server']['holidays_url_path'];
234                                }
235                                else
236                                {
237                                        $pos = strpos(' '.$GLOBALS['phpgw_info']['server']['webserver_url'],$_SERVER['HTTP_HOST']);
238                                        if($pos == 0)
239                                        {
240                                                switch($_SERVER['SERVER_PORT'])
241                                                {
242                                                        case 80:
243                                                                $http_protocol = 'http://';
244                                                                break;
245                                                        case 443:
246                                                                $http_protocol = 'https://';
247                                                                break;
248                                                }
249                                                $server_host = $http_protocol.$_SERVER['HTTP_HOST'].$GLOBALS['phpgw_info']['server']['webserver_url'];
250                                        }
251                                        else
252                                        {
253                                                $server_host = $GLOBALS['phpgw_info']['server']['webserver_url'];
254                                        }
255                                        $load_from = $server_host.'/calendar/egroupware.org';
256                                }
257//                              echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale).'.csv'."<br>\n";
258                                if(isset($GLOBALS['phpgw_info']['server']['holidays_url_path']) && $GLOBALS['phpgw_info']['server']['holidays_url_path'] != 'localhost')
259                                {
260                                    $lines = $network->gethttpsocketfile($load_from.'/holidays.'.strtoupper($locale).'.csv');
261                                }
262                                else
263                                {
264                                    $lines = explode(chr(0x0A),file_get_contents($load_from.'/holidays.'.strtoupper($locale).'.csv'));
265                                }
266                                if (!$lines)
267                                {
268                                        return false;
269                                }
270                                $c_lines = count($lines);
271                                for($i=0;$i<$c_lines;$i++)
272                                {
273//                                      echo 'Line #'.$i.' : '.$lines[$i]."<br>\n";
274                                        $holiday = explode("\t",$lines[$i]);
275                                        if(count($holiday) == 7)
276                                        {
277                                                $holiday['locale'] = $holiday[0];
278                                                $holiday['name'] = $GLOBALS['phpgw']->db->db_addslashes($holiday[1]);
279                                                $holiday['mday'] = (int)$holiday[2];
280                                                $holiday['month_num'] = (int)$holiday[3];
281                                                $holiday['occurence'] = (int)$holiday[4];
282                                                $holiday['dow'] = (int)$holiday[5];
283                                                $holiday['observance_rule'] = (int)$holiday[6];
284                                                $holiday['hol_id'] = 0;
285                                                $this->so->save_holiday($holiday);
286                                        }
287                                }
288                        }
289                }
290
291                function save_holiday($holiday)
292                {
293                        $this->so->save_holiday($holiday);
294                }
295
296                function add()
297                {
298                        if(@$_POST['submit'])
299                        {
300                                $holiday = $_POST['holiday'];
301
302                                if(empty($holiday['mday']))
303                                {
304                                        $holiday['mday'] = 0;
305                                }
306                                if(!isset($this->bo->locales[0]) || $this->bo->locales[0]=='')
307                                {
308                                        $this->bo->locales[0] = $holiday['locale'];
309                                }
310                                elseif(!isset($holiday['locale']) || $holiday['locale']=='')
311                                {
312                                        $holiday['locale'] = $this->bo->locales[0];
313                                }
314                                if(!isset($holiday['hol_id']))
315                                {
316                                        $holiday['hol_id'] = $this->bo->id;
317                                }
318
319                                // some input validation
320
321                                if (!$holiday['mday'] == !$holiday['occurence'])
322                                {
323                                        $errors[] = lang('You need to set either a day or a occurence !!!');
324                                }
325                                if($holiday['year'] && $holiday['occurence'])
326                                {
327                                        $errors[] = lang('You can only set a year or a occurence !!!');
328                                }
329                                else
330                                {
331                                        $holiday['occurence'] = (int)($holiday['occurence'] ? $holiday['occurence'] : $holiday['year']);
332                                        unset($holiday['year']);
333                                }
334
335        // Still need to put some validation in here.....
336
337                                $this->ui = CreateObject('calendar.uiholiday');
338
339                                if (is_array($errors))
340                                {
341                                        $holiday['month'] = $holiday['month_num'];
342                                        $holiday['day']   = $holiday['mday'];
343                                        $this->ui->edit_holiday($errors,$holiday);
344                                }
345                                else
346                                {
347                                        $this->so->save_holiday($holiday);
348                                        $this->ui->edit_locale($holiday['locale']);
349                                }
350                        }
351                }
352
353                function sort_holidays_by_date($holidays)
354                {
355                        $c_holidays = count($holidays);
356                        for($outer_loop=0;$outer_loop<($c_holidays - 1);$outer_loop++)
357                        {
358                                for($inner_loop=$outer_loop;$inner_loop<$c_holidays;$inner_loop++)
359                                {
360                                        if($holidays[$outer_loop]['date'] > $holidays[$inner_loop]['date'])
361                                        {
362                                                $temp = $holidays[$inner_loop];
363                                                $holidays[$inner_loop] = $holidays[$outer_loop];
364                                                $holidays[$outer_loop] = $temp;
365                                        }
366                                }
367                        }
368                        return $holidays;
369                }
370
371                function set_holidays_to_date($holidays)
372                {
373                        $new_holidays = Array();
374                        for($i=0;$i<count($holidays);$i++)
375                        {
376//      echo "Setting Holidays Date : ".date('Ymd',$holidays[$i]['date'])."<br>\n";
377                                $new_holidays[date('Ymd',$holidays[$i]['date'])][] = $holidays[$i];
378                        }
379                        return $new_holidays;
380                }
381
382                function read_holiday()
383                {
384                        if(isset($this->cached_holidays))
385                        {
386                                return $this->cached_holidays;
387                        }
388                        $holidays = $this->so->read_holidays($this->locales,'','',$this->year);
389
390                        if(count($holidays) == 0)
391                        {
392                                return $holidays;
393                        }
394
395                        $temp_locale = $GLOBALS['phpgw_info']['user']['preferences']['common']['country'];
396                        for($i=0;$i<count($holidays);$i++)
397                        {
398                                $c = $i;
399                                if($i == 0 || $holidays[$i]['locale'] != $holidays[$i - 1]['locale'])
400                                {
401                                        if(is_object($holidaycalc))
402                                        {
403                                                unset($holidaycalc);
404                                        }
405                                        $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $holidays[$i]['locale'];
406                                        $holidaycalc = CreateObject('calendar.holidaycalc');
407                                }
408                                $holidays[$i]['date'] = $holidaycalc->calculate_date($holidays[$i], $holidays, $this->year, $c);
409                                if($c != $i)
410                                {
411                                        $i = $c;
412                                }
413                        }
414                        unset($holidaycalc);
415                        $this->holidays = $this->sort_holidays_by_date($holidays);
416                        $this->cached_holidays = $this->set_holidays_to_date($this->holidays);
417                        $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $temp_locale;
418                        return $this->cached_holidays;
419                }
420                /* End Calendar functions */
421
422                function check_admin()
423                {
424                        if(!@$GLOBALS['phpgw_info']['user']['apps']['admin'])
425                        {
426                                Header('Location: ' . $GLOBALS['phpgw']->link('/index.php'));
427                        }
428                }
429
430                function rule_string($holiday)
431                {
432                        if (!is_array($holiday))
433                        {
434                                return false;
435                        }
436                        $sbox = CreateObject('phpgwapi.sbox');
437                        $month = $holiday['month'] ? lang($sbox->monthnames[$holiday['month']]) : '';
438                        unset($sbox);
439
440                        if (!$holiday['day'])
441                        {
442                                $occ = $holiday['occurence'] == 99 ? lang('last') : $holiday['occurence'].'.';
443
444                                $dow_str = Array(lang('Sun'),lang('Mon'),lang('Tue'),lang('Wed'),lang('Thu'),lang('Fri'),lang('Sat'));
445                                $dow = $dow_str[$holiday['dow']];
446                               
447                                $str = lang('%1 %2 in %3',$occ,$dow,$month);
448                        }
449                        else
450                        {
451                                $str = $GLOBALS['phpgw']->common->dateformatorder($holiday['occurence']>1900?$holiday['occurence']:'',$month,$holiday[day]);
452                        }
453                        if ($holiday['observance_rule'])
454                        {
455                                $str .= ' ('.lang('Observance Rule').')';
456                        }
457                        return $str;
458                }
459        }
460?>
Note: See TracBrowser for help on using the repository browser.