Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-14-04-44-13
[civicrm-core.git] / CRM / Pledge / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This file is for Pledge search
38 */
39 class CRM_Pledge_Form_Search extends CRM_Core_Form {
40
41 /**
42 * Are we forced to run a search
43 *
44 * @var int
45 * @access protected
46 */
47 protected $_force;
48
49 /**
50 * name of search button
51 *
52 * @var string
53 * @access protected
54 */
55 protected $_searchButtonName;
56
57 /**
58 * name of print button
59 *
60 * @var string
61 * @access protected
62 */
63 protected $_printButtonName;
64
65 /**
66 * name of action button
67 *
68 * @var string
69 * @access protected
70 */
71 protected $_actionButtonName;
72
73 /**
74 * form values that we will be using
75 *
76 * @var array
77 * @access protected
78 */
79 protected $_formValues;
80
81 /**
82 * the params that are sent to the query
83 *
84 * @var array
85 * @access protected
86 */
87 protected $_queryParams;
88
89 /**
90 * have we already done this search
91 *
92 * @access protected
93 * @var boolean
94 */
95 protected $_done;
96
97 /**
98 * are we restricting ourselves to a single contact
99 *
100 * @access protected
101 * @var boolean
102 */
103 protected $_single = FALSE;
104
105 /**
106 * are we restricting ourselves to a single contact
107 *
108 * @access protected
109 * @var boolean
110 */
111 protected $_limit = NULL;
112
113 /**
114 * what context are we being invoked from
115 *
116 * @access protected
117 * @var string
118 */
119 protected $_context = NULL;
120
121 /**
122 * prefix for the controller
123 *
124 */
125 protected $_prefix = "pledge_";
126
127 protected $_defaults;
128
129 /**
130 * processing needed for buildForm and later
131 *
132 * @return void
133 * @access public
134 */
135 function preProcess() {
136
137 /**
138 * set the button names
139 */
140 $this->_searchButtonName = $this->getButtonName('refresh');
141 $this->_printButtonName = $this->getButtonName('next', 'print');
142 $this->_actionButtonName = $this->getButtonName('next', 'action');
143
144 $this->_done = FALSE;
145 $this->defaults = array();
146
147 /*
148 * we allow the controller to set force/reset externally, useful when we are being
149 * driven by the wizard framework
150 */
151 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
152 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
153 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
154 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
155
156 $this->assign("context", $this->_context);
157
158 // get user submitted values
159 // get it from controller only if form has been submitted, else preProcess has set this
160 if (!empty($_POST) && !$this->controller->isModal()) {
161 $this->_formValues = $this->controller->exportValues($this->_name);
162 }
163 else {
164 $this->_formValues = $this->get('formValues');
165 }
166
167 if (empty($this->_formValues)) {
168 if (isset($this->_ssID)) {
169 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
170 }
171 }
172
173 if ($this->_force) {
174 $this->postProcess();
175 $this->set('force', 0);
176 }
177
178 $sortID = NULL;
179 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
180 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
181 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
182 );
183 }
184
185
186 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
187 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
188 $this->_action,
189 NULL,
190 $this->_single,
191 $this->_limit,
192 $this->_context
193 );
194 $prefix = NULL;
195 if ($this->_context == 'user') {
196 $prefix = $this->_prefix;
197 }
198
199 $this->assign("{$prefix}limit", $this->_limit);
200 $this->assign("{$prefix}single", $this->_single);
201
202 $controller = new CRM_Core_Selector_Controller($selector,
203 $this->get(CRM_Utils_Pager::PAGE_ID),
204 $sortID,
205 CRM_Core_Action::VIEW,
206 $this,
207 CRM_Core_Selector_Controller::TRANSFER,
208 $prefix
209 );
210 $controller->setEmbedded(TRUE);
211 $controller->moveFromSessionToTemplate();
212
213 $this->assign('summary', $this->get('summary'));
214 }
215
216 /**
217 * Build the form
218 *
219 * @access public
220 *
221 * @return void
222 */
223 function buildQuickForm() {
224 $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
225
226 CRM_Pledge_BAO_Query::buildSearchForm($this);
227
228 /*
229 * add form checkboxes for each row. This is needed out here to conform to QF protocol
230 * of all elements being declared in builQuickForm
231 */
232 $rows = $this->get('rows');
233 if (is_array($rows)) {
234
235 if (!$this->_single) {
236 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);"));
237
238 foreach ($rows as $row) {
239 $this->addElement('checkbox', $row['checkbox'],
240 NULL, NULL,
241 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
242 );
243 }
244 }
245
246 $total = $cancel = 0;
247
248 $permission = CRM_Core_Permission::getPermission();
249
250 $tasks = array('' => ts('- actions -')) + CRM_Pledge_Task::permissionedTaskTitles($permission);
251
252 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
253 $this->add('submit', $this->_actionButtonName, ts('Go'),
254 array(
255 'class' => 'form-submit',
256 'id' => 'Go',
257 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
258 )
259 );
260
261 $this->add('submit', $this->_printButtonName, ts('Print'),
262 array(
263 'class' => 'form-submit',
264 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
265 )
266 );
267
268 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
269 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
270 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
271 }
272
273 // add buttons
274 $this->addButtons(array(
275 array(
276 'type' => 'refresh',
277 'name' => ts('Search'),
278 'isDefault' => TRUE,
279 ),
280 ));
281 }
282
283 /**
284 * The post processing of the form gets done here.
285 *
286 * Key things done during post processing are
287 * - check for reset or next request. if present, skip post procesing.
288 * - now check if user requested running a saved search, if so, then
289 * the form values associated with the saved search are used for searching.
290 * - if user has done a submit with new values the regular post submissing is
291 * done.
292 * The processing consists of using a Selector / Controller framework for getting the
293 * search results.
294 *
295 * @param
296 *
297 * @return void
298 * @access public
299 */
300 function postProcess() {
301 if ($this->_done) {
302 return;
303 }
304
305 $this->_done = TRUE;
306
307 $this->_formValues = $this->controller->exportValues($this->_name);
308
309 $this->fixFormValues();
310
311 // We don't show test records in summaries or dashboards
312 if (empty($this->_formValues['pledge_test']) && $this->_force) {
313 $this->_formValues["pledge_test"] = 0;
314 }
315
316 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
317 if (isset($this->_formValues[$f])) {
318 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
319 }
320 }
321
322 if (isset($this->_ssID) && empty($_POST)) {
323 // if we are editing / running a saved search and the form has not been posted
324 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
325 }
326
327 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
328
329 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
330
331 $this->set('formValues', $this->_formValues);
332 $this->set('queryParams', $this->_queryParams);
333
334 $buttonName = $this->controller->getButtonName();
335 if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) {
336 // check actionName and if next, then do not repeat a search, since we are going to the next page
337
338 // hack, make sure we reset the task values
339 $stateMachine = $this->controller->getStateMachine();
340 $formName = $stateMachine->getTaskFormName();
341 $this->controller->resetPage($formName);
342 return;
343 }
344
345 $sortID = NULL;
346 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
347 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
348 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
349 );
350 }
351
352 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
353
354 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
355 $this->_action,
356 NULL,
357 $this->_single,
358 $this->_limit,
359 $this->_context
360 );
361 $selector->setKey($this->controller->_key);
362
363 $prefix = NULL;
364 if ($this->_context == 'user') {
365 $prefix = $this->_prefix;
366 }
367
368 $this->assign("{$prefix}limit", $this->_limit);
369 $this->assign("{$prefix}single", $this->_single);
370
371 $controller = new CRM_Core_Selector_Controller($selector,
372 $this->get(CRM_Utils_Pager::PAGE_ID),
373 $sortID,
374 CRM_Core_Action::VIEW,
375 $this,
376 CRM_Core_Selector_Controller::SESSION,
377 $prefix
378 );
379 $controller->setEmbedded(TRUE);
380
381 $query = &$selector->getQuery();
382 if ($this->_context == 'user') {
383 $query->setSkipPermission(TRUE);
384 }
385 $controller->run();
386 }
387
388 /**
389 * This function is used to add the rules (mainly global rules) for form.
390 * All local rules are added near the element
391 *
392 * @return None
393 * @access public
394 * @see valid_date
395 */
396 function addRules() {
397 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
398 }
399
400 /**
401 * global validation rules for the form
402 *
403 * @param array $fields posted values of the form
404 * @param array $errors list of errors to be posted back to the form
405 *
406 * @return void
407 * @static
408 * @access public
409 */
410 static function formRule($fields) {
411 $errors = array();
412
413 if (!empty($errors)) {
414 return $errors;
415 }
416
417 return TRUE;
418 }
419
420 /**
421 * Set the default form values
422 *
423 * @access protected
424 *
425 * @return array the default array reference
426 */
427 function setDefaultValues() {
428 $defaults = array();
429 $defaults = $this->_formValues;
430 return $defaults;
431 }
432
433 function fixFormValues() {
434 if (!$this->_force) {
435 return;
436 }
437
438 // set pledge payment related fields
439 $status = CRM_Utils_Request::retrieve('status', 'String',
440 CRM_Core_DAO::$_nullObject
441 );
442 if ($status) {
443 $this->_formValues['pledge_payment_status_id'] = array($status => 1);
444 $this->_defaults['pledge_payment_status_id'] = array($status => 1);
445 }
446
447 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
448 CRM_Core_DAO::$_nullObject
449 );
450 if ($fromDate) {
451 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
452 $this->_formValues['pledge_payment_date_low'] = $date;
453 $this->_defaults['pledge_payment_date_low'] = $date;
454 }
455
456 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
457 CRM_Core_DAO::$_nullObject
458 );
459 if ($toDate) {
460 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
461 $this->_formValues['pledge_payment_date_high'] = $date;
462 $this->_defaults['pledge_payment_date_high'] = $date;
463 }
464
465 // set pledge related fields
466 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String',
467 CRM_Core_DAO::$_nullObject
468 );
469 if ($pledgeStatus) {
470 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
471
472 // Remove status values that are only used for recurring contributions for now (Failed).
473 unset($statusValues['4']);
474
475 // we need set all statuses except Cancelled
476 unset($statusValues[$pledgeStatus]);
477
478 $statuses = array();
479 foreach ($statusValues as $statusId => $value) {
480 $statuses[$statusId] = 1;
481 }
482
483 $this->_formValues['pledge_status_id'] = $statuses;
484 $this->_defaults['pledge_status_id'] = $statuses;
485 }
486
487 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
488 CRM_Core_DAO::$_nullObject
489 );
490 if ($pledgeFromDate) {
491 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
492 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
493 }
494
495 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date',
496 CRM_Core_DAO::$_nullObject
497 );
498 if ($pledgeToDate) {
499 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
500 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
501 }
502
503 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
504 if ($cid) {
505 $cid = CRM_Utils_Type::escape($cid, 'Integer');
506 if ($cid > 0) {
507 $this->_formValues['contact_id'] = $cid;
508 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
509 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
510 'sort_name'
511 );
512 // also assign individual mode to the template
513 $this->_single = TRUE;
514 }
515 }
516 }
517
518 function getFormValues() {
519 return NULL;
520 }
521
522 /**
523 * Return a descriptive name for the page, used in wizard header
524 *
525 * @return string
526 * @access public
527 */
528 public function getTitle() {
529 return ts('Find Pledges');
530 }
531 }
532