Commit | Line | Data |
---|---|---|
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 | | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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. |
62d3ee27 | 62 | * @var string |
6a488035 TO |
63 | */ |
64 | protected $_prefix = "activity_"; | |
65 | ||
6a488035 | 66 | /** |
fe482240 | 67 | * The saved search ID retrieved from the GET vars. |
6a488035 TO |
68 | * |
69 | * @var int | |
6a488035 TO |
70 | */ |
71 | protected $_ssID; | |
72 | ||
e6dda67a | 73 | /** |
74 | * @return string | |
75 | */ | |
76 | public function getDefaultEntity() { | |
77 | return 'Activity'; | |
78 | } | |
79 | ||
6a488035 | 80 | /** |
fe482240 | 81 | * Processing needed for buildForm and later. |
6a488035 | 82 | */ |
00be9182 | 83 | public function preProcess() { |
6a488035 TO |
84 | $this->set('searchFormName', 'Search'); |
85 | ||
c490a46a | 86 | // set the button names |
6a488035 | 87 | $this->_searchButtonName = $this->getButtonName('refresh'); |
6a488035 TO |
88 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
89 | ||
90 | $this->_done = FALSE; | |
6a488035 | 91 | |
64ffcefd | 92 | $this->loadStandardSearchOptionsFromUrl(); |
6a488035 TO |
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. | |
e6dda67a | 105 | // @todo this is not good security practice. Instead define the fields in metadata & use |
106 | // getEntityDefaults. | |
149104e0 | 107 | $this->_formValues = array_merge((array) $this->_formValues, CRM_Utils_Request::exportValues()); |
108 | $this->_submitValues = $this->_formValues; | |
109 | } | |
6a488035 TO |
110 | } |
111 | ||
112 | if (empty($this->_formValues)) { | |
113 | if (isset($this->_ssID)) { | |
114 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
115 | } | |
116 | } | |
117 | ||
118 | if ($this->_force) { | |
119 | $this->postProcess(); | |
120 | $this->set('force', 0); | |
121 | } | |
122 | ||
123 | $sortID = NULL; | |
124 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
125 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
126 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
127 | ); | |
128 | } | |
129 | ||
130 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
131 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
132 | $this->_action, | |
133 | NULL, | |
134 | $this->_single, | |
135 | $this->_limit, | |
136 | $this->_context | |
137 | ); | |
138 | $prefix = NULL; | |
139 | if ($this->_context == 'user') { | |
140 | $prefix = $this->_prefix; | |
141 | } | |
142 | ||
143 | $this->assign("{$prefix}limit", $this->_limit); | |
144 | $this->assign("{$prefix}single", $this->_single); | |
145 | ||
146 | $controller = new CRM_Core_Selector_Controller($selector, | |
147 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
148 | $sortID, | |
149 | CRM_Core_Action::VIEW, | |
150 | $this, | |
151 | CRM_Core_Selector_Controller::TRANSFER, | |
152 | $prefix | |
153 | ); | |
154 | $controller->setEmbedded(TRUE); | |
155 | $controller->moveFromSessionToTemplate(); | |
156 | ||
157 | $this->assign('summary', $this->get('summary')); | |
158 | } | |
159 | ||
160 | /** | |
fe482240 | 161 | * Build the form object. |
6a488035 | 162 | */ |
00be9182 | 163 | public function buildQuickForm() { |
3efb5b86 | 164 | parent::buildQuickForm(); |
e597fc33 | 165 | $this->addSortNameField(); |
6a488035 TO |
166 | |
167 | CRM_Activity_BAO_Query::buildSearchForm($this); | |
168 | ||
6a488035 TO |
169 | $rows = $this->get('rows'); |
170 | if (is_array($rows)) { | |
171 | if (!$this->_single) { | |
8d36b801 | 172 | $this->addRowSelectors($rows); |
6a488035 TO |
173 | } |
174 | ||
be509c11 | 175 | $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
6a488035 TO |
176 | } |
177 | ||
6a488035 TO |
178 | } |
179 | ||
180 | /** | |
181 | * The post processing of the form gets done here. | |
182 | * | |
183 | * Key things done during post processing are | |
184 | * - check for reset or next request. if present, skip post procesing. | |
185 | * - now check if user requested running a saved search, if so, then | |
186 | * the form values associated with the saved search are used for searching. | |
187 | * - if user has done a submit with new values the regular post submissing is | |
188 | * done. | |
2e2605fe | 189 | * |
6a488035 TO |
190 | * The processing consists of using a Selector / Controller framework for getting the |
191 | * search results. | |
6a488035 | 192 | */ |
00be9182 | 193 | public function postProcess() { |
6a488035 TO |
194 | if ($this->_done) { |
195 | return; | |
196 | } | |
197 | ||
198 | $this->_done = TRUE; | |
199 | ||
200 | if (!empty($_POST)) { | |
201 | $this->_formValues = $this->controller->exportValues($this->_name); | |
be2fb01f | 202 | $specialParams = [ |
0b38e8f1 | 203 | 'activity_type_id', |
204 | 'status_id', | |
da236f9a | 205 | 'priority_id', |
288674ef | 206 | 'activity_text', |
be2fb01f CW |
207 | ]; |
208 | $changeNames = [ | |
da236f9a | 209 | 'status_id' => 'activity_status_id', |
210 | 'priority_id' => 'activity_priority_id', | |
be2fb01f | 211 | ]; |
da236f9a | 212 | |
0b38e8f1 | 213 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames); |
6a488035 | 214 | } |
6a488035 TO |
215 | $this->fixFormValues(); |
216 | ||
217 | if (isset($this->_ssID) && empty($_POST)) { | |
218 | // if we are editing / running a saved search and the form has not been posted | |
219 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
220 | } | |
6a488035 | 221 | |
d43b88cc CW |
222 | // We don't show test records in summaries or dashboards |
223 | if (empty($this->_formValues['activity_test']) && $this->_force) { | |
6a488035 TO |
224 | $this->_formValues["activity_test"] = 0; |
225 | } | |
28c666be | 226 | |
c94d39fd | 227 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 TO |
228 | |
229 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
230 | ||
231 | $this->set('formValues', $this->_formValues); | |
232 | $this->set('queryParams', $this->_queryParams); | |
233 | ||
234 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 235 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
236 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
237 | // hack, make sure we reset the task values | |
95ea77ec | 238 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
239 | $formName = $stateMachine->getTaskFormName(); |
240 | $this->controller->resetPage($formName); | |
241 | return; | |
242 | } | |
243 | ||
244 | $sortID = NULL; | |
245 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
246 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
247 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
248 | ); | |
249 | } | |
250 | ||
251 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
252 | ||
253 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
254 | $this->_action, | |
255 | NULL, | |
256 | $this->_single, | |
257 | $this->_limit, | |
258 | $this->_context | |
259 | ); | |
260 | $selector->setKey($this->controller->_key); | |
261 | ||
262 | $prefix = NULL; | |
263 | if ($this->_context == 'basic' || $this->_context == 'user') { | |
264 | $prefix = $this->_prefix; | |
265 | } | |
266 | ||
267 | $controller = new CRM_Core_Selector_Controller($selector, | |
268 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
269 | $sortID, | |
270 | CRM_Core_Action::VIEW, | |
271 | $this, | |
272 | CRM_Core_Selector_Controller::SESSION, | |
273 | $prefix | |
274 | ); | |
275 | $controller->setEmbedded(TRUE); | |
276 | $query = &$selector->getQuery(); | |
277 | ||
278 | if ($this->_context == 'user') { | |
279 | $query->setSkipPermission(TRUE); | |
280 | } | |
281 | $controller->run(); | |
282 | } | |
283 | ||
00be9182 | 284 | public function fixFormValues() { |
6a488035 TO |
285 | if (!$this->_force) { |
286 | return; | |
287 | } | |
0370ad20 | 288 | |
6a488035 TO |
289 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
290 | if ($status) { | |
97b002d7 CW |
291 | $this->_formValues['activity_status_id'] = $status; |
292 | $this->_defaults['activity_status_id'] = $status; | |
6a488035 TO |
293 | } |
294 | ||
a3d827a7 | 295 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive'); |
300c3253 | 296 | |
6a488035 | 297 | if ($survey) { |
9231464f KJ |
298 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
299 | $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues); | |
300 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); | |
301 | ||
c56c5190 | 302 | // since checkbox are replaced by multiple select option |
303 | $this->_formValues['activity_type_id'] = $activity_type_id; | |
304 | $this->_defaults['activity_type_id'] = $activity_type_id; | |
6a488035 TO |
305 | } |
306 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
307 | ||
308 | if ($cid) { | |
309 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
310 | if ($cid > 0) { | |
311 | $this->_formValues['contact_id'] = $cid; | |
312 | ||
313 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); | |
314 | ||
315 | if ($activity_role) { | |
316 | $this->_formValues['activity_role'] = $activity_role; | |
317 | } | |
318 | else { | |
6a488035 TO |
319 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
320 | } | |
321 | // also assign individual mode to the template | |
322 | $this->_single = TRUE; | |
323 | } | |
324 | } | |
8ef12e64 | 325 | |
2c8d0277 | 326 | // Enable search activity by custom value |
e6dda67a | 327 | // @todo this is not good security practice. Instead define entity fields in metadata & |
328 | // use getEntity Defaults | |
2c8d0277 DG |
329 | $requestParams = CRM_Utils_Request::exportValues(); |
330 | foreach (array_keys($requestParams) as $key) { | |
331 | if (substr($key, 0, 7) != 'custom_') { | |
332 | continue; | |
333 | } | |
334 | elseif (empty($requestParams[$key])) { | |
335 | continue; | |
336 | } | |
337 | $customValue = CRM_Utils_Request::retrieve($key, 'String', $this); | |
338 | if ($customValue) { | |
339 | $this->_formValues[$key] = $customValue; | |
340 | $this->_defaults[$key] = $customValue; | |
341 | } | |
342 | } | |
9231464f KJ |
343 | |
344 | if (!empty($this->_defaults)) { | |
345 | $this->setDefaults($this->_defaults); | |
346 | } | |
6a488035 TO |
347 | } |
348 | ||
ffd93213 EM |
349 | /** |
350 | * @return null | |
351 | */ | |
00be9182 | 352 | public function getFormValues() { |
6a488035 TO |
353 | return NULL; |
354 | } | |
355 | ||
e6dda67a | 356 | /** |
357 | * This virtual function is used to set the default values of various form elements. | |
358 | * | |
359 | * @return array|NULL | |
360 | * reference to the array of default values | |
361 | */ | |
362 | public function setDefaultValues() { | |
16b2bc5b | 363 | return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues); |
e6dda67a | 364 | } |
365 | ||
6a488035 TO |
366 | /** |
367 | * Return a descriptive name for the page, used in wizard header | |
368 | * | |
369 | * @return string | |
6a488035 TO |
370 | */ |
371 | public function getTitle() { | |
372 | return ts('Find Activities'); | |
373 | } | |
96025800 | 374 | |
e6dda67a | 375 | protected function getEntityMetadata() { |
376 | return CRM_Activity_BAO_Query::getSearchFieldMetadata(); | |
377 | } | |
378 | ||
6a488035 | 379 | } |