Update copyright date for 2020
[civicrm-core.git] / CRM / Pledge / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
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
52ddd94d 46 /**
47 * @return string
48 */
49 public function getDefaultEntity() {
50 return 'Pledge';
51 }
52
6a488035 53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
b67daa72 56 * @var bool
6a488035
TO
57 */
58 protected $_single = FALSE;
59
60 /**
fe482240 61 * Are we restricting ourselves to a single contact.
6a488035 62 *
b67daa72 63 * @var bool
6a488035
TO
64 */
65 protected $_limit = NULL;
66
6a488035 67 /**
fe482240 68 * Prefix for the controller.
c86d4e7c 69 * @var string
6a488035
TO
70 */
71 protected $_prefix = "pledge_";
72
6a488035 73 /**
fe482240 74 * Processing needed for buildForm and later.
6a488035 75 */
00be9182 76 public function preProcess() {
6a488035 77
6a488035 78 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
79 $this->_actionButtonName = $this->getButtonName('next', 'action');
80
81 $this->_done = FALSE;
6a488035 82
64ffcefd 83 $this->loadStandardSearchOptionsFromUrl();
6a488035
TO
84
85 // get user submitted values
86 // get it from controller only if form has been submitted, else preProcess has set this
87 if (!empty($_POST) && !$this->controller->isModal()) {
88 $this->_formValues = $this->controller->exportValues($this->_name);
89 }
90 else {
91 $this->_formValues = $this->get('formValues');
92 }
93
94 if (empty($this->_formValues)) {
95 if (isset($this->_ssID)) {
96 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
97 }
98 }
99
100 if ($this->_force) {
1519c828
SL
101 // pledge related dates
102 $this->addSearchFieldMetadata(['Pledge' => CRM_Pledge_BAO_Query::getSearchFieldMetadata()]);
103 $this->addSearchFieldMetadata(['PledgePayment' => CRM_Pledge_BAO_Query::getPledgePaymentSearchFieldMetadata()]);
104 $this->addFormFieldsFromMetadata();
6a488035
TO
105 $this->postProcess();
106 $this->set('force', 0);
107 }
108
109 $sortID = NULL;
110 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
111 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
112 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
113 );
114 }
115
6a488035
TO
116 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
117 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
118 $this->_action,
119 NULL,
120 $this->_single,
121 $this->_limit,
122 $this->_context
123 );
124 $prefix = NULL;
125 if ($this->_context == 'user') {
126 $prefix = $this->_prefix;
127 }
128
129 $this->assign("{$prefix}limit", $this->_limit);
130 $this->assign("{$prefix}single", $this->_single);
131
132 $controller = new CRM_Core_Selector_Controller($selector,
133 $this->get(CRM_Utils_Pager::PAGE_ID),
134 $sortID,
135 CRM_Core_Action::VIEW,
136 $this,
137 CRM_Core_Selector_Controller::TRANSFER,
138 $prefix
139 );
140 $controller->setEmbedded(TRUE);
141 $controller->moveFromSessionToTemplate();
142
143 $this->assign('summary', $this->get('summary'));
144 }
145
146 /**
fe482240 147 * Build the form object.
6a488035 148 */
00be9182 149 public function buildQuickForm() {
3efb5b86 150 parent::buildQuickForm();
0573fd28 151 $this->addContactSearchFields();
6a488035
TO
152
153 CRM_Pledge_BAO_Query::buildSearchForm($this);
154
6a488035
TO
155 $rows = $this->get('rows');
156 if (is_array($rows)) {
6a488035 157 if (!$this->_single) {
8d36b801 158 $this->addRowSelectors($rows);
6a488035
TO
159 }
160
56c77e1c 161 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
6a488035
TO
162 }
163
6a488035
TO
164 }
165
e597fc33
DG
166 /**
167 * Get the label for the sortName field if email searching is on.
168 *
169 * (email searching is a setting under search preferences).
170 *
171 * @return string
172 */
173 protected function getSortNameLabelWithEmail() {
174 return ts('Pledger Name or Email');
175 }
176
177 /**
178 * Get the label for the sortName field if email searching is off.
179 *
180 * (email searching is a setting under search preferences).
181 *
182 * @return string
183 */
184 protected function getSortNameLabelWithOutEmail() {
185 return ts('Pledger Name');
186 }
187
0573fd28 188 /**
189 * Get the label for the tag field.
190 *
191 * We do this in a function so the 'ts' wraps the whole string to allow
192 * better translation.
193 *
194 * @return string
195 */
196 protected function getTagLabel() {
197 return ts('Pledger Tag(s)');
198 }
199
200 /**
201 * Get the label for the group field.
202 *
203 * @return string
204 */
205 protected function getGroupLabel() {
206 return ts('Pledger Group(s)');
207 }
208
209 /**
210 * Get the label for the group field.
211 *
212 * @return string
213 */
214 protected function getContactTypeLabel() {
215 return ts('Pledger Contact Type');
216 }
217
6a488035
TO
218 /**
219 * The post processing of the form gets done here.
220 *
221 * Key things done during post processing are
222 * - check for reset or next request. if present, skip post procesing.
223 * - now check if user requested running a saved search, if so, then
224 * the form values associated with the saved search are used for searching.
225 * - if user has done a submit with new values the regular post submissing is
226 * done.
227 * The processing consists of using a Selector / Controller framework for getting the
228 * search results.
6a488035 229 */
00be9182 230 public function postProcess() {
6a488035
TO
231 if ($this->_done) {
232 return;
233 }
234
235 $this->_done = TRUE;
236
1519c828 237 $this->setFormValues();
6a488035
TO
238
239 $this->fixFormValues();
240
d43b88cc
CW
241 // We don't show test records in summaries or dashboards
242 if (empty($this->_formValues['pledge_test']) && $this->_force) {
6a488035
TO
243 $this->_formValues["pledge_test"] = 0;
244 }
245
be2fb01f 246 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
3f48e02b
PJ
247 if (isset($this->_formValues[$f])) {
248 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
249 }
250 }
251
6a488035
TO
252 if (isset($this->_ssID) && empty($_POST)) {
253 // if we are editing / running a saved search and the form has not been posted
254 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
255 }
256
c94d39fd 257 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
258
259 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
260
261 $this->set('formValues', $this->_formValues);
262 $this->set('queryParams', $this->_queryParams);
263
264 $buttonName = $this->controller->getButtonName();
e341bbee 265 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
266 // check actionName and if next, then do not repeat a search, since we are going to the next page
267
268 // hack, make sure we reset the task values
95ea77ec 269 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
270 $formName = $stateMachine->getTaskFormName();
271 $this->controller->resetPage($formName);
272 return;
273 }
274
275 $sortID = NULL;
276 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
277 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
278 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
279 );
280 }
281
282 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
283
284 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
285 $this->_action,
286 NULL,
287 $this->_single,
288 $this->_limit,
289 $this->_context
290 );
291 $selector->setKey($this->controller->_key);
292
293 $prefix = NULL;
294 if ($this->_context == 'user') {
295 $prefix = $this->_prefix;
296 }
297
298 $this->assign("{$prefix}limit", $this->_limit);
299 $this->assign("{$prefix}single", $this->_single);
300
301 $controller = new CRM_Core_Selector_Controller($selector,
302 $this->get(CRM_Utils_Pager::PAGE_ID),
303 $sortID,
304 CRM_Core_Action::VIEW,
305 $this,
306 CRM_Core_Selector_Controller::SESSION,
307 $prefix
308 );
309 $controller->setEmbedded(TRUE);
310
311 $query = &$selector->getQuery();
312 if ($this->_context == 'user') {
313 $query->setSkipPermission(TRUE);
314 }
315 $controller->run();
316 }
317
318 /**
dc195289 319 * add the rules (mainly global rules) for form.
6a488035
TO
320 * All local rules are added near the element
321 *
6a488035
TO
322 * @see valid_date
323 */
00be9182 324 public function addRules() {
be2fb01f 325 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
6a488035
TO
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 335 if ($status) {
be2fb01f
CW
336 $this->_formValues['pledge_payment_status_id'] = [$status => 1];
337 $this->_defaults['pledge_payment_status_id'] = [$status => 1];
6a488035
TO
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
be2fb01f 363 $statuses = [];
6a488035
TO
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
6a488035
TO
399 /**
400 * Return a descriptive name for the page, used in wizard header
401 *
402 * @return string
6a488035
TO
403 */
404 public function getTitle() {
405 return ts('Find Pledges');
406 }
96025800 407
6a488035 408}