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