source: companies/serpro/calendar/inc/class.socalendar_sql.inc.php @ 903

Revision 903, 33.9 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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