source: trunk/calendar/inc/class.socalendar.inc.php @ 7673

Revision 7673, 13.7 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Calendar                                                    *
4  * http://www.eGroupWare.org                                                *
5  * Maintained and further developed by RalfBecker@outdoor-training.de       *
6  * Based on Webcalendar by Craig Knudsen <cknudsen@radix.net>               *
7  *          http://www.radix.net/~cknudsen                                  *
8  * Originaly modified by Mark Peters <skeeter@phpgroupware.org>             *
9  * --------------------------------------------                             *
10  *  This program is free software; you can redistribute it and/or modify it *
11  *  under the terms of the GNU General Public License as published by the   *
12  *  Free Software Foundation; either version 2 of the License, or (at your  *
13  *  option) any later version.                                              *
14  \**************************************************************************/
15
16
17        class socalendar
18        {
19//              var $debug = True;
20                var $debug = False;
21                var $cal;
22                var $db;
23                var $owner;
24                var $g_owner;
25                var $is_group = False;
26                var $datetime;
27                var $filter;
28                var $cat_id;
29               
30                function socalendar($param)
31                {
32                        $this->db = $GLOBALS['phpgw']->db;
33                        if(!is_object($GLOBALS['phpgw']->datetime))
34                        {
35                                $GLOBALS['phpgw']->datetime = createobject('phpgwapi.date_time');
36                        }
37
38                        $this->owner = (!isset($param['owner']) || $param['owner'] == 0?$GLOBALS['phpgw_info']['user']['account_id']:$param['owner']);
39                        $this->filter = (isset($param['filter']) && $param['filter'] != ''?$param['filter']:$this->filter);
40                        $this->cat_id = (isset($param['category']) && $param['category'] != ''?$param['category']:$this->cat_id);
41                        if(isset($param['g_owner']) && is_array($param['g_owner']))
42                        {
43                                $this->is_group = True;
44                                $this->g_owner = $param['g_owner'];
45                        }
46                        if($this->debug)
47                        {
48                                echo '<!-- SO Filter : '.$this->filter.' -->'."\n";
49                                echo '<!-- SO cat_id : '.$this->cat_id.' -->'."\n";
50                        }
51                        $this->cal = CreateObject('calendar.socalendar_');
52                        $this->open_box($this->owner);
53                }
54               
55                // It returns uidNumber and cn ( Retorna o uidNumber e o cn )
56                function search_uidNumber($mail)
57                {
58                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
59                        $justthese = array("uidNumber","cn","mail");
60                        $search = ldap_search($connection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail, $justthese);
61                        $result = ldap_get_entries($connection, $search);
62                        ldap_close($connection);
63                        return $result;
64                }
65
66                function open_box($owner)
67                {
68                        $this->cal->open('INBOX',(int)$owner);
69                }
70
71                function maketime($time)
72                {
73                        return mktime($time['hour'],$time['min'],$time['sec'],$time['month'],$time['mday'],$time['year']);
74                }
75
76                function read_entry($id)
77                {
78                        return $this->cal->fetch_event($id);
79                }
80
81                function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$owner_id=0)
82                {
83                        $extra = '';
84                        $extra .= (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'');
85                        //$extra .= ($this->cat_id?"AND phpgw_cal.category like '%".$this->cat_id."%' ":'');
86                        if ($this->cat_id)
87                        {
88                                if (!is_object($GLOBALS['phpgw']->categories))
89                                {
90                                        $GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
91                                }
92                                $cats = $GLOBALS['phpgw']->categories->return_all_children($this->cat_id);
93                                $extra .= "AND (phpgw_cal.category".(count($cats) > 1 ? " IN ('".implode("','",$cats)."')" : '=\''.(int)$this->cat_id."'");
94                                foreach($cats as $cat)
95                                {
96                                        $extra .= " OR phpgw_cal.category LIKE '$cat,%' OR phpgw_cal.category LIKE '%,$cat,%' OR phpgw_cal.category LIKE '%,$cat'";
97                                }
98                                $extra .= ') ';
99                        }
100                        if($owner_id)
101                        {
102                                return $this->cal->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay,$extra,$GLOBALS['phpgw']->datetime->tz_offset,$owner_id);
103                        }
104                        else
105                        {
106                                return $this->cal->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay,$extra,$GLOBALS['phpgw']->datetime->tz_offset);
107                        }
108                }
109
110                function list_repeated_events($syear,$smonth,$sday,$eyear,$emonth,$eday,$owner_id=0)
111                {
112                        if(!$owner_id)
113                        {
114                                $owner_id = $this->is_group ? $this->g_owner : $this->owner;
115                        }
116                        if($GLOBALS['phpgw_info']['server']['calendar_type'] != 'sql' ||
117                                !count($owner_id))      // happens with empty groups
118                        {
119                                return Array();
120                        }
121
122                        $starttime = mktime(0,0,0,$smonth,$sday,$syear) - $GLOBALS['phpgw']->datetime->tz_offset;
123                        $endtime = mktime(23,59,59,$emonth,$eday,$eyear) - $GLOBALS['phpgw']->datetime->tz_offset;
124                        $sql = "AND phpgw_cal_user.cal_login IN (".
125                                (is_array($owner_id) ? implode(',',$owner_id) : $owner_id).')';
126
127//                      $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
128//                      @reset($member_groups);
129//                      while(list($key,$group_info) = each($member_groups))
130//                      {
131//                              $member[] = $group_info['account_id'];
132//                      }
133//                      @reset($member);
134//                      $sql .= ','.implode(',',$member).') ';
135//                      $sql .= 'AND (phpgw_cal.datetime <= '.$starttime.') ';
136//                      $sql .= 'AND (((phpgw_cal_repeats.recur_enddate >= '.$starttime.') AND (phpgw_cal_repeats.recur_enddate <= '.$endtime.')) OR (phpgw_cal_repeats.recur_enddate=0))) '
137                        $sql .= ' AND (phpgw_cal_repeats.recur_enddate >= '.$starttime.' OR phpgw_cal_repeats.recur_enddate=0) '
138                                . (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'')
139                                . ($this->cat_id?"AND phpgw_cal.category like '%".$this->cat_id."%' ":'')
140                                . 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC';
141
142                        if($this->debug)
143                        {
144                                echo '<!-- SO list_repeated_events : SQL : '.$sql.' -->'."\n";
145                        }
146
147                        return $this->get_event_ids(True,$sql);
148                }
149
150                function list_events_keyword($keywords,$members='')
151                {
152                        if (!$members)
153                        {
154                                $members[] = $this->owner;
155                        }
156                        $sql = 'AND (phpgw_cal_user.cal_login IN ('.implode(',',$members).')) AND '.
157                                '(phpgw_cal_user.cal_login=' . (int)$this->owner . ' OR phpgw_cal.is_public=1) AND (';
158
159                        $words = preg_split('/ /',$keywords);
160                        foreach($words as $i => $word)
161                        {
162                                $sql .= $i > 0 ? ' OR ' : '';
163                                $sql .= "(UPPER(phpgw_cal.title) LIKE UPPER('%".addslashes($word)."%') OR "
164                                                . "UPPER(phpgw_cal.description) LIKE UPPER('%".addslashes($word)."%') OR "
165                                                . "UPPER(phpgw_cal.location) LIKE UPPER('%".addslashes($word)."%') OR "
166                                                . "UPPER(phpgw_cal_extra.cal_extra_value) LIKE UPPER('%".addslashes($word)."%'))";
167                        }
168                        $sql .= ') ';
169
170                        $sql .= (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'');
171                        $sql .= ($this->cat_id? "AND (phpgw_cal.category='$this->cat_id' OR phpgw_cal.category like '%,".$this->cat_id.",%') ":'');
172                        $sql .= 'ORDER BY phpgw_cal.datetime DESC, phpgw_cal.edatetime DESC, phpgw_cal.priority ASC';
173
174                        return $this->get_event_ids(False,$sql,True);
175                }
176
177                function read_from_store($startYear,$startMonth,$startDay,$endYear='',$endMonth='',$endDay='')
178                {
179                        $events = $this->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay);
180                        $events_cached = Array();
181            $events_count = count($events);
182                        for($i=0;$i<$events_count;++$i)
183                        {
184                                $events_cached[] = $this->read_entry($events[$i]);
185                        }
186                        return $events_cached;
187                }
188
189                function get_event_ids($search_repeats=False, $sql='',$search_extra=False)
190                {
191                        return $this->cal->get_event_ids($search_repeats,$sql,$search_extra);
192                }
193
194                function find_uid($uid)
195                {
196                        $uid_ = substr($uid, strpos($uid,"-")+1);                               
197                        $uid_ = substr($uid_, 0, strpos($uid_,"-"));                           
198                       
199                        $sql = " AND (phpgw_cal.cal_id = '". (int)$uid_."') ";
200
201                        $found = $this->cal->get_event_ids(False,$sql);
202                        if(!$found)
203                        {
204                                $found = $this->cal->get_event_ids(True,$sql);
205                        }
206                        if(is_array($found))
207                        {
208                                return $found[0];
209                        }
210                        else
211                        {
212                                return False;
213                        }
214                }
215
216               
217                /*
218                 * @abstract: Retorna o email do organizer do evento.
219                 */
220                function get_mail_organizer($cal_id)
221                {
222                        $sql = 'SELECT organizer FROM phpgw_cal WHERE cal_id =' . $cal_id;     
223                        $this->cal->stream->query($sql,__LINE__,__FILE__);
224                       
225                        while($this->cal->stream->next_record())
226                        {
227                                $ret[0] = $this->cal->stream->f('organizer');
228                        }
229                       
230                        $return = $ret[0];
231                        $pos = strpos($return, "<");
232                        $return = substr($return, $pos);
233                        $return = str_replace('"','',$return);
234                        $return = str_replace('>','',$return);
235                        $return = str_replace('<','',$return);
236                       
237                        return $return;
238                }
239               
240               
241                /*
242                 * @abstract: Retorna os emails dos participantes do evento.
243                 */
244                function get_mail_participants($cal_id)
245                {
246               
247                }
248               
249               
250                /*
251                 * @abstract: busca no banco pra verificar se já existe um compromisso com o mesmo cal_id
252                 */
253                function find_cal_id($cal_id)
254                {
255
256                        $uid_ = substr($cal_id, strpos($cal_id,"-")+1);
257                        $uid_ = substr($uid_, 0, strpos($uid_,"-"));
258
259                        $sql = " AND phpgw_cal.cal_id = '". (int)$uid_."' OR phpgw_cal.uid = '". $cal_id."' ";
260
261                        $found = $this->cal->get_event_ids(False,$sql);
262                        if(!$found)
263                        {
264                                $found = $this->cal->get_event_ids(True,$sql);
265                        }
266                        if(is_array($found))
267                        {
268                                return $found[0];
269                        }
270                        else
271                        {
272                                return False;
273                        }
274                }
275               
276
277                function add_entry(&$event , $sendMail = true, $importAccount = false)
278                {
279                        return $this->cal->save_event($event,$sendMail,$importAccount);
280                }
281
282                function save_alarm($cal_id,$alarm,$id=0)
283                {
284                        return $this->cal->save_alarm($cal_id,$alarm,$id);
285                }
286
287                function delete_alarm($id)
288                {
289                        return $this->cal->delete_alarm($id);
290                }
291
292                function delete_alarms($cal_id)
293                {
294                        return $this->cal->delete_alarms($cal_id);
295                }
296
297                function delete_entry($id)
298                {
299                        return $this->cal->delete_event($id);
300                }
301
302                function expunge()
303                {
304                        $this->cal->expunge();
305                }
306
307                function delete_calendar($owner)
308                {
309                        $this->cal->delete_calendar($owner);
310                }
311
312                function change_owner($account_id,$new_owner)
313                {
314                        if($GLOBALS['phpgw_info']['server']['calendar_type'] == 'sql')
315                        {
316                                $db2 = $this->cal->stream;
317                                $this->cal->stream->query('SELECT cal_id FROM phpgw_cal_user WHERE cal_login='.$account_id,__LINE__,__FILE__);
318                                while($this->cal->stream->next_record())
319                                {
320                                        $id = $this->cal->stream->f('cal_id');
321                                        $db2->query('SELECT count(*) FROM phpgw_cal_user WHERE cal_id='.$id.' AND cal_login='.$new_owner,__LINE__,__FILE__);
322                                        $db2->next_record();
323                                        if($db2->f(0) == 0)
324                                        {
325                                                $db2->query('UPDATE phpgw_cal_user SET cal_login='.$new_owner.' WHERE cal_id='.$id.' AND cal_login='.$account_id,__LINE__,__FILE__);
326                                        }
327                                        else
328                                        {
329                                                $db2->query('DELETE FROM phpgw_cal_user WHERE cal_id='.$id.' AND cal_login='.$account_id,__LINE__,__FILE__);
330                                        }
331                                }
332                                $this->cal->stream->query('UPDATE phpgw_cal SET owner='.$new_owner.' WHERE owner='.$account_id,__LINE__,__FILE__);
333                        }
334                }
335
336                function set_status($id,$status,$user = false)
337                {
338                    if($user) $this->cal->set_status($id,$user,$status);
339                    else $this->cal->set_status($id,$this->owner,$status);
340                }
341
342                function get_alarm($cal_id)
343                {
344                        if (!method_exists($this->cal,'get_alarm'))
345                        {
346                                return False;
347                        }
348                        return $this->cal->get_alarm($cal_id);
349                }
350
351                function read_alarm($id)
352                {
353                        if (!method_exists($this->cal,'read_alarm'))
354                        {
355                                return False;
356                        }
357                        return $this->cal->read_alarm($id);
358                }
359
360                function read_alarms($cal_id)
361                {
362                        if (!method_exists($this->cal,'read_alarms'))
363                        {
364                                return False;
365                        }
366                        return $this->cal->read_alarms($cal_id);
367                }
368
369                function find_recur_exceptions($event_id)
370                {
371                        if($GLOBALS['phpgw_info']['server']['calendar_type'] == 'sql')
372                        {
373                                $arr = Array();
374                                $this->cal->query('SELECT datetime FROM phpgw_cal WHERE reference='.$event_id,__LINE__,__FILE__);
375                                if($this->cal->num_rows())
376                                {
377                                        while($this->cal->next_record())
378                                        {
379                                                $arr[] = (int)$this->cal->f('datetime');
380                                        }
381                                }
382                                if(count($arr) == 0)
383                                {
384                                        return False;
385                                }
386                                else
387                                {
388                                        return $arr;
389                                }
390                        }
391                        else
392                        {
393                                return False;
394                        }
395                }
396
397                /* Begin mcal equiv functions */
398                function get_cached_event()
399                {
400                        return $this->cal->event;
401                }
402               
403                function add_attribute($var,$value,$element='**(**')
404                {
405                        $this->cal->add_attribute($var,$value,$element);
406                }
407
408                function event_init()
409                {
410                        $this->cal->event_init();
411                }
412
413                function set_date($element,$year,$month,$day=0,$hour=0,$min=0,$sec=0)
414                {
415                        $this->cal->set_date($element,$year,$month,$day,$hour,$min,$sec);
416                }
417
418                function set_start($year,$month,$day=0,$hour=0,$min=0,$sec=0)
419                {
420                        $this->cal->set_start($year,$month,$day,$hour,$min,$sec);
421                }
422
423                function set_end($year,$month,$day=0,$hour=0,$min=0,$sec=0)
424                {
425                        $this->cal->set_end($year,$month,$day,$hour,$min,$sec);
426                }
427
428                function set_title($title='')
429                {
430                        $this->cal->set_title($title);
431                }
432
433                function set_description($description='')
434                {
435                        $this->cal->set_description($description);
436                }
437                function set_ex_participants($ex_participants='')
438                {
439                        $this->cal->set_ex_participants($ex_participants);
440                }
441
442                function set_class($class)
443                {
444                        $this->cal->set_class($class);
445                }
446
447                function set_category($category='')
448                {
449                        $this->cal->set_category($category);
450                }
451
452                function set_alarm($alarm)
453                {
454                        $this->cal->set_alarm($alarm);
455                }
456
457                function set_recur_none()
458                {
459                        $this->cal->set_recur_none();
460                }
461
462                function set_recur_daily($year,$month,$day,$interval)
463                {
464                        $this->cal->set_recur_daily($year,$month,$day,$interval);
465                }
466
467                function set_recur_weekly($year,$month,$day,$interval,$weekdays)
468                {
469                        $this->cal->set_recur_weekly($year,$month,$day,$interval,$weekdays);
470                }
471
472                function set_recur_monthly_mday($year,$month,$day,$interval)
473                {
474                        $this->cal->set_recur_monthly_mday($year,$month,$day,$interval);
475                }
476
477                function set_recur_monthly_wday($year,$month,$day,$interval)
478                {
479                        $this->cal->set_recur_monthly_wday($year,$month,$day,$interval);
480                }
481
482                function set_recur_yearly($year,$month,$day,$interval)
483                {
484                        $this->cal->set_recur_yearly($year,$month,$day,$interval);
485                }
486               
487                /* End mcal equiv functions */
488        }
489?>
Note: See TracBrowser for help on using the repository browser.