source: trunk/phpgwapi/inc/class.contacts.inc.php @ 7655

Revision 7655, 18.7 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**************************************************************************\
3        * eGroupWare API - Contact Management Shared Routines                      *
4        * Written by Joseph Engo <jengo@phpgroupware.org>                          *
5        *        and Miles Lott <milosch@groupwhere.org>                           *
6        *        and Bettina Gille <ceb@phpgroupware.org>                          *
7        * View and manipulate contact records                                      *
8        * Copyright (C) 2001, 2002 Joseph Engo, Miles Lott, Bettina Gille          *
9        * -------------------------------------------------------------------------*
10        * This library is part of the eGroupWare API                               *
11        * http://www.egroupware.org                                                *
12        * ------------------------------------------------------------------------ *
13        * This library is free software; you can redistribute it and/or modify it  *
14        * under the terms of the GNU Lesser General Public License as published by *
15        * the Free Software Foundation; either version 2.1 of the License,         *
16        * or any later version.                                                    *
17        * This library is distributed in the hope that it will be useful, but      *
18        * WITHOUT ANY WARRANTY; without even the implied warranty of               *
19        * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
20        * See the GNU Lesser General Public License for more details.              *
21        * You should have received a copy of the GNU Lesser General Public License *
22        * along with this library; if not, write to the Free Software Foundation,  *
23        * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
24        \**************************************************************************/
25
26        if (!isset($GLOBALS['phpgw_info']['server']['contact_repository']))
27        {
28                $GLOBALS['phpgw_info']['server']['contact_repository'] = 'sql';
29        }
30        require_once(PHPGW_API_INC . '/class.contacts_'.$GLOBALS['phpgw_info']['server']['contact_repository'] . '.inc.php');
31
32        class contacts extends contacts_
33        {
34                function contacts()
35                {
36                        $this->contacts_();     // call constructor of extended class
37                }
38
39                /*!
40                @function check_perms
41                @abstract checks if user has the necessary permissions on a contact
42                @syntax check_perms($rights,$needed,$addr=False)
43                @param $rights integer the rights the user has / grants from the owner of the contact, only used if $addr not given
44                @param $needed integer PHPGW_ACL_{READ|EDIT|DELETE}
45                @param $addr mixed contact-array or contact-id, if False rights have to be supplyed in $rights
46                */
47                function check_perms($rights,$needed,$addr=False)
48                {
49                        //echo "<p>contacts::check_perms($rights,$needed,".print_r($addr,True).")";
50                        if ($addr !== False)    // addr-record or id given
51                        {
52                                if(@is_array($addr))
53                                {
54                                        if (isset($addr['rights']))
55                                        {
56                                                $rights = $addr['rights'];
57                                        }
58                                        elseif (isset($addr['owner']))
59                                        {
60                                                $rights = $this->grants[$addr['owner']];
61                                        }
62                                        else
63                                        {
64                                                $id = (int)(isset($addr['id']) ? $addr['id'] : $addr['ab_id']);
65                                        }
66                                }
67                                else
68                                {
69                                        $id = (int)$addr;
70                                }
71                                if (isset($id))
72                                {
73                                        $addr = $this->read_single_entry($id,array('owner' => 'owner'));
74                                        $rights = @$addr[0]['rights'];
75                                        //echo "addr($id)=<pre>".print_r($addr[0],True)."</pre>\n";
76                                }
77                        }
78                        $ret = !!((int)$rights & $needed);
79                        //echo " rights=$rights, id=$id => ".($ret?'True':'False')."</p>\n";
80                        //echo "grants=<pre>".print_r($this->grants,True)."</pre>\n";
81
82                        return $ret;
83                }
84
85                /**
86                * Get the the person data what you want. Wrapper function to stay compatible with egroupware.
87                *
88                * @author Lars Kneschke <lars@kneschke.de>
89                * @param array $fields The fields that you can see from person
90                * @param integer $limit Limit of records that you want
91                * @param integer $ofset Ofset of record that you want start
92                * @param string $orderby The field which you want order
93                * @param string $sort ASC | DESC depending what you want
94                * @param mixed $criteria All criterias what you want
95                * @param mixed $criteria_token same like $criteria but builded<br>with sql_criteria class, more powerfull
96                * @return array with records
97                */
98                function get_persons($_fields, $start='', $limit='', $orderby='', $sort='', $_criteria='', $token_criteria='')
99                {
100                        // transform fields from phpgw to egw structure
101                        foreach($_fields as $fieldValue)
102                        {
103                                switch($fieldValue)
104                                {
105                                        case 'per_first_name':
106                                                $fields['n_given']='n_given';
107                                        case 'per_last_name':
108                                                $fields['n_family']='n_family';
109                                        default:
110                                                $fields[$fieldValue]=$fieldValue;
111                                }
112                        }
113                       
114                        // transform criteria from phpgw to egw structure
115                        if(is_array($_criteria))
116                        {
117                                foreach($_criteria as $criteriaKey => $criteriaValue)
118                                {
119                                        if($GLOBALS['phpgw_info']['server']['contact_repository'] == 'ldap')
120                                        {
121                                                switch($criteriaKey)
122                                                {
123                                                        case 'contact_id':
124                                                        $criteria['uid']=$criteriaValue;
125                                                                        break;
126                                                        default:
127                                                                $criteria[$criteriaKey] = $criteria[$criteriaValue];
128                                                                break;
129                                                }
130                                        }
131                                        else
132                                        {
133                                                switch($criteriaKey)
134                                                {
135                                                        case 'contact_id':
136                                                        $criteria['id']=$criteriaValue;
137                                                                        break;
138                                                        default:
139                                                                $criteria[$criteriaKey] = $criteria[$criteriaValue];
140                                                                break;
141                                                }
142                                        }
143                                }
144                        }
145                        $entries = $this->read($start,$limit,$fields,$criteria,'',$sort,$orderby);
146                       
147                        // transform entries from egw to phpgw structure
148                        if(is_array($entries))
149                        {
150                                foreach($entries as $entryKey => $entryValue)
151                                {
152                                        $entryValue['per_first_name']   = $entryValue['n_given'];
153                                        $entryValue['per_last_name']    = $entryValue['n_family'];
154                                        $entryValue['contact_id']       = $entryValue['id'];
155                                        $entries[$entryKey]             = $entryValue;
156                                }
157                        }
158                        return $entries;
159                }
160
161                function split_stock_and_extras($fields)
162                {
163                        settype($fields, 'array');
164                        foreach($fields as $field => $value)
165                        {
166                                /* Depending on how the array was built, this is needed. */
167                                if (is_int($field))
168                                {
169                                        $field = $value;
170                                }
171                                if(@is_int($value))
172                                {
173                                        $value = $field;
174                                }
175                                if ($this->stock_contact_fields[$field])
176                                {
177                                        $stock_fields[$field]     = $value;
178                                        $stock_fieldnames[$field] = $this->stock_contact_fields[$field];
179                                }
180                                elseif (!isset($this->non_contact_fields[$field]))
181                                {
182                                        $extra_fields[$field] = $value;
183                                }
184                        }
185                        return array($stock_fields,$stock_fieldnames,$extra_fields);
186                }
187
188                function loop_addslashes($fields)
189                {
190                        $absf = $this->stock_contact_fields;
191                        foreach($absf as $t => $nul)
192                        {
193                                $ta[] = $this->db->db_addslashes($fields[$t]);
194                        }
195                        return $ta;
196                }
197
198                /* This will take an array or integer */
199                function delete($id)
200                {
201                        if(@is_array($id))
202                        {
203                                foreach($id as $nul => $t_id)
204                                {
205                                        $this->delete_($t_id);
206                                }
207                        }
208                        else
209                        {
210                                $this->delete_($id);
211                        }
212                }
213
214                function asc_sort($a,$b)
215                {
216                        echo "<br>A:'".$a."' B:'".$b;
217                        if($a[1] == $b[1])
218                        {
219                                return 0;
220                        }
221                        return ($a[1]>$b[1])?1:-1;
222                }
223
224                function desc_sort($a,$b)
225                {
226                        echo "<br>A:'".$a."' B:'".$b;
227                        if($a[1]==$b[1])
228                        {
229                                return 0;
230                        }
231                        return ($a[1]<$b[1])?1:-1;
232                }
233
234                /*
235                comesafter ($s1, $s2)
236                Returns 1 if $s1 comes after $s2 alphabetically, 0 if not.
237                */
238                function comesafter ($s1, $s2)
239                {
240                        /*
241                        We don't want to overstep the bounds of one of the strings and segfault,
242                        so let's see which one is shorter.
243                        */
244                        $order = 1;
245
246                        if((strlen($s1) == 0))
247                        {
248                                return 0;
249                        }
250
251                        if((strlen($s2) == 0))
252                        {
253                                return 1;
254                        }
255
256                        if(strlen ($s1) > strlen ($s2))
257                        {
258                                $temp = $s1;
259                                $s1 = $s2;
260                                $s2 = $temp;
261                                $order = 0;
262                        }
263
264                        for ($index = 0; $index < strlen ($s1); ++$index)
265                        {
266                                /* $s1 comes after $s2 */
267                                if (strtolower($s1[$index]) > strtolower($s2[$index])) { return ($order); }
268
269                                /* $s1 comes before $s2 */
270                                if (strtolower($s1[$index]) < strtolower($s2[$index])) { return (1 - $order); }
271                        }
272                                /* Special case in which $s1 is a substring of $s2 */
273
274                        return ($order);
275                }
276
277                /*
278                * asortbyindex ($sortarray, $index)
279                *
280                * Sort a multi-dimensional array by a second-degree index. For instance, the 0th index
281                * of the Ith member of both the group and user arrays is a string identifier. In the
282                * case of a user array this is the username; with the group array it is the group name.
283                * asortby
284                */
285                function asortbyindex ($sortarray, $index)
286                {
287                        $lastindex = count($sortarray) - 2;
288                        for ($subindex = 0; $subindex < $lastindex; ++$subindex)
289                        {
290                                $lastiteration = $lastindex - $subindex;
291                                for ($iteration = 0; $iteration < $lastiteration; ++$iteration)
292                                {
293                                        $nextchar = 0;
294                                        if ($this->comesafter($sortarray[$iteration][$index], $sortarray[$iteration + 1][$index]))
295                                        {
296                                                $temp = $sortarray[$iteration];
297                                                $sortarray[$iteration] = $sortarray[$iteration + 1];
298                                                $sortarray[$iteration + 1] = $temp;
299                                        }
300                                }
301                        }
302                        return ($sortarray);
303                }
304
305                function arsortbyindex ($sortarray, $index)
306                {
307                        $lastindex = count($sortarray) - 1;
308                        for ($subindex = $lastindex; $subindex > 0; $subindex--)
309                        {
310                                $lastiteration = $lastindex - $subindex;
311                                for ($iteration = $lastiteration; $iteration > 0; $iteration--)
312                                {
313                                        $nextchar = 0;
314                                        if ($this->comesafter($sortarray[$iteration][$index], $sortarray[$iteration - 1][$index]))
315                                        {
316                                                $temp = $sortarray[$iteration];
317                                                $sortarray[$iteration] = $sortarray[$iteration - 1];
318                                                $sortarray[$iteration - 1] = $temp;
319                                        }
320                                }
321                        }
322                        return ($sortarray);
323                }
324
325                function formatted_address($id, $business = True, $afont = '', $asize = '2')
326                {
327                        $t = CreateObject('phpgwapi.Template',$GLOBALS['phpgw']->common->get_tpl_dir('addressbook'));
328                        $s = CreateObject('phpgwapi.sbox');
329
330                        $fields = array(
331                                'n_given'  => 'n_given',
332                                'n_family' => 'n_family',
333                                'title'    => 'title',
334                                'org_name' => 'org_name',
335                                'org_unit' => 'org_unit',
336                                'adr_one_street'      => 'adr_one_street',
337                                'adr_one_locality'    => 'adr_one_locality',
338                                'adr_one_postalcode'  => 'adr_one_postalcode',
339                                'adr_one_region'      => 'adr_one_region',
340                                'adr_one_countryname' => 'adr_one_countryname',
341                                'adr_two_street'      => 'adr_two_street',
342                                'adr_two_locality'    => 'adr_two_locality',
343                                'adr_two_postalcode'  => 'adr_two_postalcode',
344                                'adr_two_region'      => 'adr_two_region',
345                                'adr_two_countryname' => 'adr_two_countryname'
346                        );
347
348                        list($address) = $this->read_single_entry($id,$fields);
349                        foreach($address as $k => $val)
350                        {
351                                $address[$k] = $GLOBALS['phpgw']->strip_html($val);
352                        }
353
354                        if ($address['title'])
355                        {
356                                $title = $address['title'] . '&nbsp;';
357                        }
358
359                        if ($business)
360                        {
361                                if ($address['org_name'])
362                                {
363                                        $company = $address['org_name'];
364                                }
365                                else
366                                {
367                                        $company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
368                                }
369
370                                $street  = $address['adr_one_street'];
371                                $city    = $address['adr_one_locality'];
372                                $zip     = $address['adr_one_postalcode'];
373                                $state   = $address['adr_one_region'];
374                                $country = $address['adr_one_countryname'];
375                        }
376                        else
377                        {
378                                $company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
379                                $street  = $address['adr_two_street'];
380                                $city    = $address['adr_two_locality'];
381                                $zip     = $address['adr_two_postalcode'];
382                                $state   = $address['adr_two_region'];
383                                $country = $address['adr_two_countryname'];
384                        }
385
386                        if (! $country)
387                        {
388                                $country = $GLOBALS['phpgw_info']['user']['preferences']['common']['country'];
389                        }
390
391                        if (file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'format_' . strtolower($country) . '.tpl'))
392                        {
393                                $a = $t->set_file(array('address_format' => 'format_' . strtolower($country) . '.tpl'));
394                        }
395                        else
396                        {
397                                $a = $t->set_file(array('address_format' => 'format_us.tpl'));
398                        }
399
400                        if (!$afont)
401                        {
402                                $afont = $GLOBALS['phpgw_info']['theme']['font'];
403                        }
404
405                        $a .= $t->set_var('font',$afont);
406                        $a .= $t->set_var('fontsize',$asize);
407                        $a .= $t->set_var('company',$company);
408                        $a .= $t->set_var('department',$address['org_unit']);
409                        $a .= $t->set_var('street',$street);
410                        $a .= $t->set_var('city',$city);
411                        $a .= $t->set_var('zip',$zip);
412                        $a .= $t->set_var('state',$state);
413
414                        if ($country != $GLOBALS['phpgw_info']['user']['preferences']['common']['country'])
415                        {
416                                $countryname = $s->get_full_name($country);
417                                $a .= $t->set_var('country',lang($countryname));
418                        }
419
420                        $a .= $t->fp('out','address_format');
421                        return $a;
422                }
423
424                function formatted_address_full($id, $business = True, $afont = '', $asize = '2')
425                {
426                        $t = CreateObject('phpgwapi.Template',$GLOBALS['phpgw']->common->get_tpl_dir('addressbook'));
427                        $s = CreateObject('phpgwapi.sbox');
428
429                        $fields = array(
430                                'n_given'                               => 'n_given',
431                                'n_family'                              => 'n_family',
432                                'title'                                 => 'title',
433                                'org_name'                              => 'org_name',
434                                'org_unit'                              => 'org_unit',
435                                'adr_one_street'                => 'adr_one_street',
436                                'adr_one_locality'              => 'adr_one_locality',
437                                'adr_one_postalcode'    => 'adr_one_postalcode',
438                                'adr_one_region'                => 'adr_one_region',
439                                'tel_work'                              => 'tel_work',
440                                'tel_fax'                               => 'tel_fax',
441                                'email'                                 => 'email',
442                                'url'                                   => 'url',
443                                'adr_one_countryname'   => 'adr_one_countryname',
444                                'adr_two_street'                => 'adr_two_street',
445                                'adr_two_locality'              => 'adr_two_locality',
446                                'adr_two_postalcode'    => 'adr_two_postalcode',
447                                'adr_two_region'                => 'adr_two_region',
448                                'adr_two_countryname'   => 'adr_two_countryname',
449                                'tel_home'                              => 'tel_home',
450                                'email_home'                    => 'email_home'
451                        );
452
453                        list($address) = $this->read_single_entry($id,$fields);
454                        foreach($address as $k => $val)
455                        {
456                                $address[$k] = $GLOBALS['phpgw']->strip_html($val);
457                        }
458
459                        if($address['title'])
460                        {
461                                $title = $address['title'] . '&nbsp;';
462                        }
463
464                        if($business)
465                        {
466                                if($address['org_name'])
467                                {
468                                        $company = $address['org_name'];
469                                }
470                                else
471                                {
472                                        $company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
473                                }
474
475                                $street         = $address['adr_one_street'];
476                                $city           = $address['adr_one_locality'];
477                                $zip            = $address['adr_one_postalcode'];
478                                $state          = $address['adr_one_region'];
479                                $country        = $address['adr_one_countryname'];
480                                $tel            = $address['tel_work'];
481                                $email          = $address['email'];
482                        }
483                        else
484                        {
485                                $company        = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
486                                $street         = $address['adr_two_street'];
487                                $city           = $address['adr_two_locality'];
488                                $zip            = $address['adr_two_postalcode'];
489                                $state          = $address['adr_two_region'];
490                                $country        = $address['adr_two_countryname'];
491                                $tel            = $address['tel_home'];
492                                $email          = $address['email_home'];
493                        }
494
495                        if(!$country)
496                        {
497                                $country = $GLOBALS['phpgw_info']['user']['preferences']['common']['country'];
498                        }
499
500                        if(file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'full_format_' . strtolower($country) . '.tpl'))
501                        {
502                                $a = $t->set_file(array('address_format' => 'full_format_' . strtolower($country) . '.tpl'));
503                        }
504                        else
505                        {
506                                $a = $t->set_file(array('address_format' => 'full_format_us.tpl'));
507                        }
508
509                        if(!$afont)
510                        {
511                                $afont = $GLOBALS['phpgw_info']['theme']['font'];
512                        }
513
514                        $a .= $t->set_var('font',$afont);
515                        $a .= $t->set_var('fontsize',$asize);
516                        $a .= $t->set_var('lang_url',lang('url'));
517                        $a .= $t->set_var('lang_email',lang('email'));
518                        $a .= $t->set_var('lang_fax',lang('fax number'));
519                        $a .= $t->set_var('lang_fon',lang('phone number'));
520                        $a .= $t->set_var('company',$company);
521                        $a .= $t->set_var('department',$address['org_unit']);
522                        $a .= $t->set_var('street',$street);
523                        $a .= $t->set_var('city',$city);
524                        $a .= $t->set_var('zip',$zip);
525                        $a .= $t->set_var('state',$state);
526                        $a .= $t->set_var('email',$email);
527                        $a .= $t->set_var('tel',$tel);
528                        $a .= $t->set_var('fax',$address['tel_fax']);
529                        $a .= $t->set_var('url',$address['url']);
530
531                        if($country != $GLOBALS['phpgw_info']['user']['preferences']['common']['country'])
532                        {
533                                $countryname = $s->get_full_name($country);
534                                $a .= $t->set_var('country',lang($countryname));
535                        }
536
537                        $a .= $t->fp('out','address_format');
538                        return $a;
539                }
540
541                function formatted_address_line($id, $business = True, $afont = '', $asize = '2')
542                {
543                        $t = CreateObject('phpgwapi.Template',$GLOBALS['phpgw']->common->get_tpl_dir('addressbook'));
544                        $s = CreateObject('phpgwapi.sbox');
545
546                        $fields = array(
547                                'n_given'                               => 'n_given',
548                                'n_family'                              => 'n_family',
549                                'title'                                 => 'title',
550                                'org_name'                              => 'org_name',
551                                'adr_one_street'                => 'adr_one_street',
552                                'adr_one_locality'              => 'adr_one_locality',
553                                'adr_one_postalcode'    => 'adr_one_postalcode',
554                                'adr_one_region'                => 'adr_one_region',
555                                'adr_one_countryname'   => 'adr_one_countryname',
556                                'adr_two_street'                => 'adr_two_street',
557                                'adr_two_locality'              => 'adr_two_locality',
558                                'adr_two_postalcode'    => 'adr_two_postalcode',
559                                'adr_two_region'                => 'adr_two_region',
560                                'adr_two_countryname'   => 'adr_two_countryname'
561                        );
562
563                        list($address) = $this->read_single_entry($id,$fields);
564                        foreach($address as $k => $val)
565                        {
566                                $address[$k] = $GLOBALS['phpgw']->strip_html($val);
567                        }
568
569                        if($address['title'])
570                        {
571                                $title = $address['title'] . '&nbsp;';
572                        }
573
574                        if($business)
575                        {
576                                if($address['org_name'])
577                                {
578                                        $company = $address['org_name'];
579                                }
580                                else
581                                {
582                                        $company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
583                                }
584
585                                $street  = $address['adr_one_street'];
586                                $city    = $address['adr_one_locality'];
587                                $zip     = $address['adr_one_postalcode'];
588                                $state   = $address['adr_one_region'];
589                                $country = $address['adr_one_countryname'];
590                        }
591                        else
592                        {
593                                $company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
594                                $street  = $address['adr_two_street'];
595                                $city    = $address['adr_two_locality'];
596                                $zip     = $address['adr_two_postalcode'];
597                                $state   = $address['adr_two_region'];
598                                $country = $address['adr_two_countryname'];
599                        }
600
601                        if(!$country)
602                        {
603                                $country = $GLOBALS['phpgw_info']['user']['preferences']['common']['country'];
604                        }
605
606                        if(file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'line_format_' . strtolower($country) . '.tpl'))
607                        {
608                                $a = $t->set_file(array('address_format' => 'line_format_' . strtolower($country) . '.tpl'));
609                        }
610                        else
611                        {
612                                $a = $t->set_file(array('address_format' => 'line_format_us.tpl'));
613                        }
614
615                        if(!$afont)
616                        {
617                                $afont = $GLOBALS['phpgw_info']['theme']['font'];
618                        }
619
620                        $a .= $t->set_var('font',$afont);
621                        $a .= $t->set_var('fontsize',$asize);
622                        $a .= $t->set_var('company',$company);
623                        $a .= $t->set_var('street',$street);
624                        $a .= $t->set_var('city',$city);
625                        $a .= $t->set_var('zip',$zip);
626                        $a .= $t->set_var('state',$state);
627
628                        if($country != $GLOBALS['phpgw_info']['user']['preferences']['common']['country'])
629                        {
630                                $countryname = $s->get_full_name($country);
631                                $a .= $t->set_var('country','&nbsp;°&nbsp;' . lang($countryname));
632                        }
633
634                        $a .= $t->fp('out','address_format');
635                        return $a;
636                }
637        }
638?>
Note: See TracBrowser for help on using the repository browser.