Commit | Line | Data |
---|---|---|
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 | /** | |
b6c94f42 | 19 | * This file is for activity search. |
6a488035 | 20 | */ |
3efb5b86 | 21 | class CRM_Activity_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 22 | |
88aae6d4 A |
23 | /** |
24 | * @var bool | |
25 | */ | |
26 | public $submitOnce = TRUE; | |
27 | ||
6a488035 | 28 | /** |
fe482240 | 29 | * The params that are sent to the query. |
6a488035 TO |
30 | * |
31 | * @var array | |
6a488035 TO |
32 | */ |
33 | protected $_queryParams; | |
34 | ||
6a488035 | 35 | /** |
fe482240 | 36 | * Are we restricting ourselves to a single contact. |
6a488035 | 37 | * |
d51c6add | 38 | * @var bool |
6a488035 TO |
39 | */ |
40 | protected $_single = FALSE; | |
41 | ||
42 | /** | |
fe482240 | 43 | * Are we restricting ourselves to a single contact. |
6a488035 | 44 | * |
d51c6add | 45 | * @var bool |
6a488035 | 46 | */ |
1c1e779e | 47 | protected $_limit; |
6a488035 | 48 | |
6a488035 | 49 | /** |
fe482240 | 50 | * Prefix for the controller. |
62d3ee27 | 51 | * @var string |
6a488035 | 52 | */ |
1c1e779e | 53 | protected $_prefix = 'activity_'; |
6a488035 | 54 | |
6a488035 | 55 | /** |
fe482240 | 56 | * The saved search ID retrieved from the GET vars. |
6a488035 TO |
57 | * |
58 | * @var int | |
6a488035 TO |
59 | */ |
60 | protected $_ssID; | |
61 | ||
e6dda67a | 62 | /** |
63 | * @return string | |
64 | */ | |
13dbc546 | 65 | public function getDefaultEntity(): string { |
e6dda67a | 66 | return 'Activity'; |
67 | } | |
68 | ||
6a488035 | 69 | /** |
fe482240 | 70 | * Processing needed for buildForm and later. |
1c1e779e | 71 | * |
72 | * @throws \CRM_Core_Exception | |
73 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 74 | */ |
13dbc546 | 75 | public function preProcess(): void { |
6a488035 TO |
76 | $this->set('searchFormName', 'Search'); |
77 | ||
c490a46a | 78 | // set the button names |
6a488035 TO |
79 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
80 | ||
81 | $this->_done = FALSE; | |
c82067be | 82 | $this->sortNameOnly = TRUE; |
6a488035 | 83 | |
1c1e779e | 84 | parent::preProcess(); |
6a488035 | 85 | |
13dbc546 | 86 | if (empty($this->_formValues) && isset($this->_ssID)) { |
87 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
6a488035 TO |
88 | } |
89 | ||
6a488035 TO |
90 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
91 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
92 | $this->_action, | |
93 | NULL, | |
94 | $this->_single, | |
95 | $this->_limit, | |
96 | $this->_context | |
97 | ); | |
98 | $prefix = NULL; | |
13dbc546 | 99 | if ($this->_context === 'user') { |
6a488035 TO |
100 | $prefix = $this->_prefix; |
101 | } | |
102 | ||
103 | $this->assign("{$prefix}limit", $this->_limit); | |
104 | $this->assign("{$prefix}single", $this->_single); | |
105 | ||
106 | $controller = new CRM_Core_Selector_Controller($selector, | |
107 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
d9669ca6 | 108 | $this->getSortID(), |
6a488035 TO |
109 | CRM_Core_Action::VIEW, |
110 | $this, | |
111 | CRM_Core_Selector_Controller::TRANSFER, | |
112 | $prefix | |
113 | ); | |
114 | $controller->setEmbedded(TRUE); | |
115 | $controller->moveFromSessionToTemplate(); | |
116 | ||
117 | $this->assign('summary', $this->get('summary')); | |
118 | } | |
119 | ||
120 | /** | |
fe482240 | 121 | * Build the form object. |
1c1e779e | 122 | * |
123 | * @throws \CRM_Core_Exception | |
124 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 125 | */ |
13dbc546 | 126 | public function buildQuickForm(): void { |
3efb5b86 | 127 | parent::buildQuickForm(); |
e597fc33 | 128 | $this->addSortNameField(); |
6a488035 TO |
129 | |
130 | CRM_Activity_BAO_Query::buildSearchForm($this); | |
131 | ||
6a488035 TO |
132 | $rows = $this->get('rows'); |
133 | if (is_array($rows)) { | |
134 | if (!$this->_single) { | |
8d36b801 | 135 | $this->addRowSelectors($rows); |
6a488035 TO |
136 | } |
137 | ||
be509c11 | 138 | $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
6a488035 TO |
139 | } |
140 | ||
6a488035 TO |
141 | } |
142 | ||
143 | /** | |
144 | * The post processing of the form gets done here. | |
145 | * | |
146 | * Key things done during post processing are | |
147 | * - check for reset or next request. if present, skip post procesing. | |
148 | * - now check if user requested running a saved search, if so, then | |
149 | * the form values associated with the saved search are used for searching. | |
150 | * - if user has done a submit with new values the regular post submissing is | |
151 | * done. | |
2e2605fe | 152 | * |
6a488035 TO |
153 | * The processing consists of using a Selector / Controller framework for getting the |
154 | * search results. | |
a13c931b | 155 | * |
13dbc546 | 156 | * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception |
6a488035 | 157 | */ |
13dbc546 | 158 | public function postProcess(): void { |
6a488035 TO |
159 | if ($this->_done) { |
160 | return; | |
161 | } | |
162 | ||
163 | $this->_done = TRUE; | |
359fdb6f | 164 | $this->setFormValues(); |
6a488035 | 165 | if (!empty($_POST)) { |
be2fb01f | 166 | $specialParams = [ |
0b38e8f1 | 167 | 'activity_type_id', |
da236f9a | 168 | 'priority_id', |
be2fb01f CW |
169 | ]; |
170 | $changeNames = [ | |
da236f9a | 171 | 'priority_id' => 'activity_priority_id', |
be2fb01f | 172 | ]; |
da236f9a | 173 | |
0b38e8f1 | 174 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames); |
6a488035 | 175 | } |
6a488035 TO |
176 | $this->fixFormValues(); |
177 | ||
178 | if (isset($this->_ssID) && empty($_POST)) { | |
179 | // if we are editing / running a saved search and the form has not been posted | |
180 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
181 | } | |
6a488035 | 182 | |
d43b88cc CW |
183 | // We don't show test records in summaries or dashboards |
184 | if (empty($this->_formValues['activity_test']) && $this->_force) { | |
13dbc546 | 185 | $this->_formValues['activity_test'] = 0; |
6a488035 | 186 | } |
28c666be | 187 | |
6a488035 TO |
188 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
189 | ||
6a488035 TO |
190 | $this->set('queryParams', $this->_queryParams); |
191 | ||
192 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 193 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
194 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
195 | // hack, make sure we reset the task values | |
95ea77ec | 196 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
197 | $formName = $stateMachine->getTaskFormName(); |
198 | $this->controller->resetPage($formName); | |
199 | return; | |
200 | } | |
201 | ||
6a488035 TO |
202 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
203 | ||
204 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
205 | $this->_action, | |
206 | NULL, | |
207 | $this->_single, | |
208 | $this->_limit, | |
209 | $this->_context | |
210 | ); | |
211 | $selector->setKey($this->controller->_key); | |
212 | ||
213 | $prefix = NULL; | |
1c1e779e | 214 | if ($this->_context === 'basic' || $this->_context === 'user') { |
6a488035 TO |
215 | $prefix = $this->_prefix; |
216 | } | |
217 | ||
218 | $controller = new CRM_Core_Selector_Controller($selector, | |
219 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
d9669ca6 | 220 | $this->getSortID(), |
6a488035 TO |
221 | CRM_Core_Action::VIEW, |
222 | $this, | |
223 | CRM_Core_Selector_Controller::SESSION, | |
224 | $prefix | |
225 | ); | |
226 | $controller->setEmbedded(TRUE); | |
227 | $query = &$selector->getQuery(); | |
228 | ||
1c1e779e | 229 | if ($this->_context === 'user') { |
6a488035 TO |
230 | $query->setSkipPermission(TRUE); |
231 | } | |
232 | $controller->run(); | |
233 | } | |
234 | ||
1c1e779e | 235 | /** |
236 | * Probably more hackery than anything else. | |
237 | * | |
238 | * @throws \CRM_Core_Exception | |
239 | */ | |
00be9182 | 240 | public function fixFormValues() { |
6a488035 TO |
241 | if (!$this->_force) { |
242 | return; | |
243 | } | |
0370ad20 | 244 | |
6a488035 TO |
245 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
246 | if ($status) { | |
97b002d7 CW |
247 | $this->_formValues['activity_status_id'] = $status; |
248 | $this->_defaults['activity_status_id'] = $status; | |
6a488035 TO |
249 | } |
250 | ||
a3d827a7 | 251 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive'); |
300c3253 | 252 | |
6a488035 | 253 | if ($survey) { |
9231464f | 254 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
9c1bc317 | 255 | $sid = $this->_formValues['activity_survey_id'] ?? NULL; |
9231464f KJ |
256 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); |
257 | ||
c56c5190 | 258 | // since checkbox are replaced by multiple select option |
259 | $this->_formValues['activity_type_id'] = $activity_type_id; | |
260 | $this->_defaults['activity_type_id'] = $activity_type_id; | |
6a488035 TO |
261 | } |
262 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
263 | ||
264 | if ($cid) { | |
265 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
266 | if ($cid > 0) { | |
267 | $this->_formValues['contact_id'] = $cid; | |
268 | ||
269 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); | |
270 | ||
271 | if ($activity_role) { | |
272 | $this->_formValues['activity_role'] = $activity_role; | |
273 | } | |
274 | else { | |
6a488035 TO |
275 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
276 | } | |
277 | // also assign individual mode to the template | |
278 | $this->_single = TRUE; | |
279 | } | |
280 | } | |
8ef12e64 | 281 | |
2c8d0277 | 282 | // Enable search activity by custom value |
e6dda67a | 283 | // @todo this is not good security practice. Instead define entity fields in metadata & |
284 | // use getEntity Defaults | |
2c8d0277 DG |
285 | $requestParams = CRM_Utils_Request::exportValues(); |
286 | foreach (array_keys($requestParams) as $key) { | |
13dbc546 | 287 | if (substr($key, 0, 7) !== 'custom_') { |
2c8d0277 DG |
288 | continue; |
289 | } | |
290 | elseif (empty($requestParams[$key])) { | |
291 | continue; | |
292 | } | |
293 | $customValue = CRM_Utils_Request::retrieve($key, 'String', $this); | |
294 | if ($customValue) { | |
295 | $this->_formValues[$key] = $customValue; | |
296 | $this->_defaults[$key] = $customValue; | |
297 | } | |
298 | } | |
9231464f KJ |
299 | |
300 | if (!empty($this->_defaults)) { | |
301 | $this->setDefaults($this->_defaults); | |
302 | } | |
6a488035 TO |
303 | } |
304 | ||
6a488035 TO |
305 | /** |
306 | * Return a descriptive name for the page, used in wizard header | |
307 | * | |
308 | * @return string | |
6a488035 TO |
309 | */ |
310 | public function getTitle() { | |
311 | return ts('Find Activities'); | |
312 | } | |
96025800 | 313 | |
1c1e779e | 314 | /** |
315 | * Get metadata for the entity fields. | |
316 | * | |
317 | * @return array | |
318 | * | |
319 | * @throws \CiviCRM_API3_Exception | |
320 | */ | |
e6dda67a | 321 | protected function getEntityMetadata() { |
322 | return CRM_Activity_BAO_Query::getSearchFieldMetadata(); | |
323 | } | |
324 | ||
18db97df | 325 | /** |
326 | * Set the metadata for the form. | |
327 | */ | |
328 | protected function setSearchMetadata() { | |
329 | $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]); | |
330 | } | |
331 | ||
6a488035 | 332 | } |