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