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