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

Revision 386, 33.8 KB checked in by rafaelraymundo, 16 years ago (diff)

Correção da ocorrência #251 do Trac da Comunidade.

  • 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                                        do
312                                        {
313
314                                                $resto = ($rpt_alarm_wdays % $divisor);
315                                                $quociente = floor($rpt_alarm_wdays / $divisor);
316
317                                                if($quociente == 1)
318                                                {
319                                                        $result[] .= $dias_semana[$divisor];
320
321                                                        $divisor = $divisor / 2;
322                                                        $rpt_alarm_wdays = $resto;
323
324                                                }else {
325
326                                                        while($rpt_alarm_wdays < $divisor)
327                                                        {
328                                                                $divisor = $divisor / 2;
329                                                        }
330
331                                                        $resto = ($rpt_alarm_wdays % $divisor);
332                                                        $quociente = floor($rpt_alarm_wdays / $divisor);
333
334                                                        if($quociente == 1)
335                                                        {
336
337                                                                $result[] .= $dias_semana[$divisor];
338
339                                                                $divisor = $divisor / 2;
340                                                                $rpt_alarm_wdays = $resto;
341                                                        }
342                                                }
343
344                                        }
345                                        while($resto != 0);
346
347                                        krsort($result);
348
349                                        $week_num = 0;
350                                        $y = 0;
351
352                                        while(@isset($alarms[$id]));
353                                        {
354
355                                                if($end_alarm != 0)
356                                                {
357
358                                                        while($data_atual <= $end_alarm)
359                                                        {
360
361                                                                foreach($dia as $index => $value)
362                                                                {
363                                                                        if(in_array($value,$result))
364                                                                        {
365
366                                                                                $nova_data = $init_alarm + (86400 * ($index + (7 * $y)));
367
368                                                                                if($nova_data == $init_alarm){
369
370                                                                                        continue;
371                                                                                }
372
373                                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
374                                                                                $n++;
375
376                                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
377
378                                                                                unset($alarm['repeat']);
379                                                                                unset($alarm['init_rept']);
380                                                                                unset($alarm['end_rept']);
381                                                                                unset($alarm['rpt_wdays']);
382                                                                               
383                                                                                $data_atual = $nova_data;
384
385                                                                                if($data_atual > $end_alarm){
386                                                                                        if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
387                                                                                        {
388                                                                                                return False;
389                                                                                        }
390                                                                                        break;
391                                                                                }
392
393                                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
394                                                                                {
395                                                                                        return False;
396                                                                                }
397                                                                                        $alarm['time'] = $nova_data - $alarm['offset'] - $GLOBALS['phpgw']->datetime->tz_offset;
398                                                                        }
399                                                                }
400                                                                $y++;
401                                                        }
402                                                }
403                                        }
404
405                                }elseif($alarm['repeat'] == 3) { // repeticao do tipo "Mensalmente (por data)";
406
407                                        $n = 0;
408
409                                        $init_alarm = $alarm['init_rept'];
410                                        $end_alarm = $alarm['end_rept'];
411                                       
412                                        while(@isset($alarms[$id]));
413                                        {
414
415                                                if($end_alarm != 0)
416                                                {
417                                                        while($init_alarm <= $end_alarm)
418                                                        {
419
420                                                                $next_month = date("n",$init_alarm) + 1;
421                                                                $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']));
422
423                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
424                                                                $n++;
425
426                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
427
428                                                                unset($alarm['repeat']);
429                                                                unset($alarm['init_rept']);
430                                                                unset($alarm['end_rept']);
431                                                                unset($alarm['rpt_wdays']);
432
433                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
434                                                                {
435                                                                        return False;
436                                                                }
437
438                                                                $alarm['time'] = $next_alarm;
439                                                                $init_alarm = $next_alarm;
440
441                                                        }
442                                                }
443                                        }
444
445                                }elseif($alarm['repeat'] == 5) { // repeticao do tipo "Anualmente";
446
447                                        $n = 0;
448
449                                        $init_alarm = $alarm['init_rept'];
450                                        $end_alarm = $alarm['end_rept'];
451                                       
452                                        while(@isset($alarms[$id]));
453                                        {
454
455
456                                                if($end_alarm != 0)
457                                                {
458                                                        while($init_alarm < $end_alarm)
459                                                        {
460
461                                                                $next_year = date("Y",$init_alarm) + 1;
462                                                                $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);
463
464                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
465                                                                $n++;
466
467                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
468
469                                                                unset($alarm['repeat']);
470                                                                unset($alarm['init_rept']);
471                                                                unset($alarm['end_rept']);
472                                                                unset($alarm['rpt_wdays']);
473
474                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
475                                                                {
476                                                                        return False;
477                                                                }
478
479                                                                $alarm['time'] = $next_alarm;
480                                                                $init_alarm = $next_alarm;
481                                                        }
482                                                }
483                                        }
484
485                                }else {
486                                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
487                                        $n = count($alarms);
488                                        while(@isset($alarms[$id]));
489                                        {
490
491                                                $id = 'cal:'.(int)$cal_id.':'.$n;
492
493                                                unset($alarm['repeat']);
494                                                unset($alarm['init_rept']);
495                                                unset($alarm['end_rept']);
496                                                unset($alarm['rpt_wdays']);
497
498                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
499                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
500                                                {
501                                                        return False;
502                                                }
503
504                                                ++$n;
505                                        }
506                                }
507
508                        }
509                        else
510                        {
511                                $this->async->cancel_timer($id);
512                        }
513                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
514                        return $id;
515                }
516
517                /*!
518                @function delete_alarms($cal_id)
519                @abstract delete all alarms of a calendar-entry
520                @returns the number of alarms deleted
521                */
522                function delete_alarms($cal_id)
523                {
524                        $alarms = $this->read_alarms($cal_id);
525
526                        foreach($alarms as $id => $alarm)
527                        {
528                                $this->async->cancel_timer($id);
529                        }
530                        return count($alarms);
531                }
532
533                /*!
534                @function delete_alarm($id)
535                @abstract delete one alarms identified by its id
536                @returns the number of alarms deleted
537                */
538                function delete_alarm($id)
539                {
540                        return $this->async->cancel_timer($id);
541                }
542
543                function fetch_event($event_id,$options='')
544                {
545                        if(!isset($this->stream))
546                        {
547                                return False;
548                        }
549
550                        $event_id = (int)$event_id;
551
552                        $this->stream->lock(array('phpgw_cal','phpgw_cal_user','phpgw_cal_repeats','phpgw_cal_extra'/* OLD-ALARM,'phpgw_cal_alarm'*/));
553
554                        $this->stream->query('SELECT * FROM phpgw_cal WHERE cal_id='.$event_id,__LINE__,__FILE__);
555
556                        if($this->stream->num_rows() > 0)
557                        {
558                                $this->event_init();
559
560                                $this->stream->next_record();
561                                // Load the calendar event data from the db into $event structure
562                                // Use http://www.php.net/manual/en/function.mcal-fetch-event.php as the reference
563                                $this->add_attribute('owner',(int)$this->stream->f('owner'));
564                                $this->add_attribute('id',(int)$this->stream->f('cal_id'));
565                                $this->set_class((int)$this->stream->f('is_public'));
566                                $this->set_category($this->stream->f('category'));
567                                $this->set_title(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('title'))));
568                                $this->set_description(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('description'))));
569                                $this->set_ex_participants(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('ex_participants'))));
570                                $this->add_attribute('uid',$GLOBALS['phpgw']->strip_html($this->stream->f('uid')));
571                                $this->add_attribute('location',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('location'))));
572                                $this->add_attribute('reference',(int)$this->stream->f('reference'));
573
574                                // This is the preferred method once everything is normalized...
575                                //$this->event->alarm = (int)$this->stream->f('alarm');
576                                // But until then, do it this way...
577                                //Legacy Support (New)
578
579                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('datetime'));
580                                $this->set_start($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
581
582                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('mdatetime'));
583                                $this->set_date('modtime',$datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
584
585                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('edatetime'));
586                                $this->set_end($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
587
588                        //Legacy Support
589                                $this->add_attribute('priority',(int)$this->stream->f('priority'));
590                                if($this->stream->f('cal_group') || $this->stream->f('groups') != 'NULL')
591                                {
592                                        $groups = explode(',',$this->stream->f('groups'));
593                                        for($j=1;$j<count($groups) - 1;$j++)
594                                        {
595                                                $this->add_attribute('groups',$groups[$j],$j-1);
596                                        }
597                                }
598
599                                $this->stream->query('SELECT * FROM phpgw_cal_repeats WHERE cal_id='.$event_id,__LINE__,__FILE__);
600                                if($this->stream->num_rows())
601                                {
602                                        $this->stream->next_record();
603
604                                        $this->add_attribute('recur_type',(int)$this->stream->f('recur_type'));
605                                        $this->add_attribute('recur_interval',(int)$this->stream->f('recur_interval'));
606                                        $enddate = $this->stream->f('recur_enddate');
607                                        if($enddate != 0 && $enddate != Null)
608                                        {
609                                                $datetime = $GLOBALS['phpgw']->datetime->localdates($enddate);
610                                                $this->add_attribute('recur_enddate',$datetime['year'],'year');
611                                                $this->add_attribute('recur_enddate',$datetime['month'],'month');
612                                                $this->add_attribute('recur_enddate',$datetime['day'],'mday');
613                                                $this->add_attribute('recur_enddate',$datetime['hour'],'hour');
614                                                $this->add_attribute('recur_enddate',$datetime['minute'],'min');
615                                                $this->add_attribute('recur_enddate',$datetime['second'],'sec');
616                                        }
617                                        else
618                                        {
619                                                $this->add_attribute('recur_enddate',0,'year');
620                                                $this->add_attribute('recur_enddate',0,'month');
621                                                $this->add_attribute('recur_enddate',0,'mday');
622                                                $this->add_attribute('recur_enddate',0,'hour');
623                                                $this->add_attribute('recur_enddate',0,'min');
624                                                $this->add_attribute('recur_enddate',0,'sec');
625                                        }
626                                        $this->add_attribute('recur_enddate',0,'alarm');
627                                        if($this->debug)
628                                        {
629                                                echo 'Event ID#'.$this->event['id'].' : Enddate = '.$enddate."<br>\n";
630                                        }
631                                        $this->add_attribute('recur_data',$this->stream->f('recur_data'));
632
633                                        $exception_list = $this->stream->f('recur_exception');
634                                        $exceptions = Array();
635                                        if(strpos(' '.$exception_list,','))
636                                        {
637                                                $exceptions = explode(',',$exception_list);
638                                        }
639                                        elseif($exception_list != '')
640                                        {
641                                                $exceptions[]= $exception_list;
642                                        }
643                                        $this->add_attribute('recur_exception',$exceptions);
644                                }
645
646                        //Legacy Support
647                                $this->stream->query('SELECT * FROM phpgw_cal_user WHERE cal_id='.$event_id,__LINE__,__FILE__);
648                                if($this->stream->num_rows())
649                                {
650                                        while($this->stream->next_record())
651                                        {
652                                                if((int)$this->stream->f('cal_login') == (int)$this->user)
653                                                {
654                                                        $this->add_attribute('users_status',$this->stream->f('cal_status'));
655                                                }
656                                                $this->add_attribute('participants',$this->stream->f('cal_status'),(int)$this->stream->f('cal_login'));
657                                        }
658                                }
659
660                        // Custom fields
661                                $this->stream->query('SELECT * FROM phpgw_cal_extra WHERE cal_id='.$event_id,__LINE__,__FILE__);
662                                if($this->stream->num_rows())
663                                {
664                                        while($this->stream->next_record())
665                                        {
666                                                $this->add_attribute('#'.$this->stream->f('cal_extra_name'),$this->stream->f('cal_extra_value'));
667                                        }
668                                }
669
670        /* OLD-ALARM
671                                if($this->event['reference'])
672                                {
673                                        // What is event['reference']???
674                                        $alarm_cal_id = $event_id.','.$this->event['reference'];
675                                }
676                                else
677                                {
678                                        $alarm_cal_id = $event_id;
679                                }
680
681                                //echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n";
682                                //$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__);
683                                $this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id='.$event_id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
684                                if($this->stream->num_rows())
685                                {
686                                        while($this->stream->next_record())
687                                        {
688                                                $this->event['alarm'][] = Array(
689                                                        'id'            => (int)$this->stream->f('alarm_id'),
690                                                        'time'  => (int)$this->stream->f('cal_time'),
691                                                        'text'  => $this->stream->f('cal_text'),
692                                                        'enabled'       => (int)$this->stream->f('alarm_enabled')
693                                                );
694                                        }
695                                }
696        */
697                        }
698                        else
699                        {
700                                $this->event = False;
701                        }
702
703                        $this->stream->unlock();
704
705                        if ($this->event)
706                        {
707                                $this->event['alarm'] = $this->read_alarms($event_id);
708
709                                if($this->event['reference'])
710                                {
711                                        $this->event['alarm'] += $this->read_alarms($event_id);
712                                }
713                        }
714                        return $this->event;
715                }
716
717                function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$extra='',$tz_offset=0,$owner_id=0)
718                {
719                        if(!isset($this->stream))
720                        {
721                                return False;
722                        }
723
724                        $datetime = mktime(0,0,0,$startMonth,$startDay,$startYear) - $tz_offset;
725
726                        $user_where = ' AND (phpgw_cal_user.cal_login in (';
727                        if(is_array($owner_id) && count($owner_id))
728                        {
729                                $user_where .= implode(',',$owner_id);
730                        }
731                        else
732                        {
733                                $user_where .= $this->user;
734                        }
735                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
736                        @reset($member_groups);
737                        while($member_groups != False && list($key,$group_info) = each($member_groups))
738                        {
739                                $member[] = $group_info['account_id'];
740                        }
741                        @reset($member);
742        //              $user_where .= ','.implode(',',$member);
743                        $user_where .= ')) ';
744
745                        if($this->debug)
746                        {
747                                echo '<!-- '.$user_where.' -->'."\n";
748                        }
749
750                        $startDate = 'AND ( ( (phpgw_cal.datetime >= '.$datetime.') ';
751
752                        $enddate = '';
753                        if($endYear != 0 && $endMonth != 0 && $endDay != 0)
754                        {
755                                $edatetime = mktime(23,59,59,(int)$endMonth,(int)$endDay,(int)$endYear) - $tz_offset;
756                                $endDate .= 'AND (phpgw_cal.edatetime <= '.$edatetime.') ) '
757                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
758                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
759                                        . 'OR ( (phpgw_cal.datetime >= '.$datetime.') '
760                                        . 'AND (phpgw_cal.datetime <= '.$edatetime.') '
761                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
762                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
763                                        . 'AND (phpgw_cal.edatetime >= '.$datetime.') '
764                                        . 'AND (phpgw_cal.edatetime <= '.$edatetime.') ';
765                        }
766                        $endDate .= ') ) ';
767
768                        $order_by = 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC';
769                        if($this->debug)
770                        {
771                                echo "SQL : ".$user_where.$startDate.$endDate.$extra."<br>\n";
772                        }
773                        return $this->get_event_ids(False,$user_where.$startDate.$endDate.$extra.$order_by);
774                }
775
776                function append_event()
777                {
778                        $this->save_event($this->event);
779                        $this->send_update(MSG_ADDED,$this->event->participants,'',$this->event);
780                        return $this->event['id'];
781                }
782
783                function store_event()
784                {
785                        return $this->save_event(&$this->event);
786                }
787
788                function delete_event($event_id)
789                {
790                        $this->deleted_events[] = $event_id;
791                }
792
793                function snooze($event_id)
794                {
795                //Turn off an alarm for an event
796                //Returns true.
797                }
798
799                function list_alarms($begin_year='',$begin_month='',$begin_day='',$end_year='',$end_month='',$end_day='')
800                {
801                //Return a list of events that has an alarm triggered at the given datetime
802                //Returns an array of event ID's
803                }
804
805                // The function definition doesn't look correct...
806                // Need more information for this function
807                function next_recurrence($weekstart,$next)
808                {
809        //              return next_recurrence (int stream, int weekstart, array next);
810                }
811
812                function expunge()
813                {
814                        if(count($this->deleted_events) <= 0)
815                        {
816                                return 1;
817                        }
818                        $this_event = $this->event;
819                        $locks = Array(
820                                'phpgw_cal',
821                                'phpgw_cal_user',
822                                'phpgw_cal_repeats',
823                                'phpgw_cal_extra'
824        // OLD-ALARM                    'phpgw_cal_alarm'
825                        );
826                        $this->stream->lock($locks);
827                        foreach($this->deleted_events as $cal_id)
828                        {
829                                foreach ($locks as $table)
830                                {
831                                        $this->stream->query('DELETE FROM '.$table.' WHERE cal_id='.$cal_id,__LINE__,__FILE__);
832                                }
833                        }
834                        $this->stream->unlock();
835
836                        foreach($this->deleted_events as $cal_id)
837                        {
838                                $this->delete_alarms($cal_id);
839                        }
840                        $this->deleted_events = array();
841
842                        $this->event = $this_event;
843                        return 1;
844                }
845
846                /***************** Local functions for SQL based Calendar *****************/
847
848                function get_event_ids($search_repeats=False,$extra='',$search_extra=False)
849                {
850                        $from = $where = ' ';
851                        if($search_repeats)
852                        {
853                                $from  = ', phpgw_cal_repeats ';
854                                $where = 'AND (phpgw_cal_repeats.cal_id = phpgw_cal.cal_id) ';
855                        }
856                        if($search_extra)
857                        {
858                                $from  .= 'LEFT JOIN phpgw_cal_extra ON phpgw_cal_extra.cal_id = phpgw_cal.cal_id ';
859                        }
860
861                        $sql = 'SELECT DISTINCT phpgw_cal.cal_id,'
862                                        . 'phpgw_cal.datetime,phpgw_cal.edatetime,'
863                                        . 'phpgw_cal.priority '
864                                        . 'FROM phpgw_cal_user, phpgw_cal'
865                                        . $from
866                                        . 'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) '
867                                        . $where . $extra;
868
869                        if($this->debug)
870                        {
871                                echo "FULL SQL : ".$sql."<br>\n";
872                        }
873
874                        $this->stream->query($sql,__LINE__,__FILE__);
875
876                        $retval = Array();
877                        if($this->stream->num_rows() == 0)
878                        {
879                                if($this->debug)
880                                {
881                                        echo "No records found!<br>\n";
882                                }
883                                return $retval;
884                        }
885
886                        while($this->stream->next_record())
887                        {
888                                $retval[] = (int)$this->stream->f('cal_id');
889                        }
890                        if($this->debug)
891                        {
892                                echo "Records found!<br>\n";
893                        }
894                        return $retval;
895                }
896
897                function save_event(&$event)
898                {
899
900                        $GLOBALS['calendar']->bocalendar = CreateObject('calendar.bocalendar');
901
902                        $locks = Array(
903                                'phpgw_cal',
904                                'phpgw_cal_user',
905                                'phpgw_cal_repeats',
906                                'phpgw_cal_extra'
907        // OLD-ALARM                    'phpgw_cal_alarm'
908                        );
909                        $this->stream->lock($locks);
910                        if($event['id'] == 0)
911                        {
912                                if(!$event['uid'])
913                                {
914                                        if ($GLOBALS['phpgw_info']['server']['hostname'] != '')
915                                        {
916                                                $id_suffix = $GLOBALS['phpgw_info']['server']['hostname'];
917                                        }
918                                        else
919                                        {
920                                                $id_suffix = $GLOBALS['phpgw']->common->randomstring(3).'local';
921                                        }
922                                        $parts = Array(
923                                                0 => 'title',
924                                                1 => 'description',
925                                                2 => 'ex_participants'
926                                        );
927                                        @reset($parts);
928                                        while(list($key,$field) = each($parts))
929                                        {
930                                                $part[$key] = substr($GLOBALS['phpgw']->crypto->encrypt($event[$field]),0,20);
931                                                if(!$GLOBALS['phpgw']->crypto->enabled)
932                                                {
933                                                        $part[$key] = bin2hex(unserialize($part[$key]));
934                                                }
935                                        }
936                                        $event['uid'] = $part[0].'-'.$part[1].'@'.$id_suffix;
937                                }
938                                $this->stream->query('INSERT INTO phpgw_cal(uid,title,owner,priority,is_public,category) '
939                                        . "values('".$event['uid']."','".$this->stream->db_addslashes($event['title'])
940                                        . "',".(int)$event['owner'].','.(int)$event['priority'].','.(int)$event['public'].",'"
941                                        . $event['category']."')",__LINE__,__FILE__);
942                                $event['id'] = $this->stream->get_last_insert_id('phpgw_cal','cal_id');
943                                $last_status = true;
944                        }
945
946                        $date = $this->maketime($event['start']) - $GLOBALS['phpgw']->datetime->tz_offset;
947                        $enddate = $this->maketime($event['end']) - $GLOBALS['phpgw']->datetime->tz_offset;
948                        $today = time() - $GLOBALS['phpgw']->datetime->tz_offset;
949
950                        if($event['recur_type'] != MCAL_RECUR_NONE)
951                        {
952                                $type = 'M';
953                        }
954                        else
955                        {
956                                $type = 'E';
957                        }
958
959                        $sql = 'UPDATE phpgw_cal SET '
960                                        . 'owner='.(int)$event['owner'].', '
961                                        . 'datetime='.(int)$date.', '
962                                        . 'mdatetime='.(int)$today.', '
963                                        . 'edatetime='.(int)$enddate.', '
964                                        . 'priority='.(int)$event['priority'].', '
965                                        . "category='".$this->stream->db_addslashes($event['category'])."', "
966                                        . "cal_type='".$this->stream->db_addslashes($type)."', "
967                                        . 'is_public='.(int)$event['public'].', '
968                                        . "title='".$this->stream->db_addslashes($event['title'])."', "
969                                        . "description='".$this->stream->db_addslashes($event['description'])."', "
970                                        . "ex_participants='".$this->stream->db_addslashes($event['ex_participants'])."', "
971                                        . "location='".$this->stream->db_addslashes($event['location'])."', "
972                                        . ($event['groups']?"groups='".(count($event['groups'])>1?implode(',',$event['groups']):','.$event['groups'][0].',')."', ":'')
973                                        . 'reference='.(int)$event['reference'].' '
974                                        . ',last_status = '.($last_status ? "'N'" : "'U'").',last_update = '.time()."000". ' '
975                                        . 'WHERE cal_id='.(int)$event['id'];
976
977                        $this->stream->query($sql,__LINE__,__FILE__);
978
979                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
980
981                        @reset($event['participants']);
982                        while (list($key,$value) = @each($event['participants']))
983                        {
984                                if((int)$key == $event['owner'])
985                                {
986                                        $value = 'A';
987                                }
988                                $this->stream->query('INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) '
989                                        . 'VALUES('.(int)$event['id'].','.(int)$key.",'".$this->stream->db_addslashes($value)."')",__LINE__,__FILE__);
990                        }
991
992                        if($event['recur_type'] != MCAL_RECUR_NONE)
993                        {
994                                if($event['recur_enddate']['month'] != 0 && $event['recur_enddate']['mday'] != 0 && $event['recur_enddate']['year'] != 0)
995                                {
996                                        $end = $this->maketime($event['recur_enddate']) - $GLOBALS['phpgw']->datetime->tz_offset;
997                                }
998                                else
999                                {
1000                                        $end = 0;
1001                                }
1002
1003                                $this->stream->query('SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
1004                                $this->stream->next_record();
1005                                $num_rows = $this->stream->f(0);
1006                                if($num_rows == 0)
1007                                {
1008                                        $this->stream->query('INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) '
1009                                                .'VALUES('.(int)$event['id'].','.$event['recur_type'].','.(int)$end.','.$event['recur_data'].','.$event['recur_interval'].')',__LINE__,__FILE__);
1010                                }
1011                                else
1012                                {
1013                                        $this->stream->query('UPDATE phpgw_cal_repeats '
1014                                                . 'SET recur_type='.$event['recur_type'].', '
1015                                                . 'recur_enddate='.(int)$end.', '
1016                                                . 'recur_data='.$event['recur_data'].', '
1017                                                . 'recur_interval='.$event['recur_interval'].', '
1018                                                . "recur_exception='".(count($event['recur_exception'])>1?implode(',',$event['recur_exception']):(count($event['recur_exception'])==1?$event['recur_exception'][0]:''))."' "
1019                                                . 'WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1020                                }
1021
1022                        }
1023                        else
1024                        {
1025                                $this->stream->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1026                        }
1027                        // Custom fields
1028                        $this->stream->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1029
1030                        foreach($event as $name => $value)
1031                        {
1032                                if ($name[0] == '#' && strlen($value))
1033                                {
1034                                        $this->stream->query('INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) '
1035                                        . 'VALUES('.$event['id'].",'".addslashes(substr($name,1))."','".addslashes($value)."')",__LINE__,__FILE__);
1036                                }
1037                        }
1038        /*
1039                        $alarmcount = count($event['alarm']);
1040                        if ($alarmcount > 1)
1041                        {
1042                                // this should never happen, $event['alarm'] should only be set
1043                                // if creating a new event and uicalendar only sets up 1 alarm
1044                                // the user must use "Alarm Management" to create/establish multiple
1045                                // alarms or to edit/change an alarm
1046                                echo '<!-- how did this happen, too many alarms -->'."\n";
1047                                $this->stream->unlock();
1048                                return True;
1049                        }
1050
1051                        if ($alarmcount == 1)
1052                        {
1053
1054                                list($key,$alarm) = @each($event['alarm']);
1055
1056                                $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__);
1057                                $this->stream->query('SELECT LAST_INSERT_ID()');
1058                                $this->stream->next_record();
1059                                $alarm['id'] = $this->stream->f(0);
1060                        }
1061        */
1062                        print_debug('Event Saved: ID #',$event['id']);
1063
1064                        $this->stream->unlock();
1065
1066                        if (is_array($event['alarm']))
1067                        {
1068                                foreach ($event['alarm'] as $alarm)     // this are all new alarms
1069                                {
1070                                        $this->save_alarm($event['id'],$alarm);
1071                                }
1072                        }
1073                        $GLOBALS['phpgw_info']['cal_new_event_id'] = $event['id'];
1074                        $this->event = $event;
1075                        return True;
1076                }
1077
1078                function get_alarm($cal_id)
1079                {
1080        /* OLD-ALARM
1081                        $this->stream->query('SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id='.$id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
1082                        if($this->stream->num_rows())
1083                        {
1084                                while($this->stream->next_record())
1085                                {
1086                                        $alarm[$this->stream->f('cal_time')] = $this->stream->f('cal_text');
1087                                }
1088                                @reset($alarm);
1089                                return $alarm;
1090                        }
1091                        else
1092                        {
1093                                return False;
1094                        }
1095        */
1096                        $alarms = $this->read_alarms($cal_id);
1097                        $ret = False;
1098
1099                        foreach($alarms as $alarm)
1100                        {
1101                                if ($alarm['owner'] == $this->user || !$alarm['owner'])
1102                                {
1103                                        $ret[$alarm['time']] = $alarm['text'];
1104                                }
1105                        }
1106                        return $ret;
1107                }
1108
1109                function set_status($id,$owner,$status)
1110                {
1111                        $status_code_short = Array(
1112                                REJECTED =>     'R',
1113                                NO_RESPONSE     => 'U',
1114                                TENTATIVE       =>      'T',
1115                                ACCEPTED        =>      'A'
1116                        );
1117
1118                        $this->stream->query("UPDATE phpgw_cal_user SET cal_status='".$status_code_short[$status]."' WHERE cal_id=".$id." AND cal_login=".$owner,__LINE__,__FILE__);
1119        /* OLD-ALARM
1120                        if ($status == 'R')
1121                        {
1122                                $this->stream->query('UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id='.$id.' and cal_owner='.$owner,__LINE__,__FILE__);
1123                        }
1124        */
1125                        return True;
1126                }
1127
1128        // End of ICal style support.......
1129
1130                function group_search($owner=0)
1131                {
1132                        $owner = ($owner==$GLOBALS['phpgw_info']['user']['account_id']?0:$owner);
1133                        $groups = substr($GLOBALS['phpgw']->common->sql_search('phpgw_cal.groups',(int)$owner),4);
1134                        if (!$groups)
1135                        {
1136                                return '';
1137                        }
1138                        else
1139                        {
1140                                return "(phpgw_cal.is_public=2 AND (". $groups .')) ';
1141                        }
1142                }
1143
1144                function splittime_($time)
1145                {
1146                        $temp = array('hour','minute','second','ampm');
1147                        $time = strrev($time);
1148                        $second = (int)strrev(substr($time,0,2));
1149                        $minute = (int)strrev(substr($time,2,2));
1150                        $hour   = (int)strrev(substr($time,4));
1151                        $temp['second'] = (int)$second;
1152                        $temp['minute'] = (int)$minute;
1153                        $temp['hour']   = (int)$hour;
1154                        $temp['ampm']   = '  ';
1155
1156                        return $temp;
1157                }
1158
1159                function date_to_epoch($d)
1160                {
1161                        return $this->localdates(mktime(0,0,0,(int)(substr($d,4,2)),(int)(substr($d,6,2)),(int)(substr($d,0,4))));
1162                }
1163
1164                function list_dirty_events($lastmod=-1,$repeats=false)
1165                {
1166                        if(!isset($this->stream))
1167                        {
1168                                return False;
1169                        }
1170                        $lastmod = (int)  $lastmod;
1171                        $repeats = (bool) $repeats;
1172
1173                        $user_where = " AND phpgw_cal_user.cal_login = $this->user";
1174
1175                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
1176                        @reset($member_groups);
1177                        while($member_groups != False && list($key,$group_info) = each($member_groups))
1178                        {
1179                                $member[] = $group_info['account_id'];
1180                        }
1181                        @reset($member);
1182        //              $user_where .= ','.implode(',',$member);
1183                        //$user_where .= ')) ';
1184
1185                        if($this->debug)
1186                        {
1187                                echo '<!-- '.$user_where.' -->'."\n";
1188                        }
1189
1190                        if($lastmod > 0)
1191                        {
1192                                $wheremod = "AND mdatetime = $lastmod";
1193                        }
1194
1195                        $order_by = ' ORDER BY phpgw_cal.cal_id ASC';
1196                        if($this->debug)
1197                        {
1198                                echo "SQL : ".$user_where.$wheremod.$extra."<br>\n";
1199                        }
1200                        return $this->get_event_ids($repeats,$user_where.$wheremod.$extra.$order_by);
1201                }
1202
1203        /* OLD-ALARM
1204                function add_alarm($eventid,$alarm,$owner)
1205                {
1206                        $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__);
1207                        $this->stream->query('SELECT LAST_INSERT_ID()');
1208                        $this->stream->next_record();
1209                        return($this->stream->f(0));
1210                }
1211                function delete_alarm($alarmid)
1212                {
1213                        $this->stream->query('DELETE FROM phpgw_cal_alarm WHERE alarm_id='.$alarmid,__LINE__,__FILE__);
1214                }
1215        */
1216        }
Note: See TracBrowser for help on using the repository browser.