phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] old/addbook/phpgwapi class.contacts.inc.php, 1.1 clas


From: skwashd
Subject: [Phpgroupware-cvs] old/addbook/phpgwapi class.contacts.inc.php, 1.1 class.contacts1_sql.inc.php, 1.1
Date: Thu, 24 Feb 2005 13:20:00 -0000

Update of old/addbook/phpgwapi

Added Files:
     Branch: MAIN
            class.contacts.inc.php 
            class.contacts1_sql.inc.php 

Log Message:
keep a historic record of the app

====================================================
Index: class.contacts.inc.php
<?php

         /* $Id: class.contacts.inc.php,v 1.1 2004/12/31 05:08:56 skwashd Exp $ 
*/

        if (!$phpgw_info['server']['contact_repository'])
        {
                $phpgw_info['server']['contact_repository'] = 'sql';
        }

        //uses the standard contact_sql-class only, if addbook is disabled
        if(!$GLOBALS['phpgw_info']['user']['apps']['addbook']['enabled'])
        {
                include(PHPGW_API_INC . 
'/class.contacts_'.$phpgw_info['server']['contact_repository'] . '.inc.php');
        }
        else
        {
                include(PHPGW_API_INC . 
'/class.contacts1_'.$phpgw_info['server']['contact_repository'] . '.inc.php');
        }
        include(PHPGW_API_INC . '/class.contacts_shared.inc.php');
?>

====================================================
Index: class.contacts1_sql.inc.php
<?php
  /**************************************************************************\
  * phpGroupWare API - Contacts manager for SQL                              *
  * This file written by Joseph Engo <address@hidden>                *
  *   and Miles Lott <address@hidden>                              *
  * View and manipulate contact records using SQL                            *
  * Copyright (C) 2001 Joseph Engo                                           *
  * -------------------------------------------------------------------------*
  * This library is part of the phpGroupWare API                             *
  * http://www.phpgroupware.org/api                                          *
  * ------------------------------------------------------------------------ *
  * This library is free software; you can redistribute it and/or modify it  *
  * under the terms of the GNU Lesser General Public License as published by *
  * the Free Software Foundation; either version 2.1 of the License,         *
  * or any later version.                                                    *
  * This library is distributed in the hope that it will be useful, but      *
  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
  * See the GNU Lesser General Public License for more details.              *
  * You should have received a copy of the GNU Lesser General Public License *
  * along with this library; if not, write to the Free Software Foundation,  *
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
  \**************************************************************************/

  /* $Id: class.contacts1_sql.inc.php,v 1.1 2004/12/31 05:08:56 skwashd Exp $ */

        //adjusted to addbook
        class contacts_
        {
                var $db = '';
                var $person='phpgw_addbook_person_main';
                var $org='phpgw_addbook_org_main';
                var $meta='phpgw_addbook_meta';
                var $address='phpgw_addbook_address';
                var $ocontact='phpgw_addbook_ocontacts';
                var $pcontact='phpgw_addbook_pcontacts';
                var $contacts='phpgw_addbook_contacts';
                var $relation='phpgw_addbook_relation';

                var $account_id = 0;
                var $total_records = 0;
                var $grants = '';

                /* The left side are the array elements used throughout phpgw, 
right side are the db field names. */

                var $tables_and_fields = array(
                        'title'                 =>      
'phpgw_addbook_person_main',
                        'first_name'    =>      'phpgw_addbook_person_main',
                        'last_name'     =>      'phpgw_addbook_person_main',
                        'middle'                =>      
'phpgw_addbook_person_main',
                        'birthday'              =>      
'phpgw_addbook_person_main',
                        'organization'  =>      'phpgw_addbook_relation',
                        'street'                =>      'phpgw_addbook_address',
                        'po_box'                =>      'phpgw_addbook_address',
                        'postal_code'   =>      'phpgw_addbook_address',
                        'city'                  =>      'phpgw_addbook_address',
                        'state'                 =>      'phpgw_addbook_address',
                        'country'               =>      'phpgw_addbook_address',
                        'name'                  =>      'phpgw_addbook_org_main'
                );

                var $map_old_to_person = array(
                        'fn'                  => 'full_name',
                        'n_given'             => 'first_name',
                        'n_family'            => 'last_name',
                        'n_middle'            => 'middle',
                        'n_prefix'            => 'title',
                        'title'               => 'title',
                        'bday'                => 'birthday',
                        'adr_one_street'      => 'street',
                        'adr_one_locality'    => 'city',
                        'adr_one_region'      => 'state',
                        'adr_one_postalcode'  => 'postal_code',
                        'adr_one_countryname' => 'country',
                        'homepage'                        => 'www'
                );

                var $map_old_to_org = array(
                        'org_name'            => 'name',
                        'adr_one_street'      => 'street',
                        'adr_one_region'      => 'city',
                        'adr_one_postalcode'  => 'postal_code',
                        'adr_one_countryname' => 'country',
                        'homepage'                        => 'www'
                );

                var $stock_contact_fields_person = array(
                        'first_name'    =>      
'phpgw_addbook_person_main.first_name',
                        'last_name'             =>      
'phpgw_addbook_person_main.last_name',
                        'email'                 =>      
'phpgw_addbook_pcontacts.b_email',
                        'email_home'    =>      
'phpgw_addbook_pcontacts.p_email'
                );

                var $stock_contact_fields_org = array(
                        'name'                  =>      
'phpgw_addbook_org_main.name',
                        'email'                 =>      
'phpgw_addbook_ocontacts.email',
                        'www'                   =>      
'phpgw_addbook_ocontacts.www'
                );

                function contacts_($useacl=True)
                {
                        $this->db =  $GLOBALS['phpgw']->db;
                        $this->db2 = $this->db;
                        if($useacl)
                        {
                                $this->grants = 
$GLOBALS['phpgw']->acl->get_grants('addbook');
                        }
                        $this->account_id = 
$GLOBALS['phpgw_info']['user']['account_id'];
                }

                function get_org_name($org_link)
                {
                        $sql = 'SELECT a.name from '.$this->org.' as a ';
                        $sql.= 'WHERE a.id = '.$org_link;
                        //$sql.= ' AND a.id = b.id';

                        $this->db2->query($sql);
                        $this->db2->next_record();
                        return $this->db2->f('name');
                }

                function read_single_entry($id='',$fields='')
                {
                        if($id == '')
                        {
                                return;
                        }

                        if(!$fields || empty($fields))
                        {
                                $pfields = $this->stock_contact_fields_person;
                                $ofields = $this->stock_contact_fields_org;
                        }
                        else
                        {
                                $pfields = array();
                                $pfields = array();

                                while(list($key, $value) = each($fields))
                                {
                                        if($this->map_old_to_person[$value])
                                        {
                                                $pfields[] = 
$this->map_old_to_person[$value];
                                        }
                                        else 
if($this->map_old_to_org[$fields[$value]])
                                        {
                                                $ofields[] = 
$this->map_old_to_org[$value];
                                        }

                                }
                        }

                        $this->db->query("SELECT * FROM $this->meta WHERE 
id=$id");
                        $this->db->next_record();

                        $return_fields[0]['id']     = $this->db->f('id');
                        $return_fields[0]['lid']    = $this->db->f('lid');
                        $return_fields[0]['tid']    = $this->db->f('tid');
                        $return_fields[0]['owner']  = $this->db->f('owner');
                        $return_fields[0]['access'] = $this->db->f('access');
                        $return_fields[0]['cat_id'] = $this->db->f('cat_id');


                        $target = $this->get_target($id);
                        if($target == 'person')
                        {
                                $sql = 'SELECT a.*, b.street, b.postal_code, 
b.po_box, b.city, b.state, b.country, c.org_link';
                                $sql.= ' FROM ('.$this->person.' as a LEFT 
OUTER JOIN '.$this->address.' as b';
                                $sql.= ' ON a.id = b.id)';
                                $sql.= ' LEFT OUTER JOIN '.$this->relation.' as 
c';
                                $sql.= ' ON a.id = c.id';
                                $sql.= ' WHERE a.id = '.$id;

                                $this->db->query($sql);

                                $contact_table = $this->pcontact;

                                $this->db->next_record();
                                $return_fields[0]['n_prefix'] = 
$this->db->f('title');
                                $return_fields[0]['n_family'] = 
$this->db->f('last_name');
                                $return_fields[0]['n_middle'] = 
$this->db->f('middle');
                                $return_fields[0]['fn'] = 
$this->db->f('title').' '.$this->db->f('first_name').' 
'.$this->db->f('middle').' '.$this->db->f('last_name');
                                $return_fields[0]['n_given'] = 
$this->db->f('first_name');
                                $return_fields[0]['bday'] = 
$this->db->f('birthday');
                                $return_fields[0]['adr_one_street'] = 
$this->db->f('street');
                                $return_fields[0]['adr_one_postalcode'] = 
$this->db->f('postal_code');
                                $return_fields[0]['adr_one_locality'] = 
$this->db->f('city');
                                $return_fields[0]['adr_one_region'] = 
$this->db->f('state');
                                $return_fields[0]['adr_one_countryname'] = 
$this->db->f('country');
                                $return_fields[0]['org_name'] = 
$this->get_org_name($id, $this->db->f('org_link'));
                        }
                        else
                        {
                                //requested id represents a organization
                                $contact_table = $this->ocontact;
                                $target = 'org';
                                $sql = 'SELECT a.*, b.street, b.nr, 
b.postal_code, b.po_box, b.city, b.state, b.country';
                                $sql.= ' FROM '.$this->org.' as a LEFT OUTER 
JOIN '.$this->address.' as b';
                                $sql.= ' ON a.id = b.id';
                                $sql.= ' WHERE a.id = '.$id;
                                $this->db->query($sql);
                                $this->db->next_record();

                                $return_fields[0]['org_name'] = 
$this->db->f('name');
                                $return_fields[0]['adr_one_street'] = 
$this->db->f('street');
                                $return_fields[0]['adr_one_postalcode'] = 
$this->db->f('postal_code');
                                $return_fields[0]['adr_one_locality'] = 
$this->db->f('city');
                                $return_fields[0]['adr_one_region'] = 
$this->db->f('state');
                                $return_fields[0]['adr_one_countryname'] = 
$this->db->f('country');
                        }

                        //get contacts
                        $sql = 'SELECT * FROM '.$contact_table;
                        $sql.= ' WHERE id = '.$id;
                        $this->db->query($sql);
                        $this->db->next_record();

                        if($target == 'person')
                        {
                                $return_fields[0]['email'] = 
$this->db->f('b_email');
                                $return_fields[0]['email_home'] = 
$this->db->f('p_email');
                        }
                        else
                        {
                                $return_fields[0]['email'] = 
$this->db->f('email');
                        }

                        return $return_fields;
                }

                function 
read($start=0,$limit=0,$fields='',$query='',$filter='',$sort='',$order='')
                {
                        if(!$start)  { $start  = 0; }
                        if(!$limit)  { $limit  = 0; }
                        if(!$filter) { $filter = 'tid=n'; }

                        if(!$fields || empty($fields))
                        {
                                $pfields = $this->stock_contact_fields_person;
                                $ofields = $this->stock_contact_fields_org;
                        }
                        else
                        {
                                $pfields=array();
                                $ofields=array();
                                while(list($key, $value) = each($fields))
                                {
                                        if($this->map_old_to_person[$value])
                                        {
                                                $pfields[] = 
$this->map_old_to_person[$value];
                                        }
                                        else 
if($this->map_old_to_org[$fields[$value]])
                                        {
                                                $ofields[] = 
$this->map_old_to_org[$value];
                                        }

                                }
                        }

                        //maps the old field_names to the new ones
                        if($order != '')
                        {
                                $tmp_order = explode(',' , $order);

                                unset($order);
                                $porder = array();
                                $oorder = array();
                                for($i=0; $i < count($tmp_order); $i++)
                                {
                                        
if($this->map_old_to_person[$tmp_order[$i]])
                                        {
                                                $porder[] = 
$this->map_old_to_person[$tmp_order[$i]];
                                        }
                                        else 
if($this->map_old_to_org[$tmp_order[$i]])
                                        {
                                                $oorder[] = 
$this->map_old_to_org[$tmp_order[$i]];
                                        }
                                }

                                $porder = implode (',', $porder);
                                $oorder = implode (',', $oorder);

                                $porder = ereg_replace ("^,|,$", '', $porder);
                                $oorder = ereg_replace ("^,|,$", '', $oorder);

                                unset($tmp_order);
                        }
                        // turn filter's a=b,c=d OR a=b into an array
                        if($filter)
                        {
                                if($DEBUG) { echo 'DEBUG - Inbound filter is: 
#'.$filter.'#'; }
                                $filterarray = split(',',$filter);
                                if($filterarray[1])
                                {
                                        $i=0;
                                        for($i=0;$i<count($filterarray);$i++)
                                        {
                                                list($name,$value) = 
split('=',$filterarray[$i]);
                                                if($name)
                                                {
                                                        if($DEBUG) { echo 
'<br>DEBUG - Filter intermediate strings 1: #'.$name.'# => #'.$value.'#'; }
                                                        $filterfields[$name] = 
$value;
                                                        //HEIKO
                                                }
                                        }
                                }
                                else
                                {
                                        list($name,$value) = split('=',$filter);
                                        if($DEBUG)
                                        {
                                                echo '<br>DEBUG - Filter 
intermediate strings 1: #'.$name.'# => #'.$value.'#';
                                        }
                                        $filterfields = array($name => $value);
                                }

                                // now check each element of the array and 
convert into SQL for queries below
                                $i=0;
                                reset($filterfields);
                                while(list($name,$value) = each($filterfields))
                                {
                                        if($DEBUG) { echo '<br>DEBUG - Filter 
intermediate strings 2: #'.$name.'# => #'.$value.'#'; }
                                        $isstd=0;
                                        if($name && empty($value))
                                        {
                                                if($DEBUG) { echo '<br>DEBUG - 
filter field "'.$name.'" is empty (NULL)'; }

                                                //person
                                                
while(list($fname,$fvalue)=each($pfields))
                                                {
                                                        if($fvalue==$name)
                                                        {
                                                                $filterlist .= 
$name.' is NULL,';
                                                                if($DEBUG) { 
echo '<br>DEBUG - filter field "'.$name.'" is a stock field'; }
                                                                break;
                                                        }
                                                }
                                        }
                                        elseif($name && $value)
                                        {
                                                reset($pfields);
                                                
while(list($fname,$fvalue)=each($pfields))
                                                {
                                                        if($fvalue==$name)
                                                        {
                                                                if($name == 
'cat_id')
                                                                {
                                                                        
$filterlist .= '(' . $name . " LIKE '%," . $value . ",%' OR " . 
$name."='".$value."');";
                                                                }
                                                                
elseif(is_int($value))
                                                                {
                                                                        
$filterlist .= $name.'='.$value.';';
                                                                }
                                                                else
                                                                {
                                                                        
$filterlist .= $name."='".$value."';";
                                                                }
                                                                break;
                                                        }
                                                }
                                        }
                                        $i++;
                                }
                                $filterlist = substr($filterlist,0,-1);
                                $filterlist = ereg_replace(';',' AND 
',$filterlist);

                                if($DEBUG)
                                {
                                        echo '<br>DEBUG - Filter output string: 
#'.$filterlist.'#';
                                }

                                if($filterlist)
                                {
                                        $filtermethod = $filterlist.') ';
                                        $fwhere = ' WHERE '; $fand = ' AND ';
                                }
                        }
                        else
                        {
                                $filtermethod = " AND (tid='n' OR tid is null)";
                        }

                        if(!$filtermethod)
                        {
                                if($this->account_id)
                                {
                                        $fwhere .= ' (owner=' . 
$this->account_id;
                                        $fand   .= ' (owner=' . 
$this->account_id;
                                }
                        }
                        else
                        {
                                if($this->account_id)
                                {
                                        $fwhere .= $filtermethod . ' AND 
(owner=' . $this->account_id;
                                        $fand   .= $filtermethod . ' AND 
(owner=' . $this->account_id;
                                }
                                else
                                {
                                        $filtermethod = 
substr($filtermethod,0,-2);
                                        $fwhere .= $filtermethod;
                                        $fand   .= $filtermethod;
                                }
                        }

                        if(is_array($this->grants))
                        {
                                $grants = $this->grants;
                                while(list($user) = each($grants))
                                {
                                        $public_user_list[] = $user;
                                }
                                reset($public_user_list);
                                $fwhere .= " OR (access='public' AND owner in(" 
. implode(',',$public_user_list) . "))) ";
                                $fand   .= " OR (access='public' AND owner in(" 
. implode(',',$public_user_list) . "))) ";
                        }
                        else
                        {
                                $fwhere .= ') '; $fand .= ') ';
                        }

                        if(!$sort)
                        {
                                $sort = 'ASC';
                        }

                        if($porder != '')
                        {
                                $porder = "ORDER BY $porder $sort ";
                        }
                        else
                        {
                                $porder = "ORDER BY last_name, first_name 
$sort";
                        }

                        if($oorder != '')
                        {
                                $oorder = "ORDER BY $oorder $sort ";
                        }
                        else
                        {
                                $oorder = "ORDER BY name $sort";
                        }

                        $filtermethod = '';

                        //=================== <persons> 
=========================
                        if($query)
                        {
                                $query  = ereg_replace("'",'',$query);
                                $query  = ereg_replace('"','',$query);

                                $sql = 'SELECT * FROM ';
                                $sql.= $this->person.', ';
                                $sql.= $this->address.', ';
                                $sql.= $this->meta.', ';
                                $sql.= $this->relation.', ';
                                $sql.= $this->pcontact;
                                $sql.= ' WHERE '.$this->person.'.id = 
'.$this->address.'.id';
                                $sql.= ' AND 
'.$this->person.'.id='.$this->meta.'.id';
                                $sql.= ' AND 
'.$this->person.'.id='.$this->pcontact.'.id';
                                $sql.= ' AND 
'.$this->person.'.id='.$this->relation.'.id AND (';
                                reset($pfields);
                                while(list($f,$x) = each($pfields))
                                {
                                        $like .= " $x LIKE '%$query%' OR ";
                                }

                                //remove the unwanted ' OR' at the end
                                $like = substr($like, 0, strlen($like)-3);

                                $sql .= $like.') AND '.$fand . $filtermethod . 
$porder;
                                unset($f);
                                unset($x);
                                unset($like);
                        }
                        else
                        {
                                $sql = 'SELECT * FROM ';
                                $sql.= $this->person.' as person, ';
                                $sql.= $this->address.' as address, ';
                                $sql.= $this->meta.' as meta, ';
                                $sql.= $this->pcontact.' as pcontact, ';
                                $sql.= $this->relation.' as relation';
                                $sql.= ' WHERE person.id = address.id AND 
person.id=meta.id AND person.id=pcontact.id AND person.id=relation.id AND';
                                $sql.=   $fwhere. $filtermethod . ' ' . $porder;
                        }
                        if($DEBUG) { echo '<br>' . $sql; }

                        $this->db->query($sql,__LINE__,__FILE__);
                        $total_records_person = $this->db->num_rows();

                        if($start && $limit)
                        {
                                
$this->db->limit_query($sql,$start,__LINE__,__FILE__,$limit);
                        }
                        elseif(!$limit)
                        {
                                $this->db->query($sql,__LINE__,__FILE__);
                        }
                        else
                        {
                                
$this->db->limit_query($sql,$start,__LINE__,__FILE__);
                        }
                        $i = 0;
                        while($this->db->next_record())
                        {
                                $return_fields[$i]['id']     = 
$this->db->f('id');
                                $return_fields[$i]['lid']    = 
$this->db->f('lid');
                                $return_fields[$i]['tid']    = 
$this->db->f('tid');
                                $return_fields[$i]['owner']  = 
$this->db->f('owner');
                                $return_fields[$i]['access'] = 
$this->db->f('access');
                                $return_fields[$i]['cat_id'] = 
$this->db->f('cat_id');
                                $return_fields[$i]['email'] = 
$this->db->f('b_email');
                                $return_fields[$i]['email_home'] = 
$this->db->f('p_email');
                                $return_fields[$i]['n_given'] = 
$this->db->f('first_name');
                                $return_fields[$i]['n_family'] = 
$this->db->f('last_name');
                                $return_fields[$i]['org_name'] = 
$this->get_org_name($this->db->f('org_link'));
                                $i++;
                        }
                        //================ </persons> 
=============================

                        //repeat these steps for organizations

                        //================ <organizations> 
========================
                        if($query)
                        {
                                $query  = ereg_replace("'",'',$query);
                                $query  = ereg_replace('"','',$query);

                                $sql = 'SELECT * FROM ';
                                $sql.= $this->org.', ';
                                $sql.= $this->address.', ';
                                $sql.= $this->meta.', ';
                                $sql.= $this->ocontact;
                                $sql.= ' WHERE '.$this->org.'.id = 
'.$this->address.'.id';
                                $sql.= ' AND 
'.$this->org.'.id='.$this->meta.'.id';
                                $sql.= ' AND 
'.$this->org.'.id='.$this->ocontact.'.id AND (';
                                reset($ofields);
                                while(list($f,$x) = each($ofields))
                                {
                                        $like .= " $x LIKE '%$query%' OR ";
                                }

                                //remove the unwanted ' OR' at the end
                                $like = substr($like, 0, strlen($like)-3);

                                $sql .= $like.') AND '.$fand . $filtermethod . 
$oorder;
                                unset($f);
                                unset($x);
                                unset($like);
                        }
                        else
                        {
                                $sql = 'SELECT * FROM ';
                                $sql.= $this->org.' as org, ';
                                $sql.= $this->address.' as address, ';
                                $sql.= $this->meta.' as meta, ';
                                $sql.= $this->ocontact.' as ocontact ';
                                $sql.= ' WHERE org.id = address.id AND 
org.id=meta.id AND org.id=ocontact.id AND ';
                                $sql.=   $fwhere. $filtermethod . ' ' . $oorder;
                        }
                        $this->db->query($sql,__LINE__,__FILE__);
                        $total_records_org = $this->db->num_rows();

                        if($start && $limit)
                        {
                                
$this->db->limit_query($sql,$start,__LINE__,__FILE__,$limit);
                        }
                        elseif(!$limit)
                        {
                                $this->db->query($sql,__LINE__,__FILE__);
                        }
                        else
                        {
                                
$this->db->limit_query($sql,$start,__LINE__,__FILE__);
                        }
                        while($this->db->next_record())
                        {
                                $return_fields[$i]['id']     = 
$this->db->f('id');
                                $return_fields[$i]['lid']    = 
$this->db->f('lid');
                                $return_fields[$i]['tid']    = 
$this->db->f('tid');
                                $return_fields[$i]['owner']  = 
$this->db->f('owner');
                                $return_fields[$i]['access'] = 
$this->db->f('access');
                                $return_fields[$i]['cat_id'] = 
$this->db->f('cat_id');
                                $return_fields[$i]['email'] = 
$this->db->f('email');
                                $return_fields[$i]['homepage'] = 
$this->db->f('www');
                                $return_fields[$i]['org_name'] = 
$this->db->f('name');
                                $return_fields[$i]['n_given'] = 
$this->db->f('name');
                                $i++;
                        }

                        //================ </organizations> ==================


                        $this->total_records = $total_records_person + 
$total_records_org;
                        return $return_fields;
                }

                //returns whether the given id belongs to a person or an 
organization
                //this is needed by read_single_entry
                function get_target($id)
                {
                        $sql = 'SELECT id as test from '.$this->org;
                        $sql.= ' WHERE id = '.$id;

                        $this->db->query($sql);
                        if(!$this->db->f('test'))
                        {
                                return 'person';
                        }
                        else
                        {
                                return 'org';
                        }
                }
        }
?>






reply via email to

[Prev in Thread] Current Thread [Next in Thread]