Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 and the CiviCRM Licensing Exception. | | |
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 and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Files required | |
38 | */ | |
39 | ||
40 | /** | |
41 | * advanced search, extends basic search | |
42 | */ | |
3efb5b86 | 43 | class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 44 | |
6a488035 TO |
45 | /** |
46 | * the params that are sent to the query | |
47 | * | |
48 | * @var array | |
49 | * @access protected | |
50 | */ | |
51 | protected $_queryParams; | |
52 | ||
6a488035 TO |
53 | /** |
54 | * are we restricting ourselves to a single contact | |
55 | * | |
56 | * @access protected | |
57 | * @var boolean | |
58 | */ | |
59 | protected $_single = FALSE; | |
60 | ||
61 | /** | |
62 | * are we restricting ourselves to a single contact | |
63 | * | |
64 | * @access protected | |
65 | * @var boolean | |
66 | */ | |
67 | protected $_limit = NULL; | |
68 | ||
6a488035 TO |
69 | protected $_defaults; |
70 | ||
71 | /** | |
72 | * prefix for the controller | |
73 | * | |
74 | */ | |
75 | protected $_prefix = "contribute_"; | |
76 | ||
77 | /** | |
78 | * processing needed for buildForm and later | |
79 | * | |
80 | * @return void | |
81 | * @access public | |
82 | */ function preProcess() { | |
83 | $this->set('searchFormName', 'Search'); | |
84 | ||
85 | /** | |
86 | * set the button names | |
87 | */ | |
88 | $this->_searchButtonName = $this->getButtonName('refresh'); | |
6a488035 TO |
89 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
90 | ||
91 | $this->_done = FALSE; | |
92 | $this->defaults = array(); | |
93 | ||
94 | /* | |
95 | * we allow the controller to set force/reset externally, useful when we are being | |
96 | * driven by the wizard framework | |
97 | */ | |
ed106721 | 98 | |
6a488035 TO |
99 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
100 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
101 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
102 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); | |
103 | ||
104 | $this->assign("context", $this->_context); | |
105 | ||
106 | // get user submitted values | |
107 | // get it from controller only if form has been submitted, else preProcess has set this | |
108 | if (!empty($_POST)) { | |
109 | $this->_formValues = $this->controller->exportValues($this->_name); | |
110 | } | |
111 | else { | |
112 | $this->_formValues = $this->get('formValues'); | |
113 | } | |
114 | ||
115 | //membership ID | |
116 | $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this); | |
117 | if (isset($memberShipId)) { | |
118 | $this->_formValues['contribution_membership_id'] = $memberShipId; | |
119 | } | |
120 | $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); | |
121 | if (isset($participantId)) { | |
122 | $this->_formValues['contribution_participant_id'] = $participantId; | |
123 | } | |
124 | ||
125 | if ($this->_force) { | |
126 | $this->postProcess(); | |
127 | $this->set('force', 0); | |
128 | } | |
129 | ||
130 | $sortID = NULL; | |
131 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
132 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
133 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
134 | ); | |
135 | } | |
136 | ||
137 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
138 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, | |
139 | $this->_action, | |
140 | NULL, | |
141 | $this->_single, | |
142 | $this->_limit, | |
143 | $this->_context | |
144 | ); | |
145 | $prefix = NULL; | |
146 | if ($this->_context == 'user') { | |
147 | $prefix = $this->_prefix; | |
148 | } | |
149 | ||
150 | $this->assign("{$prefix}limit", $this->_limit); | |
151 | $this->assign("{$prefix}single", $this->_single); | |
152 | ||
153 | $controller = new CRM_Core_Selector_Controller($selector, | |
154 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
155 | $sortID, | |
156 | CRM_Core_Action::VIEW, | |
157 | $this, | |
158 | CRM_Core_Selector_Controller::TRANSFER, | |
159 | $prefix | |
160 | ); | |
161 | ||
162 | $controller->setEmbedded(TRUE); | |
163 | $controller->moveFromSessionToTemplate(); | |
164 | ||
165 | $this->assign('contributionSummary', $this->get('summary')); | |
166 | } | |
167 | ||
168 | function setDefaultValues() { | |
a7488080 CW |
169 | if (empty($this->_defaults |
170 | ['contribution_status'])) { | |
6a488035 TO |
171 | $this->_defaults['contribution_status'][1] = 1; |
172 | } | |
173 | return $this->_defaults; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Build the form | |
178 | * | |
179 | * @access public | |
180 | * | |
181 | * @return void | |
182 | */ | |
183 | function buildQuickForm() { | |
3efb5b86 | 184 | parent::buildQuickForm(); |
6a488035 TO |
185 | // text for sort_name |
186 | $this->addElement('text', | |
187 | 'sort_name', | |
188 | ts('Contributor Name or Email'), | |
189 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', | |
190 | 'sort_name' | |
191 | ) | |
192 | ); | |
193 | ||
24431f7b | 194 | $this->_group = CRM_Core_PseudoConstant::nestedGroup(); |
6a488035 TO |
195 | |
196 | // multiselect for groups | |
197 | if ($this->_group) { | |
198 | $this->add('select', 'group', ts('Groups'), $this->_group, FALSE, | |
ab345ca5 | 199 | array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2') |
6a488035 TO |
200 | ); |
201 | } | |
202 | ||
203 | // multiselect for tags | |
6a488035 TO |
204 | $contactTags = CRM_Core_BAO_Tag::getTags(); |
205 | ||
206 | if ($contactTags) { | |
207 | $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE, | |
ab345ca5 | 208 | array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2') |
6a488035 TO |
209 | ); |
210 | } | |
211 | ||
212 | CRM_Contribute_BAO_Query::buildSearchForm($this); | |
213 | ||
6a488035 TO |
214 | $rows = $this->get('rows'); |
215 | if (is_array($rows)) { | |
216 | if (!$this->_single) { | |
8d36b801 | 217 | $this->addRowSelectors($rows); |
6a488035 TO |
218 | } |
219 | ||
6a488035 TO |
220 | $permission = CRM_Core_Permission::getPermission(); |
221 | ||
c410490a DS |
222 | $queryParams = $this->get('queryParams'); |
223 | $softCreditFiltering = FALSE; | |
224 | if (!empty($queryParams)) { | |
225 | $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams); | |
226 | } | |
34197a55 CW |
227 | $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering); |
228 | $this->addTaskMenu($tasks); | |
6a488035 TO |
229 | } |
230 | ||
6a488035 TO |
231 | } |
232 | ||
233 | /** | |
234 | * The post processing of the form gets done here. | |
235 | * | |
236 | * Key things done during post processing are | |
237 | * - check for reset or next request. if present, skip post procesing. | |
238 | * - now check if user requested running a saved search, if so, then | |
239 | * the form values associated with the saved search are used for searching. | |
240 | * - if user has done a submit with new values the regular post submissing is | |
241 | * done. | |
242 | * The processing consists of using a Selector / Controller framework for getting the | |
243 | * search results. | |
244 | * | |
245 | * @param | |
246 | * | |
247 | * @return void | |
248 | * @access public | |
249 | */ | |
250 | function postProcess() { | |
251 | if ($this->_done) { | |
252 | return; | |
253 | } | |
254 | ||
255 | $this->_done = TRUE; | |
256 | ||
257 | if (!empty($_POST)) { | |
258 | $this->_formValues = $this->controller->exportValues($this->_name); | |
259 | } | |
260 | ||
261 | $this->fixFormValues(); | |
262 | ||
d43b88cc | 263 | // We don't show test records in summaries or dashboards |
b899db8f | 264 | if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') { |
6a488035 TO |
265 | $this->_formValues["contribution_test"] = 0; |
266 | } | |
267 | ||
268 | foreach (array( | |
269 | 'contribution_amount_low', 'contribution_amount_high') as $f) { | |
270 | if (isset($this->_formValues[$f])) { | |
271 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); | |
272 | } | |
273 | } | |
ed106721 | 274 | |
6a488035 | 275 | $config = CRM_Core_Config::singleton(); |
a2be2b19 PN |
276 | if (!empty($_POST)) { |
277 | // CRM-13848 | |
278 | $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues); | |
279 | if ($financialType && is_array($financialType)) { | |
280 | unset($this->_formValues['financial_type_id']); | |
281 | foreach($financialType as $notImportant => $typeID) { | |
282 | $this->_formValues['financial_type_id'][$typeID] = 1; | |
283 | } | |
1f0d8c92 | 284 | } |
1f0d8c92 | 285 | |
a2be2b19 PN |
286 | $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues); |
287 | if ($tags && !is_array($tags)) { | |
288 | unset($this->_formValues['contact_tags']); | |
289 | $this->_formValues['contact_tags'][$tags] = 1; | |
290 | } | |
ed106721 | 291 | |
a2be2b19 PN |
292 | if ($tags && is_array($tags)) { |
293 | unset($this->_formValues['contact_tags']); | |
294 | foreach($tags as $notImportant => $tagID) { | |
295 | $this->_formValues['contact_tags'][$tagID] = 1; | |
296 | } | |
6a488035 | 297 | } |
ed106721 DL |
298 | |
299 | ||
a2be2b19 PN |
300 | if (!$config->groupTree) { |
301 | $group = CRM_Utils_Array::value('group', $this->_formValues); | |
302 | if ($group && !is_array($group)) { | |
303 | unset($this->_formValues['group']); | |
304 | $this->_formValues['group'][$group] = 1; | |
305 | } | |
ed106721 | 306 | |
a2be2b19 PN |
307 | if ($group && is_array($group)) { |
308 | unset($this->_formValues['group']); | |
309 | foreach($group as $notImportant => $groupID) { | |
310 | $this->_formValues['group'][$groupID] = 1; | |
311 | } | |
6a488035 TO |
312 | } |
313 | } | |
6a488035 TO |
314 | } |
315 | ||
316 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); | |
317 | ||
318 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
319 | ||
320 | $this->set('formValues', $this->_formValues); | |
321 | $this->set('queryParams', $this->_queryParams); | |
322 | ||
323 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 324 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
325 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
326 | ||
327 | // hack, make sure we reset the task values | |
ed106721 | 328 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
329 | $formName = $stateMachine->getTaskFormName(); |
330 | $this->controller->resetPage($formName); | |
331 | return; | |
332 | } | |
333 | ||
334 | ||
335 | $sortID = NULL; | |
336 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
337 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
338 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
339 | ); | |
340 | } | |
341 | ||
342 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
343 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, | |
344 | $this->_action, | |
345 | NULL, | |
346 | $this->_single, | |
347 | $this->_limit, | |
348 | $this->_context | |
349 | ); | |
350 | $selector->setKey($this->controller->_key); | |
351 | ||
352 | $prefix = NULL; | |
353 | if ($this->_context == 'basic' || $this->_context == 'user') { | |
354 | $prefix = $this->_prefix; | |
355 | } | |
356 | ||
357 | $controller = new CRM_Core_Selector_Controller($selector, | |
358 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
359 | $sortID, | |
360 | CRM_Core_Action::VIEW, | |
361 | $this, | |
362 | CRM_Core_Selector_Controller::SESSION, | |
363 | $prefix | |
364 | ); | |
365 | $controller->setEmbedded(TRUE); | |
366 | ||
367 | $query = &$selector->getQuery(); | |
368 | if ($this->_context == 'user') { | |
369 | $query->setSkipPermission(TRUE); | |
370 | } | |
371 | $summary = &$query->summaryContribution($this->_context); | |
372 | $this->set('summary', $summary); | |
373 | $this->assign('contributionSummary', $summary); | |
374 | $controller->run(); | |
375 | } | |
376 | ||
377 | function fixFormValues() { | |
378 | // if this search has been forced | |
379 | // then see if there are any get values, and if so over-ride the post values | |
380 | // note that this means that GET over-rides POST :) | |
6a488035 TO |
381 | if (!$this->_force) { |
382 | return; | |
383 | } | |
384 | ||
385 | $status = CRM_Utils_Request::retrieve('status', 'String', | |
386 | CRM_Core_DAO::$_nullObject | |
387 | ); | |
388 | if ($status) { | |
389 | $this->_formValues['contribution_status_id'] = array($status => 1); | |
390 | $this->_defaults['contribution_status_id'] = array($status => 1); | |
391 | } | |
392 | ||
393 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
394 | ||
395 | if ($cid) { | |
396 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
397 | if ($cid > 0) { | |
398 | $this->_formValues['contact_id'] = $cid; | |
399 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); | |
400 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, | |
401 | 'sort_name' | |
402 | ); | |
403 | // also assign individual mode to the template | |
404 | $this->_single = TRUE; | |
405 | } | |
406 | } | |
407 | ||
408 | $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp', | |
409 | CRM_Core_DAO::$_nullObject | |
410 | ); | |
411 | if ($lowDate) { | |
412 | $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp'); | |
413 | $date = CRM_Utils_Date::setDateDefaults($lowDate); | |
414 | $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0]; | |
415 | } | |
416 | ||
417 | $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp', | |
418 | CRM_Core_DAO::$_nullObject | |
419 | ); | |
420 | if ($highDate) { | |
421 | $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp'); | |
422 | $date = CRM_Utils_Date::setDateDefaults($highDate); | |
423 | $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0]; | |
424 | } | |
425 | ||
426 | if ($highDate || $lowDate) { | |
427 | //set the Choose Date Range value | |
428 | $this->_formValues['contribution_date_relative'] = 0; | |
429 | } | |
430 | ||
431 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', | |
432 | $this | |
433 | ); | |
434 | ||
435 | $test = CRM_Utils_Request::retrieve('test', 'Boolean', | |
436 | CRM_Core_DAO::$_nullObject | |
437 | ); | |
438 | if (isset($test)) { | |
439 | $test = CRM_Utils_Type::escape($test, 'Boolean'); | |
440 | $this->_formValues['contribution_test'] = $test; | |
441 | } | |
442 | //Recurring id | |
443 | $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE); | |
444 | if ($recur) { | |
445 | $this->_formValues['contribution_recur_id'] = $recur; | |
446 | $this->_formValues['contribution_recurring'] = 1; | |
447 | } | |
448 | ||
449 | //check for contribution page id. | |
450 | $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this); | |
451 | if ($contribPageId) { | |
452 | $this->_formValues['contribution_page_id'] = $contribPageId; | |
453 | } | |
454 | ||
455 | //give values to default. | |
456 | $this->_defaults = $this->_formValues; | |
457 | } | |
458 | ||
459 | /** | |
460 | * Return a descriptive name for the page, used in wizard header | |
461 | * | |
462 | * @return string | |
463 | * @access public | |
464 | */ | |
465 | public function getTitle() { | |
466 | return ts('Find Contributions'); | |
467 | } | |
468 | } | |
469 |