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