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