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