Merge pull request #14282 from eileenmcnaughton/profiletest
[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 *
b67daa72 49 * @var bool
6a488035
TO
50 */
51 protected $_single = FALSE;
52
53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
b67daa72 56 * @var bool
6a488035
TO
57 */
58 protected $_limit = NULL;
59
6a488035 60 /**
fe482240 61 * Prefix for the controller.
c86d4e7c 62 * @var string
6a488035
TO
63 */
64 protected $_prefix = "pledge_";
65
6a488035 66 /**
fe482240 67 * Processing needed for buildForm and later.
6a488035 68 */
00be9182 69 public function preProcess() {
6a488035 70
6a488035 71 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
72 $this->_actionButtonName = $this->getButtonName('next', 'action');
73
74 $this->_done = FALSE;
6a488035 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
be2fb01f 235 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
3f48e02b
PJ
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() {
be2fb01f 314 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
6a488035
TO
315 }
316
6a488035 317 /**
fe482240 318 * Set the default form values.
6a488035 319 *
6a488035 320 *
a6c01b45
CW
321 * @return array
322 * the default array reference
6a488035 323 */
00be9182 324 public function setDefaultValues() {
be2fb01f 325 $defaults = [];
6a488035
TO
326 $defaults = $this->_formValues;
327 return $defaults;
328 }
329
00be9182 330 public function fixFormValues() {
6a488035
TO
331 if (!$this->_force) {
332 return;
333 }
334
335 // set pledge payment related fields
167bcb5f 336 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035 337 if ($status) {
be2fb01f
CW
338 $this->_formValues['pledge_payment_status_id'] = [$status => 1];
339 $this->_defaults['pledge_payment_status_id'] = [$status => 1];
6a488035
TO
340 }
341
167bcb5f 342 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
6a488035
TO
343 if ($fromDate) {
344 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
345 $this->_formValues['pledge_payment_date_low'] = $date;
346 $this->_defaults['pledge_payment_date_low'] = $date;
347 }
348
167bcb5f 349 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
6a488035
TO
350 if ($toDate) {
351 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
352 $this->_formValues['pledge_payment_date_high'] = $date;
353 $this->_defaults['pledge_payment_date_high'] = $date;
354 }
355
356 // set pledge related fields
167bcb5f 357 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String');
6a488035 358
ab6ba136 359 if ($pledgeStatus) {
360 $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
6a488035
TO
361
362 // we need set all statuses except Cancelled
363 unset($statusValues[$pledgeStatus]);
364
be2fb01f 365 $statuses = [];
6a488035
TO
366 foreach ($statusValues as $statusId => $value) {
367 $statuses[$statusId] = 1;
368 }
369
370 $this->_formValues['pledge_status_id'] = $statuses;
371 $this->_defaults['pledge_status_id'] = $statuses;
372 }
373
1273d77c 374 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
375 if ($pledgeFromDate) {
376 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
377 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
378 }
379
1273d77c 380 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
381 if ($pledgeToDate) {
382 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
383 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
384 }
385
386 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
387 if ($cid) {
388 $cid = CRM_Utils_Type::escape($cid, 'Integer');
389 if ($cid > 0) {
390 $this->_formValues['contact_id'] = $cid;
391 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
392 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
393 'sort_name'
394 );
395 // also assign individual mode to the template
396 $this->_single = TRUE;
397 }
398 }
399 }
400
ffd93213
EM
401 /**
402 * @return null
403 */
00be9182 404 public function getFormValues() {
6a488035
TO
405 return NULL;
406 }
407
408 /**
409 * Return a descriptive name for the page, used in wizard header
410 *
411 * @return string
6a488035
TO
412 */
413 public function getTitle() {
414 return ts('Find Pledges');
415 }
96025800 416
6a488035 417}