From 670b8313c40dbf33c1759dd508f6c9c3e3e76278 Mon Sep 17 00:00:00 2001 From: JKingsnorth Date: Mon, 16 Mar 2015 10:08:41 +0000 Subject: [PATCH] CRM-10551: Allow authenticated users to delete websites via profile --- CRM/Contact/BAO/Contact.php | 7 +-- CRM/Contact/Form/Inline/Website.php | 5 ++- CRM/Core/BAO/Website.php | 66 ++++++++++++++++++----------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 5d643efdb6..bfef9229e9 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -276,9 +276,6 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { * @param bool $invokeHooks * If we need to invoke hooks. * - * @param bool $skipDelete - * Unclear parameter, passed to website create - * * @todo explain this parameter * * @throws Exception @@ -286,7 +283,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { * Created or updated contribution object. We are deprecating returning an error in * favour of exceptions */ - public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE, $skipDelete = FALSE) { + public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE) { $contact = NULL; if (empty($params['contact_type']) && empty($params['contact_id'])) { return $contact; @@ -367,7 +364,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { } //add website - CRM_Core_BAO_Website::create($params['website'], $contact->id, $skipDelete); + CRM_Core_BAO_Website::create($params, $contact->id); //get userID from session $session = CRM_Core_Session::singleton(); diff --git a/CRM/Contact/Form/Inline/Website.php b/CRM/Contact/Form/Inline/Website.php index 554ba66f2c..99e1ee1569 100644 --- a/CRM/Contact/Form/Inline/Website.php +++ b/CRM/Contact/Form/Inline/Website.php @@ -123,7 +123,10 @@ class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline { $params = $this->exportValues(); // Process / save websites - CRM_Core_BAO_Website::create($params['website'], $this->_contactId, TRUE); + // CRM-10551 + // Use updateBlankLocInfo to overwrite blanked values of matching type + $params['updateBlankLocInfo'] = TRUE; + CRM_Core_BAO_Website::create($params, $this->_contactId); $this->log(); $this->response(); diff --git a/CRM/Core/BAO/Website.php b/CRM/Core/BAO/Website.php index 090969c68a..e866396699 100644 --- a/CRM/Core/BAO/Website.php +++ b/CRM/Core/BAO/Website.php @@ -66,46 +66,62 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { * @param int $contactID * Contact id. * - * @param $skipDelete - * * @return void */ - public static function create(&$params, $contactID, $skipDelete) { - if (empty($params)) { + public static function create(&$params, $contactID) { + + // CRM-10551 + // Use updateBlankLocInfo to overwrite blanked values of matching type + $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); + + // Get the websites submitted in the form + $submitted_websites = $params['website']; + + if (empty($submitted_websites)) { return FALSE; } - - $ids = self::allWebsites($contactID); - foreach ($params as $key => $values) { - $websiteId = CRM_Utils_Array::value('id', $values); + + // Get the websites currently on the Contact + $existing_websites = self::allWebsites($contactID); + + // For each website submitted on the form + foreach ($submitted_websites as $key => $submitted_value) { + + // Check for matching IDs on submitted / existing data + $websiteId = CRM_Utils_Array::value('id', $submitted_value); if ($websiteId) { - if (array_key_exists($websiteId, $ids)) { - unset($ids[$websiteId]); + if (array_key_exists($websiteId, $existing_websites)) { + unset($existing_websites[$websiteId]); } else { - unset($values['id']); + unset($submitted_value['id']); } } - if (empty($values['id']) && - is_array($ids) && !empty($ids) - ) { - foreach ($ids as $id => $value) { - if (($value['website_type_id'] == $values['website_type_id']) && !empty($values['url'])) { - $values['id'] = $id; - unset($ids[$id]); + // Match up submitted values to existing ones, based on type + if (empty($submitted_value['id']) && !empty($existing_websites)) { + foreach ($existing_websites as $id => $existing_value) { + if ($existing_value['website_type_id'] == $submitted_value['website_type_id']) { + $submitted_value['id'] = $id; + unset($existing_websites[$id]); break; } } } - $values['contact_id'] = $contactID; - if (!empty($values['url'])) { - self::add($values); + + $submitted_value['contact_id'] = $contactID; + + // CRM-10551 + // If there is a matching ID, the URL is empty and we are deleting blanked values + // Then remove it from the contact + if (!empty($submitted_value['id']) && empty($submitted_value['url']) && $updateBlankLocInfo) { + self::del(array($submitted_value['id'])); + } + + // Otherwise, add the website if the URL isn't empty + elseif (!empty($submitted_value['url'])) { + self::add($submitted_value); } - } - - if ($skipDelete && !empty($ids)) { - self::del(array_keys($ids)); } } -- 2.25.1