Use Case form for case actions
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 1 Oct 2021 07:51:55 +0000 (20:51 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 1 Oct 2021 16:48:23 +0000 (05:48 +1300)
CRM/Activity/Page/AJAX.php
CRM/Case/Form/CaseView.php
CRM/Case/Form/Task.php
CRM/Case/Form/Task/Email.php [new file with mode: 0644]
CRM/Case/xml/Menu/Case.xml
CRM/Contact/Form/Task/EmailTrait.php
templates/CRM/Case/Form/CaseView.tpl
templates/CRM/Case/Form/Task/Email.tpl [new file with mode: 0644]
tests/phpunit/CRM/Case/Form/TaskTest.php

index 7cf1613e2fa6cbf12b7113e06794b33cfad89659..4f85789576b532f7e7ead274e74aed88faaebe84 100644 (file)
@@ -217,7 +217,7 @@ class CRM_Activity_Page_AJAX {
       }
       // email column links/icon
       if ($row['email']) {
-        $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/activity/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope" aria-hidden="true"></i></a>';
+        $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/case/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope" aria-hidden="true"></i></a>';
       }
 
       // view end date if set
index c1031b20f54518a589fd540f6bff596fc08c9ad2..2123f2f7ee281deeb68865dc63dad436f005c6ef 100644 (file)
@@ -251,7 +251,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     $activityLinks = ['' => ts('Add Activity')];
     foreach ($aTypes as $type => $label) {
       if ($type == $emailActivityType) {
-        $url = CRM_Utils_System::url('civicrm/activity/email/add',
+        $url = CRM_Utils_System::url('civicrm/case/email/add',
           "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
           FALSE, NULL, FALSE
         );
index d1f318ac432223aa0732d9cf324a60d5c06a6918..45c73958e10cd211a36960fa98d54c0459752518 100644 (file)
@@ -34,11 +34,7 @@ class CRM_Case_Form_Task extends CRM_Core_Form_Task {
    * @inheritDoc
    */
   public function setContactIDs() {
-    // @todo Parameters shouldn't be needed and should be class member
-    // variables instead, set appropriately by each subclass.
-    $this->_contactIds = $this->getContactIDsFromComponent($this->_entityIds,
-      'civicrm_case_contact', 'case_id'
-    );
+    $this->_contactIds = $this->getContactIDs();
   }
 
   /**
@@ -86,4 +82,19 @@ class CRM_Case_Form_Task extends CRM_Core_Form_Task {
     return 'case_id';
   }
 
+  protected function getContactIDs(): array {
+    if (isset($this->_contactIds)) {
+      return $this->_contactIds;
+    }
+    $contactIDSFromUrl = CRM_Utils_Request::retrieve('cid', 'CommaSeparatedIntegers', $this);
+    if (!empty($contactIDSFromUrl)) {
+      return explode(',', $contactIDSFromUrl);
+    }
+    // @todo Parameters shouldn't be needed and should be class member
+    // variables instead, set appropriately by each subclass.
+    return $this->getContactIDsFromComponent($this->_entityIds,
+      'civicrm_case_contact', 'case_id'
+    );
+  }
+
 }
diff --git a/CRM/Case/Form/Task/Email.php b/CRM/Case/Form/Task/Email.php
new file mode 100644 (file)
index 0000000..47c4bb1
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+/**
+ * This class provides the functionality to email a group of contacts.
+ */
+class CRM_Case_Form_Task_Email extends CRM_Case_Form_Task {
+  use CRM_Contact_Form_Task_EmailTrait;
+
+  /**
+   * Getter for isSearchContext.
+   *
+   * @return bool
+   */
+  public function isSearchContext(): bool {
+    return FALSE;
+  }
+
+  /**
+   * List available tokens for this form.
+   *
+   * @return array
+   * @throws \CRM_Core_Exception
+   */
+  public function listTokens() {
+    $tokens = CRM_Core_SelectValues::contactTokens();
+
+    if ($this->getCaseID()) {
+      // For a single case, list tokens relevant for only that case type
+      $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->getCaseID(), 'case_type_id');
+      $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
+    }
+
+    return $tokens;
+  }
+
+  /**
+   * Get the subject for the message.
+   *
+   * The case handling should possibly be on the case form.....
+   *
+   * @param string $subject
+   *
+   * @return string
+   * @throws \CRM_Core_Exception
+   */
+  protected function getSubject(string $subject):string {
+    // CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects
+    if ($this->getCaseID()) {
+      $hash = substr(sha1(CIVICRM_SITE_KEY . $this->getCaseID()), 0, 7);
+      $subject = "[case #$hash] $subject";
+    }
+    return $subject;
+  }
+
+}
index 0d77eb9f8e282739b9975f10a4fc9565a1c3110f..91348e1c9a3d7e27a2e7028d4f0d4e250a2c13b1 100644 (file)
     <path>civicrm/ajax/get-cases</path>
     <page_callback>CRM_Case_Page_AJAX::getCases</page_callback>
   </item>
+  <item>
+    <path>civicrm/case/email/add</path>
+    <path_arguments>action=add,task=email</path_arguments>
+    <title>Email</title>
+    <page_callback>CRM_Case_Form_Task_Email</page_callback>
+  </item>
 </menu>
index cb04bdc304a1f99fd77a1cb45d1b8450be2d4373..bbbd67ebf039b27de12442b8e838d6cf32e3b6d9 100644 (file)
@@ -581,11 +581,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
    * @throws \CRM_Core_Exception
    */
   protected function getSubject(string $subject):string {
-    // CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects
-    if ($this->getCaseID()) {
-      $hash = substr(sha1(CIVICRM_SITE_KEY . $this->getCaseID()), 0, 7);
-      $subject = "[case #$hash] $subject";
-    }
     return $subject;
   }
 
index 89c46727780a3d9046b6473ef1541c5642dd945a..0c2422ba0323f8c4b5efc8d58760fdf9ef38b634 100644 (file)
@@ -45,7 +45,7 @@
             {foreach from=$caseRoles.client item=client}
               <tr class="crm-case-caseview-display_name">
                 <td class="label-left bold" style="padding: 0px; border: none;">
-                  <a href="{crmURL p='civicrm/contact/view' q="action=view&reset=1&cid=`$client.contact_id`"}" title="{ts}View contact record{/ts}">{$client.display_name}</a>{if $client.email}{crmAPI var='email_type_id' entity='OptionValue' action='getsingle' return="value" name="Email" option_group_id="activity_type"}<span class="crm-case-caseview-email"><a class="crm-hover-button crm-popup" href="{crmURL p='civicrm/activity/email/add' q="reset=1&action=add&atype=`$email_type_id.value`&cid=`$client.contact_id`&caseid=`$caseId`"}" title="{ts 1=$client.email|escape}Email: %1{/ts}"><i class="crm-i fa-envelope" aria-hidden="true"></i></a></span>{/if}
+                  <a href="{crmURL p='civicrm/contact/view' q="action=view&reset=1&cid=`$client.contact_id`"}" title="{ts}View contact record{/ts}">{$client.display_name}</a>{if $client.email}{crmAPI var='email_type_id' entity='OptionValue' action='getsingle' return="value" name="Email" option_group_id="activity_type"}<span class="crm-case-caseview-email"><a class="crm-hover-button crm-popup" href="{crmURL p='civicrm/case/email/add' q="reset=1&action=add&atype=`$email_type_id.value`&cid=`$client.contact_id`&caseid=`$caseId`"}" title="{ts 1=$client.email|escape}Email: %1{/ts}"><i class="crm-i fa-envelope" aria-hidden="true"></i></a></span>{/if}
                 </td>
               </tr>
               {if $client.phone}
diff --git a/templates/CRM/Case/Form/Task/Email.tpl b/templates/CRM/Case/Form/Task/Email.tpl
new file mode 100644 (file)
index 0000000..b47a6c4
--- /dev/null
@@ -0,0 +1 @@
+{include file="CRM/Contact/Form/Task/Email.tpl"}
index 9e0e3610b0857fef70f174b4a8285c815b8ba651..01e6bbf5d5cd6bbce19eeaa9e791717148f2e791 100644 (file)
@@ -17,9 +17,9 @@ class CRM_Case_Form_TaskTest extends CiviCaseTestCase {
    *
    * @dataProvider contactIDProvider
    */
-  public function testSetContactIDs($input, $selected_search_results, $expected) {
+  public function testSetContactIDs($input, $selected_search_results, $expected): void {
     $this->createCaseContacts($input);
-    $task = new CRM_Case_Form_Task();
+    $task = $this->getFormObject('CRM_Case_Form_Task');
 
     // This simulates the selection from the search results list. What we're
     // testing is that no matter what order the cases were created or what