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