Merge pull request #4545 from monishdeb/CRM-15361
[civicrm-core.git] / CRM / Contribute / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Files required
38 */
39
40 /**
41 * advanced search, extends basic search
42 */
43 class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
44
45 /**
46 * the params that are sent to the query
47 *
48 * @var array
49 * @access protected
50 */
51 protected $_queryParams;
52
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
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');
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 */
98
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() {
169 if (empty($this->_defaults
170 ['contribution_status'])) {
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() {
184 parent::buildQuickForm();
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
194 $this->_group = CRM_Core_PseudoConstant::nestedGroup();
195
196 // multiselect for groups
197 if ($this->_group) {
198 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
199 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
200 );
201 }
202
203 // multiselect for tags
204 $contactTags = CRM_Core_BAO_Tag::getTags();
205
206 if ($contactTags) {
207 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
208 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
209 );
210 }
211
212 CRM_Contribute_BAO_Query::buildSearchForm($this);
213
214 $rows = $this->get('rows');
215 if (is_array($rows)) {
216 if (!$this->_single) {
217 $this->addRowSelectors($rows);
218 }
219
220 $permission = CRM_Core_Permission::getPermission();
221
222 $queryParams = $this->get('queryParams');
223 $softCreditFiltering = FALSE;
224 if (!empty($queryParams)) {
225 $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
226 }
227 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering);
228 $this->addTaskMenu($tasks);
229 }
230
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
263 // We don't show test records in summaries or dashboards
264 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
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 }
274
275 $config = CRM_Core_Config::singleton();
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 }
284 }
285
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 }
291
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 }
297 }
298
299
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 }
306
307 if ($group && is_array($group)) {
308 unset($this->_formValues['group']);
309 foreach($group as $notImportant => $groupID) {
310 $this->_formValues['group'][$groupID] = 1;
311 }
312 }
313 }
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();
324 if ($buttonName == $this->_actionButtonName) {
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
328 $stateMachine = $this->controller->getStateMachine();
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 :)
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