home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / addressbook.class.inc < prev    next >
Text File  |  2004-03-08  |  26KB  |  765 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 addressbook extends db
  13. {
  14.     var $selector_is_enabled = false;
  15.  
  16.     function addressbook()
  17.     {
  18.         $this->db();
  19.     }
  20.  
  21.     function select_contacts($target_field, $handler_file, $multiselect='true', $require_email_address='true', $show_users='true', $show_contacts='true', $show_companies='true', $pass_value='email')
  22.     {
  23.         if (!$this->selector_is_enabled)
  24.         {
  25.             die('FATAL ERROR: called select contacts while contact_selector is not enabled. Call enable_contact_selector() first outside a form!');
  26.         }else
  27.         {
  28.             $link =  "javascript:open_addressbook('$target_field',";
  29.             if ($target_field != '')
  30.             {
  31.                 $link .= $target_field.'.value';
  32.             }else
  33.             {
  34.                 $link .= "''";
  35.             }
  36.             $link .= ", '$handler_file', '$multiselect', '$require_email_address', '$show_users', '$show_contacts', '$show_companies', '$pass_value');";
  37.             return $link;
  38.         }
  39.     }
  40.  
  41.     function enable_contact_selector()
  42.     {
  43.         global $GO_CONFIG, $GO_MODULES;
  44.  
  45.         $ab_module = $GO_MODULES->get_module('addressbook');
  46.         if (!$ab_module)
  47.         {
  48.             return false;
  49.         }
  50.         echo '
  51.             <form name="ab_form" action="'.$ab_module['url'].'select.php" method="post" target="ab_select">
  52.             <input type="hidden" name="address_string" />
  53.             <input type="hidden" name="GO_HANDLER" />
  54.             <input type="hidden" name="GO_FIELD" />
  55.             <input type="hidden" name="multiselect" />
  56.             <input type="hidden" name="require_email_address" />
  57.             <input type="hidden" name="show_users" />
  58.             <input type="hidden" name="show_contacts" />
  59.             <input type="hidden" name="show_companies" />
  60.             <input type="hidden" name="pass_value" />
  61.             </form>
  62.  
  63.             <script type="text/javascript">
  64.             function open_addressbook(field, addresses, handler_file, multiselect, require_email_address, show_users, show_contacts, show_companies, pass_value)
  65.             {
  66.                 var popup = window.open("about:blank", "ab_select", "width=600,height=400,scrollbars=yes,resizable=yes,status=yes");
  67.  
  68.                 document.ab_form.address_string.value = addresses;
  69.                 document.ab_form.GO_HANDLER.value = handler_file;
  70.                 document.ab_form.GO_FIELD.value = field;
  71.                 document.ab_form.multiselect.value = multiselect;
  72.                 document.ab_form.require_email_address.value = require_email_address;
  73.                 document.ab_form.show_users.value = show_users;
  74.                 document.ab_form.show_contacts.value = show_contacts;
  75.                 document.ab_form.show_companies.value = show_companies;
  76.                 document.ab_form.pass_value.value = pass_value;
  77.                 document.ab_form.submit();
  78.  
  79.                 if (!popup.opener) popup.opener = self;
  80.                 popup.focus();
  81.             }
  82.             </script>
  83.         ';
  84.         $this->selector_is_enabled = true;
  85.     }
  86.  
  87.     function get_default_addressbook($user_id)
  88.     {
  89.         $sql = "SELECT addressbook_id FROM ab_subscribed WHERE user_id='$user_id' AND standard='1'";
  90.         $this->query($sql);
  91.         if($this->next_record())
  92.         {
  93.             return $this->f('addressbook_id');
  94.         }
  95.         return false;
  96.     }
  97.  
  98.     function set_default_addressbook($user_id, $addressbook_id)
  99.     {
  100.         $sql = "UPDATE ab_subscribed SET standard='0' WHERE user_id='$user_id' AND standard='1'";
  101.         if ($this->query($sql))
  102.         {
  103.             return $this->query("UPDATE ab_subscribed SET standard='1' WHERE user_id='$user_id' AND addressbook_id='$addressbook_id'");
  104.         }
  105.         return false;
  106.     }
  107.  
  108.     function get_addressbooks()
  109.     {
  110.         $sql = "SELECT * FROM ab_addressbooks ORDER BY name ASC";
  111.         $this->query($sql);
  112.         return $this->num_rows();
  113.     }
  114.  
  115.     function get_user_addressbooks($user_id)
  116.     {
  117.         $sql = "SELECT DISTINCT ab_addressbooks.* FROM ab_addressbooks, acl, users_groups WHERE (".
  118.                 "ab_addressbooks.acl_read = acl.acl_id OR ab_addressbooks.acl_write = acl.acl_id".
  119.                 ") AND ( ( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  120.                 "acl.group_id = 0 AND acl.user_id = ".$user_id." ) ) ORDER BY name ASC";
  121.         $this->query($sql);
  122.         return $this->num_rows();
  123.     }
  124.  
  125.  
  126.     function add_addressbook($user_id, $name)
  127.     {
  128.         global $GO_SECURITY;
  129.  
  130.         $addressbook_id = $this->nextid('ab_addressbooks');
  131.         $acl_read = $GO_SECURITY->get_new_acl('acl_read addressbook_id: '.$addressbook_id);
  132.         $acl_write = $GO_SECURITY->get_new_acl('acl_write addressbook_id: '.$addressbook_id);
  133.  
  134.         if($addressbook_id > 0 && $acl_read > 0 && $acl_write > 0)
  135.         {
  136.             $sql = "INSERT INTO ab_addressbooks (id, user_id, name, acl_read, acl_write) VALUES ('$addressbook_id', '$user_id', '".smart_addslashes($name)."', '$acl_read', '$acl_write')";
  137.             if($this->query($sql))
  138.             {
  139.                 $GO_SECURITY->add_user_to_acl($user_id, $acl_write);
  140.  
  141.                 $this->subscribe($user_id, $addressbook_id);
  142.  
  143.                 if (!$this->get_default_addressbook($user_id))
  144.                 {
  145.                     $this->set_default_addressbook($user_id, $addressbook_id);
  146.                 }
  147.                 return $addressbook_id;
  148.             }
  149.         }else
  150.         {
  151.             $GO_SECURITY->delete_acl($acl_read);
  152.             $GO_SECURITY->delete_acl($acl_write);
  153.         }
  154.         return false;
  155.     }
  156.  
  157.     function update_addressbook($addressbook_id, $name)
  158.     {
  159.         $sql = "UPDATE ab_addressbooks SET name='".smart_addslashes($name)."' WHERE id='$addressbook_id'";
  160.         return $this->query($sql);
  161.     }
  162.  
  163.     function get_addressbook_by_name($name)
  164.     {
  165.         $sql = "SELECT * FROM ab_addressbooks WHERE name='".smart_addslashes($name)."'";
  166.         $this->query($sql);
  167.         if ($this->next_record())
  168.         {
  169.             return $this->Record;
  170.         }else
  171.         {
  172.             return false;
  173.         }
  174.     }
  175.  
  176.     function delete_addressbook($addressbook_id)
  177.     {
  178.         global $GO_SECURITY;
  179.  
  180.         $ab = new addressbook();
  181.  
  182.         $sql = "SELECT * FROM ab_contacts WHERE addressbook_id='$addressbook_id'";
  183.         $this->query($sql);
  184.         while($this->next_record())
  185.         {
  186.             $GO_SECURITY->delete_acl($this->f('acl_read'));
  187.             $GO_SECURITY->delete_acl($this->f('acl_write'));
  188.             $ab->delete_contact($this->f('id'));
  189.         }
  190.  
  191.         $sql = "SELECT * FROM ab_companies WHERE addressbook_id='$addressbook_id'";
  192.         $this->query($sql);
  193.         while($this->next_record())
  194.         {
  195.             $GO_SECURITY->delete_acl($this->f('acl_read'));
  196.             $GO_SECURITY->delete_acl($this->f('acl_write'));
  197.             $ab->delete_contact($this->f('id'));
  198.         }
  199.  
  200.         if ($this->query("DELETE FROM ab_subscribed WHERE addressbook_id='$addressbook_id'"))
  201.         {
  202.             $sql = "DELETE FROM ab_addressbooks WHERE id='$addressbook_id'";
  203.             return $this->query($sql);
  204.         }
  205.     }
  206.  
  207.     function get_addressbook($addressbook_id)
  208.     {
  209.         $sql = "SELECT * FROM ab_addressbooks WHERE id='$addressbook_id'";
  210.         $this->query($sql);
  211.         if($this->next_record())
  212.         {
  213.             return $this->Record;
  214.         }
  215.         return false;
  216.     }
  217.  
  218.     function get_subscribed_addressbooks($user_id)
  219.     {
  220.         $sql = "SELECT ab_addressbooks.* FROM ab_addressbooks LEFT JOIN ab_subscribed ON (ab_subscribed.addressbook_id=ab_addressbooks.id) WHERE ab_subscribed.user_id='$user_id' ORDER BY name ASC";
  221.         $this->query($sql);
  222.         return $this->num_rows();
  223.     }
  224.  
  225.     function subscribe($user_id, $addressbook_id)
  226.     {
  227.         $sql = "INSERT INTO ab_subscribed (user_id, addressbook_id) VALUES ('$user_id', '$addressbook_id')";
  228.         return $this->query($sql);
  229.     }
  230.  
  231.     function is_subscribed($user_id, $addressbook_id)
  232.     {
  233.         $sql = "SELECT addressbook_id FROM ab_subscribed WHERE user_id='$user_id' AND addressbook_id='$addressbook_id'";
  234.         $this->query($sql);
  235.         return $this->next_record();
  236.     }
  237.  
  238.     function unsubscribe_all($user_id)
  239.     {
  240.         $sql = "DELETE FROM ab_subscribed WHERE user_id='$user_id'";
  241.         return $this->query($sql);
  242.     }
  243.  
  244.     function unsubscribe($user_id, $addressbook_id)
  245.     {
  246.         $sql = "DELETE FROM ab_subscribed WHERE user_id='$user_id' AND addressbook_id='$addressbook_id'";
  247.         return $this->query($sql);
  248.     }
  249.  
  250.     function search_contacts($user_id, $query, $field='last_name', $addressbook_id=0)
  251.     {
  252.         $query = str_replace('*', '%', $query);
  253.  
  254.         $sql = "SELECT DISTINCT ab_contacts.* FROM ab_contacts ";
  255.  
  256.         if ($addressbook_id > 0)
  257.         {
  258.             $sql .= "WHERE ab_contacts.addressbook_id='$addressbook_id' AND ";
  259.         }else
  260.         {
  261.             $sql .=    "INNER JOIN ab_subscribed ON (ab_subscribed.addressbook_id=ab_contacts.addressbook_id) WHERE ab_subscribed.user_id='$user_id' AND ";
  262.         }
  263.  
  264.         $sql .= "ab_contacts.$field LIKE '".smart_addslashes($query)."' ".
  265.                 "ORDER BY first_name ASC, last_name ASC";
  266.         $this->query($sql);
  267.         return $this->num_rows();
  268.     }
  269.  
  270.     function search_companies($user_id, $query, $field='name', $addressbook_id=0)
  271.     {
  272.         $query = str_replace('*', '%', $query);
  273.  
  274.         $sql = "SELECT DISTINCT ab_companies.* FROM ab_companies ";
  275.  
  276.         if ($addressbook_id > 0)
  277.         {
  278.             $sql .= "WHERE ab_companies.addressbook_id='$addressbook_id' AND ";
  279.         }else
  280.         {
  281.             $sql .=    "INNER JOIN ab_subscribed ON (ab_subscribed.addressbook_id=ab_companies.addressbook_id) WHERE ab_subscribed.user_id='$user_id' AND ";
  282.         }
  283.  
  284.         $sql .= "ab_companies.$field LIKE '".smart_addslashes($query)."' ".
  285.                 "ORDER BY name ASC";
  286.         $this->query($sql);
  287.         return $this->num_rows();
  288.     }
  289.  
  290.     function get_contacts($addressbook_id, $sort = "name", $direction = "ASC", $user_id=0)
  291.     {
  292.         global $GO_SECURITY;
  293.  
  294.         if ($user_id == 0)
  295.         {
  296.             $user_id = $GO_SECURITY->user_id;
  297.         }
  298.         if($sort == 'name')
  299.         {
  300.             $sort = 'first_name '.$direction.', last_name';
  301.         }
  302.         $sql = "SELECT DISTINCT * FROM ab_contacts ".
  303.                 "INNER JOIN acl ON (ab_contacts.acl_read = acl.acl_id OR ab_contacts.acl_write = acl.acl_id) ".
  304.                 "LEFT JOIN users_groups ON (acl.group_id = users_groups.group_id) ".
  305.                 " WHERE ((users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  306.                 "acl.group_id = 0 AND acl.user_id = ".$user_id.")) ";
  307.                 "AND ab_contacts.addressbook_id='$addressbook_id' ".
  308.                 "ORDER BY $sort $direction";
  309.  
  310.         $this->query($sql);
  311.         return $this->num_rows();
  312.     }
  313.  
  314.     function get_company_contacts($company_id, $sort="name", $direction="ASC", $start, $offset)
  315.     {
  316.         if($sort == 'name')
  317.         {
  318.             $sort = 'first_name '.$direction.', last_name';
  319.         }
  320.         $sql = "SELECT * FROM ab_contacts WHERE company_id='$company_id' ORDER BY $sort $direction";
  321.  
  322.         if ($offset != 0)
  323.         {
  324.             $sql .= " LIMIT $start, $offset";
  325.  
  326.             $sql2= "SELECT * FROM ab_contacts WHERE company_id='$company_id'";
  327.  
  328.             $this->query($sql2);
  329.             $count = $this->num_rows();
  330.  
  331.             if ($count > 0)
  332.             {
  333.                 $this->query($sql);
  334.                 return $count;
  335.             }
  336.             return 0;
  337.  
  338.         }else
  339.         {
  340.             $this->query($sql);
  341.             return $this->num_rows();
  342.         }
  343.     }
  344.  
  345.     function get_contacts_for_export($addressbook_id, $user_id=0)
  346.     {
  347.         global $GO_SECURITY;
  348.  
  349.         if ($user_id == 0)
  350.         {
  351.             $user_id = $GO_SECURITY->user_id;
  352.         }
  353.         $sql = "SELECT DISTINCT ab_contacts.*, ab_groups.name AS group_name,".
  354.                 "ab_companies.name AS company FROM ab_contacts ".
  355.                 "INNER JOIN acl ON (ab_contacts.acl_read = acl.acl_id OR ab_contacts.acl_write = acl.acl_id) ".
  356.                 "LEFT JOIN users_groups ON (acl.group_id = users_groups.group_id) ".
  357.                 "LEFT JOIN ab_groups ON (ab_contacts.group_id=ab_groups.id) ".
  358.                 "LEFT JOIN ab_companies ON (ab_contacts.company_id=ab_companies.id) ".
  359.                 " WHERE ((users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  360.                 "acl.group_id = 0 AND acl.user_id = ".$user_id.")) ".
  361.                 "AND ab_contacts.addressbook_id='$addressbook_id' ".
  362.                 "ORDER BY ab_contacts.first_name, ab_contacts.last_name ASC";
  363.  
  364.         $this->query($sql);
  365.         return $this->num_rows();
  366.     }
  367.  
  368.     function get_contacts_group($addressbook_id, $group_id, $sort = "name", $direction = "ASC", $user_id=0)
  369.     {
  370.         global $GO_SECURITY;
  371.         if ($user_id == 0)
  372.         {
  373.             $user_id = $GO_SECURITY->user_id;
  374.         }
  375.         if($sort == 'name')
  376.         {
  377.             $sort = 'first_name '.$direction.', last_name';
  378.         }
  379.         $sql = "SELECT DISTINCT ab_contacts.* FROM ab_contacts ".
  380.                 "INNER JOIN acl ON (ab_contacts.acl_read = acl.acl_id OR ab_contacts.acl_write = acl.acl_id) ".
  381.                 "LEFT JOIN users_groups ON (acl.group_id = users_groups.group_id) ".
  382.                 "WHERE ((users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  383.                 "acl.group_id = 0 AND acl.user_id = ".$user_id.")) ".
  384.                 "AND ab_contacts.addressbook_id='$addressbook_id' ".
  385.                 "AND ab_contacts.group_id='$group_id' ".
  386.                 "ORDER BY $sort $direction";
  387.         $this->query($sql);
  388.         return $this->num_rows();
  389.        }
  390.  
  391.     function get_contact($contact_id)
  392.     {
  393.         $this->query("SELECT ab_contacts.*, ab_companies.address AS work_address, ab_companies.zip AS work_zip, ".
  394.                         "ab_companies.city AS work_city, ab_companies.state AS work_state, ".
  395.                         "ab_companies.country AS work_country, ab_companies.homepage, ".
  396.                         "ab_companies.bank_no, ab_companies.email AS company_email, ".
  397.                         "ab_companies.phone AS company_phone, ab_companies.fax AS company_fax, ".
  398.                         "ab_companies.name AS company_name FROM ab_contacts ".
  399.                         "LEFT JOIN ab_companies ON (ab_contacts.company_id=ab_companies.id) WHERE ab_contacts.id='$contact_id'");
  400.  
  401.         if ($this->next_record())
  402.         {
  403.             return $this->Record;
  404.         }
  405.     }
  406.  
  407.     function add_contact($source_id, $addressbook_id, $first_name, $middle_name, $last_name, $initials, $title, $sex, $birthday, $email, $work_phone="", $home_phone="", $fax="", $cellular="", $country="", $state="", $city="", $zip="", $address="", $company_id=0, $work_fax = "", $department="", $function="", $comment="", $group_id=0, $color='', $acl_read, $acl_write)
  408.     {
  409.         $first_name = htmlentities(smart_addslashes($first_name));
  410.         $middle_name = htmlentities(smart_addslashes($middle_name));
  411.         $last_name = htmlentities(smart_addslashes($last_name));
  412.         $initials = htmlentities(smart_addslashes($initials));
  413.         $title = htmlentities(smart_addslashes($title));
  414.         $birthday = htmlentities(smart_addslashes($birthday));
  415.         $email = htmlentities(smart_addslashes($email));
  416.         $work_phone = htmlentities(smart_addslashes($work_phone));
  417.         $home_phone = htmlentities(smart_addslashes($home_phone));
  418.         $fax = htmlentities(smart_addslashes($fax));
  419.         $cellular = htmlentities(smart_addslashes($cellular));
  420.         $country = htmlentities(smart_addslashes($country));
  421.         $state = htmlentities(smart_addslashes($state));
  422.         $city = htmlentities(smart_addslashes($city));
  423.         $zip = htmlentities(smart_addslashes($zip));
  424.         $address = htmlentities(smart_addslashes($address));
  425.         $work_fax = htmlentities(smart_addslashes($work_fax));
  426.         $function = htmlentities(smart_addslashes($function));
  427.  
  428.         $contact_id = $this->nextid("contacts");
  429.         if ($contact_id > 0)
  430.         {
  431.             $sql = "INSERT INTO ab_contacts ";
  432.             $sql .= "(id, source_id, addressbook_id, first_name, middle_name, last_name, initials, title, sex, birthday, email, work_phone, home_phone, fax, cellular, country, state, city, zip, address, company_id, work_fax, department, function, comment, group_id, color, acl_read, acl_write) VALUES ";
  433.             $sql .= "('$contact_id', '$source_id', '$addressbook_id', '$first_name', '$middle_name', '$last_name', '$initials', '$title', '$sex', '$birthday', '$email', '$work_phone', '$home_phone', '$fax', '$cellular', '$country', '$state', '$city', '$zip', '$address', '$company_id', '$work_fax', '$department', '$function','$comment', '$group_id', '$color', '$acl_read', '$acl_write')";
  434.             $query = $this->query($sql);
  435.             if ($this->affected_rows() > 0)
  436.             {
  437.                 return $contact_id;
  438.             }else
  439.             {
  440.                 return false;
  441.             }
  442.         }else
  443.         {
  444.             return false;
  445.         }
  446.     }
  447.  
  448.     function update_contact($id, $addressbook_id, $first_name, $middle_name, $last_name, $initials, $title, $sex, $birthday, $email, $work_phone, $home_phone, $fax, $cellular, $country, $state, $city, $zip, $address, $company_id, $work_fax, $department, $function, $comment='', $group_id='0', $color)
  449.     {
  450.         $first_name = htmlentities(smart_addslashes($first_name));
  451.         $middle_name = htmlentities(smart_addslashes($middle_name));
  452.         $last_name = htmlentities(smart_addslashes($last_name));
  453.         $initials = htmlentities(smart_addslashes($initials));
  454.         $title = htmlentities(smart_addslashes($title));
  455.         $birthday = htmlentities(smart_addslashes($birthday));
  456.         $email = htmlentities(smart_addslashes($email));
  457.         $work_phone = htmlentities(smart_addslashes($work_phone));
  458.         $home_phone = htmlentities(smart_addslashes($home_phone));
  459.         $fax = htmlentities(smart_addslashes($fax));
  460.         $cellular = htmlentities(smart_addslashes($cellular));
  461.         $country = htmlentities(smart_addslashes($country));
  462.         $state = htmlentities(smart_addslashes($state));
  463.         $city = htmlentities(smart_addslashes($city));
  464.         $zip = htmlentities(smart_addslashes($zip));
  465.         $address = htmlentities(smart_addslashes($address));
  466.         $work_fax = htmlentities(smart_addslashes($work_fax));
  467.         $function = htmlentities(smart_addslashes($function));
  468.  
  469.         $sql = "UPDATE ab_contacts SET ";
  470.         $sql .= "addressbook_id='$addressbook_id', first_name='$first_name', middle_name='$middle_name', last_name='$last_name', initials='$initials', title='$title', sex='$sex', birthday='$birthday', email='$email', work_phone='$work_phone', home_phone='$home_phone', fax='$fax', cellular='$cellular', state='$state'";
  471.         $sql .= ", country='$country', city='$city', zip='$zip', address='$address', company_id='$company_id', department='$department', function='$function', work_fax='$work_fax', comment='$comment', group_id='$group_id', color='$color'";
  472.         $sql .= " WHERE id='$id'";
  473.         $query = $this->query($sql);
  474.         if (isset($query))
  475.         {
  476.             return true;
  477.         }else
  478.         {
  479.             return false;
  480.         }
  481.     }
  482.  
  483.     function delete_contact($contact_id)
  484.     {
  485.  
  486.         $sql1 = "DELETE FROM tp_mailing_contacts WHERE contact_id='$contact_id'";
  487.         $sql2 = "DELETE FROM ab_custom_contact_fields WHERE id='$contact_id'";
  488.  
  489.         if ($this->query($sql1) && $this->query($sql2))
  490.         {
  491.             return $this->query("DELETE FROM ab_contacts WHERE id='$contact_id'");
  492.         }
  493.     }
  494.  
  495.     function user_is_contact($user_id, $id)
  496.     {
  497.         $sql = "SELECT ab_contacts.* FROM ab_contacts LEFT JOIN ab_addressbooks ON (ab_addressbooks.id=ab_contacts.addressbook_id) WHERE ab_contacts.source_id='$id' AND ab_addressbooks.user_id='$user_id'";
  498.         $this->query($sql);
  499.         if ($this->next_record())
  500.         {
  501.             return $this->Record;
  502.         }else
  503.         {
  504.             return false;
  505.         }
  506.     }
  507.  
  508.     function user_is_in_addressbook($user_id, $addressbook_id)
  509.     {
  510.         $sql = "SELECT id FROM ab_contacts WHERE source_id='$user_id' AND addressbook_id='$addressbook_id'";
  511.         $this->query($sql);
  512.         return $this->next_record();
  513.     }
  514.  
  515.     function get_contact_id_by_email($email, $user_id)
  516.     {
  517.         $sql = "SELECT ab_contacts.id FROM ab_contacts LEFT JOIN ab_addressbooks ON (ab_addressbooks.id=ab_contacts.addressbook_id) WHERE ab_contacts.email='".smart_addslashes($email)."' AND ab_addressbooks.user_id='$user_id'";
  518.         $this->query($sql);
  519.         if ($this->next_record())
  520.             return $this->f("id");
  521.         else
  522.             return false;
  523.     }
  524.  
  525.     function get_contact_profile_by_email($email, $user_id)
  526.     {
  527.         $sql = "SELECT ab_contacts.* FROM ab_contacts LEFT JOIN ab_addressbooks ON (ab_addressbooks.id=ab_contacts.addressbook_id) WHERE ab_contacts.email='".smart_addslashes($email)."' AND ab_addressbooks.user_id='$user_id'";
  528.         $this->query($sql);
  529.         if ($this->next_record())
  530.             return $this->Record;
  531.         else
  532.             return false;
  533.     }
  534.  
  535.     function get_groups($addressbook_id)
  536.     {
  537.         $sql= "SELECT * FROM ab_groups WHERE addressbook_id='$addressbook_id'";
  538.         $this->query($sql);
  539.         return $this->num_rows();
  540.     }
  541.  
  542.     function add_group($addressbook_id, $name)
  543.     {
  544.         $name = htmlentities(smart_addslashes($name));
  545.         $group_id = $this->nextid("ab_groups");
  546.         if ($group_id > 0)
  547.         {
  548.             $sql = "INSERT INTO ab_groups (id, addressbook_id, name) VALUES ('$group_id', '$addressbook_id', '".smart_addslashes($name)."')";
  549.             $this->query($sql);
  550.             return $group_id;
  551.         }else
  552.         {
  553.             return false;
  554.         }
  555.     }
  556.  
  557.     function delete_group($group_id)
  558.     {
  559.         $sql = "UPDATE ab_contacts SET group_id='0' WHERE group_id='$group_id'";
  560.         if ($this->query($sql))
  561.         {
  562.             $sql = "DELETE FROM ab_groups WHERE id='$group_id'";
  563.             $this->query($sql);
  564.             return true;
  565.         }
  566.         return false;
  567.     }
  568.  
  569.     function move_contact_to_group($contact_id, $group_id)
  570.     {
  571.         $sql = "UPDATE ab_contacts SET group_id='$group_id' WHERE id='$contact_id'";
  572.         $this->query($sql);
  573.     }
  574.  
  575.     function clear_group($group_id)
  576.     {
  577.         $sql = "UPDATE ab_contacts SET group_id='0' WHERE group_id='$group_id'";
  578.         $this->query($sql);
  579.     }
  580.  
  581.     function change_group_name($group_id, $name)
  582.     {
  583.         $sql = "UPDATE ab_groups SET name='".smart_addslashes($name)."' WHERE id='$group_id'";
  584.         $this->query($sql);
  585.     }
  586.  
  587.     function get_group($group_id)
  588.     {
  589.         $sql= "SELECT * FROM ab_groups WHERE id='$group_id'";
  590.         $this->query($sql);
  591.         if ($this->next_record())
  592.         {
  593.             return $this->Record;
  594.         }
  595.         return false;
  596.     }
  597.  
  598.     function get_group_by_name($addressbook_id, $name)
  599.     {
  600.         $sql= "SELECT * FROM ab_groups WHERE addressbook_id='$addressbook_id' AND name='$name'";
  601.         $this->query($sql);
  602.         if ($this->next_record())
  603.         {
  604.             return $this->Record;
  605.         }
  606.         return false;
  607.     }
  608.  
  609.     function add_contact_to_company($contact_id, $company_id)
  610.     {
  611.         return $this->query("UPDATE ab_contacts SET company_id='$company_id' WHERE id='$contact_id'");
  612.     }
  613.  
  614.     function add_company($addressbook_id, $name, $address, $zip, $city, $state, $country, $email, $phone, $fax, $homepage, $bank_no, $vat_no, $acl_read, $acl_write)
  615.     {
  616.         $company_id = $this->nextid("ab_companies");
  617.         if ($company_id > 0)
  618.         {
  619.             $name = smart_addslashes($name);
  620.             $address = smart_addslashes($address);
  621.             $zip = smart_addslashes($zip);
  622.             $city = smart_addslashes($city);
  623.             $state = smart_addslashes($state);
  624.             $country = smart_addslashes($country);
  625.             $email = smart_addslashes($email);
  626.             $phone = smart_addslashes($phone);
  627.             $fax = smart_addslashes($fax);
  628.             $homepage = smart_addslashes($homepage);
  629.             $bank_no = smart_addslashes($bank_no);
  630.             $vat_no = smart_addslashes($vat_no);
  631.  
  632.             $address = smart_addslashes($address);
  633.             $sql = "INSERT INTO ab_companies ".
  634.                     "(id, addressbook_id, name, address, zip, city, state, country, email, phone, fax, homepage, bank_no, vat_no, acl_read, acl_write) VALUES ".
  635.                     "('$company_id', '$addressbook_id', '$name', '$address', '$zip', '$city', '$state', '$country', '$email', '$phone', '$fax', '$homepage', '$bank_no', '$vat_no', '$acl_read', '$acl_write')";
  636.             if ($this->query($sql))
  637.             {
  638.                 return $company_id;
  639.             }
  640.         }
  641.         return false;
  642.     }
  643.  
  644.     function update_company($company_id, $addressbook_id, $name, $address, $zip, $city, $state, $country, $email, $phone, $fax, $homepage, $bank_no, $vat_no)
  645.     {
  646.         $name = smart_addslashes($name);
  647.         $address = smart_addslashes($address);
  648.         $zip = smart_addslashes($zip);
  649.         $city = smart_addslashes($city);
  650.         $state = smart_addslashes($state);
  651.         $country = smart_addslashes($country);
  652.         $phone = smart_addslashes($phone);
  653.         $email = smart_addslashes($email);
  654.         $fax = smart_addslashes($fax);
  655.         $homepage = smart_addslashes($homepage);
  656.         $bank_no = smart_addslashes($bank_no);
  657.         $vat_no = smart_addslashes($vat_no);
  658.  
  659.         $address = smart_addslashes($address);
  660.         $sql = "UPDATE ab_companies SET ".
  661.                 "addressbook_id='$addressbook_id', name='$name', ".
  662.                 "address='$address', zip='$zip', city='$city', state='$state', ".
  663.                 "country='$country', email='$email', phone='$phone', fax='$fax', ".
  664.                 "homepage='$homepage', bank_no='$bank_no', vat_no='$vat_no' WHERE id='$company_id'";
  665.  
  666.         return $this->query($sql);
  667.  
  668.     }
  669.  
  670.     function get_company($company_id)
  671.     {
  672.         $sql = "SELECT * FROM ab_companies WHERE id='$company_id'";
  673.         $this->query($sql);
  674.         if ($this->next_record())
  675.         {
  676.             return $this->Record;
  677.         }
  678.         return false;
  679.     }
  680.  
  681.     function get_companies($addressbook_id, $sort='name', $direction='ASC', $start=0, $offset=0, $user_id=0)
  682.     {
  683.         global $GO_SECURITY;
  684.         if ($user_id == 0)
  685.         {
  686.             $user_id = $GO_SECURITY->user_id;
  687.         }
  688.         $sql = "SELECT DISTINCT ab_companies.* FROM ab_companies ".
  689.                 "INNER JOIN acl ON (ab_companies.acl_read = acl.acl_id OR ab_companies.acl_write = acl.acl_id) ".
  690.                 "LEFT JOIN users_groups ON (acl.group_id = users_groups.group_id) ".
  691.                 "WHERE ((users_groups.user_id='$user_id' AND acl.user_id=0) OR (".
  692.                 "acl.group_id=0 AND acl.user_id='$user_id')) ".
  693.                 "AND addressbook_id='$addressbook_id' ORDER BY $sort $direction";
  694.         if ($offset != 0)
  695.         {
  696.             $sql .= " LIMIT $start, $offset";
  697.  
  698.             $sql2= "SELECT DISTINCT ab_companies.id FROM ab_companies ".
  699.                     "INNER JOIN acl ON (ab_companies.acl_read = acl.acl_id OR ab_companies.acl_write = acl.acl_id) ".
  700.                     "LEFT JOIN users_groups ON (acl.group_id = users_groups.group_id) ".
  701.                     "WHERE ((users_groups.user_id='$user_id' AND acl.user_id=0) OR (".
  702.                     "acl.group_id=0 AND acl.user_id='$user_id')) ".
  703.                     "AND addressbook_id='$addressbook_id'";
  704.             $this->query($sql);
  705.             $count = $this->num_rows();
  706.  
  707.             if ($count > 0)
  708.             {
  709.                 $this->query($sql);
  710.                 return $count;
  711.             }
  712.             return 0;
  713.  
  714.         }else
  715.         {
  716.             $this->query($sql);
  717.             return $this->num_rows();
  718.         }
  719.     }
  720.  
  721.     function search_company($name, $addressbook_id)
  722.     {
  723.         $sql = "SELECT * FROM ab_companies WHERE addressbook_id='$addressbook_id' AND name LIKE '%$name%'";
  724.         $this->query($sql);
  725.         return $this->num_rows();
  726.     }
  727.  
  728.     function get_company_id_by_name($name, $addressbook_id)
  729.     {
  730.         $sql = "SELECT id FROM ab_companies WHERE addressbook_id='$addressbook_id' AND name='".smart_addslashes($name)."'";
  731.         $this->query($sql);
  732.         if($this->next_record())
  733.         {
  734.             return $this->f('id');
  735.         }
  736.         return false;
  737.     }
  738.  
  739.     function delete_company($company_id)
  740.     {
  741.         $sql = "DELETE FROM ab_companies WHERE id='$company_id'";
  742.         if ($this->query($sql))
  743.         {
  744.             return $this->query("DELETE FROM ab_custom_company_fields WHERE id='$company_id'");
  745.         }
  746.     }
  747.  
  748.  
  749.     function delete_user($user_id)
  750.     {
  751.         $sql = "UPDATE ab_contacts SET source_id='0' WHERE source_id='$user_id'";
  752.         $this->query($sql);
  753.  
  754.         $ab = new addressbook();
  755.  
  756.         $sql = "SELECT id FROM ab_addressbooks WHERE user_id='$user_id'";
  757.         $this->query($sql);
  758.         while ($this->next_record())
  759.         {
  760.             $ab->delete_addressbook($this->f('id'));
  761.         }
  762.     }
  763. }
  764. ?>
  765.