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