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