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