Mysqli errors: Trying to access array offset on value of type bool. Fixed. Please...
[fa-stable.git] / includes / db / crm_contacts_db.inc
index 3b47790fb313b851fb289d9cf16469157eecc750..98862b73ae3c25d2a6ad338a34eef770e38850cb 100644 (file)
@@ -41,7 +41,7 @@ function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $e
 }
 
 function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, $fax, $email, 
-       $lang, $notes, $cat_ids, $entity=null)
+       $lang, $notes, $cat_ids, $entity=null, $type=null)
 {
        $sql = "UPDATE ".TB_PREF."crm_persons SET "
                  ."ref=".db_escape($ref) . ", "
@@ -60,7 +60,7 @@ function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2,
 
        $ret = db_query($sql, "Can't update crm person");
        if ($ret) {
-               if(!update_person_contacts($id, $cat_ids, $entity))
+               if(!update_person_contacts($id, $cat_ids, $entity, $type))
                        return null;
        }
        commit_transaction();
@@ -110,7 +110,12 @@ function get_crm_persons($type=null, $action=null, $entity=null, $person=null, $
        else
                $sql .= " ORDER BY contact_id";
 
-       return db_query($sql, "Can't get crm persons");
+       $result = db_query($sql, "Can't get crm persons");
+       // fallback to general contacts
+       if (!db_num_rows($result) && $action && $action != 'general')
+               return get_crm_persons($type, 'general', $entity, $person, $unique);
+       else
+               return $result;
 }
 
 function get_crm_person($id)
@@ -142,9 +147,11 @@ function get_person_contacts($id)
        return $contacts;
 }
 
-function update_person_contacts($id, $cat_ids, $entity_id=null
+function update_person_contacts($id, $cat_ids, $entity_id=null, $type=null)
 {
        $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($id);
+       if ($type)
+               $sql .= " AND type=".db_escape($type);
 
        begin_transaction();
 
@@ -158,6 +165,8 @@ function update_person_contacts($id, $cat_ids, $entity_id=null)
                $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id,type,action,entity_id)
                        SELECT ".db_escape($id).",t.type, t.action,".db_escape($entity_id, true)."
                        FROM ".TB_PREF."crm_categories t WHERE t.id=".implode(' OR t.id=', $cat_ids);
+               if ($type)
+                       $sql .= " AND t.type=".db_escape($type);
                $ret = db_query($sql, "Can't update person contacts");
        }
        commit_transaction();
@@ -166,10 +175,14 @@ function update_person_contacts($id, $cat_ids, $entity_id=null)
 
 function delete_entity_contacts($class, $entity)
 {
+       delete_crm_contacts(null, $class, $entity);
+       // cleanup
        $res = get_crm_persons($class, null, $entity, null, true);
-
        while($person = db_fetch($res)) {
-               delete_crm_person($person['id'], true);
+               $rels = get_person_contacts($person['id']);
+               if (count($rels) == 0) {
+                       delete_crm_person($person['id']);
+               }
        }
 }
 
@@ -229,7 +242,7 @@ function get_crm_category_name($id)
        $result = db_query($sql, "could not get sales type");
 
        $row = db_fetch_row($result);
-       return $row[0];
+       return is_array($row) ? $row[0] : false;
 }
 
 //----------------------------------------------------------------------------------------
@@ -256,17 +269,20 @@ function delete_crm_contact($id)
 /*
        Delete selected contacts for given person
 */
-function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null)
+function delete_crm_contacts($person_id = null, $type = null, $entity_id=null, $action = null)
 {
-       $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id);
+       $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE ";
+
+       if ($person_id)
+               $where[] = 'person_id='.db_escape($person_id);
        if ($type)
-               $sql .= ' AND type='.db_escape($type);
+               $where[] = 'type='.db_escape($type);
        if ($entity_id)
-               $sql .= ' AND entity_id='.db_escape($entity_id);
+               $where[] = 'entity_id='.db_escape($entity_id);
        if ($action)
-               $sql .= ' AND action='.db_escape($action);
+               $where[] = 'action='.db_escape($action);
 
-       return db_query($sql, "Can't delete crm contact");
+       return db_query($sql.implode(' AND ', $where), "Can't delete crm contact");
 }
 
 /*
@@ -284,4 +300,16 @@ function get_crm_contact($id)
        return $ret;
 }
 
-?>
\ No newline at end of file
+/*
+        Check for whether category is used in contacts.
+*/
+function is_crm_category_used($id)
+{
+       $row = get_crm_category($id);
+       $sql = "SELECT COUNT(*) FROM ".TB_PREF."crm_contacts WHERE type='".$row['type']."' AND action='".$row['action']."'";
+       $result = db_query($sql, "check relations for crm_contacts failed");
+       $contacts = db_fetch($result);
+       return $contacts[0];
+}
+
+