source: branches/2.2.0.1/calendar/inc/class.socalendar_sql.inc.php @ 4217

Revision 4217, 35.3 KB checked in by brunocosta, 13 years ago (diff)

Ticket #1802 - Opção nos agendamentos para enviar notificações como dono.

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