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