Merge pull request #7190 from galgeek/patch-1
[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 */
33
34/**
35 * This file is for Pledge search
36 */
3efb5b86 37class CRM_Pledge_Form_Search extends CRM_Core_Form_Search {
6a488035 38
6a488035 39 /**
fe482240 40 * The params that are sent to the query.
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 protected $_queryParams;
45
6a488035 46 /**
fe482240 47 * Are we restricting ourselves to a single contact.
6a488035 48 *
6a488035
TO
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
6a488035
TO
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
6a488035 60 /**
fe482240 61 * Prefix for the controller.
6a488035
TO
62 */
63 protected $_prefix = "pledge_";
64
6a488035 65 /**
fe482240 66 * Processing needed for buildForm and later.
6a488035 67 */
00be9182 68 public function preProcess() {
6a488035 69
cc28438b
SB
70 // set the button names
71
6a488035 72 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
73 $this->_actionButtonName = $this->getButtonName('next', 'action');
74
75 $this->_done = FALSE;
76 $this->defaults = array();
77
cc28438b
SB
78
79 // we allow the controller to set force/reset externally, useful when we are being
80 // driven by the wizard framework
81
353ffa53
TO
82 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
83 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
84 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
85 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
86
87 $this->assign("context", $this->_context);
88
89 // get user submitted values
90 // get it from controller only if form has been submitted, else preProcess has set this
91 if (!empty($_POST) && !$this->controller->isModal()) {
92 $this->_formValues = $this->controller->exportValues($this->_name);
93 }
94 else {
95 $this->_formValues = $this->get('formValues');
96 }
97
98 if (empty($this->_formValues)) {
99 if (isset($this->_ssID)) {
100 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
101 }
102 }
103
104 if ($this->_force) {
105 $this->postProcess();
106 $this->set('force', 0);
107 }
108
109 $sortID = NULL;
110 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
111 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
112 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
113 );
114 }
115
6a488035
TO
116 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
117 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
118 $this->_action,
119 NULL,
120 $this->_single,
121 $this->_limit,
122 $this->_context
123 );
124 $prefix = NULL;
125 if ($this->_context == 'user') {
126 $prefix = $this->_prefix;
127 }
128
129 $this->assign("{$prefix}limit", $this->_limit);
130 $this->assign("{$prefix}single", $this->_single);
131
132 $controller = new CRM_Core_Selector_Controller($selector,
133 $this->get(CRM_Utils_Pager::PAGE_ID),
134 $sortID,
135 CRM_Core_Action::VIEW,
136 $this,
137 CRM_Core_Selector_Controller::TRANSFER,
138 $prefix
139 );
140 $controller->setEmbedded(TRUE);
141 $controller->moveFromSessionToTemplate();
142
143 $this->assign('summary', $this->get('summary'));
144 }
145
146 /**
fe482240 147 * Build the form object.
6a488035 148 */
00be9182 149 public function buildQuickForm() {
3efb5b86 150 parent::buildQuickForm();
e597fc33 151 $this->addSortNameField();
6a488035
TO
152
153 CRM_Pledge_BAO_Query::buildSearchForm($this);
154
6a488035
TO
155 $rows = $this->get('rows');
156 if (is_array($rows)) {
6a488035 157 if (!$this->_single) {
8d36b801 158 $this->addRowSelectors($rows);
6a488035
TO
159 }
160
6a488035
TO
161 $permission = CRM_Core_Permission::getPermission();
162
34197a55 163 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles($permission));
6a488035
TO
164 }
165
6a488035
TO
166 }
167
e597fc33
DG
168 /**
169 * Get the label for the sortName field if email searching is on.
170 *
171 * (email searching is a setting under search preferences).
172 *
173 * @return string
174 */
175 protected function getSortNameLabelWithEmail() {
176 return ts('Pledger Name or Email');
177 }
178
179 /**
180 * Get the label for the sortName field if email searching is off.
181 *
182 * (email searching is a setting under search preferences).
183 *
184 * @return string
185 */
186 protected function getSortNameLabelWithOutEmail() {
187 return ts('Pledger Name');
188 }
189
6a488035
TO
190 /**
191 * The post processing of the form gets done here.
192 *
193 * Key things done during post processing are
194 * - check for reset or next request. if present, skip post procesing.
195 * - now check if user requested running a saved search, if so, then
196 * the form values associated with the saved search are used for searching.
197 * - if user has done a submit with new values the regular post submissing is
198 * done.
199 * The processing consists of using a Selector / Controller framework for getting the
200 * search results.
6a488035 201 */
00be9182 202 public function postProcess() {
6a488035
TO
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
d43b88cc
CW
213 // We don't show test records in summaries or dashboards
214 if (empty($this->_formValues['pledge_test']) && $this->_force) {
6a488035
TO
215 $this->_formValues["pledge_test"] = 0;
216 }
217
3f48e02b
PJ
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
6a488035
TO
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
c94d39fd 229 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
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();
e341bbee 237 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
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
95ea77ec 241 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
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 /**
dc195289 291 * add the rules (mainly global rules) for form.
6a488035
TO
292 * All local rules are added near the element
293 *
6a488035
TO
294 * @see valid_date
295 */
00be9182 296 public function addRules() {
6a488035
TO
297 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
298 }
299
300 /**
fe482240 301 * Global validation rules for the form.
6a488035 302 *
3a1617b6
TO
303 * @param array $fields
304 * Posted values of the form.
6a488035 305 */
00be9182 306 public static function formRule($fields) {
6a488035
TO
307 $errors = array();
308
309 if (!empty($errors)) {
310 return $errors;
311 }
312
313 return TRUE;
314 }
315
316 /**
fe482240 317 * Set the default form values.
6a488035 318 *
6a488035 319 *
a6c01b45
CW
320 * @return array
321 * the default array reference
6a488035 322 */
00be9182 323 public function setDefaultValues() {
6a488035
TO
324 $defaults = array();
325 $defaults = $this->_formValues;
326 return $defaults;
327 }
328
00be9182 329 public function fixFormValues() {
6a488035
TO
330 if (!$this->_force) {
331 return;
332 }
333
334 // set pledge payment related fields
335 $status = CRM_Utils_Request::retrieve('status', 'String',
336 CRM_Core_DAO::$_nullObject
337 );
338 if ($status) {
339 $this->_formValues['pledge_payment_status_id'] = array($status => 1);
340 $this->_defaults['pledge_payment_status_id'] = array($status => 1);
341 }
342
343 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
344 CRM_Core_DAO::$_nullObject
345 );
346 if ($fromDate) {
347 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
348 $this->_formValues['pledge_payment_date_low'] = $date;
349 $this->_defaults['pledge_payment_date_low'] = $date;
350 }
351
352 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
353 CRM_Core_DAO::$_nullObject
354 );
355 if ($toDate) {
356 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
357 $this->_formValues['pledge_payment_date_high'] = $date;
358 $this->_defaults['pledge_payment_date_high'] = $date;
359 }
360
361 // set pledge related fields
362 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String',
363 CRM_Core_DAO::$_nullObject
364 );
365 if ($pledgeStatus) {
366 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
367
368 // Remove status values that are only used for recurring contributions for now (Failed).
369 unset($statusValues['4']);
370
371 // we need set all statuses except Cancelled
372 unset($statusValues[$pledgeStatus]);
373
374 $statuses = array();
375 foreach ($statusValues as $statusId => $value) {
376 $statuses[$statusId] = 1;
377 }
378
379 $this->_formValues['pledge_status_id'] = $statuses;
380 $this->_defaults['pledge_status_id'] = $statuses;
381 }
382
383 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
384 CRM_Core_DAO::$_nullObject
385 );
386 if ($pledgeFromDate) {
387 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
388 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
389 }
390
391 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date',
392 CRM_Core_DAO::$_nullObject
393 );
394 if ($pledgeToDate) {
395 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
396 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
397 }
398
399 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
400 if ($cid) {
401 $cid = CRM_Utils_Type::escape($cid, 'Integer');
402 if ($cid > 0) {
403 $this->_formValues['contact_id'] = $cid;
404 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
405 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
406 'sort_name'
407 );
408 // also assign individual mode to the template
409 $this->_single = TRUE;
410 }
411 }
412 }
413
ffd93213
EM
414 /**
415 * @return null
416 */
00be9182 417 public function getFormValues() {
6a488035
TO
418 return NULL;
419 }
420
421 /**
422 * Return a descriptive name for the page, used in wizard header
423 *
424 * @return string
6a488035
TO
425 */
426 public function getTitle() {
427 return ts('Find Pledges');
428 }
96025800 429
6a488035 430}