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