Merge pull request #15815 from artfulrobot/issue-1108-fix-unsubscribe
[civicrm-core.git] / CRM / Activity / Form / Task / FileOnCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to email a group of contacts
20 */
21 class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {
22
23 /**
24 * The title of the group.
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Variable to store redirect path.
32 * @var string
33 */
34 protected $_userContext;
35
36 /**
37 * Variable to store contact Ids.
38 * @var array
39 */
40 public $_contacts;
41
42 /**
43 * Build all the data structures needed to build the form.
44 */
45 public function preProcess() {
46 parent::preProcess();
47 $session = CRM_Core_Session::singleton();
48 $this->_userContext = $session->readUserContext();
49
50 CRM_Utils_System::setTitle(ts('File on Case'));
51 }
52
53 /**
54 * Build the form object.
55 */
56 public function buildQuickForm() {
57 $this->addEntityRef('unclosed_case_id', ts('Select Case'), ['entity' => 'Case'], TRUE);
58 $this->addDefaultButtons(ts('Save'));
59 }
60
61 /**
62 * Process the form after the input has been submitted and validated.
63 */
64 public function postProcess() {
65 $formparams = $this->exportValues();
66 $caseId = $formparams['unclosed_case_id'];
67 $filedActivities = 0;
68 foreach ($this->_activityHolderIds as $key => $id) {
69 $targetContactValues = $defaults = [];
70 $params = ['id' => $id];
71 CRM_Activity_BAO_Activity::retrieve($params, $defaults);
72 if (CRM_Case_BAO_Case::checkPermission($id, 'File On Case', $defaults['activity_type_id'])) {
73
74 if (!CRM_Utils_Array::crmIsEmptyArray($defaults['target_contact'])) {
75 $targetContactValues = array_combine(array_unique($defaults['target_contact']),
76 explode(';', trim($defaults['target_contact_value']))
77 );
78 $targetContactValues = implode(',', array_keys($targetContactValues));
79 }
80
81 $params = [
82 'caseID' => $caseId,
83 'activityID' => $id,
84 'newSubject' => empty($defaults['subject']) ? '' : $defaults['subject'],
85 'targetContactIds' => $targetContactValues,
86 'mode' => 'file',
87 ];
88
89 $error_msg = CRM_Activity_Page_AJAX::_convertToCaseActivity($params);
90 if (empty($error_msg['error_msg'])) {
91 $filedActivities++;
92 }
93 else {
94 CRM_Core_Session::setStatus($error_msg['error_msg'], ts("Error"), "error");
95 }
96 }
97 else {
98 CRM_Core_Session::setStatus(
99 ts('Not permitted to file activity %1 %2.', [
100 1 => empty($defaults['subject']) ? '' : $defaults['subject'],
101 2 => $defaults['activity_date_time'],
102 ]),
103 ts("Error"), "error");
104 }
105 }
106
107 CRM_Core_Session::setStatus($filedActivities, ts("Filed Activities"), "success");
108 CRM_Core_Session::setStatus("", ts('Total Selected Activities: %1', [1 => count($this->_activityHolderIds)]), "info");
109 }
110
111 }