source: branches/2.2/calendar/inc/class.socalendar_sql.inc.php @ 3018

Revision 3018, 34.0 KB checked in by amuller, 14 years ago (diff)

Ticket #1135 - Aplicando alterações do branches 2.0 no branches 2.2

  • 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_ex_participants(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('ex_participants'))));
572                                $this->add_attribute('uid',$GLOBALS['phpgw']->strip_html($this->stream->f('uid')));
573                                $this->add_attribute('location',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('location'))));
574                                $this->add_attribute('reference',(int)$this->stream->f('reference'));
575
576                                // This is the preferred method once everything is normalized...
577                                //$this->event->alarm = (int)$this->stream->f('alarm');
578                                // But until then, do it this way...
579                                //Legacy Support (New)
580
581                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('datetime'));
582                                $this->set_start($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
583
584                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('mdatetime'));
585                                $this->set_date('modtime',$datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
586
587                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('edatetime'));
588                                $this->set_end($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
589
590                        //Legacy Support
591                                $this->add_attribute('priority',(int)$this->stream->f('priority'));
592                                if($this->stream->f('cal_group') || $this->stream->f('groups') != 'NULL')
593                                {
594                                        $groups = explode(',',$this->stream->f('groups'));
595                                        for($j=1;$j<count($groups) - 1;$j++)
596                                        {
597                                                $this->add_attribute('groups',$groups[$j],$j-1);
598                                        }
599                                }
600
601                                $this->stream->query('SELECT * FROM phpgw_cal_repeats WHERE cal_id='.$event_id,__LINE__,__FILE__);
602                                if($this->stream->num_rows())
603                                {
604                                        $this->stream->next_record();
605
606                                        $this->add_attribute('recur_type',(int)$this->stream->f('recur_type'));
607                                        $this->add_attribute('recur_interval',(int)$this->stream->f('recur_interval'));
608                                        $enddate = $this->stream->f('recur_enddate');
609                                        if($enddate != 0 && $enddate != Null)
610                                        {
611                                                $datetime = $GLOBALS['phpgw']->datetime->localdates($enddate);
612                                                $this->add_attribute('recur_enddate',$datetime['year'],'year');
613                                                $this->add_attribute('recur_enddate',$datetime['month'],'month');
614                                                $this->add_attribute('recur_enddate',$datetime['day'],'mday');
615                                                $this->add_attribute('recur_enddate',$datetime['hour'],'hour');
616                                                $this->add_attribute('recur_enddate',$datetime['minute'],'min');
617                                                $this->add_attribute('recur_enddate',$datetime['second'],'sec');
618                                        }
619                                        else
620                                        {
621                                                $this->add_attribute('recur_enddate',0,'year');
622                                                $this->add_attribute('recur_enddate',0,'month');
623                                                $this->add_attribute('recur_enddate',0,'mday');
624                                                $this->add_attribute('recur_enddate',0,'hour');
625                                                $this->add_attribute('recur_enddate',0,'min');
626                                                $this->add_attribute('recur_enddate',0,'sec');
627                                        }
628                                        $this->add_attribute('recur_enddate',0,'alarm');
629                                        if($this->debug)
630                                        {
631                                                echo 'Event ID#'.$this->event['id'].' : Enddate = '.$enddate."<br>\n";
632                                        }
633                                        $this->add_attribute('recur_data',$this->stream->f('recur_data'));
634
635                                        $exception_list = $this->stream->f('recur_exception');
636                                        $exceptions = Array();
637                                        if(strpos(' '.$exception_list,','))
638                                        {
639                                                $exceptions = explode(',',$exception_list);
640                                        }
641                                        elseif($exception_list != '')
642                                        {
643                                                $exceptions[]= $exception_list;
644                                        }
645                                        $this->add_attribute('recur_exception',$exceptions);
646                                }
647
648                        //Legacy Support
649                                $this->stream->query('SELECT * FROM phpgw_cal_user WHERE cal_id='.$event_id,__LINE__,__FILE__);
650                                if($this->stream->num_rows())
651                                {
652                                        while($this->stream->next_record())
653                                        {
654                                                if((int)$this->stream->f('cal_login') == (int)$this->user)
655                                                {
656                                                        $this->add_attribute('users_status',$this->stream->f('cal_status'));
657                                                }
658                                                $this->add_attribute('participants',$this->stream->f('cal_status'),(int)$this->stream->f('cal_login'));
659                                        }
660                                }
661
662                        // Custom fields
663                                $this->stream->query('SELECT * FROM phpgw_cal_extra WHERE cal_id='.$event_id,__LINE__,__FILE__);
664                                if($this->stream->num_rows())
665                                {
666                                        while($this->stream->next_record())
667                                        {
668                                                $this->add_attribute('#'.$this->stream->f('cal_extra_name'),$this->stream->f('cal_extra_value'));
669                                        }
670                                }
671
672        /* OLD-ALARM
673                                if($this->event['reference'])
674                                {
675                                        // What is event['reference']???
676                                        $alarm_cal_id = $event_id.','.$this->event['reference'];
677                                }
678                                else
679                                {
680                                        $alarm_cal_id = $event_id;
681                                }
682
683                                //echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n";
684                                //$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__);
685                                $this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id='.$event_id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
686                                if($this->stream->num_rows())
687                                {
688                                        while($this->stream->next_record())
689                                        {
690                                                $this->event['alarm'][] = Array(
691                                                        'id'            => (int)$this->stream->f('alarm_id'),
692                                                        'time'  => (int)$this->stream->f('cal_time'),
693                                                        'text'  => $this->stream->f('cal_text'),
694                                                        'enabled'       => (int)$this->stream->f('alarm_enabled')
695                                                );
696                                        }
697                                }
698        */
699                        }
700                        else
701                        {
702                                $this->event = False;
703                        }
704
705                        $this->stream->unlock();
706
707                        if ($this->event)
708                        {
709                                $this->event['alarm'] = $this->read_alarms($event_id);
710
711                                if($this->event['reference'])
712                                {
713                                        $this->event['alarm'] += $this->read_alarms($event_id);
714                                }
715                        }
716                        return $this->event;
717                }
718
719                function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$extra='',$tz_offset=0,$owner_id=0)
720                {
721                        if(!isset($this->stream))
722                        {
723                                return False;
724                        }
725
726                        $datetime = mktime(0,0,0,$startMonth,$startDay,$startYear) - $tz_offset;
727
728                        $user_where = ' AND (phpgw_cal_user.cal_login in (';
729                        if(is_array($owner_id) && count($owner_id))
730                        {
731                                $user_where .= implode(',',$owner_id);
732                        }
733                        else
734                        {
735                                $user_where .= $this->user;
736                        }
737                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
738                        @reset($member_groups);
739                        while($member_groups != False && list($key,$group_info) = each($member_groups))
740                        {
741                                $member[] = $group_info['account_id'];
742                        }
743                        @reset($member);
744        //              $user_where .= ','.implode(',',$member);
745                        $user_where .= ')) ';
746
747                        if($this->debug)
748                        {
749                                echo '<!-- '.$user_where.' -->'."\n";
750                        }
751
752                        $startDate = 'AND ( ( (phpgw_cal.datetime >= '.$datetime.') ';
753
754                        $enddate = '';
755                        if($endYear != 0 && $endMonth != 0 && $endDay != 0)
756                        {
757                                $edatetime = mktime(23,59,59,(int)$endMonth,(int)$endDay,(int)$endYear) - $tz_offset;
758                                $endDate .= 'AND (phpgw_cal.edatetime <= '.$edatetime.') ) '
759                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
760                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
761                                        . 'OR ( (phpgw_cal.datetime >= '.$datetime.') '
762                                        . 'AND (phpgw_cal.datetime <= '.$edatetime.') '
763                                        . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
764                                        . 'OR ( (phpgw_cal.datetime <= '.$datetime.') '
765                                        . 'AND (phpgw_cal.edatetime >= '.$datetime.') '
766                                        . 'AND (phpgw_cal.edatetime <= '.$edatetime.') ';
767                        }
768                        $endDate .= ') ) ';
769
770                        $order_by = 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC';
771                        if($this->debug)
772                        {
773                                echo "SQL : ".$user_where.$startDate.$endDate.$extra."<br>\n";
774                        }
775                        return $this->get_event_ids(False,$user_where.$startDate.$endDate.$extra.$order_by);
776                }
777
778                function append_event()
779                {
780                        $this->save_event($this->event);
781                        $this->send_update(MSG_ADDED,$this->event->participants,'',$this->event);
782                        return $this->event['id'];
783                }
784
785                function store_event()
786                {
787                        return $this->save_event(&$this->event);
788                }
789
790                function delete_event($event_id)
791                {
792                        $this->deleted_events[] = $event_id;
793                }
794
795                function snooze($event_id)
796                {
797                //Turn off an alarm for an event
798                //Returns true.
799                }
800
801                function list_alarms($begin_year='',$begin_month='',$begin_day='',$end_year='',$end_month='',$end_day='')
802                {
803                //Return a list of events that has an alarm triggered at the given datetime
804                //Returns an array of event ID's
805                }
806
807                // The function definition doesn't look correct...
808                // Need more information for this function
809                function next_recurrence($weekstart,$next)
810                {
811        //              return next_recurrence (int stream, int weekstart, array next);
812                }
813
814                function expunge()
815                {
816                        if(count($this->deleted_events) <= 0)
817                        {
818                                return 1;
819                        }
820                        $this_event = $this->event;
821                        $locks = Array(
822                                'phpgw_cal',
823                                'phpgw_cal_user',
824                                'phpgw_cal_repeats',
825                                'phpgw_cal_extra'
826        // OLD-ALARM                    'phpgw_cal_alarm'
827                        );
828                        $this->stream->lock($locks);
829                        foreach($this->deleted_events as $cal_id)
830                        {
831                                foreach ($locks as $table)
832                                {
833                                        $this->stream->query('DELETE FROM '.$table.' WHERE cal_id='.$cal_id,__LINE__,__FILE__);
834                                }
835                        }
836                        $this->stream->unlock();
837
838                        foreach($this->deleted_events as $cal_id)
839                        {
840                                $this->delete_alarms($cal_id);
841                        }
842                        $this->deleted_events = array();
843
844                        $this->event = $this_event;
845                        return 1;
846                }
847
848                /***************** Local functions for SQL based Calendar *****************/
849
850                function get_event_ids($search_repeats=False,$extra='',$search_extra=False)
851                {
852                        $from = $where = ' ';
853                        if($search_repeats)
854                        {
855                                $from  = ', phpgw_cal_repeats ';
856                                $where = 'AND (phpgw_cal_repeats.cal_id = phpgw_cal.cal_id) ';
857                        }
858                        if($search_extra)
859                        {
860                                $from  .= 'LEFT JOIN phpgw_cal_extra ON phpgw_cal_extra.cal_id = phpgw_cal.cal_id ';
861                        }
862
863                        $sql = 'SELECT DISTINCT phpgw_cal.cal_id,'
864                                        . 'phpgw_cal.datetime,phpgw_cal.edatetime,'
865                                        . 'phpgw_cal.priority '
866                                        . 'FROM phpgw_cal_user, phpgw_cal'
867                                        . $from
868                                        . 'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) '
869                                        . $where . $extra;
870
871                        if($this->debug)
872                        {
873                                echo "FULL SQL : ".$sql."<br>\n";
874                        }
875
876                        $this->stream->query($sql,__LINE__,__FILE__);
877
878                        $retval = Array();
879                        if($this->stream->num_rows() == 0)
880                        {
881                                if($this->debug)
882                                {
883                                        echo "No records found!<br>\n";
884                                }
885                                return $retval;
886                        }
887
888                        while($this->stream->next_record())
889                        {
890                                $retval[] = (int)$this->stream->f('cal_id');
891                        }
892                        if($this->debug)
893                        {
894                                echo "Records found!<br>\n";
895                        }
896                        return $retval;
897                }
898
899                function save_event(&$event)
900                {
901
902                        $GLOBALS['calendar']->bocalendar = CreateObject('calendar.bocalendar');
903
904                        $locks = Array(
905                                'phpgw_cal',
906                                'phpgw_cal_user',
907                                'phpgw_cal_repeats',
908                                'phpgw_cal_extra'
909        // OLD-ALARM                    'phpgw_cal_alarm'
910                        );
911                        $this->stream->lock($locks);
912                        if($event['id'] == 0)
913                        {
914                                if(!$event['uid'])
915                                {
916                                        if ($GLOBALS['phpgw_info']['server']['hostname'] != '')
917                                        {
918                                                $id_suffix = $GLOBALS['phpgw_info']['server']['hostname'];
919                                        }
920                                        else
921                                        {
922                                                $id_suffix = $GLOBALS['phpgw']->common->randomstring(3).'local';
923                                        }
924                                        $parts = Array(
925                                                0 => 'title',
926                                                1 => 'description',
927                                                2 => 'ex_participants'
928                                        );
929                                        @reset($parts);
930                                        while(list($key,$field) = each($parts))
931                                        {
932                                                $part[$key] = substr($GLOBALS['phpgw']->crypto->encrypt($event[$field]),0,20);
933                                                if(!$GLOBALS['phpgw']->crypto->enabled)
934                                                {
935                                                        $part[$key] = bin2hex(unserialize($part[$key]));
936                                                }
937                                        }
938                                        $event['uid'] = $part[0].'-'.$part[1].'@'.$id_suffix;
939                                }
940                                $this->stream->query('INSERT INTO phpgw_cal(uid,title,owner,priority,is_public,category) '
941                                        . "values('".$event['uid']."','".$this->stream->db_addslashes($event['title'])
942                                        . "',".(int)$event['owner'].','.(int)$event['priority'].','.(int)$event['public'].",'"
943                                        . $event['category']."')",__LINE__,__FILE__);
944                                $event['id'] = $this->stream->get_last_insert_id('phpgw_cal','cal_id');
945                                $last_status = true;
946                        }
947
948                        $date = $this->maketime($event['start']) - $GLOBALS['phpgw']->datetime->tz_offset;
949                        $enddate = $this->maketime($event['end']) - $GLOBALS['phpgw']->datetime->tz_offset;
950                        $today = time() - $GLOBALS['phpgw']->datetime->tz_offset;
951
952                        if($event['recur_type'] != MCAL_RECUR_NONE)
953                        {
954                                $type = 'M';
955                        }
956                        else
957                        {
958                                if ($event['type'] == 'hourAppointment')
959                                        $type = 'H';
960                                else if($event['type'] == 'privateHiddenFields'){
961                                        $type = 'P';
962                                }else
963                                        $type = 'E';
964                        }
965
966                        $sql = 'UPDATE phpgw_cal SET '
967                                        . 'owner='.(int)$event['owner'].', '
968                                        . 'datetime='.(int)$date.', '
969                                        . 'mdatetime='.(int)$today.', '
970                                        . 'edatetime='.(int)$enddate.', '
971                                        . 'priority='.(int)$event['priority'].', '
972                                        . "category='".$this->stream->db_addslashes($event['category'])."', "
973                                        . "cal_type='".$this->stream->db_addslashes($type)."', "
974                                        . 'is_public='.(int)$event['public'].', '
975                                        . "title='".$this->stream->db_addslashes($event['title'])."', "
976                                        . "description='".$this->stream->db_addslashes($event['description'])."', "
977                                        . "ex_participants='".$this->stream->db_addslashes($event['ex_participants'])."', "
978                                        . "location='".$this->stream->db_addslashes($event['location'])."', "
979                                        . ($event['groups']?"groups='".(count($event['groups'])>1?implode(',',$event['groups']):','.$event['groups'][0].',')."', ":'')
980                                        . 'reference='.(int)$event['reference'].' '
981                                        . ',last_status = '.($last_status ? "'N'" : "'U'").',last_update = '.time()."000". ' '
982                                        . 'WHERE cal_id='.(int)$event['id'];
983
984                        $this->stream->query($sql,__LINE__,__FILE__);
985
986                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
987
988                        @reset($event['participants']);
989                        while (list($key,$value) = @each($event['participants']))
990                        {
991                                if((int)$key == $event['owner'])
992                                {
993                                        $value = 'A';
994                                }
995                                $this->stream->query('INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) '
996                                        . 'VALUES('.(int)$event['id'].','.(int)$key.",'".$this->stream->db_addslashes($value)."')",__LINE__,__FILE__);
997                        }
998
999                        if($event['recur_type'] != MCAL_RECUR_NONE)
1000                        {
1001                                if($event['recur_enddate']['month'] != 0 && $event['recur_enddate']['mday'] != 0 && $event['recur_enddate']['year'] != 0)
1002                                {
1003                                        $end = $this->maketime($event['recur_enddate']) - $GLOBALS['phpgw']->datetime->tz_offset;
1004                                }
1005                                else
1006                                {
1007                                        $end = 0;
1008                                }
1009
1010                                $this->stream->query('SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
1011                                $this->stream->next_record();
1012                                $num_rows = $this->stream->f(0);
1013                                if($num_rows == 0)
1014                                {
1015                                        $this->stream->query('INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) '
1016                                                .'VALUES('.(int)$event['id'].','.$event['recur_type'].','.(int)$end.','.$event['recur_data'].','.$event['recur_interval'].')',__LINE__,__FILE__);
1017                                }
1018                                else
1019                                {
1020                                        $this->stream->query('UPDATE phpgw_cal_repeats '
1021                                                . 'SET recur_type='.$event['recur_type'].', '
1022                                                . 'recur_enddate='.(int)$end.', '
1023                                                . 'recur_data='.$event['recur_data'].', '
1024                                                . 'recur_interval='.$event['recur_interval'].', '
1025                                                . "recur_exception='".(count($event['recur_exception'])>1?implode(',',$event['recur_exception']):(count($event['recur_exception'])==1?$event['recur_exception'][0]:''))."' "
1026                                                . 'WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1027                                }
1028
1029                        }
1030                        else
1031                        {
1032                                $this->stream->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1033                        }
1034                        // Custom fields
1035                        $this->stream->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1036
1037                        foreach($event as $name => $value)
1038                        {
1039                                if ($name[0] == '#' && strlen($value))
1040                                {
1041                                        $this->stream->query('INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) '
1042                                        . 'VALUES('.$event['id'].",'".addslashes(substr($name,1))."','".addslashes($value)."')",__LINE__,__FILE__);
1043                                }
1044                        }
1045        /*
1046                        $alarmcount = count($event['alarm']);
1047                        if ($alarmcount > 1)
1048                        {
1049                                // this should never happen, $event['alarm'] should only be set
1050                                // if creating a new event and uicalendar only sets up 1 alarm
1051                                // the user must use "Alarm Management" to create/establish multiple
1052                                // alarms or to edit/change an alarm
1053                                echo '<!-- how did this happen, too many alarms -->'."\n";
1054                                $this->stream->unlock();
1055                                return True;
1056                        }
1057
1058                        if ($alarmcount == 1)
1059                        {
1060
1061                                list($key,$alarm) = @each($event['alarm']);
1062
1063                                $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__);
1064                                $this->stream->query('SELECT LAST_INSERT_ID()');
1065                                $this->stream->next_record();
1066                                $alarm['id'] = $this->stream->f(0);
1067                        }
1068        */
1069                        print_debug('Event Saved: ID #',$event['id']);
1070
1071                        $this->stream->unlock();
1072
1073                        if (is_array($event['alarm']))
1074                        {
1075                                foreach ($event['alarm'] as $alarm)     // this are all new alarms
1076                                {
1077                                        $this->save_alarm($event['id'],$alarm);
1078                                }
1079                        }
1080                        $GLOBALS['phpgw_info']['cal_new_event_id'] = $event['id'];
1081                        $this->event = $event;
1082                        return True;
1083                }
1084
1085                function get_alarm($cal_id)
1086                {
1087        /* OLD-ALARM
1088                        $this->stream->query('SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id='.$id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
1089                        if($this->stream->num_rows())
1090                        {
1091                                while($this->stream->next_record())
1092                                {
1093                                        $alarm[$this->stream->f('cal_time')] = $this->stream->f('cal_text');
1094                                }
1095                                @reset($alarm);
1096                                return $alarm;
1097                        }
1098                        else
1099                        {
1100                                return False;
1101                        }
1102        */
1103                        $alarms = $this->read_alarms($cal_id);
1104                        $ret = False;
1105
1106                        foreach($alarms as $alarm)
1107                        {
1108                                if ($alarm['owner'] == $this->user || !$alarm['owner'])
1109                                {
1110                                        $ret[$alarm['time']] = $alarm['text'];
1111                                }
1112                        }
1113                        return $ret;
1114                }
1115
1116                function set_status($id,$owner,$status)
1117                {
1118                        $status_code_short = Array(
1119                                REJECTED =>     'R',
1120                                NO_RESPONSE     => 'U',
1121                                TENTATIVE       =>      'T',
1122                                ACCEPTED        =>      'A'
1123                        );
1124
1125                        $this->stream->query("UPDATE phpgw_cal_user SET cal_status='".$status_code_short[$status]."' WHERE cal_id=".$id." AND cal_login=".$owner,__LINE__,__FILE__);
1126        /* OLD-ALARM
1127                        if ($status == 'R')
1128                        {
1129                                $this->stream->query('UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id='.$id.' and cal_owner='.$owner,__LINE__,__FILE__);
1130                        }
1131        */
1132                        return True;
1133                }
1134
1135        // End of ICal style support.......
1136
1137                function group_search($owner=0)
1138                {
1139                        $owner = ($owner==$GLOBALS['phpgw_info']['user']['account_id']?0:$owner);
1140                        $groups = substr($GLOBALS['phpgw']->common->sql_search('phpgw_cal.groups',(int)$owner),4);
1141                        if (!$groups)
1142                        {
1143                                return '';
1144                        }
1145                        else
1146                        {
1147                                return "(phpgw_cal.is_public=2 AND (". $groups .')) ';
1148                        }
1149                }
1150
1151                function splittime_($time)
1152                {
1153                        $temp = array('hour','minute','second','ampm');
1154                        $time = strrev($time);
1155                        $second = (int)strrev(substr($time,0,2));
1156                        $minute = (int)strrev(substr($time,2,2));
1157                        $hour   = (int)strrev(substr($time,4));
1158                        $temp['second'] = (int)$second;
1159                        $temp['minute'] = (int)$minute;
1160                        $temp['hour']   = (int)$hour;
1161                        $temp['ampm']   = '  ';
1162
1163                        return $temp;
1164                }
1165
1166                function date_to_epoch($d)
1167                {
1168                        return $this->localdates(mktime(0,0,0,(int)(substr($d,4,2)),(int)(substr($d,6,2)),(int)(substr($d,0,4))));
1169                }
1170
1171                function list_dirty_events($lastmod=-1,$repeats=false)
1172                {
1173                        if(!isset($this->stream))
1174                        {
1175                                return False;
1176                        }
1177                        $lastmod = (int)  $lastmod;
1178                        $repeats = (bool) $repeats;
1179
1180                        $user_where = " AND phpgw_cal_user.cal_login = $this->user";
1181
1182                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
1183                        @reset($member_groups);
1184                        while($member_groups != False && list($key,$group_info) = each($member_groups))
1185                        {
1186                                $member[] = $group_info['account_id'];
1187                        }
1188                        @reset($member);
1189        //              $user_where .= ','.implode(',',$member);
1190                        //$user_where .= ')) ';
1191
1192                        if($this->debug)
1193                        {
1194                                echo '<!-- '.$user_where.' -->'."\n";
1195                        }
1196
1197                        if($lastmod > 0)
1198                        {
1199                                $wheremod = "AND mdatetime = $lastmod";
1200                        }
1201
1202                        $order_by = ' ORDER BY phpgw_cal.cal_id ASC';
1203                        if($this->debug)
1204                        {
1205                                echo "SQL : ".$user_where.$wheremod.$extra."<br>\n";
1206                        }
1207                        return $this->get_event_ids($repeats,$user_where.$wheremod.$extra.$order_by);
1208                }
1209
1210        /* OLD-ALARM
1211                function add_alarm($eventid,$alarm,$owner)
1212                {
1213                        $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__);
1214                        $this->stream->query('SELECT LAST_INSERT_ID()');
1215                        $this->stream->next_record();
1216                        return($this->stream->f(0));
1217                }
1218                function delete_alarm($alarmid)
1219                {
1220                        $this->stream->query('DELETE FROM phpgw_cal_alarm WHERE alarm_id='.$alarmid,__LINE__,__FILE__);
1221                }
1222        */
1223        }
Note: See TracBrowser for help on using the repository browser.