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