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

Revision 3976, 34.7 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1607 - Incluir um campo Observações na Agenda

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