Merge pull request #4806 from civicrm/4.5
[civicrm-core.git] / CRM / Pledge / 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 * This file is for Pledge search
38 */
39 class CRM_Pledge_Form_Search extends CRM_Core_Form_Search {
40
41 /**
42 * The params that are sent to the query
43 *
44 * @var array
45 * @access protected
46 */
47 protected $_queryParams;
48
49 /**
50 * Are we restricting ourselves to a single contact
51 *
52 * @access protected
53 * @var boolean
54 */
55 protected $_single = FALSE;
56
57 /**
58 * Are we restricting ourselves to a single contact
59 *
60 * @access protected
61 * @var boolean
62 */
63 protected $_limit = NULL;
64
65 /**
66 * Prefix for the controller
67 *
68 */
69 protected $_prefix = "pledge_";
70
71 protected $_defaults;
72
73 /**
74 * Processing needed for buildForm and later
75 *
76 * @return void
77 * @access public
78 */
79 function preProcess() {
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 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
95 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
96 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
97 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
98
99 $this->assign("context", $this->_context);
100
101 // get user submitted values
102 // get it from controller only if form has been submitted, else preProcess has set this
103 if (!empty($_POST) && !$this->controller->isModal()) {
104 $this->_formValues = $this->controller->exportValues($this->_name);
105 }
106 else {
107 $this->_formValues = $this->get('formValues');
108 }
109
110 if (empty($this->_formValues)) {
111 if (isset($this->_ssID)) {
112 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
113 }
114 }
115
116 if ($this->_force) {
117 $this->postProcess();
118 $this->set('force', 0);
119 }
120
121 $sortID = NULL;
122 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
123 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
124 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
125 );
126 }
127
128
129 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
130 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
131 $this->_action,
132 NULL,
133 $this->_single,
134 $this->_limit,
135 $this->_context
136 );
137 $prefix = NULL;
138 if ($this->_context == 'user') {
139 $prefix = $this->_prefix;
140 }
141
142 $this->assign("{$prefix}limit", $this->_limit);
143 $this->assign("{$prefix}single", $this->_single);
144
145 $controller = new CRM_Core_Selector_Controller($selector,
146 $this->get(CRM_Utils_Pager::PAGE_ID),
147 $sortID,
148 CRM_Core_Action::VIEW,
149 $this,
150 CRM_Core_Selector_Controller::TRANSFER,
151 $prefix
152 );
153 $controller->setEmbedded(TRUE);
154 $controller->moveFromSessionToTemplate();
155
156 $this->assign('summary', $this->get('summary'));
157 }
158
159 /**
160 * Build the form object
161 *
162 * @access public
163 *
164 * @return void
165 */
166 function buildQuickForm() {
167 parent::buildQuickForm();
168 $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
169
170 CRM_Pledge_BAO_Query::buildSearchForm($this);
171
172 $rows = $this->get('rows');
173 if (is_array($rows)) {
174 if (!$this->_single) {
175 $this->addRowSelectors($rows);
176 }
177
178 $permission = CRM_Core_Permission::getPermission();
179
180 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles($permission));
181 }
182
183 }
184
185 /**
186 * The post processing of the form gets done here.
187 *
188 * Key things done during post processing are
189 * - check for reset or next request. if present, skip post procesing.
190 * - now check if user requested running a saved search, if so, then
191 * the form values associated with the saved search are used for searching.
192 * - if user has done a submit with new values the regular post submissing is
193 * done.
194 * The processing consists of using a Selector / Controller framework for getting the
195 * search results.
196 *
197 * @param
198 *
199 * @return void
200 * @access public
201 */
202 function postProcess() {
203 if ($this->_done) {
204 return;
205 }
206
207 $this->_done = TRUE;
208
209 $this->_formValues = $this->controller->exportValues($this->_name);
210
211 $this->fixFormValues();
212
213 // We don't show test records in summaries or dashboards
214 if (empty($this->_formValues['pledge_test']) && $this->_force) {
215 $this->_formValues["pledge_test"] = 0;
216 }
217
218 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
219 if (isset($this->_formValues[$f])) {
220 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
221 }
222 }
223
224 if (isset($this->_ssID) && empty($_POST)) {
225 // if we are editing / running a saved search and the form has not been posted
226 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
227 }
228
229 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
230
231 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
232
233 $this->set('formValues', $this->_formValues);
234 $this->set('queryParams', $this->_queryParams);
235
236 $buttonName = $this->controller->getButtonName();
237 if ($buttonName == $this->_actionButtonName) {
238 // check actionName and if next, then do not repeat a search, since we are going to the next page
239
240 // hack, make sure we reset the task values
241 $stateMachine = $this->controller->getStateMachine();
242 $formName = $stateMachine->getTaskFormName();
243 $this->controller->resetPage($formName);
244 return;
245 }
246
247 $sortID = NULL;
248 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
249 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
250 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
251 );
252 }
253
254 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
255
256 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
257 $this->_action,
258 NULL,
259 $this->_single,
260 $this->_limit,
261 $this->_context
262 );
263 $selector->setKey($this->controller->_key);
264
265 $prefix = NULL;
266 if ($this->_context == 'user') {
267 $prefix = $this->_prefix;
268 }
269
270 $this->assign("{$prefix}limit", $this->_limit);
271 $this->assign("{$prefix}single", $this->_single);
272
273 $controller = new CRM_Core_Selector_Controller($selector,
274 $this->get(CRM_Utils_Pager::PAGE_ID),
275 $sortID,
276 CRM_Core_Action::VIEW,
277 $this,
278 CRM_Core_Selector_Controller::SESSION,
279 $prefix
280 );
281 $controller->setEmbedded(TRUE);
282
283 $query = &$selector->getQuery();
284 if ($this->_context == 'user') {
285 $query->setSkipPermission(TRUE);
286 }
287 $controller->run();
288 }
289
290 /**
291 * This function is used to add the rules (mainly global rules) for form.
292 * All local rules are added near the element
293 *
294 * @return void
295 * @access public
296 * @see valid_date
297 */
298 function addRules() {
299 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
300 }
301
302 /**
303 * Global validation rules for the form
304 *
305 * @param array $fields posted values of the form
306 *
307 * @return void
308 * @static
309 * @access public
310 */
311 static function formRule($fields) {
312 $errors = array();
313
314 if (!empty($errors)) {
315 return $errors;
316 }
317
318 return TRUE;
319 }
320
321 /**
322 * Set the default form values
323 *
324 * @access protected
325 *
326 * @return array the default array reference
327 */
328 function setDefaultValues() {
329 $defaults = array();
330 $defaults = $this->_formValues;
331 return $defaults;
332 }
333
334 function fixFormValues() {
335 if (!$this->_force) {
336 return;
337 }
338
339 // set pledge payment related fields
340 $status = CRM_Utils_Request::retrieve('status', 'String',
341 CRM_Core_DAO::$_nullObject
342 );
343 if ($status) {
344 $this->_formValues['pledge_payment_status_id'] = array($status => 1);
345 $this->_defaults['pledge_payment_status_id'] = array($status => 1);
346 }
347
348 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
349 CRM_Core_DAO::$_nullObject
350 );
351 if ($fromDate) {
352 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
353 $this->_formValues['pledge_payment_date_low'] = $date;
354 $this->_defaults['pledge_payment_date_low'] = $date;
355 }
356
357 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
358 CRM_Core_DAO::$_nullObject
359 );
360 if ($toDate) {
361 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
362 $this->_formValues['pledge_payment_date_high'] = $date;
363 $this->_defaults['pledge_payment_date_high'] = $date;
364 }
365
366 // set pledge related fields
367 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String',
368 CRM_Core_DAO::$_nullObject
369 );
370 if ($pledgeStatus) {
371 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
372
373 // Remove status values that are only used for recurring contributions for now (Failed).
374 unset($statusValues['4']);
375
376 // we need set all statuses except Cancelled
377 unset($statusValues[$pledgeStatus]);
378
379 $statuses = array();
380 foreach ($statusValues as $statusId => $value) {
381 $statuses[$statusId] = 1;
382 }
383
384 $this->_formValues['pledge_status_id'] = $statuses;
385 $this->_defaults['pledge_status_id'] = $statuses;
386 }
387
388 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
389 CRM_Core_DAO::$_nullObject
390 );
391 if ($pledgeFromDate) {
392 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
393 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
394 }
395
396 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date',
397 CRM_Core_DAO::$_nullObject
398 );
399 if ($pledgeToDate) {
400 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
401 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
402 }
403
404 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
405 if ($cid) {
406 $cid = CRM_Utils_Type::escape($cid, 'Integer');
407 if ($cid > 0) {
408 $this->_formValues['contact_id'] = $cid;
409 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
410 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
411 'sort_name'
412 );
413 // also assign individual mode to the template
414 $this->_single = TRUE;
415 }
416 }
417 }
418
419 /**
420 * @return null
421 */
422 function getFormValues() {
423 return NULL;
424 }
425
426 /**
427 * Return a descriptive name for the page, used in wizard header
428 *
429 * @return string
430 * @access public
431 */
432 public function getTitle() {
433 return ts('Find Pledges');
434 }
435 }
436