Merge pull request #6559 from jitendrapurohit/CRM-16877
[civicrm-core.git] / CRM / Core / BAO / Website.php
index 3de574495330121ed99e9aa064917c851973c80b..af12e416a6b156b1727b1dbeec466f01cbe6622e 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2015
  */
 
 /**
- * This class contain function for Website handling
+ * This class contain function for Website handling.
  */
 class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
 
@@ -66,59 +64,30 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
    * @param int $contactID
    *   Contact id.
    *
-   * @return void
+   * @param bool $skipDelete
+   *
+   * @return bool
    */
-  public static function create(&$params, $contactID) {
-
-    if (empty($params['website'])) {
+  public static function create(&$params, $contactID, $skipDelete) {
+    if (empty($params)) {
       return FALSE;
     }
 
-    // CRM-10551
-    // Use updateBlankLocInfo to overwrite blanked values of matching type
-    $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
-
-    // Get websites submitted in the form, and already on the Contact
-    $submitted_websites = $params['website'];
-    $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, $existing_websites)) {
-          unset($existing_websites[$websiteId]);
-        }
-        else {
-          unset($submitted_value['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;
+    $ids = self::allWebsites($contactID);
+    foreach ($params as $key => $values) {
+      if (empty($values['id']) && is_array($ids) && !empty($ids)) {
+        foreach ($ids as $id => $value) {
+          if (($value['website_type_id'] == $values['website_type_id'])) {
+            $values['id'] = $id;
           }
         }
       }
-
-      $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']));
+      if (!empty($values['url'])) {
+        $values['contact_id'] = $contactID;
+        self::add($values);
       }
-
-      // Otherwise, add the website if the URL isn't empty
-      elseif (!empty($submitted_value['url'])) {
-        self::add($submitted_value);
+      elseif ($skipDelete && !empty($values['id'])) {
+        self::del(array($values['id']));
       }
     }
   }
@@ -129,7 +98,7 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
    * @param array $ids
    *   Website ids.
    *
-   * @return void
+   * @return bool
    */
   public static function del($ids) {
     $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';