Merge pull request #15650 from demeritcowboy/mode-value-missing
[civicrm-core.git] / CRM / Activity / Form / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
31aaf096
MW
35 * Class for activity form task actions.
36 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
6a488035 37 */
31aaf096 38class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
6a488035
TO
39
40 /**
fe482240 41 * The array that holds all the member ids.
6a488035
TO
42 *
43 * @var array
44 */
45 public $_activityHolderIds;
46
47 /**
fe482240 48 * Build all the data structures needed to build the form.
6a488035 49 */
00be9182 50 public function preProcess() {
6a488035
TO
51 self::preProcessCommon($this);
52 }
53
ffd93213 54 /**
b6c94f42 55 * Common pre-process function.
56 *
c490a46a 57 * @param CRM_Core_Form $form
ffd93213 58 */
2b089ce1 59 public static function preProcessCommon(&$form) {
be2fb01f 60 $form->_activityHolderIds = [];
6a488035
TO
61
62 $values = $form->controller->exportValues($form->get('searchFormName'));
63
64 $form->_task = $values['task'];
65 $activityTasks = CRM_Activity_Task::tasks();
66 $form->assign('taskName', $activityTasks[$form->_task]);
67
be2fb01f 68 $ids = [];
6a488035
TO
69 if ($values['radio_ts'] == 'ts_sel') {
70 foreach ($values as $name => $value) {
71 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
72 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
73 }
74 }
75 }
76 else {
77 $queryParams = $form->get('queryParams');
78 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
79 CRM_Contact_BAO_Query::MODE_ACTIVITY
80 );
81 $query->_distinctComponentClause = '( civicrm_activity.id )';
82 $query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
87767abb 83
84 // CRM-12675
85 $activityClause = NULL;
9d6dcd43 86
2f9320ea 87 $components = CRM_Core_Component::getNames();
be2fb01f 88 $componentClause = [];
f121d107 89 foreach ($components as $componentID => $componentName) {
9aa863df 90 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
4f490fbf 91 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
2f9320ea 92 }
93 }
94 if (!empty($componentClause)) {
95 $activityClause = implode(' AND ', $componentClause);
87767abb 96 }
87767abb 97 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
6a488035
TO
98
99 while ($result->fetch()) {
22e263ad 100 if (!empty($result->activity_id)) {
32864ccf
TO
101 $ids[] = $result->activity_id;
102 }
6a488035
TO
103 }
104 }
6a488035
TO
105
106 if (!empty($ids)) {
107 $form->_componentClause = ' civicrm_activity.id IN ( ' . implode(',', $ids) . ' ) ';
108 $form->assign('totalSelectedActivities', count($ids));
109 }
110
111 $form->_activityHolderIds = $form->_componentIds = $ids;
112
7808aae6 113 // Set the context for redirection for any task actions.
6a488035
TO
114 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
115 $urlParams = 'force=1';
116 if (CRM_Utils_Rule::qfKey($qfKey)) {
117 $urlParams .= "&qfKey=$qfKey";
118 }
119
120 $session = CRM_Core_Session::singleton();
121 $searchFormName = strtolower($form->get('searchFormName'));
122 if ($searchFormName == 'search') {
123 $session->replaceUserContext(CRM_Utils_System::url('civicrm/activity/search', $urlParams));
124 }
125 else {
126 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
353ffa53
TO
127 $urlParams
128 ));
6a488035
TO
129 }
130 }
131
132 /**
133 * Given the membership id, compute the contact id
7808aae6 134 * since it's used for things like send email.
6a488035
TO
135 */
136 public function setContactIDs() {
137 $IDs = implode(',', $this->_activityHolderIds);
1adb1bc9 138
44f817d4 139 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
1adb1bc9 140 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 141 $query = "
1adb1bc9 142SELECT contact_id
143FROM civicrm_activity_contact
144WHERE activity_id IN ( $IDs ) AND
145 record_type_id = {$sourceID}";
146
6a488035
TO
147 $dao = CRM_Core_DAO::executeQuery($query);
148 while ($dao->fetch()) {
1adb1bc9 149 $contactIDs[] = $dao->contact_id;
6a488035
TO
150 }
151 $this->_contactIds = $contactIDs;
152 }
153
154 /**
100fef9d 155 * Simple shell that derived classes can call to add buttons to
6a488035
TO
156 * the form with a customized title for the main Submit
157 *
041ab3d1
TO
158 * @param string $title
159 * Title of the main button.
160 * @param string $nextType
161 * Button type for the form after processing.
fd31fa4c
EM
162 * @param string $backType
163 * @param bool $submitOnce
6a488035 164 */
00be9182 165 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
be2fb01f
CW
166 $this->addButtons([
167 [
c5c263ca
AH
168 'type' => $nextType,
169 'name' => $title,
170 'isDefault' => TRUE,
be2fb01f
CW
171 ],
172 [
c5c263ca
AH
173 'type' => $backType,
174 'name' => ts('Cancel'),
be2fb01f
CW
175 ],
176 ]);
6a488035 177 }
96025800 178
6a488035 179}