Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | | |
c73475ea | 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
6a488035 TO |
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 | | |
c73475ea WA |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | | |
6a488035 TO |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
b6c94f42 | 35 | * This file is for activity search. |
6a488035 | 36 | */ |
3efb5b86 | 37 | class CRM_Activity_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 38 | |
6a488035 | 39 | /** |
fe482240 | 40 | * The params that are sent to the query. |
6a488035 TO |
41 | * |
42 | * @var array | |
6a488035 TO |
43 | */ |
44 | protected $_queryParams; | |
45 | ||
6a488035 | 46 | /** |
fe482240 | 47 | * Are we restricting ourselves to a single contact. |
6a488035 | 48 | * |
6a488035 TO |
49 | * @var boolean |
50 | */ | |
51 | protected $_single = FALSE; | |
52 | ||
53 | /** | |
fe482240 | 54 | * Are we restricting ourselves to a single contact. |
6a488035 | 55 | * |
6a488035 TO |
56 | * @var boolean |
57 | */ | |
58 | protected $_limit = NULL; | |
59 | ||
6a488035 | 60 | /** |
fe482240 | 61 | * Prefix for the controller. |
6a488035 TO |
62 | */ |
63 | protected $_prefix = "activity_"; | |
64 | ||
6a488035 | 65 | /** |
fe482240 | 66 | * The saved search ID retrieved from the GET vars. |
6a488035 TO |
67 | * |
68 | * @var int | |
6a488035 TO |
69 | */ |
70 | protected $_ssID; | |
71 | ||
72 | /** | |
fe482240 | 73 | * Processing needed for buildForm and later. |
6a488035 | 74 | */ |
00be9182 | 75 | public function preProcess() { |
6a488035 TO |
76 | $this->set('searchFormName', 'Search'); |
77 | ||
c490a46a | 78 | // set the button names |
6a488035 | 79 | $this->_searchButtonName = $this->getButtonName('refresh'); |
6a488035 TO |
80 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
81 | ||
82 | $this->_done = FALSE; | |
83 | $this->defaults = array(); | |
84 | ||
c490a46a CW |
85 | // we allow the controller to set force/reset externally, useful when we are being |
86 | // driven by the wizard framework | |
a3d827a7 | 87 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
353ffa53 TO |
88 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
89 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
6a488035 TO |
90 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
91 | ||
92 | $this->assign("context", $this->_context); | |
93 | ||
94 | // get user submitted values | |
95 | // get it from controller only if form has been submitted, else preProcess has set this | |
96 | if (!empty($_POST) && !$this->controller->isModal()) { | |
97 | $this->_formValues = $this->controller->exportValues($this->_name); | |
98 | } | |
99 | else { | |
100 | $this->_formValues = $this->get('formValues'); | |
998fb051 | 101 | |
102 | if ($this->_force) { | |
149104e0 | 103 | // If we force the search then merge form values with url values |
104 | // and set submit values to form values. | |
105 | $this->_formValues = array_merge((array) $this->_formValues, CRM_Utils_Request::exportValues()); | |
106 | $this->_submitValues = $this->_formValues; | |
107 | } | |
6a488035 TO |
108 | } |
109 | ||
110 | if (empty($this->_formValues)) { | |
111 | if (isset($this->_ssID)) { | |
112 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
113 | } | |
114 | } | |
115 | ||
116 | if ($this->_force) { | |
117 | $this->postProcess(); | |
118 | $this->set('force', 0); | |
119 | } | |
120 | ||
121 | $sortID = NULL; | |
122 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
123 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
124 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
125 | ); | |
126 | } | |
127 | ||
128 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
129 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
130 | $this->_action, | |
131 | NULL, | |
132 | $this->_single, | |
133 | $this->_limit, | |
134 | $this->_context | |
135 | ); | |
136 | $prefix = NULL; | |
137 | if ($this->_context == 'user') { | |
138 | $prefix = $this->_prefix; | |
139 | } | |
140 | ||
141 | $this->assign("{$prefix}limit", $this->_limit); | |
142 | $this->assign("{$prefix}single", $this->_single); | |
143 | ||
144 | $controller = new CRM_Core_Selector_Controller($selector, | |
145 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
146 | $sortID, | |
147 | CRM_Core_Action::VIEW, | |
148 | $this, | |
149 | CRM_Core_Selector_Controller::TRANSFER, | |
150 | $prefix | |
151 | ); | |
152 | $controller->setEmbedded(TRUE); | |
153 | $controller->moveFromSessionToTemplate(); | |
154 | ||
155 | $this->assign('summary', $this->get('summary')); | |
156 | } | |
157 | ||
158 | /** | |
fe482240 | 159 | * Build the form object. |
6a488035 | 160 | */ |
00be9182 | 161 | public function buildQuickForm() { |
3efb5b86 | 162 | parent::buildQuickForm(); |
e597fc33 | 163 | $this->addSortNameField(); |
6a488035 TO |
164 | |
165 | CRM_Activity_BAO_Query::buildSearchForm($this); | |
166 | ||
6a488035 TO |
167 | $rows = $this->get('rows'); |
168 | if (is_array($rows)) { | |
169 | if (!$this->_single) { | |
8d36b801 | 170 | $this->addRowSelectors($rows); |
6a488035 TO |
171 | } |
172 | ||
be509c11 | 173 | $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
6a488035 TO |
174 | } |
175 | ||
6a488035 TO |
176 | } |
177 | ||
178 | /** | |
179 | * The post processing of the form gets done here. | |
180 | * | |
181 | * Key things done during post processing are | |
182 | * - check for reset or next request. if present, skip post procesing. | |
183 | * - now check if user requested running a saved search, if so, then | |
184 | * the form values associated with the saved search are used for searching. | |
185 | * - if user has done a submit with new values the regular post submissing is | |
186 | * done. | |
2e2605fe | 187 | * |
6a488035 TO |
188 | * The processing consists of using a Selector / Controller framework for getting the |
189 | * search results. | |
6a488035 | 190 | */ |
00be9182 | 191 | public function postProcess() { |
6a488035 TO |
192 | if ($this->_done) { |
193 | return; | |
194 | } | |
195 | ||
196 | $this->_done = TRUE; | |
197 | ||
198 | if (!empty($_POST)) { | |
199 | $this->_formValues = $this->controller->exportValues($this->_name); | |
0b38e8f1 | 200 | $specialParams = array( |
201 | 'activity_type_id', | |
202 | 'status_id', | |
da236f9a | 203 | 'priority_id', |
288674ef | 204 | 'activity_text', |
0b38e8f1 | 205 | ); |
da236f9a | 206 | $changeNames = array( |
207 | 'status_id' => 'activity_status_id', | |
208 | 'priority_id' => 'activity_priority_id', | |
209 | ); | |
210 | ||
0b38e8f1 | 211 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames); |
6a488035 TO |
212 | } |
213 | ||
214 | $this->fixFormValues(); | |
215 | ||
216 | if (isset($this->_ssID) && empty($_POST)) { | |
217 | // if we are editing / running a saved search and the form has not been posted | |
218 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
219 | } | |
6a488035 | 220 | |
d43b88cc CW |
221 | // We don't show test records in summaries or dashboards |
222 | if (empty($this->_formValues['activity_test']) && $this->_force) { | |
6a488035 TO |
223 | $this->_formValues["activity_test"] = 0; |
224 | } | |
28c666be | 225 | |
c94d39fd | 226 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 TO |
227 | |
228 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
229 | ||
230 | $this->set('formValues', $this->_formValues); | |
231 | $this->set('queryParams', $this->_queryParams); | |
232 | ||
233 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 234 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
235 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
236 | // hack, make sure we reset the task values | |
95ea77ec | 237 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
238 | $formName = $stateMachine->getTaskFormName(); |
239 | $this->controller->resetPage($formName); | |
240 | return; | |
241 | } | |
242 | ||
243 | $sortID = NULL; | |
244 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
245 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
246 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
247 | ); | |
248 | } | |
249 | ||
250 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
251 | ||
252 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
253 | $this->_action, | |
254 | NULL, | |
255 | $this->_single, | |
256 | $this->_limit, | |
257 | $this->_context | |
258 | ); | |
259 | $selector->setKey($this->controller->_key); | |
260 | ||
261 | $prefix = NULL; | |
262 | if ($this->_context == 'basic' || $this->_context == 'user') { | |
263 | $prefix = $this->_prefix; | |
264 | } | |
265 | ||
266 | $controller = new CRM_Core_Selector_Controller($selector, | |
267 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
268 | $sortID, | |
269 | CRM_Core_Action::VIEW, | |
270 | $this, | |
271 | CRM_Core_Selector_Controller::SESSION, | |
272 | $prefix | |
273 | ); | |
274 | $controller->setEmbedded(TRUE); | |
275 | $query = &$selector->getQuery(); | |
276 | ||
277 | if ($this->_context == 'user') { | |
278 | $query->setSkipPermission(TRUE); | |
279 | } | |
280 | $controller->run(); | |
281 | } | |
282 | ||
00be9182 | 283 | public function fixFormValues() { |
6a488035 TO |
284 | if (!$this->_force) { |
285 | return; | |
286 | } | |
0370ad20 | 287 | |
6a488035 TO |
288 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
289 | if ($status) { | |
97b002d7 CW |
290 | $this->_formValues['activity_status_id'] = $status; |
291 | $this->_defaults['activity_status_id'] = $status; | |
6a488035 TO |
292 | } |
293 | ||
a3d827a7 | 294 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive'); |
300c3253 | 295 | |
6a488035 | 296 | if ($survey) { |
9231464f KJ |
297 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
298 | $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues); | |
299 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); | |
300 | ||
c56c5190 | 301 | // since checkbox are replaced by multiple select option |
302 | $this->_formValues['activity_type_id'] = $activity_type_id; | |
303 | $this->_defaults['activity_type_id'] = $activity_type_id; | |
6a488035 TO |
304 | } |
305 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
306 | ||
307 | if ($cid) { | |
308 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
309 | if ($cid > 0) { | |
310 | $this->_formValues['contact_id'] = $cid; | |
311 | ||
312 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); | |
313 | ||
314 | if ($activity_role) { | |
315 | $this->_formValues['activity_role'] = $activity_role; | |
316 | } | |
317 | else { | |
6a488035 TO |
318 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
319 | } | |
320 | // also assign individual mode to the template | |
321 | $this->_single = TRUE; | |
322 | } | |
323 | } | |
8ef12e64 | 324 | |
4e636a74 | 325 | // Added for membership search |
8ef12e64 | 326 | |
1273d77c | 327 | $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive'); |
8ef12e64 | 328 | |
4e636a74 | 329 | if ($signupType) { |
2e2acd1f | 330 | $this->_formValues['activity_role'] = 1; |
2e2acd1f | 331 | $this->_defaults['activity_role'] = 1; |
4e636a74 | 332 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
0370ad20 AH |
333 | |
334 | $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes); | |
335 | $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes); | |
8ef12e64 | 336 | |
4e636a74 AH |
337 | switch ($signupType) { |
338 | case 3: // signups and renewals | |
339 | $this->_formValues['activity_type_id'][$renew] = 1; | |
340 | $this->_defaults['activity_type_id'][$renew] = 1; | |
341 | case 1: // signups only | |
342 | $this->_formValues['activity_type_id'][$signup] = 1; | |
343 | $this->_defaults['activity_type_id'][$signup] = 1; | |
344 | break; | |
8ef12e64 | 345 | |
4e636a74 AH |
346 | case 2: // renewals only |
347 | $this->_formValues['activity_type_id'][$renew] = 1; | |
348 | $this->_defaults['activity_type_id'][$renew] = 1; | |
349 | break; | |
350 | } | |
351 | } | |
8ef12e64 | 352 | |
1273d77c | 353 | $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String'); |
8ef12e64 | 354 | |
4e636a74 | 355 | if ($dateLow) { |
0370ad20 AH |
356 | $dateLow = date('m/d/Y', strtotime($dateLow)); |
357 | $this->_formValues['activity_date_relative'] = 0; | |
358 | $this->_defaults['activity_date_relative'] = 0; | |
4e636a74 AH |
359 | $this->_formValues['activity_date_low'] = $dateLow; |
360 | $this->_defaults['activity_date_low'] = $dateLow; | |
361 | } | |
8ef12e64 | 362 | |
1273d77c | 363 | $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String'); |
8ef12e64 | 364 | |
4e636a74 AH |
365 | if ($dateHigh) { |
366 | // Activity date time assumes midnight at the beginning of the date | |
367 | // This sets it to almost midnight at the end of the date | |
32864ccf | 368 | /* if ($dateHigh <= 99999999) { |
e70a7fc0 | 369 | $dateHigh = 1000000 * $dateHigh + 235959; |
0370ad20 AH |
370 | } */ |
371 | $dateHigh = date('m/d/Y', strtotime($dateHigh)); | |
372 | $this->_formValues['activity_date_relative'] = 0; | |
373 | $this->_defaults['activity_date_relative'] = 0; | |
4e636a74 AH |
374 | $this->_formValues['activity_date_high'] = $dateHigh; |
375 | $this->_defaults['activity_date_high'] = $dateHigh; | |
8ef12e64 | 376 | } |
19c7dd92 | 377 | |
2c8d0277 DG |
378 | // Enable search activity by custom value |
379 | $requestParams = CRM_Utils_Request::exportValues(); | |
380 | foreach (array_keys($requestParams) as $key) { | |
381 | if (substr($key, 0, 7) != 'custom_') { | |
382 | continue; | |
383 | } | |
384 | elseif (empty($requestParams[$key])) { | |
385 | continue; | |
386 | } | |
387 | $customValue = CRM_Utils_Request::retrieve($key, 'String', $this); | |
388 | if ($customValue) { | |
389 | $this->_formValues[$key] = $customValue; | |
390 | $this->_defaults[$key] = $customValue; | |
391 | } | |
392 | } | |
9231464f KJ |
393 | |
394 | if (!empty($this->_defaults)) { | |
395 | $this->setDefaults($this->_defaults); | |
396 | } | |
6a488035 TO |
397 | } |
398 | ||
ffd93213 EM |
399 | /** |
400 | * @return null | |
401 | */ | |
00be9182 | 402 | public function getFormValues() { |
6a488035 TO |
403 | return NULL; |
404 | } | |
405 | ||
406 | /** | |
407 | * Return a descriptive name for the page, used in wizard header | |
408 | * | |
409 | * @return string | |
6a488035 TO |
410 | */ |
411 | public function getTitle() { | |
412 | return ts('Find Activities'); | |
413 | } | |
96025800 | 414 | |
6a488035 | 415 | } |