dev/core#1790 - Contact Card - Email Links
authoreileen <emcnaughton@wikimedia.org>
Mon, 28 Sep 2020 01:41:10 +0000 (14:41 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 2 Oct 2020 07:11:35 +0000 (20:11 +1300)
Alternate to https://github.com/civicrm/civicrm-core/pull/17517

The current PR stalled on the issue that passing 'email' to the email task could load up
the wrong contact. This gets around that by doing a separate (but hopefully quick) lookup
to grab the email id. I also made it dependent on the url having is_show_email_task=1 in
it. I went back & forwards on that vs is_backoffice

CRM/Contact/BAO/Contact/Utils.php
CRM/Profile/Page/Dynamic.php
CRM/Profile/Page/View.php
tests/phpunit/CRM/Activity/Form/SearchTest.php
tests/phpunit/CRM/Financial/Page/AjaxTest.php
tests/phpunit/CRM/Member/Selector/SearchTest.php
tests/phpunit/CRM/Pledge/Form/SearchTest.php

index 056f792c290f349e4a9bd9c26ed7fccc1b19c33b..7e5477e18e044c79c7969edd4ea3c9ce7620021d 100644 (file)
@@ -80,7 +80,7 @@ class CRM_Contact_BAO_Contact_Utils {
       }
 
       $profileURL = CRM_Utils_System::url('civicrm/profile/view',
-        "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4"
+        "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4&is_show_email_task=1"
       );
 
       $imageInfo[$contactType]['summary-link'] = '<a href="' . $profileURL . '" class="crm-summary-link">' . $imageInfo[$contactType]['image'] . '</a>';
index 67f7edf0d508da8bf61ad99b64d6ea93efc51349..7ce7eeef62bb2bb7d03554cb11457771d41d3c2f 100644 (file)
@@ -9,6 +9,8 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\Email;
+
 /**
  *
  * @package CRM
@@ -78,6 +80,13 @@ class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
 
   protected $_recordId = NULL;
 
+  /**
+   * Should the primary email be converted into a link, if emailabe.
+   *
+   * @var bool
+   */
+  protected $isShowEmailTaskLink = FALSE;
+
   /**
    *
    * fetch multirecord as well as non-multirecord fields
@@ -97,15 +106,18 @@ class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
    * @param bool $skipPermission
    * @param null $profileIds
    *
-   * @return \CRM_Profile_Page_Dynamic
+   * @param bool $isShowEmailTaskLink
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL) {
+  public function __construct($id, $gid, $restrict, $skipPermission = FALSE, $profileIds = NULL, $isShowEmailTaskLink = FALSE) {
     parent::__construct();
 
     $this->_id = $id;
     $this->_gid = $gid;
     $this->_restrict = $restrict;
     $this->_skipPermission = $skipPermission;
+    $this->isShowEmailTaskLink = $isShowEmailTaskLink;
 
     if (!array_key_exists('multiRecord', $_GET)) {
       $this->set('multiRecord', NULL);
@@ -315,6 +327,11 @@ class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
         $labels[$index] = preg_replace('/\s+|\W+/', '_', $name);
       }
 
+      if ($this->isShowEmailTaskLink) {
+        foreach ($this->getEmailFields($fields) as $fieldName) {
+          $values[$fields[$fieldName]['title']] = $this->getLinkedEmail($values[$fields[$fieldName]['title']]);
+        }
+      }
       foreach ($values as $title => $value) {
         $profileFields[$labels[$title]] = [
           'label' => $title,
@@ -416,4 +433,47 @@ class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
   }
 
+  /**
+   * Get the email field as a task link, if not on hold or set to do_not_email.
+   *
+   * @param string $email
+   *
+   * @return string
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  protected function getLinkedEmail($email): string {
+    if (!$email) {
+      return '';
+    }
+    $emailID = Email::get()->setOrderBy(['is_primary' => 'DESC'])->setWhere([['contact_id', '=', $this->_id], ['email', '=', $email], ['on_hold', '=', FALSE], ['contact.is_deceased', '=', FALSE], ['contact.is_deleted', '=', FALSE], ['contact.do_not_email', '=', FALSE]])->execute()->first()['id'];
+    if (!$emailID) {
+      return $email;
+    }
+    $emailPopupUrl = CRM_Utils_System::url('civicrm/activity/email/add', [
+      'action' => 'add',
+      'reset' => '1',
+      'email_id' => $emailID,
+    ], TRUE);
+
+    return '<a class="crm-popup" href="' . $emailPopupUrl . '">' . $email . '</a>';
+  }
+
+  /**
+   * Get the email fields from within the fields array.
+   *
+   * @param array $fields
+   */
+  protected function getEmailFields(array $fields): array {
+    $emailFields = [];
+    foreach (array_keys($fields) as $fieldName) {
+      if (substr($fieldName, 0, 6) === 'email-'
+          && (is_numeric(substr($fieldName, 6)) || substr($fieldName, 6) ===
+        'Primary')) {
+        $emailFields[] = $fieldName;
+      }
+    }
+    return $emailFields;
+  }
+
 }
index 54f563d4e9e441a930ab64a98fb5b09946217434..185980e8ed6f2a44630ab11ca54795ac0752655e 100644 (file)
@@ -35,6 +35,13 @@ class CRM_Profile_Page_View extends CRM_Core_Page {
    */
   protected $_gid;
 
+  /**
+   * Should the primary email be converted into a link, if emailabe.
+   *
+   * @var bool
+   */
+  protected $isShowEmailTaskLink = FALSE;
+
   /**
    * Heart of the viewing process. The runner gets all the meta data for
    * the contact and calls the appropriate type of page to view.
@@ -44,6 +51,7 @@ class CRM_Profile_Page_View extends CRM_Core_Page {
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
       $this, FALSE
     );
+    $this->isShowEmailTaskLink = CRM_Utils_Request::retrieve('is_show_email_task', 'Positive', $this);
     if (!$this->_id) {
       $session = CRM_Core_Session::singleton();
       $this->_id = $session->get('userID');
@@ -77,7 +85,7 @@ class CRM_Profile_Page_View extends CRM_Core_Page {
 
     $anyContent = TRUE;
     if ($this->_gid) {
-      $page = new CRM_Profile_Page_Dynamic($this->_id, $this->_gid, 'Profile', FALSE, $profileIds);
+      $page = new CRM_Profile_Page_Dynamic($this->_id, $this->_gid, 'Profile', FALSE, $profileIds, $this->isShowEmailTaskLink);
       $profileGroup = [];
       $profileGroup['title'] = NULL;
       $profileGroup['content'] = $page->run();
index a976d278391451e801c534dfdf6763828e47d566..de46b7eeebf1377f2aebdda1ed5c09e141ac0388 100644 (file)
@@ -38,7 +38,7 @@ class CRM_Activity_Form_SearchTest extends CiviUnitTestCase {
     $this->assertEquals([
       [
         'contact_id' => '3',
-        'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
+        'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4&amp;is_show_email_task=1" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
         'sort_name' => 'Anderson, Anthony',
         'display_name' => 'Mr. Anthony Anderson II',
         'activity_id' => '1',
index d6c349bc613035ad407d139983858c75514f6c51..edef40e678c9b80e97c97e58bb9cbf72542119d0 100644 (file)
@@ -33,7 +33,7 @@ class CRM_Financial_Page_AjaxTest extends CiviUnitTestCase {
     $_REQUEST['return'] = TRUE;
     $json = CRM_Financial_Page_AJAX::getFinancialTransactionsList();
     $json = str_replace(rtrim(CIVICRM_UF_BASEURL, '/'), 'http://FIX ME', $json);
-    $this->assertEquals($json, '{"sEcho": 1, "iTotalRecords": 1, "iTotalDisplayRecords": 1, "aaData": [ ["","<a href=\"/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4\" class=\"crm-summary-link\"><div'
+    $this->assertEquals($json, '{"sEcho": 1, "iTotalRecords": 1, "iTotalDisplayRecords": 1, "aaData": [ ["","<a href=\"/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4&amp;is_show_email_task=1\" class=\"crm-summary-link\"><div'
     . ' class=\"icon crm-icon Individual-icon\"></div></a>","<a href=/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=3>Anderson, Anthony</a>","$ 100.00","12345","' . CRM_Utils_Date::customFormat(date('Ymd')) . ' 12:00 AM","' . CRM_Utils_Date::customFormat(date('Ymd')) . ' 12:00 AM",'
     . '"Credit Card","Completed","Donation","<span><a href=\"/index.php?q=civicrm/contact/view/contribution&amp;reset=1&amp;id=1&amp;cid=3&amp;action=view&amp;context=contribution&amp;'
     . 'selectedChild=contribute\" class=\"action-item crm-hover-button\" title=\'View Contribution\' >View</a></span>"]] }');
index 3eafc2979116030d506dac90fc3e8716a8728d8a..55e50139b67e11b0bc96dcf1b2b208a4ba5d4c56 100644 (file)
@@ -35,7 +35,7 @@ class CRM_Member_Selector_SearchTest extends CiviUnitTestCase {
     $this->assertEquals([
       'contact_id' => $this->_contactID,
       'membership_id' => $membershipID,
-      'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=' . $this->_contactID . '&amp;snippet=4" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
+      'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=' . $this->_contactID . '&amp;snippet=4&amp;is_show_email_task=1" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
       'sort_name' => 'Anderson, Anthony',
       'membership_type' => 'General',
       'membership_join_date' => date('Y-m-d'),
index 9e34d8099be04a027af890b6ebea5f538c3ea5a5..d8bba2d71ce46ff514d366fe41746aad804e69c8 100644 (file)
@@ -53,7 +53,7 @@ class CRM_Pledge_Form_SearchTest extends CiviUnitTestCase {
       'pledge_status_name' => 'Pending Label**',
       'checkbox' => 'mark_x_1',
       'action' => '<span><a href="/index.php?q=civicrm/contact/view/pledge&amp;reset=1&amp;id=1&amp;cid=3&amp;action=view&amp;context=search&amp;selectedChild=pledge&amp;key=' . $qfKey . '" class="action-item crm-hover-button" title=' . "'" . 'View Pledge' . "'" . ' >View</a><a href="/index.php?q=civicrm/contact/view/pledge&amp;reset=1&amp;action=update&amp;id=1&amp;cid=3&amp;context=search&amp;key=' . $qfKey . '" class="action-item crm-hover-button" title=' . "'" . 'Edit Pledge' . "'" . ' >Edit</a></span><span class=' . "'" . 'btn-slide crm-hover-button' . "'" . '>more<ul class=' . "'" . 'panel' . "'" . '><li><a href="/index.php?q=civicrm/contact/view/pledge&amp;reset=1&amp;action=detach&amp;id=1&amp;cid=3&amp;context=search&amp;key=' . $qfKey . '" class="action-item crm-hover-button" title=' . "'" . 'Cancel Pledge' . "'" . ' onclick = "return confirm(' . "'" . 'Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments. This action cannot be undone. Do you want to continue?' . "'" . ');">Cancel</a></li><li><a href="/index.php?q=civicrm/contact/view/pledge&amp;reset=1&amp;action=delete&amp;id=1&amp;cid=3&amp;context=search&amp;key=' . $qfKey . '" class="action-item crm-hover-button small-popup" title=' . "'" . 'Delete Pledge' . "'" . ' >Delete</a></li></ul></span>',
-      'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
+      'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4&amp;is_show_email_task=1" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
     ], $rows[0]);
   }