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

Revision 3968, 34.5 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1596 - Colocar campo que identifique o ultimo usuário que alterou agenda compartilhada

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