From 12445e1cb9b36385fee1e87a554da7993e6274f5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 26 Mar 2013 16:17:37 -0700 Subject: [PATCH] Enforce is_primary while deleting objects CRM-10732 --- CRM/Contact/BAO/Contact.php | 42 +++++++++++++++++++++++++++++++++++++ CRM/Core/BAO/Address.php | 7 +++++++ CRM/Core/BAO/Email.php | 7 +++++++ CRM/Core/BAO/IM.php | 7 +++++++ CRM/Core/BAO/OpenID.php | 11 ++++++++-- CRM/Core/BAO/Phone.php | 7 +++++++ 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 5988ab2d52..723745e397 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2974,4 +2974,46 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) return FALSE; } } + + + /** + * Delete a contact-related object that has an 'is_primary' field + * Ensures that is_primary gets assigned to another object if available + * Also calls pre/post hooks + * + * @var $type: object type + * @var $id: object id + */ + public static function deleteObjectWithPrimary($type, $id) { + if (!$id || !is_numeric($id)) { + return false; + } + $daoName = "CRM_Core_DAO_$type"; + $obj = new $daoName(); + $obj->id = $id; + $obj->find(); + if ($obj->fetch()) { + CRM_Utils_Hook::pre('delete', $type, $id, CRM_Core_DAO::$_nullArray); + $contactId = $obj->contact_id; + $obj->delete(); + } + else { + return false; + } + $dao = new $daoName(); + $dao->contact_id = $contactId; + $dao->is_primary = 1; + // Pick another record to be primary (if one isn't already) + if (!$dao->find(TRUE)) { + $dao->is_primary = 0; + $dao->find(); + if ($dao->fetch()) { + $dao->is_primary = 1; + $dao->save(); + } + } + $dao->free(); + CRM_Utils_Hook::post('delete', $type, $id, $obj); + $obj->free(); + } } diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 7c6cb7b9df..fba41e28d3 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1130,4 +1130,11 @@ SELECT is_primary, ); } } + + /** + * Call common delete function + */ + static function del($id) { + CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Address', $id); + } } diff --git a/CRM/Core/BAO/Email.php b/CRM/Core/BAO/Email.php index 20b45ed096..6de62fdcea 100644 --- a/CRM/Core/BAO/Email.php +++ b/CRM/Core/BAO/Email.php @@ -306,5 +306,12 @@ AND reset_date IS NULL static function isMultipleBulkMail() { return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'civimail_multiple_bulk_emails', NULL, FALSE); } + + /** + * Call common delete function + */ + static function del($id) { + CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Email', $id); + } } diff --git a/CRM/Core/BAO/IM.php b/CRM/Core/BAO/IM.php index 221a8f5682..a20f81541c 100644 --- a/CRM/Core/BAO/IM.php +++ b/CRM/Core/BAO/IM.php @@ -166,5 +166,12 @@ ORDER BY cim.is_primary DESC, im_id ASC "; } return $ims; } + + /** + * Call common delete function + */ + static function del($id) { + CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id); + } } diff --git a/CRM/Core/BAO/OpenID.php b/CRM/Core/BAO/OpenID.php index 853c34afed..4ece5a75a9 100644 --- a/CRM/Core/BAO/OpenID.php +++ b/CRM/Core/BAO/OpenID.php @@ -49,13 +49,13 @@ class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID { */ static function add(&$params) { $hook = empty($params['id']) ? 'create' : 'edit'; - CRM_Utils_Hook::pre($hook, 'openId', CRM_Utils_Array::value('id', $params), $params); + CRM_Utils_Hook::pre($hook, 'OpenID', CRM_Utils_Array::value('id', $params), $params); $openId = new CRM_Core_DAO_OpenID(); $openId->copyValues($params); $openId->save(); - CRM_Utils_Hook::post($hook, 'openId', $openId->id, $openId); + CRM_Utils_Hook::post($hook, 'OpenID', $openId->id, $openId); return $openId; } @@ -140,5 +140,12 @@ ORDER BY } return $openids; } + + /** + * Call common delete function + */ + static function del($id) { + CRM_Contact_BAO_Contact::deleteObjectWithPrimary('OpenID', $id); + } } diff --git a/CRM/Core/BAO/Phone.php b/CRM/Core/BAO/Phone.php index a128d18c9d..a514671e19 100644 --- a/CRM/Core/BAO/Phone.php +++ b/CRM/Core/BAO/Phone.php @@ -247,5 +247,12 @@ ORDER BY ph.is_primary DESC, phone_id ASC "; CRM_Core_DAO::executeQuery($query, $params); } } + + /** + * Call common delete function + */ + static function del($id) { + CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); + } } -- 2.25.1