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

Revision 7429, 36.7 KB checked in by antonio, 11 years ago (diff)

Ticket #3164 - Corrigido problema na criação de eventos na agenda antiga.

  • 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  \**************************************************************************/
15require_once dirname(__FILE__).'/../../services/class.servicelocator.php';
16
17if(isset($_SESSION['isCrun']))
18        $_SESSION['isCrun'] = false;
19   
20        if (@$GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_'])
21        {
22                return;
23        }
24
25        $GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_'] = True;
26
27        class socalendar_ extends socalendar__
28        {
29                var $deleted_events = Array();
30
31                var $cal_event;
32                var $today = Array('raw','day','month','year','full','dow','dm','bd');
33
34
35                function socalendar_()
36                {
37                        $this->socalendar__();
38
39                        if (!is_object($GLOBALS['phpgw']->asyncservice))
40                        {
41                                $GLOBALS['phpgw']->asyncservice = CreateObject('phpgwapi.asyncservice');
42                        }
43                        $this->async = &$GLOBALS['phpgw']->asyncservice;
44                }
45
46                function open($calendar='',$user='',$passwd='',$options='')
47                {
48                        if($user=='')
49                        {
50        //                      settype($user,'integer');
51                                $this->user = $GLOBALS['phpgw_info']['user']['account_id'];
52                        }
53                        elseif(is_int($user))
54                        {
55                                $this->user = $user;
56                        }
57                        elseif(is_string($user))
58                        {
59                                $this->user = $GLOBALS['phpgw']->accounts->name2id($user);
60                        }
61
62                        $this->stream = $GLOBALS['phpgw']->db;
63                        return $this->stream;
64                }
65
66                function popen($calendar='',$user='',$passwd='',$options='')
67                {
68                        return $this->open($calendar,$user,$passwd,$options);
69                }
70
71                function reopen($calendar,$options='')
72                {
73                        return $this->stream;
74                }
75
76                function close($options='')
77                {
78                        return True;
79                }
80
81                function create_calendar($calendar='')
82                {
83                        return $calendar;
84                }
85
86                function rename_calendar($old_name='',$new_name='')
87                {
88                        return $new_name;
89                }
90
91                function delete_calendar($calendar='')
92                {
93                        $this->stream->query('SELECT cal_id FROM phpgw_cal WHERE owner='.(int)$calendar,__LINE__,__FILE__);
94                        if($this->stream->num_rows())
95                        {
96                                while($this->stream->next_record())
97                                {
98                                        $this->delete_event((int)$this->stream->f('cal_id'));
99                                }
100                                $this->expunge();
101                        }
102                        $this->stream->lock(array('phpgw_cal_user'));
103                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_login='.(int)$calendar,__LINE__,__FILE__);
104                        $this->stream->unlock();
105
106                        return $calendar;
107                }
108
109                /*!
110                @function read_alarms
111                @abstract read the alarms of a calendar-event specified by $cal_id
112                @returns array of alarms with alarm-id as key
113                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too
114                */
115                function read_alarms($cal_id)
116                {
117                        $alarms = array();
118
119                        if ($jobs = $this->async->read('cal:'.(int)$cal_id.':%'))
120                        {
121                                foreach($jobs as $id => $job)
122                                {
123                                        $alarm         = $job['data'];  // text, enabled
124                                        $alarm['id']   = $id;
125                                        $alarm['time'] = $job['next'];
126
127                                        $alarms[$id] = $alarm;
128                                }
129                        }
130                        return $alarms;
131                }
132
133                /*!
134                @function read_alarm
135                @abstract read a single alarm specified by it's $id
136                @returns array with data of the alarm
137                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too
138                */
139                function read_alarm($id)
140                {
141                        if (!($jobs = $this->async->read($id)))
142                        {
143                                return False;
144                        }
145                        list($id,$job) = each($jobs);
146                        $alarm         = $job['data'];  // text, enabled
147                        $alarm['id']   = $id;
148                        $alarm['time'] = $job['next'];
149
150                        //echo "<p>read_alarm('$id')="; print_r($alarm); echo "</p>\n";
151                        return $alarm;
152                }
153
154/* Funcao save_alarm modificada para gerar alarmes repetidos, caso seja marcado um evento igualmente repetido. A funcao recebe qual o tipo
155de repeticao escolhido pelo usuario (diario, semanal, mensal, anual) e insere alarmes nas respectivas repeticoes do evento. */
156                /*!
157                @function save_alarm
158                @abstract saves a new or updated alarm
159                @syntax save_alarm($cal_id,$alarm,$id=False)
160                @param $cal_id Id of the calendar-entry
161                @param $alarm array with fields: text, owner, enabled, ..
162                @returns the id of the alarm
163                */
164                function save_alarm($cal_id,$alarm)
165                {
166
167                        if (!($id = $alarm['id']))
168                        {
169                                $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
170                                $alarms = $this->read_alarms($cal_id);  // find a free alarm#
171
172                                if($alarm['repeat'] == 1) // repeticao do tipo "Diariamente";
173                                {
174                                        $n = 0;
175
176                                        while(@isset($alarms[$id]));
177                                        {
178
179                                                $init_alarm = $alarm['init_rept'];
180                                                $end_alarm = $alarm['end_rept'];
181
182                                                if($end_alarm != 0)
183                                                {
184                                                        while($init_alarm <= $end_alarm)
185                                                        {
186                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
187                                                                $n++;
188                                                               
189                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
190
191                                                                unset($alarm['repeat']);
192                                                                unset($alarm['init_rept']);
193                                                                unset($alarm['end_rept']);
194                                                                unset($alarm['rpt_wdays']);
195
196                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
197                                                                {
198                                                                        return False;
199                                                                }
200
201                                                                $alarm['time'] += 86400;
202                                                                $init_alarm += 86400;
203                                                        }
204                                                }
205                                        }
206
207                                }elseif($alarm['repeat'] == 2) { // repeticao do tipo "Semanalmente";
208
209                                        $n = 0;
210
211                                        $init_alarm = $data_atual = $alarm['init_rept'];
212                                        $end_alarm = $alarm['end_rept'];
213
214                                        $rpt_alarm_wdays = $alarm['rpt_wdays'];
215
216                                        $divisor = 64;
217                                        $quociente = 0;
218                                        $resto = 0;
219
220                                        $dia_semana = date("w",$init_alarm);
221
222                                        switch($dia_semana)
223                                        {
224                                                case 0:
225                                                        $dia = array(
226                                                                        0 => 'domingo',
227                                                                        1 => 'segunda',
228                                                                        2 => 'terca',
229                                                                        3 => 'quarta',
230                                                                        4 => 'quinta',
231                                                                        5 => 'sexta',
232                                                                        6 => 'sabado'
233                                                        );
234                                                        break;
235                                                case 1:
236                                                        $dia = array(
237                                                                        0 => 'segunda',
238                                                                        1 => 'terca',
239                                                                        2 => 'quarta',
240                                                                        3 => 'quinta',
241                                                                        4 => 'sexta',
242                                                                        5 => 'sabado',
243                                                                        6 => 'domingo'
244                                                        );
245                                                        break;
246                                                case 2:
247                                                        $dia = array(
248                                                                        0 => 'terca',
249                                                                        1 => 'quarta',
250                                                                        2 => 'quinta',
251                                                                        3 => 'sexta',
252                                                                        4 => 'sabado',
253                                                                        5 => 'domingo',
254                                                                        6 => 'segunda'
255                                                        );
256                                                        break;
257                                                case 3:
258                                                        $dia = array(
259                                                                        0 => 'quarta',
260                                                                        1 => 'quinta',
261                                                                        2 => 'sexta',
262                                                                        3 => 'sabado',
263                                                                        4 => 'domingo',
264                                                                        5 => 'segunda',
265                                                                        6 => 'terca'
266                                                        );
267                                                        break;
268                                                case 4:
269                                                        $dia = array(
270                                                                        0 => 'quinta',
271                                                                        1 => 'sexta',
272                                                                        2 => 'sabado',
273                                                                        3 => 'domingo',
274                                                                        4 => 'segunda',
275                                                                        5 => 'terca',
276                                                                        6 => 'quarta'
277                                                        );
278                                                        break;
279                                                case 5:
280                                                        $dia = array(
281                                                                        0 => 'sexta',
282                                                                        1 => 'sabado',
283                                                                        2 => 'domingo',
284                                                                        3 => 'segunda',
285                                                                        4 => 'terca',
286                                                                        5 => 'quarta',
287                                                                        6 => 'quinta'
288                                                        );
289                                                        break;
290                                                case 6:
291                                                        $dia = array(
292                                                                        0 => 'sabado',
293                                                                        1 => 'domingo',
294                                                                        2 => 'segunda',
295                                                                        3 => 'terca',
296                                                                        4 => 'quarta',
297                                                                        5 => 'quinta',
298                                                                        6 => 'sexta'
299                                                        );
300                                                        break;
301                                        }
302
303
304                                        $dias_semana = array(
305                                                                64 => 'sabado',
306                                                                32 => 'sexta',
307                                                                16 => 'quinta',
308                                                                8 => 'quarta',
309                                                                4 => 'terca',
310                                                                2 => 'segunda',
311                                                                1 => 'domingo'
312                                                        );
313
314                                        $result = array();
315                                        do
316                                        {
317
318                                                $resto = ($rpt_alarm_wdays % $divisor);
319                                                $quociente = floor($rpt_alarm_wdays / $divisor);
320
321                                                if($quociente == 1)
322                                                {
323                                                        $result[] = $dias_semana[$divisor];
324
325                                                        $divisor = $divisor / 2;
326                                                        $rpt_alarm_wdays = $resto;
327
328                                                }else {
329
330                                                        while($rpt_alarm_wdays < $divisor)
331                                                        {
332                                                                $divisor = $divisor / 2;
333                                                        }
334
335                                                        $resto = ($rpt_alarm_wdays % $divisor);
336                                                        $quociente = floor($rpt_alarm_wdays / $divisor);
337
338                                                        if($quociente == 1)
339                                                        {
340
341                                                                $result[] = $dias_semana[$divisor];
342
343                                                                $divisor = $divisor / 2;
344                                                                $rpt_alarm_wdays = $resto;
345                                                        }
346                                                }
347
348                                        }
349                                        while($resto != 0);
350
351                                        krsort($result);
352
353                                        $week_num = 0;
354                                        $y = 0;
355
356                                        while(@isset($alarms[$id]));
357                                        {
358
359                                                if($end_alarm != 0)
360                                                {
361
362                                                        while($data_atual <= $end_alarm)
363                                                        {
364
365                                                                foreach($dia as $index => $value)
366                                                                {
367                                                                        if(in_array($value,$result))
368                                                                        {
369
370                                                                                $nova_data = $init_alarm + (86400 * ($index + (7 * $y)));
371
372                                                                                if($nova_data == $init_alarm){
373
374                                                                                        continue;
375                                                                                }
376
377                                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
378                                                                                $n++;
379
380                                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
381
382                                                                                unset($alarm['repeat']);
383                                                                                unset($alarm['init_rept']);
384                                                                                unset($alarm['end_rept']);
385                                                                                unset($alarm['rpt_wdays']);
386                                                                               
387                                                                                $data_atual = $nova_data;
388
389                                                                                if($data_atual > $end_alarm){
390                                                                                        if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
391                                                                                        {
392                                                                                                return False;
393                                                                                        }
394                                                                                        break;
395                                                                                }
396
397                                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
398                                                                                {
399                                                                                        return False;
400                                                                                }
401                                                                                        $alarm['time'] = $nova_data - $alarm['offset'] - $GLOBALS['phpgw']->datetime->tz_offset;
402                                                                        }
403                                                                }
404                                                                $y++;
405                                                        }
406                                                }
407                                        }
408
409                                }elseif($alarm['repeat'] == 3) { // repeticao do tipo "Mensalmente (por data)";
410
411                                        $n = 0;
412
413                                        $init_alarm = $alarm['init_rept'];
414                                        $end_alarm = $alarm['end_rept'];
415                                       
416                                        while(@isset($alarms[$id]));
417                                        {
418
419                                                if($end_alarm != 0)
420                                                {
421                                                        while($init_alarm <= $end_alarm)
422                                                        {
423
424                                                                $next_month = date("n",$init_alarm) + 1;
425                                                                $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']));
426
427                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
428                                                                $n++;
429
430                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
431
432                                                                unset($alarm['repeat']);
433                                                                unset($alarm['init_rept']);
434                                                                unset($alarm['end_rept']);
435                                                                unset($alarm['rpt_wdays']);
436
437                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
438                                                                {
439                                                                        return False;
440                                                                }
441
442                                                                $alarm['time'] = $next_alarm;
443                                                                $init_alarm = $next_alarm;
444
445                                                        }
446                                                }
447                                        }
448
449                                }elseif($alarm['repeat'] == 5) { // repeticao do tipo "Anualmente";
450
451                                        $n = 0;
452
453                                        $init_alarm = $alarm['init_rept'];
454                                        $end_alarm = $alarm['end_rept'];
455                                       
456                                        while(@isset($alarms[$id]));
457                                        {
458
459
460                                                if($end_alarm != 0)
461                                                {
462                                                        while($init_alarm < $end_alarm)
463                                                        {
464
465                                                                $next_year = date("Y",$init_alarm) + 1;
466                                                                $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);
467
468                                                                $id = 'cal:'.(int)$cal_id.':'.$n;
469                                                                $n++;
470
471                                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
472
473                                                                unset($alarm['repeat']);
474                                                                unset($alarm['init_rept']);
475                                                                unset($alarm['end_rept']);
476                                                                unset($alarm['rpt_wdays']);
477
478                                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
479                                                                {
480                                                                        return False;
481                                                                }
482
483                                                                $alarm['time'] = $next_alarm;
484                                                                $init_alarm = $next_alarm;
485                                                        }
486                                                }
487                                        }
488
489                                }else {
490                                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
491                                        $n = count($alarms);
492                                        while(@isset($alarms[$id]));
493                                        {
494
495                                                $id = 'cal:'.(int)$cal_id.':'.$n;
496
497                                                unset($alarm['repeat']);
498                                                unset($alarm['init_rept']);
499                                                unset($alarm['end_rept']);
500                                                unset($alarm['rpt_wdays']);
501
502                                                $alarm['cal_id'] = $cal_id;             // we need the back-reference
503                                                if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm))
504                                                {
505                                                        return False;
506                                                }
507
508                                                ++$n;
509                                        }
510                                }
511
512                        }
513                        else
514                        {
515                                $this->async->cancel_timer($id);
516                        }
517                        $alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset;       // time should be stored in server timezone
518                        return $id;
519                }
520
521                /*!
522                @function delete_alarms($cal_id)
523                @abstract delete all alarms of a calendar-entry
524                @returns the number of alarms deleted
525                */
526                function delete_alarms($cal_id)
527                {
528                        $alarms = $this->read_alarms($cal_id);
529
530                        foreach($alarms as $id => $alarm)
531                        {
532                                $this->async->cancel_timer($id);
533                        }
534                        return count($alarms);
535                }
536
537                /*!
538                @function delete_alarm($id)
539                @abstract delete one alarms identified by its id
540                @returns the number of alarms deleted
541                */
542                function delete_alarm($id)
543                {
544                        return $this->async->cancel_timer($id);
545                }
546
547                function fetch_event($event_id,$options='')
548                {
549                        if(!isset($this->stream))
550                        {
551                                return False;
552                        }
553
554                        $event_id = (int)$event_id;
555
556                        $this->stream->lock(array('phpgw_cal','phpgw_cal_user','phpgw_cal_repeats','phpgw_cal_extra'/* OLD-ALARM,'phpgw_cal_alarm'*/));
557
558                        $this->stream->query('SELECT * FROM phpgw_cal WHERE cal_id='.$event_id,__LINE__,__FILE__);
559
560                        if($this->stream->num_rows() > 0)
561                        {
562                                $this->event_init();
563
564                                $this->stream->next_record();
565                                // Load the calendar event data from the db into $event structure
566                                // Use http://www.php.net/manual/en/function.mcal-fetch-event.php as the reference
567                                $this->add_attribute('owner',(int)$this->stream->f('owner'));
568                                $this->add_attribute('id',(int)$this->stream->f('cal_id'));
569                                $this->add_attribute('type',$this->stream->f('cal_type'));
570                                $this->set_class((int)$this->stream->f('is_public'));
571                                $this->set_category($this->stream->f('category'));
572                                $this->set_title(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('title'))));
573                                $this->set_description(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('description'))));
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('organizer',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('organizer'))));
578                                $this->add_attribute('reference',(int)$this->stream->f('reference'));
579
580                                // This is the preferred method once everything is normalized...
581                                //$this->event->alarm = (int)$this->stream->f('alarm');
582                                // But until then, do it this way...
583                                //Legacy Support (New)
584
585                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('datetime'));
586                                $this->set_start($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
587
588                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('mdatetime'));
589                                $this->set_date('modtime',$datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
590
591                                $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('edatetime'));
592                                $this->set_end($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']);
593
594                        //Legacy Support
595                                $this->add_attribute('priority',(int)$this->stream->f('priority'));
596                                if($this->stream->f('cal_group') || $this->stream->f('groups') != 'NULL')
597                                {
598                                        $groups = explode(',',$this->stream->f('groups'));
599                                        for($j=1;$j<count($groups) - 1;$j++)
600                                        {
601                                                $this->add_attribute('groups',$groups[$j],$j-1);
602                                        }
603                                }
604
605                                $this->stream->query('SELECT * FROM phpgw_cal_repeats WHERE cal_id='.$event_id,__LINE__,__FILE__);
606                                if($this->stream->num_rows())
607                                {
608                                        $this->stream->next_record();
609
610                                        $this->add_attribute('recur_type',(int)$this->stream->f('recur_type'));
611                                        $this->add_attribute('recur_interval',(int)$this->stream->f('recur_interval'));
612                                        $enddate = $this->stream->f('recur_enddate');
613                                        if($enddate != 0 && $enddate != Null)
614                                        {
615                                                $datetime = $GLOBALS['phpgw']->datetime->localdates($enddate);
616                                                $this->add_attribute('recur_enddate',$datetime['year'],'year');
617                                                $this->add_attribute('recur_enddate',$datetime['month'],'month');
618                                                $this->add_attribute('recur_enddate',$datetime['day'],'mday');
619                                                $this->add_attribute('recur_enddate',$datetime['hour'],'hour');
620                                                $this->add_attribute('recur_enddate',$datetime['minute'],'min');
621                                                $this->add_attribute('recur_enddate',$datetime['second'],'sec');
622                                        }
623                                        else
624                                        {
625                                                $this->add_attribute('recur_enddate',0,'year');
626                                                $this->add_attribute('recur_enddate',0,'month');
627                                                $this->add_attribute('recur_enddate',0,'mday');
628                                                $this->add_attribute('recur_enddate',0,'hour');
629                                                $this->add_attribute('recur_enddate',0,'min');
630                                                $this->add_attribute('recur_enddate',0,'sec');
631                                        }
632                                        $this->add_attribute('recur_enddate',0,'alarm');
633                                        if($this->debug)
634                                        {
635                                                echo 'Event ID#'.$this->event['id'].' : Enddate = '.$enddate."<br>\n";
636                                        }
637                                        $this->add_attribute('recur_data',$this->stream->f('recur_data'));
638
639                                        $exception_list = $this->stream->f('recur_exception');
640                                        $exceptions = Array();
641                                        if(strpos(' '.$exception_list,','))
642                                        {
643                                                $exceptions = explode(',',$exception_list);
644                                        }
645                                        elseif($exception_list != '')
646                                        {
647                                                $exceptions[]= $exception_list;
648                                        }
649                                        $this->add_attribute('recur_exception',$exceptions);
650                                }
651
652                        //Legacy Support
653                                $this->stream->query('SELECT * FROM phpgw_cal_user WHERE cal_id='.$event_id,__LINE__,__FILE__);
654                                if($this->stream->num_rows())
655                                {
656                                        while($this->stream->next_record())
657                                        {
658                                                if((int)$this->stream->f('cal_login') == (int)$this->user)
659                                                {
660                                                        $this->add_attribute('users_status',$this->stream->f('cal_status'));
661                                                }
662                                                $this->add_attribute('participants',$this->stream->f('cal_status'),(int)$this->stream->f('cal_login'));
663                                        }
664                                }
665
666                        // Custom fields
667                                $this->stream->query('SELECT * FROM phpgw_cal_extra WHERE cal_id='.$event_id,__LINE__,__FILE__);
668                                if($this->stream->num_rows())
669                                {
670                                        while($this->stream->next_record())
671                                        {
672                                                $this->add_attribute('#'.$this->stream->f('cal_extra_name'),$this->stream->f('cal_extra_value'));
673                                        }
674                                }
675
676        /* OLD-ALARM
677                                if($this->event['reference'])
678                                {
679                                        // What is event['reference']???
680                                        $alarm_cal_id = $event_id.','.$this->event['reference'];
681                                }
682                                else
683                                {
684                                        $alarm_cal_id = $event_id;
685                                }
686
687                                //echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n";
688                                //$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__);
689                                $this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id='.$event_id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
690                                if($this->stream->num_rows())
691                                {
692                                        while($this->stream->next_record())
693                                        {
694                                                $this->event['alarm'][] = Array(
695                                                        'id'            => (int)$this->stream->f('alarm_id'),
696                                                        'time'  => (int)$this->stream->f('cal_time'),
697                                                        'text'  => $this->stream->f('cal_text'),
698                                                        'enabled'       => (int)$this->stream->f('alarm_enabled')
699                                                );
700                                        }
701                                }
702        */
703                        }
704                        else
705                        {
706                                $this->event = False;
707                        }
708
709                        $this->stream->unlock();
710
711                        if ($this->event)
712                        {
713                                $this->event['alarm'] = $this->read_alarms($event_id);
714
715                                if($this->event['reference'])
716                                {
717                                        $this->event['alarm'] += $this->read_alarms($event_id);
718                                }
719                        }
720                        return $this->event;
721                }
722
723                function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$extra='',$tz_offset=0,$owner_id=0)
724                {
725                        if(!isset($this->stream))
726                        {
727                                return False;
728                        }
729
730                        $datetime = mktime(0,0,0,$startMonth,$startDay,$startYear) - $tz_offset;
731
732                        $user_where = ' AND (phpgw_cal_user.cal_login in (';
733                        if(is_array($owner_id) && count($owner_id))
734                        {
735                                $user_where .= implode(',',$owner_id);
736                        }
737                        else
738                        {
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,$sendMail = true, $importAccount = false)
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                                $returned_id = $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']."') RETURNING cal_id",__LINE__,__FILE__);
948
949                                // get_last_insert_id não funciona direito com Postgres 9
950                                $event['id'] =  $returned_id->fields['cal_id']; //$this->stream->get_last_insert_id('phpgw_cal','cal_id');
951                                $last_status = true;
952                        }
953
954                        $date = $this->maketime($event['start']) - $GLOBALS['phpgw']->datetime->tz_offset;
955                        $enddate = $this->maketime($event['end']) - $GLOBALS['phpgw']->datetime->tz_offset;
956                        $today = time() - $GLOBALS['phpgw']->datetime->tz_offset;
957
958
959                                if ($event['type'] == 'hourAppointment')
960                                        $type = 'H';
961                                else if($event['type'] == 'privateHiddenFields'){
962                                        $type = 'P';
963                                }else
964                                        $type = 'E';
965                       
966
967                        $sql = 'UPDATE phpgw_cal SET '
968                                        . 'owner='.(int)$event['owner'].', '
969                                        . 'datetime='.(int)$date.', '
970                                        . 'mdatetime='.(int)$today.', '
971                                        . 'edatetime='.(int)$enddate.', '
972                                        . 'priority='.(int)$event['priority'].', '
973                                        . "category='".$this->stream->db_addslashes($event['category'])."', "
974                                        . "cal_type='".$this->stream->db_addslashes($type)."', "
975                                        . 'is_public='.(int)$event['public'].', '
976                                        . "title='".$this->stream->db_addslashes($event['title'])."', "
977                                        . "description='".$this->stream->db_addslashes($event['description'])."', "
978                                        . "ex_participants='".$this->stream->db_addslashes($event['ex_participants'])."', "
979                                        . "organizer='".$this->stream->db_addslashes($event['organizer'])."', "
980                                        . "location='".$this->stream->db_addslashes($event['location'])."', "
981                                        . ($event['groups']?"groups='".(count($event['groups'])>1?implode(',',$event['groups']):','.$event['groups'][0].',')."', ":'')
982                                        . 'reference='.(int)$event['reference'].' '
983                                        . ',last_status = '.($last_status ? "'N'" : "'U'").',last_update = '.time()."000". ' '
984                                        . 'WHERE cal_id='.(int)$event['id'];
985
986                        $this->stream->query($sql,__LINE__,__FILE__);
987
988                        $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
989
990                        @reset($event['participants']);
991                        while (list($key,$value) = @each($event['participants']))
992                        {
993                                if((int)$key == $event['owner'])
994                                {
995                                        $value = 'A';
996                                }
997                                $this->stream->query('INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) '
998                                        . 'VALUES('.(int)$event['id'].','.(int)$key.",'".$this->stream->db_addslashes($value)."')",__LINE__,__FILE__);
999                        }
1000
1001                        if($event['recur_type'] != MCAL_RECUR_NONE)
1002                        {
1003                                if($event['recur_enddate']['month'] != 0 && $event['recur_enddate']['mday'] != 0 && $event['recur_enddate']['year'] != 0)
1004                                {
1005                                        $end = $this->maketime($event['recur_enddate']) - $GLOBALS['phpgw']->datetime->tz_offset;
1006                                }
1007                                else
1008                                {
1009                                        $end = 0;
1010                                }
1011
1012                                $this->stream->query('SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__);
1013                                $this->stream->next_record();
1014                                $num_rows = $this->stream->f(0);
1015                                if($num_rows == 0)
1016                                {
1017                                        $this->stream->query('INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) '
1018                                                .'VALUES('.(int)$event['id'].','.$event['recur_type'].','.(int)$end.','.$event['recur_data'].','.$event['recur_interval'].')',__LINE__,__FILE__);
1019                                }
1020                                else
1021                                {
1022                                        $this->stream->query('UPDATE phpgw_cal_repeats '
1023                                                . 'SET recur_type='.$event['recur_type'].', '
1024                                                . 'recur_enddate='.(int)$end.', '
1025                                                . 'recur_data='.$event['recur_data'].', '
1026                                                . 'recur_interval='.$event['recur_interval'].', '
1027                                                . "recur_exception='".(count($event['recur_exception'])>1?implode(',',$event['recur_exception']):(count($event['recur_exception'])==1?$event['recur_exception'][0]:''))."' "
1028                                                . 'WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1029                                }
1030
1031                        }
1032                        else
1033                        {
1034                                $this->stream->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1035                        }
1036                        // Custom fields
1037                        $this->stream->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$event['id'],__LINE__,__FILE__);
1038
1039                        foreach($event as $name => $value)
1040                        {
1041                                if ($name[0] == '#' && strlen($value))
1042                                {
1043                                        $this->stream->query('INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) '
1044                                        . 'VALUES('.$event['id'].",'".addslashes(substr($name,1))."','".addslashes($value)."')",__LINE__,__FILE__);
1045                                }
1046                        }
1047        /*
1048                        $alarmcount = count($event['alarm']);
1049                        if ($alarmcount > 1)
1050                        {
1051                                // this should never happen, $event['alarm'] should only be set
1052                                // if creating a new event and uicalendar only sets up 1 alarm
1053                                // the user must use "Alarm Management" to create/establish multiple
1054                                // alarms or to edit/change an alarm
1055                                echo '<!-- how did this happen, too many alarms -->'."\n";
1056                                $this->stream->unlock();
1057                                return True;
1058                        }
1059
1060                        if ($alarmcount == 1)
1061                        {
1062
1063                                list($key,$alarm) = @each($event['alarm']);
1064
1065                                $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__);
1066                                $this->stream->query('SELECT LAST_INSERT_ID()');
1067                                $this->stream->next_record();
1068                                $alarm['id'] = $this->stream->f(0);
1069                        }
1070        */
1071                        print_debug('Event Saved: ID #',$event['id']);
1072
1073                        $this->stream->unlock();
1074
1075                        if (is_array($event['alarm']))
1076                        {
1077                                foreach ($event['alarm'] as $alarm)     // this are all new alarms
1078                                {
1079                                        $this->save_alarm($event['id'],$alarm);
1080                                }
1081                        }
1082                        $GLOBALS['phpgw_info']['cal_new_event_id'] = $event['id'];
1083                        $this->event = $event;
1084
1085                        if($sendMail)
1086                        {
1087                            if($importAccount)
1088                                $this->sendEmailEvendAccepted($event,$importAccount);
1089                            else
1090                                $this->sendEmailEvendAccepted($event);
1091                        }
1092       
1093                        return True;
1094                }
1095
1096                function sendEmailEvendAccepted($event,$importAccount = false)
1097                {
1098
1099                    if(!$importAccount)
1100                        $importAccount['mail'] = $GLOBALS['phpgw_info']['user']['email'];
1101
1102                    //verifica se eh um evento externo se sim envia o vcard de aceitação em anexo
1103                    if($event['owner'] == '-2')
1104                    {
1105                        $mailService = ServiceLocator::getService('mail');
1106
1107                        $tmpbody.= "<b>".lang("Event Accepted")."</b> ";
1108                        $tmpbody.= "<br><br>".lang("name").": ".$event['title']."<br>";
1109                        $tmpbody.= "<br>".lang("description").": ".$event['description']."<br>";
1110
1111                        $ev[0] = $event;
1112
1113                        $tmpattach=  $GLOBALS['calendar']->bocalendar->create_vcard($ev,'REPLY',true,$importAccount);
1114                       
1115                        $mailService->addStringAttachment($tmpattach, 'suggestion.ics', 'application/ics');
1116                        $mailService->addStringAttachment($tmpattach, 'vcalendar.ics', 'utf-8', 'text/calendar; charset=utf-8; method=REPLY;', '7bit', '');
1117                 
1118                        if(strchr($event['organizer'],'@') && strchr($event['organizer'],'<') && strchr($event['organizer'],'>'))
1119                        {
1120                            $mail = substr($event['organizer'], strpos($event['organizer'],'<'), strpos($event['organizer'],'>'));
1121                            $mail = str_replace('<','', str_replace('>','',$mail));
1122                        }
1123
1124                        if($mail)
1125                            $mailService->sendMail($mail,$importAccount['mail'],$event['title'],$tmpbody);
1126
1127                    }
1128                    else
1129                        $GLOBALS['calendar']->bocalendar->send_update(MSG_ACCEPTED, $event['participants'],$event);
1130
1131                    return;
1132                }
1133
1134                function get_alarm($cal_id)
1135                {
1136        /* OLD-ALARM
1137                        $this->stream->query('SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id='.$id.' AND cal_owner='.$this->user,__LINE__,__FILE__);
1138                        if($this->stream->num_rows())
1139                        {
1140                                while($this->stream->next_record())
1141                                {
1142                                        $alarm[$this->stream->f('cal_time')] = $this->stream->f('cal_text');
1143                                }
1144                                @reset($alarm);
1145                                return $alarm;
1146                        }
1147                        else
1148                        {
1149                                return False;
1150                        }
1151        */
1152                        $alarms = $this->read_alarms($cal_id);
1153                        $ret = False;
1154
1155                        foreach($alarms as $alarm)
1156                        {
1157                                if ($alarm['owner'] == $this->user || !$alarm['owner'])
1158                                {
1159                                        $ret[$alarm['time']] = $alarm['text'];
1160                                }
1161                        }
1162                        return $ret;
1163                }
1164
1165                function set_status($id,$owner,$status)
1166                {
1167                        $status_code_short = Array(
1168                                REJECTED =>     'R',
1169                                NO_RESPONSE     => 'U',
1170                                TENTATIVE       =>      'T',
1171                                ACCEPTED        =>      'A'
1172                        );
1173
1174                        $this->stream->query("UPDATE phpgw_cal_user SET cal_status='".$status_code_short[$status]."' WHERE cal_id=".$id." AND cal_login=".$owner,__LINE__,__FILE__);
1175        /* OLD-ALARM
1176                        if ($status == 'R')
1177                        {
1178                                $this->stream->query('UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id='.$id.' and cal_owner='.$owner,__LINE__,__FILE__);
1179                        }
1180        */
1181                        return True;
1182                }
1183
1184        // End of ICal style support.......
1185
1186                function group_search($owner=0)
1187                {
1188                        $owner = ($owner==$GLOBALS['phpgw_info']['user']['account_id']?0:$owner);
1189                        $groups = substr($GLOBALS['phpgw']->common->sql_search('phpgw_cal.groups',(int)$owner),4);
1190                        if (!$groups)
1191                        {
1192                                return '';
1193                        }
1194                        else
1195                        {
1196                                return "(phpgw_cal.is_public=2 AND (". $groups .')) ';
1197                        }
1198                }
1199
1200                function splittime_($time)
1201                {
1202                        $temp = array('hour','minute','second','ampm');
1203                        $time = strrev($time);
1204                        $second = (int)strrev(substr($time,0,2));
1205                        $minute = (int)strrev(substr($time,2,2));
1206                        $hour   = (int)strrev(substr($time,4));
1207                        $temp['second'] = (int)$second;
1208                        $temp['minute'] = (int)$minute;
1209                        $temp['hour']   = (int)$hour;
1210                        $temp['ampm']   = '  ';
1211
1212                        return $temp;
1213                }
1214
1215                function date_to_epoch($d)
1216                {
1217                        return $this->localdates(mktime(0,0,0,(int)(substr($d,4,2)),(int)(substr($d,6,2)),(int)(substr($d,0,4))));
1218                }
1219
1220                function list_dirty_events($lastmod=-1,$repeats=false)
1221                {
1222                        if(!isset($this->stream))
1223                        {
1224                                return False;
1225                        }
1226                        $lastmod = (int)  $lastmod;
1227                        $repeats = (bool) $repeats;
1228
1229                        $user_where = " AND phpgw_cal_user.cal_login = $this->user";
1230
1231                        $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
1232                        @reset($member_groups);
1233                        while($member_groups != False && list($key,$group_info) = each($member_groups))
1234                        {
1235                                $member[] = $group_info['account_id'];
1236                        }
1237                        @reset($member);
1238        //              $user_where .= ','.implode(',',$member);
1239                        //$user_where .= ')) ';
1240
1241                        if($this->debug)
1242                        {
1243                                echo '<!-- '.$user_where.' -->'."\n";
1244                        }
1245
1246                        if($lastmod > 0)
1247                        {
1248                                $wheremod = "AND mdatetime = $lastmod";
1249                        }
1250
1251                        $order_by = ' ORDER BY phpgw_cal.cal_id ASC';
1252                        if($this->debug)
1253                        {
1254                                echo "SQL : ".$user_where.$wheremod.$extra."<br>\n";
1255                        }
1256                        return $this->get_event_ids($repeats,$user_where.$wheremod.$extra.$order_by);
1257                }
1258
1259        /* OLD-ALARM
1260                function add_alarm($eventid,$alarm,$owner)
1261                {
1262                        $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__);
1263                        $this->stream->query('SELECT LAST_INSERT_ID()');
1264                        $this->stream->next_record();
1265                        return($this->stream->f(0));
1266                }
1267                function delete_alarm($alarmid)
1268                {
1269                        $this->stream->query('DELETE FROM phpgw_cal_alarm WHERE alarm_id='.$alarmid,__LINE__,__FILE__);
1270                }
1271        */
1272        }
Note: See TracBrowser for help on using the repository browser.