Merge pull request #17387 from eileenmcnaughton/ct
[civicrm-core.git] / CRM / Activity / Form / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
31aaf096
MW
19 * Class for activity form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
6a488035 21 */
31aaf096 22class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
6a488035
TO
23
24 /**
fe482240 25 * The array that holds all the member ids.
6a488035
TO
26 *
27 * @var array
28 */
29 public $_activityHolderIds;
30
31 /**
fe482240 32 * Build all the data structures needed to build the form.
6a488035 33 */
00be9182 34 public function preProcess() {
6a488035
TO
35 self::preProcessCommon($this);
36 }
37
ffd93213 38 /**
b6c94f42 39 * Common pre-process function.
40 *
c490a46a 41 * @param CRM_Core_Form $form
946d8b80 42 *
43 * @throws \CRM_Core_Exception
ffd93213 44 */
2b089ce1 45 public static function preProcessCommon(&$form) {
be2fb01f 46 $form->_activityHolderIds = [];
6a488035
TO
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
be2fb01f 54 $ids = [];
6a488035
TO
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 ";
87767abb 69
70 // CRM-12675
71 $activityClause = NULL;
9d6dcd43 72
2f9320ea 73 $components = CRM_Core_Component::getNames();
be2fb01f 74 $componentClause = [];
f121d107 75 foreach ($components as $componentID => $componentName) {
9aa863df 76 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
4f490fbf 77 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
2f9320ea 78 }
79 }
80 if (!empty($componentClause)) {
81 $activityClause = implode(' AND ', $componentClause);
87767abb 82 }
87767abb 83 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
6a488035
TO
84
85 while ($result->fetch()) {
22e263ad 86 if (!empty($result->activity_id)) {
32864ccf
TO
87 $ids[] = $result->activity_id;
88 }
6a488035
TO
89 }
90 }
6a488035
TO
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
7808aae6 99 // Set the context for redirection for any task actions.
6a488035
TO
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",
353ffa53
TO
113 $urlParams
114 ));
6a488035
TO
115 }
116 }
117
118 /**
119 * Given the membership id, compute the contact id
7808aae6 120 * since it's used for things like send email.
6a488035
TO
121 */
122 public function setContactIDs() {
123 $IDs = implode(',', $this->_activityHolderIds);
1adb1bc9 124
44f817d4 125 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
1adb1bc9 126 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 127 $query = "
1adb1bc9 128SELECT contact_id
129FROM civicrm_activity_contact
130WHERE activity_id IN ( $IDs ) AND
131 record_type_id = {$sourceID}";
132
6a488035
TO
133 $dao = CRM_Core_DAO::executeQuery($query);
134 while ($dao->fetch()) {
1adb1bc9 135 $contactIDs[] = $dao->contact_id;
6a488035
TO
136 }
137 $this->_contactIds = $contactIDs;
138 }
139
140 /**
100fef9d 141 * Simple shell that derived classes can call to add buttons to
6a488035
TO
142 * the form with a customized title for the main Submit
143 *
041ab3d1
TO
144 * @param string $title
145 * Title of the main button.
146 * @param string $nextType
147 * Button type for the form after processing.
fd31fa4c
EM
148 * @param string $backType
149 * @param bool $submitOnce
6a488035 150 */
00be9182 151 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
be2fb01f
CW
152 $this->addButtons([
153 [
c5c263ca
AH
154 'type' => $nextType,
155 'name' => $title,
156 'isDefault' => TRUE,
be2fb01f
CW
157 ],
158 [
c5c263ca
AH
159 'type' => $backType,
160 'name' => ts('Cancel'),
be2fb01f
CW
161 ],
162 ]);
6a488035 163 }
96025800 164
6a488035 165}