Merge pull request #17957 from demeritcowboy/combine-createcase
[civicrm-core.git] / CRM / Activity / Form / Task.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 * Class for activity form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the member ids.
26 *
27 * @var array
28 */
29 public $_activityHolderIds;
30
31 /**
32 * Build all the data structures needed to build the form.
33 */
34 public function preProcess() {
35 self::preProcessCommon($this);
36 }
37
38 /**
39 * Common pre-process function.
40 *
41 * @param CRM_Core_Form $form
42 *
43 * @throws \CRM_Core_Exception
44 */
45 public static function preProcessCommon(&$form) {
46 $form->_activityHolderIds = [];
47
48 $values = $form->controller->exportValues($form->get('searchFormName'));
49
50 $form->_task = $values['task'];
51 $activityTasks = CRM_Activity_Task::tasks();
52 $form->assign('taskName', $activityTasks[$form->_task]);
53
54 $ids = [];
55 if ($values['radio_ts'] == 'ts_sel') {
56 foreach ($values as $name => $value) {
57 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
58 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
59 }
60 }
61 }
62 else {
63 $queryParams = $form->get('queryParams');
64 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
65 CRM_Contact_BAO_Query::MODE_ACTIVITY
66 );
67 $query->_distinctComponentClause = '( civicrm_activity.id )';
68 $query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
69
70 // CRM-12675
71 $activityClause = NULL;
72
73 $components = CRM_Core_Component::getNames();
74 $componentClause = [];
75 foreach ($components as $componentID => $componentName) {
76 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
77 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
78 }
79 }
80 if (!empty($componentClause)) {
81 $activityClause = implode(' AND ', $componentClause);
82 }
83 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
84
85 while ($result->fetch()) {
86 if (!empty($result->activity_id)) {
87 $ids[] = $result->activity_id;
88 }
89 }
90 }
91
92 if (!empty($ids)) {
93 $form->_componentClause = ' civicrm_activity.id IN ( ' . implode(',', $ids) . ' ) ';
94 $form->assign('totalSelectedActivities', count($ids));
95 }
96
97 $form->_activityHolderIds = $form->_componentIds = $ids;
98
99 // Set the context for redirection for any task actions.
100 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
101 $urlParams = 'force=1';
102 if (CRM_Utils_Rule::qfKey($qfKey)) {
103 $urlParams .= "&qfKey=$qfKey";
104 }
105
106 $session = CRM_Core_Session::singleton();
107 $searchFormName = strtolower($form->get('searchFormName'));
108 if ($searchFormName == 'search') {
109 $session->replaceUserContext(CRM_Utils_System::url('civicrm/activity/search', $urlParams));
110 }
111 else {
112 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
113 $urlParams
114 ));
115 }
116 }
117
118 /**
119 * Given the membership id, compute the contact id
120 * since it's used for things like send email.
121 */
122 public function setContactIDs() {
123 $IDs = implode(',', $this->_activityHolderIds);
124
125 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
126 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
127 $query = "
128 SELECT contact_id
129 FROM civicrm_activity_contact
130 WHERE activity_id IN ( $IDs ) AND
131 record_type_id = {$sourceID}";
132
133 $dao = CRM_Core_DAO::executeQuery($query);
134 while ($dao->fetch()) {
135 $contactIDs[] = $dao->contact_id;
136 }
137 $this->_contactIds = $contactIDs;
138 }
139
140 /**
141 * Simple shell that derived classes can call to add buttons to
142 * the form with a customized title for the main Submit
143 *
144 * @param string $title
145 * Title of the main button.
146 * @param string $nextType
147 * Button type for the form after processing.
148 * @param string $backType
149 * @param bool $submitOnce
150 */
151 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
152 $this->addButtons([
153 [
154 'type' => $nextType,
155 'name' => $title,
156 'isDefault' => TRUE,
157 ],
158 [
159 'type' => $backType,
160 'name' => ts('Cancel'),
161 ],
162 ]);
163 }
164
165 }