home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / contacts.class.inc < prev    next >
Text File  |  2004-03-08  |  9KB  |  252 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12. class contacts extends db
  13. {
  14.     function contacts()
  15.     {
  16.         $this->db();
  17.     }
  18.  
  19.     function search($user_id, $query)
  20.     {
  21.         $sql = "SELECT * FROM contacts WHERE user_id='$user_id' AND name LIKE '".smart_addslashes($query)."'";
  22.         $this->query($sql);
  23.         return $this->num_rows();
  24.     }
  25.  
  26.  
  27.     function get_contacts($user_id, $sort = "name", $direction = "ASC")
  28.     {
  29.         $this->query("SELECT * FROM contacts WHERE user_id='$user_id' ORDER BY ".$sort." ".$direction);
  30.         return $this->num_rows();
  31.     }
  32.  
  33.     function get_contacts_with_group($user_id)
  34.     {
  35.         $this->query("SELECT contacts.*, contact_groups.name AS group_name FROM contacts LEFT JOIN contact_groups ON (contacts.group_id=contact_groups.id) WHERE contacts.user_id='$user_id'");
  36.         return $this->num_rows();
  37.     }
  38.  
  39.     function get_contacts_group($user_id, $group_id, $sort = "name", $direction = "ASC")
  40.     {
  41.         $this->query("SELECT * FROM contacts WHERE group_id='$group_id' AND user_id='$user_id' ORDER BY ".$sort." ".$direction);
  42.         return $this->num_rows();
  43.        }
  44.  
  45.     function get_contact($contact_id)
  46.     {
  47.         $this->query("SELECT * FROM contacts WHERE id='$contact_id'");
  48.         if ($this->next_record())
  49.         {
  50.             return $this->Record;
  51.         }
  52.     }
  53.  
  54.     function add_contact($source_id, $user_id, $name, $email, $work_phone = "", $home_phone = "", $fax = "", $cellular = "", $country = "", $state = "", $city = "", $zip = "", $address = "", $company = "", $work_country = "", $work_state = "", $work_city = "", $work_zip = "", $work_address = "", $work_fax = "", $homepage = "", $department = "", $function = "", $comments="", $group_id = 0, $color='')
  55.     {
  56.         $name = htmlentities(smart_addslashes($name));
  57.         $work_phone = htmlentities(smart_addslashes($work_phone));
  58.         $home_phone = htmlentities(smart_addslashes($home_phone));
  59.         $fax = htmlentities(smart_addslashes($fax));
  60.         $cellular = htmlentities(smart_addslashes($cellular));
  61.         $country = htmlentities(smart_addslashes($country));
  62.         $state = htmlentities(smart_addslashes($state));
  63.         $city = htmlentities(smart_addslashes($city));
  64.         $zip = htmlentities(smart_addslashes($zip));
  65.         $address = htmlentities(smart_addslashes($address));
  66.         $company = htmlentities(smart_addslashes($company));
  67.         $work_country = htmlentities(smart_addslashes($work_country));
  68.         $work_state = htmlentities(smart_addslashes($work_state));
  69.         $work_city = htmlentities(smart_addslashes($work_city));
  70.         $work_zip = htmlentities(smart_addslashes($work_zip));
  71.         $work_address = htmlentities(smart_addslashes($work_address));
  72.         $work_fax = htmlentities(smart_addslashes($work_fax));
  73.         $homepage = htmlentities(smart_addslashes($homepage));
  74.         $function = htmlentities(smart_addslashes($function));
  75.  
  76.         if ($group_id == '')
  77.         {
  78.             $group_id = 0;
  79.         }
  80.  
  81.         $contact_id = $this->nextid("contacts");
  82.         if ($contact_id > 0)
  83.         {
  84.             $sql = "INSERT INTO contacts ";
  85.             $sql .= "(id, source_id, user_id, name, email, work_phone, home_phone, fax, cellular, country, state, city, zip, address, company, work_country, work_state, work_city, work_zip, work_address, work_fax, homepage, department, function, comments, group_id, color) VALUES ";
  86.             $sql .= "('$contact_id', '$source_id', '$user_id', '$name', '$email', '$work_phone', '$home_phone', '$fax', '$cellular', '$country', '$state', '$city', '$zip', '$address', '$company', '$work_country', '$work_state', '$work_city', '$work_zip', '$work_address', '$work_fax', '$homepage', '$department', '$function','$comments', '$group_id', '$color')";
  87.             $query = $this->query($sql);
  88.             if ($this->affected_rows() > 0)
  89.             {
  90.                 return true;
  91.             }else
  92.             {
  93.                 return false;
  94.             }
  95.         }else
  96.         {
  97.             return false;
  98.         }
  99.     }
  100.  
  101.     function update_contact($id, $name, $email, $work_phone, $home_phone, $fax, $cellular, $country, $state, $city, $zip, $address, $company, $work_country, $work_state, $work_city, $work_zip, $work_address, $work_fax, $homepage, $department, $function, $comments = '', $group_id='0', $color)
  102.     {
  103.         $name = htmlentities(smart_addslashes($name));
  104.         $work_phone = htmlentities(smart_addslashes($work_phone));
  105.         $home_phone = htmlentities(smart_addslashes($home_phone));
  106.         $fax = htmlentities(smart_addslashes($fax));
  107.         $cellular = htmlentities(smart_addslashes($cellular));
  108.         $country = htmlentities(smart_addslashes($country));
  109.         $state = htmlentities(smart_addslashes($state));
  110.         $city = htmlentities(smart_addslashes($city));
  111.         $zip = htmlentities(smart_addslashes($zip));
  112.         $address = htmlentities(smart_addslashes($address));
  113.         $company = htmlentities(smart_addslashes($company));
  114.         $work_country = htmlentities(smart_addslashes($work_country));
  115.         $work_state = htmlentities(smart_addslashes($work_state));
  116.         $work_city = htmlentities(smart_addslashes($work_city));
  117.         $work_zip = htmlentities(smart_addslashes($work_zip));
  118.         $work_address = htmlentities(smart_addslashes($work_address));
  119.         $work_fax = htmlentities(smart_addslashes($work_fax));
  120.         $homepage = htmlentities(smart_addslashes($homepage));
  121.         $function = htmlentities(smart_addslashes($function));
  122.  
  123.         $sql = "UPDATE contacts SET ";
  124.         $sql .= "name='$name', email='$email', work_phone='$work_phone', home_phone='$home_phone', fax='$fax', cellular='$cellular', state='$state'";
  125.         $sql .= ", country='$country', city='$city', zip='$zip', address='$address', company='$company', department='$department', function='$function', work_country='$work_country', work_state='$work_state', work_city='$work_city', work_zip='$work_zip', work_address='$work_address', work_fax='$work_fax', homepage='$homepage', comments='$comments', group_id='$group_id', color='$color'";
  126.         $sql .= " WHERE id='$id'";
  127.         $query = $this->query($sql);
  128.         if (isset($query))
  129.         {
  130.             return true;
  131.         }else
  132.         {
  133.             return false;
  134.         }
  135.     }
  136.  
  137.     function delete_contact($id)
  138.     {
  139.         $sql = "DELETE FROM contacts WHERE id='$id'";
  140.         $this->query($sql);
  141.         return true;
  142.     }
  143.  
  144.     function synchronise($contact_id, $user_id)
  145.     {
  146.         $sql = "SELECT * FROM users WHERE id='$user_id'";
  147.         $this->query($sql);
  148.         if ($this->next_record())
  149.         {
  150.             $sql  = "UPDATE contacts SET ";
  151.             $sql .= "name='".$this->f("name")."', email='".$this->f("email")."', work_phone='".$this->f("work_phone")."', home_phone='".$this->f("home_phone")."', fax='".$this->f("fax")."', cellular='".$this->f("cellular")."', state='".$this->f("state")."'";
  152.             $sql .= ", country='".$this->f("country")."', city='".$this->f("city")."', zip='".$this->f("zip")."', address='".$this->f("address")."', company='".$this->f("company")."', department='".$this->f("department")."', function='".$this->f("function")."'";
  153.             $sql .= " WHERE id='$contact_id'";
  154.             $this->query($sql);
  155.         }
  156.     }
  157.  
  158.     function user_is_contact($user_id, $id)
  159.     {
  160.         $sql = "SELECT * FROM contacts WHERE source_id='$id' AND user_id='$user_id'";
  161.         $this->query($sql);
  162.         if ($this->next_record())
  163.         {
  164.             return $this->Record;
  165.         }else
  166.         {
  167.             return false;
  168.         }
  169.     }
  170.  
  171.     function get_contact_id_by_email($email, $user_id)
  172.     {
  173.         $sql = "SELECT id FROM contacts WHERE email='".smart_addslashes($email)."' AND user_id='$user_id'";
  174.         $this->query($sql);
  175.         if ($this->next_record())
  176.             return $this->f("id");
  177.         else
  178.             return false;
  179.     }
  180.  
  181.     function get_contact_profile_by_email($email, $user_id)
  182.     {
  183.         $sql = "SELECT * FROM contacts WHERE email='".smart_addslashes($email)."' AND user_id='$user_id'";
  184.         $this->query($sql);
  185.         if ($this->next_record())
  186.             return $this->Record;
  187.         else
  188.             return false;
  189.     }
  190.  
  191.     function get_groups($user_id)
  192.     {
  193.         $sql= "SELECT * FROM contact_groups WHERE user_id='$user_id'";
  194.         $this->query($sql);
  195.         return $this->num_rows();
  196.     }
  197.  
  198.     function add_group($user_id, $name)
  199.     {
  200.         $name = htmlentities(smart_addslashes($name));
  201.         $group_id = $this->nextid("contact_groups");
  202.         if ($group_id > 0)
  203.         {
  204.             $sql = "INSERT INTO contact_groups (id, user_id, name) VALUES ('$group_id', '$user_id', '".smart_addslashes($name)."')";
  205.             $this->query($sql);
  206.             return $group_id;
  207.         }else
  208.         {
  209.             return false;
  210.         }
  211.     }
  212.  
  213.     function delete_group($group_id)
  214.     {
  215.         $sql = "UPDATE contacts SET group_id='0' WHERE group_id='$group_id'";
  216.         if ($this->query($sql))
  217.         {
  218.             $sql = "DELETE FROM contact_groups WHERE id='$group_id'";
  219.             $this->query($sql);
  220.             return true;
  221.         }
  222.         return false;
  223.     }
  224.  
  225.     function move_contact_to_group($contact_id, $group_id)
  226.     {
  227.         $sql = "UPDATE contacts SET group_id='$group_id' WHERE id='$contact_id'";
  228.         $this->query($sql);
  229.     }
  230.  
  231.     function clear_group($group_id)
  232.     {
  233.         $sql = "UPDATE contacts SET group_id='0' WHERE group_id='$group_id'";
  234.         $this->query($sql);
  235.     }
  236.  
  237.     function change_group_name($group_id, $name)
  238.     {
  239.         $sql = "UPDATE contact_groups SET name='".smart_addslashes($name)."' WHERE id='$group_id'";
  240.         $this->query($sql);
  241.     }
  242.  
  243.     function delete_user($user_id)
  244.     {
  245.         $sql = "UPDATE contacts SET source_id='0' WHERE source_id='$user_id'";
  246.         $this->query($sql);
  247.         $sql = "DELETE FROM contacts WHERE user_id ='$user_id'";
  248.         $this->query($sql);
  249.     }
  250. }
  251. ?>
  252.