Merge pull request #2714 from jitendrapurohit/CRMTestFix
[civicrm-core.git] / CRM / Pledge / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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_Search {
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 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');
133 $this->_actionButtonName = $this->getButtonName('next', 'action');
134
135 $this->_done = FALSE;
136 $this->defaults = array();
137
138 /*
139 * we allow the controller to set force/reset externally, useful when we are being
140 * driven by the wizard framework
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 parent::buildQuickForm();
216 $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
217
218 CRM_Pledge_BAO_Query::buildSearchForm($this);
219
220 /*
221 * add form checkboxes for each row. This is needed out here to conform to QF protocol
222 * of all elements being declared in builQuickForm
223 */
224 $rows = $this->get('rows');
225 if (is_array($rows)) {
226 if (!$this->_single) {
227 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows'));
228
229 foreach ($rows as $row) {
230 $this->addElement('checkbox', $row['checkbox'],
231 NULL, NULL,
232 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
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
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'));
254 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
255 }
256
257 }
258
259 /**
260 * The post processing of the form gets done here.
261 *
262 * Key things done during post processing are
263 * - check for reset or next request. if present, skip post procesing.
264 * - now check if user requested running a saved search, if so, then
265 * the form values associated with the saved search are used for searching.
266 * - if user has done a submit with new values the regular post submissing is
267 * done.
268 * The processing consists of using a Selector / Controller framework for getting the
269 * search results.
270 *
271 * @param
272 *
273 * @return void
274 * @access public
275 */
276 function postProcess() {
277 if ($this->_done) {
278 return;
279 }
280
281 $this->_done = TRUE;
282
283 $this->_formValues = $this->controller->exportValues($this->_name);
284
285 $this->fixFormValues();
286
287 // We don't show test records in summaries or dashboards
288 if (empty($this->_formValues['pledge_test']) && $this->_force) {
289 $this->_formValues["pledge_test"] = 0;
290 }
291
292 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
293 if (isset($this->_formValues[$f])) {
294 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
295 }
296 }
297
298 if (isset($this->_ssID) && empty($_POST)) {
299 // if we are editing / running a saved search and the form has not been posted
300 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
301 }
302
303 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
304
305 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
306
307 $this->set('formValues', $this->_formValues);
308 $this->set('queryParams', $this->_queryParams);
309
310 $buttonName = $this->controller->getButtonName();
311 if ($buttonName == $this->_actionButtonName) {
312 // check actionName and if next, then do not repeat a search, since we are going to the next page
313
314 // hack, make sure we reset the task values
315 $stateMachine = $this->controller->getStateMachine();
316 $formName = $stateMachine->getTaskFormName();
317 $this->controller->resetPage($formName);
318 return;
319 }
320
321 $sortID = NULL;
322 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
323 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
324 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
325 );
326 }
327
328 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
329
330 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
331 $this->_action,
332 NULL,
333 $this->_single,
334 $this->_limit,
335 $this->_context
336 );
337 $selector->setKey($this->controller->_key);
338
339 $prefix = NULL;
340 if ($this->_context == 'user') {
341 $prefix = $this->_prefix;
342 }
343
344 $this->assign("{$prefix}limit", $this->_limit);
345 $this->assign("{$prefix}single", $this->_single);
346
347 $controller = new CRM_Core_Selector_Controller($selector,
348 $this->get(CRM_Utils_Pager::PAGE_ID),
349 $sortID,
350 CRM_Core_Action::VIEW,
351 $this,
352 CRM_Core_Selector_Controller::SESSION,
353 $prefix
354 );
355 $controller->setEmbedded(TRUE);
356
357 $query = &$selector->getQuery();
358 if ($this->_context == 'user') {
359 $query->setSkipPermission(TRUE);
360 }
361 $controller->run();
362 }
363
364 /**
365 * This function is used to add the rules (mainly global rules) for form.
366 * All local rules are added near the element
367 *
368 * @return void
369 * @access public
370 * @see valid_date
371 */
372 function addRules() {
373 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
374 }
375
376 /**
377 * global validation rules for the form
378 *
379 * @param array $fields posted values of the form
380 * @param array $errors list of errors to be posted back to the form
381 *
382 * @return void
383 * @static
384 * @access public
385 */
386 static function formRule($fields) {
387 $errors = array();
388
389 if (!empty($errors)) {
390 return $errors;
391 }
392
393 return TRUE;
394 }
395
396 /**
397 * Set the default form values
398 *
399 * @access protected
400 *
401 * @return array the default array reference
402 */
403 function setDefaultValues() {
404 $defaults = array();
405 $defaults = $this->_formValues;
406 return $defaults;
407 }
408
409 function fixFormValues() {
410 if (!$this->_force) {
411 return;
412 }
413
414 // set pledge payment related fields
415 $status = CRM_Utils_Request::retrieve('status', 'String',
416 CRM_Core_DAO::$_nullObject
417 );
418 if ($status) {
419 $this->_formValues['pledge_payment_status_id'] = array($status => 1);
420 $this->_defaults['pledge_payment_status_id'] = array($status => 1);
421 }
422
423 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
424 CRM_Core_DAO::$_nullObject
425 );
426 if ($fromDate) {
427 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
428 $this->_formValues['pledge_payment_date_low'] = $date;
429 $this->_defaults['pledge_payment_date_low'] = $date;
430 }
431
432 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
433 CRM_Core_DAO::$_nullObject
434 );
435 if ($toDate) {
436 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
437 $this->_formValues['pledge_payment_date_high'] = $date;
438 $this->_defaults['pledge_payment_date_high'] = $date;
439 }
440
441 // set pledge related fields
442 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String',
443 CRM_Core_DAO::$_nullObject
444 );
445 if ($pledgeStatus) {
446 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
447
448 // Remove status values that are only used for recurring contributions for now (Failed).
449 unset($statusValues['4']);
450
451 // we need set all statuses except Cancelled
452 unset($statusValues[$pledgeStatus]);
453
454 $statuses = array();
455 foreach ($statusValues as $statusId => $value) {
456 $statuses[$statusId] = 1;
457 }
458
459 $this->_formValues['pledge_status_id'] = $statuses;
460 $this->_defaults['pledge_status_id'] = $statuses;
461 }
462
463 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
464 CRM_Core_DAO::$_nullObject
465 );
466 if ($pledgeFromDate) {
467 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
468 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
469 }
470
471 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date',
472 CRM_Core_DAO::$_nullObject
473 );
474 if ($pledgeToDate) {
475 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
476 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
477 }
478
479 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
480 if ($cid) {
481 $cid = CRM_Utils_Type::escape($cid, 'Integer');
482 if ($cid > 0) {
483 $this->_formValues['contact_id'] = $cid;
484 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
485 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
486 'sort_name'
487 );
488 // also assign individual mode to the template
489 $this->_single = TRUE;
490 }
491 }
492 }
493
494 function getFormValues() {
495 return NULL;
496 }
497
498 /**
499 * Return a descriptive name for the page, used in wizard header
500 *
501 * @return string
502 * @access public
503 */
504 public function getTitle() {
505 return ts('Find Pledges');
506 }
507 }
508