Merged stable branch up to 2.3.10
[fa-stable.git] / includes / db / crm_contacts_db.inc
index 1f4051a6b4c3af57a3193be1f784b77a7ffed5d5..3b47790fb313b851fb289d9cf16469157eecc750 100644 (file)
@@ -108,7 +108,7 @@ function get_crm_persons($type=null, $action=null, $entity=null, $person=null, $
        if ($unique)
                $sql .= " GROUP BY person_id";
        else
-               $sql .= " ORDER BY t.id";
+               $sql .= " ORDER BY contact_id";
 
        return db_query($sql, "Can't get crm persons");
 }
@@ -150,6 +150,9 @@ function update_person_contacts($id, $cat_ids, $entity_id=null)
 
        $ret = db_query($sql, "Can't delete person contacts");
 
+       foreach($cat_ids as $n => $cid)
+               $cat_ids[$n] = db_escape($cid);
+
        if($ret && count($cat_ids)) {
                array_walk($cat_ids,'db_escape');
                $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id,type,action,entity_id)
@@ -241,13 +244,30 @@ function add_crm_contact($type, $action, $entity_id, $person_id)
                .db_escape($entity_id) . ")";
        return db_query($sql, "Can't insert crm contact");
 }
-
+/*
+       Delete contact selected by unique id.
+*/
 function delete_crm_contact($id)
 {
        $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE id=".db_escape($id);
 
        return db_query($sql, "Can't delete crm contact");
 }
+/*
+       Delete selected contacts for given person
+*/
+function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null)
+{
+       $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id);
+       if ($type)
+               $sql .= ' AND type='.db_escape($type);
+       if ($entity_id)
+               $sql .= ' AND entity_id='.db_escape($entity_id);
+       if ($action)
+               $sql .= ' AND action='.db_escape($action);
+
+       return db_query($sql, "Can't delete crm contact");
+}
 
 /*
        Returns person data for given contact id