}
function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, $fax, $email,
}
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) . ", "
{
$sql = "UPDATE ".TB_PREF."crm_persons SET "
."ref=".db_escape($ref) . ", "
- if(!update_person_contacts($id, $cat_ids, $entity))
+ if(!update_person_contacts($id, $cat_ids, $entity, $type))
- 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 update_person_contacts($id, $cat_ids, $entity_id=null)
+function update_person_contacts($id, $cat_ids, $entity_id=null, $type=null)
if($ret && count($cat_ids)) {
array_walk($cat_ids,'db_escape');
$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($ret && count($cat_ids)) {
array_walk($cat_ids,'db_escape');
$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);
$res = get_crm_persons($class, null, $entity, null, true);
$res = get_crm_persons($class, null, $entity, null, true);
- delete_crm_person($person['id'], true);
+ $rels = get_person_contacts($person['id']);
+ if (count($rels) == 0) {
+ delete_crm_person($person['id']);
+ }
-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)
-?>
\ 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];
+}
+
+