Render contact type icons in std way on merge screen
[civicrm-core.git] / CRM / Contact / Form / Merge.php
index ba9cfce432262c099f83075baf0f8e4bb354f569..cbb1c3a8e8f398f3d4c6b91232e8bc2d339a304c 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
+ | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 
 /**
@@ -85,10 +85,12 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
 
       $this->bounceIfInvalid($this->_cid, $this->_oid);
 
-      $this->_contactType = civicrm_api3('Contact', 'getvalue', array(
-        'id' => $this->_cid,
-        'return' => 'contact_type',
-      ));
+      $contacts = civicrm_api3('Contact', 'get', [
+        'id' => ['IN' => [$this->_cid, $this->_oid]],
+        'return' => ['contact_type', 'modified_date', 'created_date', 'contact_sub_type'],
+      ])['values'];
+
+      $this->_contactType = $contacts[$this->_cid]['contact_type'];
 
       $browseUrl = CRM_Utils_System::url('civicrm/contact/dedupefind', array_merge($urlParams, ['action' => 'browse']));
 
@@ -120,16 +122,9 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       $mainUfId = CRM_Core_BAO_UFMatch::getUFId($this->_cid);
       $mainUser = NULL;
       if ($mainUfId) {
-        // d6 compatible
-        if ($config->userSystem->is_drupal == '1') {
-          $mainUser = user_load($mainUfId);
-        }
-        elseif ($config->userFramework == 'Joomla') {
-          $mainUser = JFactory::getUser($mainUfId);
-        }
-
+        $mainUser = $config->userSystem->getUser($this->_cid);
         $this->assign('mainUfId', $mainUfId);
-        $this->assign('mainUfName', $mainUser ? $mainUser->name : NULL);
+        $this->assign('mainUfName', $mainUser ? $mainUser['name'] : NULL);
       }
       $flipParams = array_merge($urlParams, ['action' => 'update', 'cid' => $this->_oid, 'oid' => $this->_cid]);
       if (!$flip) {
@@ -164,16 +159,9 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       $otherUser = NULL;
 
       if ($otherUfId) {
-        // d6 compatible
-        if ($config->userSystem->is_drupal == '1') {
-          $otherUser = user_load($otherUfId);
-        }
-        elseif ($config->userFramework == 'Joomla') {
-          $otherUser = JFactory::getUser($otherUfId);
-        }
-
+        $otherUser = $config->userSystem->getUser($this->_oid);
         $this->assign('otherUfId', $otherUfId);
-        $this->assign('otherUfName', $otherUser ? $otherUser->name : NULL);
+        $this->assign('otherUfName', $otherUser ? $otherUser['name'] : NULL);
       }
 
       $cmsUser = ($mainUfId && $otherUfId) ? TRUE : FALSE;
@@ -189,11 +177,28 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       $this->assign('main_cid', $main['contact_id']);
       $this->assign('other_cid', $other['contact_id']);
       $this->assign('rgid', $this->_rgid);
+      $this->assignSummaryRowsToTemplate($contacts);
 
       $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
 
       $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_details']['location_blocks']));
       $this->assign('locationBlockInfo', json_encode(CRM_Dedupe_Merger::getLocationBlockInfo()));
+      $this->assign('mainContactTypeIcon', CRM_Contact_BAO_Contact_Utils::getImage($contacts[$this->_cid]['contact_sub_type'] ? $contacts[$this->_cid]['contact_sub_type'] : $contacts[$this->_cid]['contact_type'],
+        FALSE,
+        $this->_cid
+      ));
+      $this->assign('otherContactTypeIcon', CRM_Contact_BAO_Contact_Utils::getImage($contacts[$this->_oid]['contact_sub_type'] ? $contacts[$this->_oid]['contact_sub_type'] : $contacts[$this->_oid]['contact_type'],
+        FALSE,
+        $this->_oid
+      ));
+
+      if (isset($rowsElementsAndInfo['rows']['move_contact_type'])) {
+        // We don't permit merging contacts of different types so this is just clutter - putting
+        // the icon next to the contact name is consistent with elsewhere and permits hover-info
+        // https://lab.civicrm.org/dev/core/issues/824
+        unset($rowsElementsAndInfo['rows']['move_contact_type']);
+      }
+
       $this->assign('rows', $rowsElementsAndInfo['rows']);
 
       // add elements
@@ -379,4 +384,34 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     }
   }
 
+  /**
+   * Assign the summary_rows variable to the tpl.
+   *
+   * This adds rows to the beginning of the block that will help in making merge choices.
+   *
+   * It can be modified by a hook by altering what is assigned. Although not technically supported this
+   * is an easy tweak with no earth-shattering impacts if later changes stop if from working.
+   *
+   * https://lab.civicrm.org/dev/core/issues/824
+   *
+   * @param array $contacts
+   */
+  protected function assignSummaryRowsToTemplate($contacts) {
+    $mostRecent = ($contacts[$this->_cid]['modified_date'] < $contacts[$this->_oid]['modified_date']) ? $this->_oid : $this->_cid;
+    $this->assign('summary_rows', [
+      [
+        'name' => 'created_date',
+        'label' => ts('Created'),
+        'main_contact_value' => CRM_Utils_Date::customFormat($contacts[$this->_cid]['created_date']),
+        'other_contact_value' => CRM_Utils_Date::customFormat($contacts[$this->_oid]['created_date']),
+      ],
+      [
+        'name' => 'modified_date',
+        'label' => ts('Last Modified'),
+        'main_contact_value' => CRM_Utils_Date::customFormat($contacts[$this->_cid]['modified_date']) . ($mostRecent == $this->_cid ? ' (' . ts('Most Recent') . ')' : ''),
+        'other_contact_value' => CRM_Utils_Date::customFormat($contacts[$this->_oid]['modified_date']) . ($mostRecent == $this->_oid ? ' (' . ts('Most Recent') . ')' : ''),
+      ],
+    ]);
+  }
+
 }