source: trunk/calendar/inc/class.socalendar_sql.inc.php @ 5132

Revision 5132, 36.7 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo calendar.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Calendar                                                    *
4  * http://www.eGroupWare.org                                                *
5  * Maintained and further developed by RalfBecker@outdoor-training.de       *
6  * Based on Webcalendar by Craig Knudsen <cknudsen@radix.net>               *
7  *          http://www.radix.net/~cknudsen                                  *
8  * Originaly modified by Mark Peters <skeeter@phpgroupware.org>             *
9  * --------------------------------------------                             *
10  *  This program is free software; you can redistribute it and/or modify it *
11  *  under the terms of the GNU General Public License as published by the   *
12  *  Free Software Foundation; either version 2 of the License, or (at your  *
13  *  option) any later version.                                              *
14  \**************************************************************************/
15if($_SESSION['isCrun']){
16        $_SESSION['isCrun'] = false;
17        $_SESSION['rootPath'] = PHPGW_INCLUDE_ROOT;
18        require_once $_SESSION['rootPath'].'/API/class.servicelocator.php';
19}else{
20        require_once $_SESSION['rootPath'].'/API/class.servicelocator.php';
21}
22
23        if (@$GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_'])
24        {
25                return;
26        }
27
28        $GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_'] = True;
29
30        class socalendar_ extends socalendar__
31        {
32                var $deleted_events = Array();
33
34                var $cal_event;
35                var $today = Array('raw','day','month','year','full','dow','dm','bd');
36
37
38                function socalendar_()
39                {
40                        $this->socalendar__();
41
42                        if (!is_object($GLOBALS['phpgw']->asyncservice))
43                        {
44                                $GLOBALS['phpgw']->asyncservice = CreateObject('phpgwapi.asyncservice');
45                        }
46                        $this->async = &$GLOBALS['phpgw']->asyncservice;
47                }
48
49                function open($calendar='',$user='',$passwd='',$options='')
50                {
51                        if($user=='')
52                        {
53        //                      settype($user,'integer');
54                                $this->user = $GLOBALS['phpgw_info']['user']['account_id'];
55                        }
56                        elseif(is_int($user))
57                        {
58                                $this->user = $user;
59                        }
60                        elseif(is_string($user))
61                        {
62                                $this->user = $GLOBALS['phpgw']->accounts->name2id($user);
63                        }
64
65                        $this->stream = $GLOBALS['phpgw']->db;
66                        return $this->stream;
67                }
68
69                function popen($calendar='',$user='',$passwd='',$options='')
70                {
71                        return $this->open($calendar,$user,$passwd,$options);
72                }
73
74                function reopen($calendar,$options='')
75                {
76                        return $this->stream;
77                }
78
79                function close($options='')
80                {
81                        return True;
82                }
83
84                function create_calendar($calendar='')
85                {
86                        return $calendar;
87                }
88
89                function rename_calendar($old_name='',$new_name='')
90                {
91                        return $new_name;
92                }
93
94                function delete_calendar($calendar='')
95                {
96                        $this->stream->query('SELECT cal_id FROM phpgw_cal WHERE owner='.(int)$calendar,__LINE__,__FILE__);
97                        if($this->stream->num_rows())
98                        {
99                                while($this->stream->next_record())
100                                {
101                                        $this->delete_event((int)$this->stream->f('cal_id'));
102                                }
103                                $this->expunge();
104                        }
105                        $this->stream->lock(array('phpgw_cal_user'));
106                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_login='.(int)$calendar,__LINE__,__FILE__);
107                        $this->stream->unlock();
108
109                        return $calendar;
110                }
111
112                /*!
113                @function read_alarms
114                @abstract read the alarms of a calendar-event specified by $cal_id
115                @returns array of alarms with alarm-id as key
116                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too
117                */
118                function read_alarms($cal_id)
119                {
120                        $alarms = array();
121
122                        if ($jobs = $this->async->read('cal:'.(int)$cal_id.':%'))
123                        {
124                                foreach($jobs as $id => $job)
125                                {
126                                        $alarm         = $job['data'];  // text, enabled
127                                        $alarm['id']   = $id;
128                                        $alarm['time'] = $job['next'];
129
130                                        $alarms[$id] = $alarm;
131                                }
132                        }
133                        return $alarms;
134                }
135
136                /*!
137                @function read_alarm
138                @abstract read a single alarm specified by it's $id
139                @returns array with data of the alarm
140                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too
141                */
142                function read_alarm($id)
143                {
144                        if (!($jobs = $this->async->read($id)))
145                        {
146                                return False;
147                        }
148                        list($id,$job) = each($jobs);
149                        $alarm         = $job['data'];  // text, enabled
150                        $alarm['id']   = $id;
151                        $alarm['time'] = $job['next'];
152
153                        //echo "<p>read_alarm('$id')="; print_r($alarm); echo "</p>\n";
154                        return $alarm;
155                }
156
157/* Funcao save_alarm modificada para gerar alarmes repetidos, caso seja marcado um evento igualmente repetido. A funcao recebe qual o tipo
158de repeticao escolhido pelo usuario (diario, semanal, mensal, anual) e insere alarmes nas respectivas repeticoes do evento. */
159                /*!
160                @function save_alarm
161                @abstract saves a new or updated alarm
162                @syntax save_alarm($cal_id,$alarm,$id=False)
163                @param $cal_id Id of the calendar-entry
164                @param $alarm array with fields: text, owner, enabled, ..
165                @returns the id of the alarm
166                */
167                function save_alarm($cal_id,$alarm)
168                {
169
170                        if (!($id = $alarm['id']))
171                        {
172                                $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
173                                $alarms = $this->read_alarms($cal_id);  // find a free alarm#
174
175                                if($alarm['repeat'] == 1) // repeticao do tipo "Diariamente";
176                                {
177                                        $n = 0;
178
179                                        while(@isset($alarms[$id]));
180                                        {
181
182                                                $init_alarm = $alarm['init_rept'];
183                                                $end_alarm = $alarm['end_rept'];
184
185                                                if($end_alarm != 0)
186                                                {
187                                                        while($init_alarm <= $end_alarm)
188                                                        {
189                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
190                                                                $n++;
191                                                               
192                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
193
194                                                                unset($alarm['repeat']);
195                                                                unset($alarm['init_rept']);
196                                                                unset($alarm['end_rept']);
197                                                                unset($alarm['rpt_wdays']);
198
199                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
200                                                                {
201                                                                        return False;
202                                                                }
203
204                                                                $alarm['time'] += 86400;
205                                                                $init_alarm += 86400;
206                                                        }
207                                                }
208                                        }
209
210                                }elseif($alarm['repeat'] == 2) { // repeticao do tipo "Semanalmente";
211
212                                        $n = 0;
213
214                                        $init_alarm = $data_atual = $alarm['init_rept'];
215                                        $end_alarm = $alarm['end_rept'];
216
217                                        $rpt_alarm_wdays = $alarm['rpt_wdays'];
218
219                                        $divisor = 64;
220                                        $quociente = 0;
221                                        $resto = 0;
222
223                                        $dia_semana = date("w",$init_alarm);
224
225                                        switch($dia_semana)
226                                        {
227                                                case 0:
228                                                        $dia = array(
229                                                                        0 => 'domingo',
230                                                                        1 => 'segunda',
231                                                                        2 => 'terca',
232                                                                        3 => 'quarta',
233                                                                        4 => 'quinta',
234                                                                        5 => 'sexta',
235                                                                        6 => 'sabado'
236                                                        );
237                                                        break;
238                                                case 1:
239                                                        $dia = array(
240                                                                        0 => 'segunda',
241                                                                        1 => 'terca',
242                                                                        2 => 'quarta',
243                                                                        3 => 'quinta',
244                                                                        4 => 'sexta',
245                                                                        5 => 'sabado',
246                                                                        6 => 'domingo'
247                                                        );
248                                                        break;
249                                                case 2:
250                                                        $dia = array(
251                                                                        0 => 'terca',
252                                                                        1 => 'quarta',
253                                                                        2 => 'quinta',
254                                                                        3 => 'sexta',
255                                                                        4 => 'sabado',
256                                                                        5 => 'domingo',
257                                                                        6 => 'segunda'
258                                                        );
259                                                        break;
260                                                case 3:
261                                                        $dia = array(
262                                                                        0 => 'quarta',
263                                                                        1 => 'quinta',
264                                                                        2 => 'sexta',
265                                                                        3 => 'sabado',
266                                                                        4 => 'domingo',
267                                                                        5 => 'segunda',
268                                                                        6 => 'terca'
269                                                        );
270                                                        break;
271                                                case 4:
272                                                        $dia = array(
273                                                                        0 => 'quinta',
274                                                                        1 => 'sexta',
275                                                                        2 => 'sabado',
276                                                                        3 => 'domingo',
277                                                                        4 => 'segunda',
278                                                                        5 => 'terca',
279                                                                        6 => 'quarta'
280                                                        );
281                                                        break;
282                                                case 5:
283                                                        $dia = array(
284                                                                        0 => 'sexta',
285                                                                        1 => 'sabado',
286                                                                        2 => 'domingo',
287                                                                        3 => 'segunda',
288                                                                        4 => 'terca',
289                                                                        5 => 'quarta',
290                                                                        6 => 'quinta'
291                                                        );
292                                                        break;
293                                                case 6:
294                                                        $dia = array(
295                                                                        0 => 'sabado',
296                                                                        1 => 'domingo',
297                                                                        2 => 'segunda',
298                                                                        3 => 'terca',
299                                                                        4 => 'quarta',
300                                                                        5 => 'quinta',
301                                                                        6 => 'sexta'
302                                                        );
303                                                        break;
304                                        }
305
306
307                                        $dias_semana = array(
308                                                                64 => 'sabado',
309                                                                32 => 'sexta',
310                                                                16 => 'quinta',
311                                                                8 => 'quarta',
312                                                                4 => 'terca',
313                                                                2 => 'segunda',
314                                                                1 => 'domingo'
315                                                        );
316
317                                        $result = array();
318                                        do
319                                        {
320
321                                                $resto = ($rpt_alarm_wdays % $divisor);
322                                                $quociente = floor($rpt_alarm_wdays / $divisor);
323
324                                                if($quociente == 1)
325                                                {
326                                                        $result[] = $dias_semana[$divisor];
327
328                                                        $divisor = $divisor / 2;
329                                                        $rpt_alarm_wdays = $resto;
330
331                                                }else {
332
333                                                        while($rpt_alarm_wdays < $divisor)
334                                                        {
335                                                                $divisor = $divisor / 2;
336                                                        }
337
338                                                        $resto = ($rpt_alarm_wdays % $divisor);
339                                                        $quociente = floor($rpt_alarm_wdays / $divisor);
340
341                                                        if($quociente == 1)
342                                                        {
343
344                                                                $result[] = $dias_semana[$divisor];
345
346                                                                $divisor = $divisor / 2;
347                                                                $rpt_alarm_wdays = $resto;
348                                                        }
349                                                }
350
351                                        }
352                                        while($resto != 0);
353
354                                        krsort($result);
355
356                                        $week_num = 0;
357                                        $y = 0;
358
359                                        while(@isset($alarms[$id]));
360                                        {
361
362                                                if($end_alarm != 0)
363                                                {
364
365                                                        while($data_atual <= $end_alarm)
366                                                        {
367
368                                                                foreach($dia as $index => $value)
369                                                                {
370                                                                        if(in_array($value,$result))
371                                                                        {
372
373                                                                                $nova_data = $init_alarm + (86400 * ($index + (7 * $y)));
374
375                                                                                if($nova_data == $init_alarm){
376
377                                                                                        continue;
378                                                                                }
379
380                                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
381                                                                                $n++;
382
383                                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
384
385                                                                                unset($alarm['repeat']);
386                                                                                unset($alarm['init_rept']);
387                                                                                unset($alarm['end_rept']);
388                                                                                unset($alarm['rpt_wdays']);
389                                                                               
390                                                                                $data_atual = $nova_data;
391
392                                                                                if($data_atual > $end_alarm){
393                                                                                        if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
394                                                                                        {
395                                                                                                return False;
396                                                                                        }
397                                                                                        break;
398                                                                                }
399
400                                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
401                                                                                {
402                                                                                        return False;
403                                                                                }
404                                                                                        $alarm['time'] = $nova_data - $alarm['offset'] - $GLOBALS['phpgw']->datetime->tz_offset;
405                                                                        }
406                                                                }
407                                                                $y++;
408                                                        }
409                                                }
410                                        }
411
412                                }elseif($alarm['repeat'] == 3) { // repeticao do tipo "Mensalmente (por data)";
413
414                                        $n = 0;
415
416                                        $init_alarm = $alarm['init_rept'];
417                                        $end_alarm = $alarm['end_rept'];
418                                       
419                                        while(@isset($alarms[$id]));
420                                        {
421
422                                                if($end_alarm != 0)
423                                                {
424                                                        while($init_alarm <= $end_alarm)
425                                                        {
426
427                                                                $next_month = date("n",$init_alarm) + 1;
428                                                                $next_alarm = mktime(date("G",$alarm['time']),date("i",$alarm['time']),date("s",$alarm['time']),$next_month,date("j",$alarm['time']),date("Y",$alarm['time']));
429
430                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
431                                                                $n++;
432
433                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
434
435                                                                unset($alarm['repeat']);
436                                                                unset($alarm['init_rept']);
437                                                                unset($alarm['end_rept']);
438                                                                unset($alarm['rpt_wdays']);
439
440                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
441                                                                {
442                                                                        return False;
443                                                                }
444
445                                                                $alarm['time'] = $next_alarm;
446                                                                $init_alarm = $next_alarm;
447
448                                                        }
449                                                }
450                                        }
451
452                                }elseif($alarm['repeat'] == 5) { // repeticao do tipo "Anualmente";
453
454                                        $n = 0;
455
456                                        $init_alarm = $alarm['init_rept'];
457                                        $end_alarm = $alarm['end_rept'];
458                                       
459                                        while(@isset($alarms[$id]));
460                                        {
461
462
463                                                if($end_alarm != 0)
464                                                {
465                                                        while($init_alarm < $end_alarm)
466                                                        {
467
468                                                                $next_year = date("Y",$init_alarm) + 1;
469                                                                $next_alarm = mktime(date("G",$alarm['time']),date("i",$alarm['time']),date("s",$alarm['time']),date("n",$alarm['time']),date("j",$alarm['time']),$next_year);
470
471                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
472                                                                $n++;
473
474                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
475
476                                                                unset($alarm['repeat']);
477                                                                unset($alarm['init_rept']);
478                                                                unset($alarm['end_rept']);
479                                                                unset($alarm['rpt_wdays']);
480
481                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
482                                                                {
483                                                                        return False;
484                                                                }
485
486                                                                $alarm['time'] = $next_alarm;
487                                                                $init_alarm = $next_alarm;
488                                                        }
489                                                }
490                                        }
491
492                                }else {
493                                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
494                                        $n = count($alarms);
495                                        while(@isset($alarms[$id]));
496                                        {
497
498                                                $id = 'cal:'.(int)$cal_id.':'.$n;
499
500                                                unset($alarm['repeat']);
501                                                unset($alarm['init_rept']);
502                                                unset($alarm['end_rept']);
503                                                unset($alarm['rpt_wdays']);
504
505                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
506                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
507                                                {
508                                                        return False;
509                                                }
510
511                                                ++$n;
512                                        }
513                                }
514
515                        }
516                        else
517                        {
518                                $this->async->cancel_timer($id);
519                        }
520                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
521                        return $id;
522                }
523
524                /*!
525                @function delete_alarms($cal_id)
526                @abstract delete all alarms of a calendar-entry
527                @returns the number of alarms deleted
528                */
529                function delete_alarms($cal_id)
530                {
531                        $alarms = $this->read_alarms($cal_id);
532
533                        foreach($alarms as $id => $alarm)
534                        {
535                                $this->async->cancel_timer($id);
536                        }
537                        return count($alarms);
538                }
539
540                /*!
541                @function delete_alarm($id)
542                @abstract delete one alarms identified by its id
543                @returns the number of alarms deleted
544                */
545                function delete_alarm($id)
546                {
547                        return $this->async->cancel_timer($id);
548                }
549
550                function fetch_event($event_id,$options='')
551                {
552                        if(!isset($this->stream))
553                        {
554                                return False;
555                        }
556
557                        $event_id = (int)$event_id;
558
559                        $this->stream->lock(array('phpgw_cal','phpgw_cal_user','phpgw_cal_repeats','phpgw_cal_extra'/* OLD-ALARM,'phpgw_cal_alarm'*/));
560
561                        $this->stream->query('SELECT * FROM phpgw_cal WHERE cal_id='.$event_id,__LINE__,__FILE__);
562
563                        if($this->stream->num_rows() > 0)
564                        {
565                                $this->event_init();
566
567                                $this->stream->next_record();
568                                // Load the calendar event data from the db into $event structure
569                                // Use http://www.php.net/manual/en/function.mcal-fetch-event.php as the reference
570                                $this->add_attribute('owner',(int)$this->stream->f('owner'));
571                                $this->add_attribute('id',(int)$this->stream->f('cal_id'));
572                                $this->add_attribute('type',$this->stream->f('cal_type'));
573                                $this->set_class((int)$this->stream->f('is_public'));
574                                $this->set_category($this->stream->f('category'));
575                                $this->set_title(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('title'))));
576                                $this->set_description(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('description'))));
577                                $this->set_ex_participants(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('ex_participants'))));
578                                $this->add_attribute('uid',$GLOBALS['phpgw']->strip_html($this->stream->f('uid')));
579                                $this->add_attribute('location',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('location'))));
580                                $this->add_attribute('organizer',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('organizer'))));
581                                $this->add_attribute('reference',(int)$this->stream->f('reference'));
582
583                                // This is the preferred method once everything is normalized...
584                                //$this->event->alarm = (int)$this->stream->f('alarm');
585                                // But until then, do it this way...
586                                //Legacy Support (New)
587
588                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('datetime'));
589                                $this->set_start($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
590
591                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('mdatetime'));
592                                $this->set_date('modtime',$datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
593
594                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('edatetime'));
595                                $this->set_end($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
596
597                        //Legacy Support
598                                $this->add_attribute('priority',(int)$this->stream->f('priority'));
599                                if($this->stream->f('cal_group') || $this->stream->f('groups') != 'NULL')
600                                {
601                                        $groups = explode(',',$this->stream->f('groups'));
602                                        for($j=1;$j<count($groups) - 1;$j++)
603                                        {
604                                                $this->add_attribute('groups',$groups[$j],$j-1);
605                                        }
606                                }
607
608                                $this->stream->query('SELECT * FROM phpgw_cal_repeats WHERE cal_id='.$event_id,__LINE__,__FILE__);
609                                if($this->stream->num_rows())
610                                {
611                                        $this->stream->next_record();
612
613                                        $this->add_attribute('recur_type',(int)$this->stream->f('recur_type'));
614                                        $this->add_attribute('recur_interval',(int)$this->stream->f('recur_interval'));
615                                        $enddate = $this->stream->f('recur_enddate');
616                                        if($enddate != 0 && $enddate != Null)
617                                        {
618                                                $datetime = $GLOBALS['phpgw']->datetime->localdates($enddate);
619                                                $this->add_attribute('recur_enddate',$datetime['year'],'year');
620                                                $this->add_attribute('recur_enddate',$datetime['month'],'month');
621                                                $this->add_attribute('recur_enddate',$datetime['day'],'mday');
622                                                $this->add_attribute('recur_enddate',$datetime['hour'],'hour');
623                                                $this->add_attribute('recur_enddate',$datetime['minute'],'min');
624                                                $this->add_attribute('recur_enddate',$datetime['second'],'sec');
625                                        }
626                                        else
627                                        {
628                                                $this->add_attribute('recur_enddate',0,'year');
629                                                $this->add_attribute('recur_enddate',0,'month');
630                                                $this->add_attribute('recur_enddate',0,'mday');
631                                                $this->add_attribute('recur_enddate',0,'hour');
632                                                $this->add_attribute('recur_enddate',0,'min');
633                                                $this->add_attribute('recur_enddate',0,'sec');
634                                        }
635                                        $this->add_attribute('recur_enddate',0,'alarm');
636                                        if($this->debug)
637                                        {
638                                                echo 'Event ID#'.$this->event['id'].' : Enddate = '.$enddate."<br>\n";
639                                        }
640                                        $this->add_attribute('recur_data',$this->stream->f('recur_data'));
641
642                                        $exception_list = $this->stream->f('recur_exception');
643                                        $exceptions = Array();
644                                        if(strpos(' '.$exception_list,','))
645                                        {
646                                                $exceptions = explode(',',$exception_list);
647                                        }
648                                        elseif($exception_list != '')
649                                        {
650                                                $exceptions[]= $exception_list;
651                                        }
652                                        $this->add_attribute('recur_exception',$exceptions);
653                                }
654
655                        //Legacy Support
656                                $this->stream->query('SELECT * FROM phpgw_cal_user WHERE cal_id='.$event_id,__LINE__,__FILE__);
657                                if($this->stream->num_rows())
658                                {
659                                        while($this->stream->next_record())
660                                        {
661                                                if((int)$this->stream->f('cal_login') == (int)$this->user)
662                                                {
663                                                        $this->add_attribute('users_status',$this->stream->f('cal_status'));
664                                                }
665                                                $this->add_attribute('participants',$this->stream->f('cal_status'),(int)$this->stream->f('cal_login'));
666                                        }
667                                }
668
669                        // Custom fields
670                                $this->stream->query('SELECT * FROM phpgw_cal_extra WHERE cal_id='.$event_id,__LINE__,__FILE__);
671                                if($this->stream->num_rows())
672                                {
673                                        while($this->stream->next_record())
674                                        {
675                                                $this->add_attribute('#'.$this->stream->f('cal_extra_name'),$this->stream->f('cal_extra_value'));
676                                        }
677                                }
678
679        /* OLD-ALARM
680                                if($this->event['reference'])
681                                {
682                                        // What is event['reference']???
683                                        $alarm_cal_id = $event_id.','.$this->event['reference'];
684                                }
685                                else
686                                {
687                                        $alarm_cal_id = $event_id;
688                                }
689
690                                //echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n";
691                                //$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__);
692                                $this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id='.$event_id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
693                                if($this->stream->num_rows())
694                                {
695                                        while($this->stream->next_record())
696                                        {
697                                                $this->event['alarm'][] = Array(
698                                                        'id'            => (int)$this->stream->f('alarm_id'),
699                                                        'time'  => (int)$this->stream->f('cal_time'),
700                                                        'text'  => $this->stream->f('cal_text'),
701                                                        'enabled'       => (int)$this->stream->f('alarm_enabled')
702                                                );
703                                        }
704                                }
705        */
706                        }
707                        else
708                        {
709                                $this->event = False;
710                        }
711
712                        $this->stream->unlock();
713
714                        if ($this->event)
715                        {
716                                $this->event['alarm'] = $this->read_alarms($event_id);
717
718                                if($this->event['reference'])
719                                {
720                                        $this->event['alarm'] += $this->read_alarms($event_id);
721                                }
722                        }
723                        return $this->event;
724                }
725
726                function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$extra='',$tz_offset=0,$owner_id=0)
727                {
728                        if(!isset($this->stream))
729                        {
730                                return False;
731                        }
732
733                        $datetime = mktime(0,0,0,$startMonth,$startDay,$startYear) - $tz_offset;
734
735                        $user_where = ' AND (phpgw_cal_user.cal_login in (';
736                        if(is_array($owner_id) && count($owner_id))
737                        {
738                                $user_where .= implode(',',$owner_id);
739                        }
740                        else
741                        {
742                                $user_where .= $this->user . ') OR (phpgw_cal.owner=' . $this->user;
743                        }
744                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
745                        @reset($member_groups);
746                        while($member_groups != False && list($key,$group_info) = each($member_groups))
747                        {
748                                $member[] = $group_info['account_id'];
749                        }
750                        @reset($member);
751        //              $user_where .= ','.implode(',',$member);
752                        $user_where .= ')) ';
753
754                        if($this->debug)
755                        {
756                                echo '<!-- '.$user_where.' -->'."\n";
757                        }
758
759                        $startDate = 'AND ( ( (phpgw_cal.datetime >= '.$datetime.') ';
760
761                        $enddate = '';
762                        if($endYear != 0 && $endMonth != 0 && $endDay != 0)
763                        {
764                                $edatetime = mktime(23,59,59,(int)$endMonth,(int)$endDay,(int)$endYear) - $tz_offset;
765                                $endDate .= 'AND (phpgw_cal.edatetime <= '.$edatetime.') ) '
766                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
767                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
768                                        . 'OR ( (phpgw_cal.datetime >= '.$datetime.') '
769                                        . 'AND (phpgw_cal.datetime <= '.$edatetime.') '
770                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
771                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
772                                        . 'AND (phpgw_cal.edatetime >= '.$datetime.') '
773                                        . 'AND (phpgw_cal.edatetime <= '.$edatetime.') ';
774                        }
775                        $endDate .= ') ) ';
776
777                        $order_by = 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC';
778                        if($this->debug)
779                        {
780                                echo "SQL : ".$user_where.$startDate.$endDate.$extra."<br>\n";
781                        }
782                        return $this->get_event_ids(False,$user_where.$startDate.$endDate.$extra.$order_by);
783                }
784
785                function append_event()
786                {
787                        $this->save_event($this->event);
788                        $this->send_update(MSG_ADDED,$this->event->participants,'',$this->event);
789                        return $this->event['id'];
790                }
791
792                function store_event()
793                {
794                        return $this->save_event(&$this->event);
795                }
796
797                function delete_event($event_id)
798                {
799                        $this->deleted_events[] = $event_id;
800                }
801
802                function snooze($event_id)
803                {
804                //Turn off an alarm for an event
805                //Returns true.
806                }
807
808                function list_alarms($begin_year='',$begin_month='',$begin_day='',$end_year='',$end_month='',$end_day='')
809                {
810                //Return a list of events that has an alarm triggered at the given datetime
811                //Returns an array of event ID's
812                }
813
814                // The function definition doesn't look correct...
815                // Need more information for this function
816                function next_recurrence($weekstart,$next)
817                {
818        //              return next_recurrence (int stream, int weekstart, array next);
819                }
820
821                function expunge()
822                {
823                        if(count($this->deleted_events) <= 0)
824                        {
825                                return 1;
826                        }
827                        $this_event = $this->event;
828                        $locks = Array(
829                                'phpgw_cal',
830                                'phpgw_cal_user',
831                                'phpgw_cal_repeats',
832                                'phpgw_cal_extra'
833        // OLD-ALARM                    'phpgw_cal_alarm'
834                        );
835                        $this->stream->lock($locks);
836                        foreach($this->deleted_events as $cal_id)
837                        {
838                                foreach ($locks as $table)
839                                {
840                                        $this->stream->query('DELETE FROM '.$table.' WHERE cal_id='.$cal_id,__LINE__,__FILE__);
841                                }
842                        }
843                        $this->stream->unlock();
844
845                        foreach($this->deleted_events as $cal_id)
846                        {
847                                $this->delete_alarms($cal_id);
848                        }
849                        $this->deleted_events = array();
850
851                        $this->event = $this_event;
852                        return 1;
853                }
854
855                /***************** Local functions for SQL based Calendar *****************/
856
857                function get_event_ids($search_repeats=False,$extra='',$search_extra=False)
858                {
859                        $from = $where = ' ';
860                        if($search_repeats)
861                        {
862                                $from  = ', phpgw_cal_repeats ';
863                                $where = 'AND (phpgw_cal_repeats.cal_id = phpgw_cal.cal_id) ';
864                        }
865                        if($search_extra)
866                        {
867                                $from  .= 'LEFT JOIN phpgw_cal_extra ON phpgw_cal_extra.cal_id = phpgw_cal.cal_id ';
868                        }
869
870                        $sql = 'SELECT DISTINCT phpgw_cal.cal_id,'
871                                        . 'phpgw_cal.datetime,phpgw_cal.edatetime,'
872                                        . 'phpgw_cal.priority '
873                                        . 'FROM phpgw_cal_user, phpgw_cal'
874                                        . $from
875                                        . 'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) '
876                                        . $where . $extra;
877
878                        if($this->debug)
879                        {
880                                echo "FULL SQL : ".$sql."<br>\n";
881                        }
882
883                        $this->stream->query($sql,__LINE__,__FILE__);
884
885                        $retval = Array();
886                        if($this->stream->num_rows() == 0)
887                        {
888                                if($this->debug)
889                                {
890                                        echo "No records found!<br>\n";
891                                }
892                                return $retval;
893                        }
894
895                        while($this->stream->next_record())
896                        {
897                                $retval[] = (int)$this->stream->f('cal_id');
898                        }
899                        if($this->debug)
900                        {
901                                echo "Records found!<br>\n";
902                        }
903                        return $retval;
904                }
905
906                function save_event(&$event,$sendMail = true, $importAccount = false)
907                {
908
909                        $GLOBALS['calendar']->bocalendar = CreateObject('calendar.bocalendar');
910
911                        $locks = Array(
912                                'phpgw_cal',
913                                'phpgw_cal_user',
914                                'phpgw_cal_repeats',
915                                'phpgw_cal_extra'
916        // OLD-ALARM                    'phpgw_cal_alarm'
917                        );
918                        $this->stream->lock($locks);
919                        if($event['id'] == 0)
920                        {
921                                if(!$event['uid'])
922                                {
923                                        if ($GLOBALS['phpgw_info']['server']['hostname'] != '')
924                                        {
925                                                $id_suffix = $GLOBALS['phpgw_info']['server']['hostname'];
926                                        }
927                                        else
928                                        {
929                                                $id_suffix = $GLOBALS['phpgw']->common->randomstring(3).'local';
930                                        }
931                                        $parts = Array(
932                                                0 => 'title',
933                                                1 => 'description',
934                                                2 => 'ex_participants'
935                                        );
936                                        @reset($parts);
937                                        while(list($key,$field) = each($parts))
938                                        {
939                                                $part[$key] = substr($GLOBALS['phpgw']->crypto->encrypt($event[$field]),0,20);
940                                                if(!$GLOBALS['phpgw']->crypto->enabled)
941                                                {
942                                                        $part[$key] = bin2hex(unserialize($part[$key]));
943                                                }
944                                        }
945                                        $event['uid'] = $part[0].'-'.$part[1].'@'.$id_suffix;
946                                }
947                                $this->stream->query('INSERT INTO phpgw_cal(uid,title,owner,priority,is_public,category) '
948                                        . "values('".$event['uid']."','".$this->stream->db_addslashes($event['title'])
949                                        . "',".(int)$event['owner'].','.(int)$event['priority'].','.(int)$event['public'].",'"
950                                        . $event['category']."')",__LINE__,__FILE__);
951                                $event['id'] = $this->stream->get_last_insert_id('phpgw_cal','cal_id');
952                                $last_status = true;
953                        }
954
955                        $date = $this->maketime($event['start']) - $GLOBALS['phpgw']->datetime->tz_offset;
956                        $enddate = $this->maketime($event['end']) - $GLOBALS['phpgw']->datetime->tz_offset;
957                        $today = time() - $GLOBALS['phpgw']->datetime->tz_offset;
958
959
960                                if ($event['type'] == 'hourAppointment')
961                                        $type = 'H';
962                                else if($event['type'] == 'privateHiddenFields'){
963                                        $type = 'P';
964                                }else
965                                        $type = 'E';
966                       
967
968                        $sql = 'UPDATE phpgw_cal SET '
969                                        . 'owner='.(int)$event['owner'].', '
970                                        . 'datetime='.(int)$date.', '
971                                        . 'mdatetime='.(int)$today.', '
972                                        . 'edatetime='.(int)$enddate.', '
973                                        . 'priority='.(int)$event['priority'].', '
974                                        . "category='".$this->stream->db_addslashes($event['category'])."', "
975                                        . "cal_type='".$this->stream->db_addslashes($type)."', "
976                                        . 'is_public='.(int)$event['public'].', '
977                                        . "title='".$this->stream->db_addslashes($event['title'])."', "
978                                        . "description='".$this->stream->db_addslashes($event['description'])."', "
979                                        . "ex_participants='".$this->stream->db_addslashes($event['ex_participants'])."', "
980                                        . "organizer='".$this->stream->db_addslashes($event['organizer'])."', "
981                                        . "location='".$this->stream->db_addslashes($event['location'])."', "
982                                        . ($event['groups']?"groups='".(count($event['groups'])>1?implode(',',$event['groups']):','.$event['groups'][0].',')."', ":'')
983                                        . 'reference='.(int)$event['reference'].' '
984                                        . ',last_status = '.($last_status ? "'N'" : "'U'").',last_update = '.time()."000". ' '
985                                        . 'WHERE cal_id='.(int)$event['id'];
986
987                        $this->stream->query($sql,__LINE__,__FILE__);
988
989                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
990
991                        @reset($event['participants']);
992                        while (list($key,$value) = @each($event['participants']))
993                        {
994                                if((int)$key == $event['owner'])
995                                {
996                                        $value = 'A';
997                                }
998                                $this->stream->query('INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) '
999                                        . 'VALUES('.(int)$event['id'].','.(int)$key.",'".$this->stream->db_addslashes($value)."')",__LINE__,__FILE__);
1000                        }
1001
1002                        if($event['recur_type'] != MCAL_RECUR_NONE)
1003                        {
1004                                if($event['recur_enddate']['month'] != 0 && $event['recur_enddate']['mday'] != 0 && $event['recur_enddate']['year'] != 0)
1005                                {
1006                                        $end = $this->maketime($event['recur_enddate']) - $GLOBALS['phpgw']->datetime->tz_offset;
1007                                }
1008                                else
1009                                {
1010                                        $end = 0;
1011                                }
1012
1013                                $this->stream->query('SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
1014                                $this->stream->next_record();
1015                                $num_rows = $this->stream->f(0);
1016                                if($num_rows == 0)
1017                                {
1018                                        $this->stream->query('INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) '
1019                                                .'VALUES('.(int)$event['id'].','.$event['recur_type'].','.(int)$end.','.$event['recur_data'].','.$event['recur_interval'].')',__LINE__,__FILE__);
1020                                }
1021                                else
1022                                {
1023                                        $this->stream->query('UPDATE phpgw_cal_repeats '
1024                                                . 'SET recur_type='.$event['recur_type'].', '
1025                                                . 'recur_enddate='.(int)$end.', '
1026                                                . 'recur_data='.$event['recur_data'].', '
1027                                                . 'recur_interval='.$event['recur_interval'].', '
1028                                                . "recur_exception='".(count($event['recur_exception'])>1?implode(',',$event['recur_exception']):(count($event['recur_exception'])==1?$event['recur_exception'][0]:''))."' "
1029                                                . 'WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1030                                }
1031
1032                        }
1033                        else
1034                        {
1035                                $this->stream->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1036                        }
1037                        // Custom fields
1038                        $this->stream->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1039
1040                        foreach($event as $name => $value)
1041                        {
1042                                if ($name[0] == '#' && strlen($value))
1043                                {
1044                                        $this->stream->query('INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) '
1045                                        . 'VALUES('.$event['id'].",'".addslashes(substr($name,1))."','".addslashes($value)."')",__LINE__,__FILE__);
1046                                }
1047                        }
1048        /*
1049                        $alarmcount = count($event['alarm']);
1050                        if ($alarmcount > 1)
1051                        {
1052                                // this should never happen, $event['alarm'] should only be set
1053                                // if creating a new event and uicalendar only sets up 1 alarm
1054                                // the user must use "Alarm Management" to create/establish multiple
1055                                // alarms or to edit/change an alarm
1056                                echo '<!-- how did this happen, too many alarms -->'."\n";
1057                                $this->stream->unlock();
1058                                return True;
1059                        }
1060
1061                        if ($alarmcount == 1)
1062                        {
1063
1064                                list($key,$alarm) = @each($event['alarm']);
1065
1066                                $this->stream->query('INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES('.$event['id'].','.$event['owner'].','.$alarm['time'].",'".$alarm['text']."',".$alarm['enabled'].')',__LINE__,__FILE__);
1067                                $this->stream->query('SELECT LAST_INSERT_ID()');
1068                                $this->stream->next_record();
1069                                $alarm['id'] = $this->stream->f(0);
1070                        }
1071        */
1072                        print_debug('Event Saved: ID #',$event['id']);
1073
1074                        $this->stream->unlock();
1075
1076                        if (is_array($event['alarm']))
1077                        {
1078                                foreach ($event['alarm'] as $alarm)     // this are all new alarms
1079                                {
1080                                        $this->save_alarm($event['id'],$alarm);
1081                                }
1082                        }
1083                        $GLOBALS['phpgw_info']['cal_new_event_id'] = $event['id'];
1084                        $this->event = $event;
1085
1086                        if($sendMail)
1087                        {
1088                            if($importAccount)
1089                                $this->sendEmailEvendAccepted($event,$importAccount);
1090                            else
1091                                $this->sendEmailEvendAccepted($event);
1092                        }
1093       
1094                        return True;
1095                }
1096
1097                function sendEmailEvendAccepted($event,$importAccount = false)
1098                {
1099
1100                    if(!$importAccount)
1101                        $importAccount['mail'] = $GLOBALS['phpgw_info']['user']['email'];
1102
1103                    //verifica se eh um evento externo se sim envia o vcard de aceitação em anexo
1104                    if($event['owner'] == '-2')
1105                    {
1106                        $mailService = ServiceLocator::getService('mail');
1107
1108                        $tmpbody.= "<b>".lang("Event Accepted")."</b> ";
1109                        $tmpbody.= "<br><br>".lang("name").": ".$event['title']."<br>";
1110                        $tmpbody.= "<br>".lang("description").": ".$event['description']."<br>";
1111
1112                        $ev[0] = $event;
1113
1114                        $tmpattach=  $GLOBALS['calendar']->bocalendar->create_vcard($ev,'REPLY',true,$importAccount);
1115                       
1116                        $mailService->addStringAttachment($tmpattach, 'suggestion.ics', 'application/ics');
1117                        $mailService->addStringAttachment($tmpattach, 'vcalendar.ics', 'utf-8', 'text/calendar; charset=utf-8; method=REPLY;', '7bit', '');
1118                 
1119                        if(strchr($event['organizer'],'@') && strchr($event['organizer'],'<') && strchr($event['organizer'],'>'))
1120                        {
1121                            $mail = substr($event['organizer'], strpos($event['organizer'],'<'), strpos($event['organizer'],'>'));
1122                            $mail = str_replace('<','', str_replace('>','',$mail));
1123                        }
1124
1125                        if($mail)
1126                            $mailService->sendMail($mail,$importAccount['mail'],$event['title'],$tmpbody);
1127
1128                    }
1129                    else
1130                        $GLOBALS['calendar']->bocalendar->send_update(MSG_ACCEPTED, $event['participants'],$event);
1131
1132                    return;
1133                }
1134
1135                function get_alarm($cal_id)
1136                {
1137        /* OLD-ALARM
1138                        $this->stream->query('SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id='.$id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
1139                        if($this->stream->num_rows())
1140                        {
1141                                while($this->stream->next_record())
1142                                {
1143                                        $alarm[$this->stream->f('cal_time')] = $this->stream->f('cal_text');
1144                                }
1145                                @reset($alarm);
1146                                return $alarm;
1147                        }
1148                        else
1149                        {
1150                                return False;
1151                        }
1152        */
1153                        $alarms = $this->read_alarms($cal_id);
1154                        $ret = False;
1155
1156                        foreach($alarms as $alarm)
1157                        {
1158                                if ($alarm['owner'] == $this->user || !$alarm['owner'])
1159                                {
1160                                        $ret[$alarm['time']] = $alarm['text'];
1161                                }
1162                        }
1163                        return $ret;
1164                }
1165
1166                function set_status($id,$owner,$status)
1167                {
1168                        $status_code_short = Array(
1169                                REJECTED =>     'R',
1170                                NO_RESPONSE     => 'U',
1171                                TENTATIVE       =>      'T',
1172                                ACCEPTED        =>      'A'
1173                        );
1174
1175                        $this->stream->query("UPDATE phpgw_cal_user SET cal_status='".$status_code_short[$status]."' WHERE cal_id=".$id." AND cal_login=".$owner,__LINE__,__FILE__);
1176        /* OLD-ALARM
1177                        if ($status == 'R')
1178                        {
1179                                $this->stream->query('UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id='.$id.' and cal_owner='.$owner,__LINE__,__FILE__);
1180                        }
1181        */
1182                        return True;
1183                }
1184
1185        // End of ICal style support.......
1186
1187                function group_search($owner=0)
1188                {
1189                        $owner = ($owner==$GLOBALS['phpgw_info']['user']['account_id']?0:$owner);
1190                        $groups = substr($GLOBALS['phpgw']->common->sql_search('phpgw_cal.groups',(int)$owner),4);
1191                        if (!$groups)
1192                        {
1193                                return '';
1194                        }
1195                        else
1196                        {
1197                                return "(phpgw_cal.is_public=2 AND (". $groups .')) ';
1198                        }
1199                }
1200
1201                function splittime_($time)
1202                {
1203                        $temp = array('hour','minute','second','ampm');
1204                        $time = strrev($time);
1205                        $second = (int)strrev(substr($time,0,2));
1206                        $minute = (int)strrev(substr($time,2,2));
1207                        $hour   = (int)strrev(substr($time,4));
1208                        $temp['second'] = (int)$second;
1209                        $temp['minute'] = (int)$minute;
1210                        $temp['hour']   = (int)$hour;
1211                        $temp['ampm']   = '  ';
1212
1213                        return $temp;
1214                }
1215
1216                function date_to_epoch($d)
1217                {
1218                        return $this->localdates(mktime(0,0,0,(int)(substr($d,4,2)),(int)(substr($d,6,2)),(int)(substr($d,0,4))));
1219                }
1220
1221                function list_dirty_events($lastmod=-1,$repeats=false)
1222                {
1223                        if(!isset($this->stream))
1224                        {
1225                                return False;
1226                        }
1227                        $lastmod = (int)  $lastmod;
1228                        $repeats = (bool) $repeats;
1229
1230                        $user_where = " AND phpgw_cal_user.cal_login = $this->user";
1231
1232                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
1233                        @reset($member_groups);
1234                        while($member_groups != False && list($key,$group_info) = each($member_groups))
1235                        {
1236                                $member[] = $group_info['account_id'];
1237                        }
1238                        @reset($member);
1239        //              $user_where .= ','.implode(',',$member);
1240                        //$user_where .= ')) ';
1241
1242                        if($this->debug)
1243                        {
1244                                echo '<!-- '.$user_where.' -->'."\n";
1245                        }
1246
1247                        if($lastmod > 0)
1248                        {
1249                                $wheremod = "AND mdatetime = $lastmod";
1250                        }
1251
1252                        $order_by = ' ORDER BY phpgw_cal.cal_id ASC';
1253                        if($this->debug)
1254                        {
1255                                echo "SQL : ".$user_where.$wheremod.$extra."<br>\n";
1256                        }
1257                        return $this->get_event_ids($repeats,$user_where.$wheremod.$extra.$order_by);
1258                }
1259
1260        /* OLD-ALARM
1261                function add_alarm($eventid,$alarm,$owner)
1262                {
1263                        $this->stream->query('INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES('.$eventid.','.$owner.','.$alarm['time'].",'".$alarm['text']."',1)",__LINE__,__FILE__);
1264                        $this->stream->query('SELECT LAST_INSERT_ID()');
1265                        $this->stream->next_record();
1266                        return($this->stream->f(0));
1267                }
1268                function delete_alarm($alarmid)
1269                {
1270                        $this->stream->query('DELETE FROM phpgw_cal_alarm WHERE alarm_id='.$alarmid,__LINE__,__FILE__);
1271                }
1272        */
1273        }
Note: See TracBrowser for help on using the repository browser.