Merge pull request #18177 from eileenmcnaughton/gross
[civicrm-core.git] / CRM / Pledge / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This file is for Pledge search
20 */
3efb5b86 21class CRM_Pledge_Form_Search extends CRM_Core_Form_Search {
6a488035 22
6a488035 23 /**
fe482240 24 * The params that are sent to the query.
6a488035
TO
25 *
26 * @var array
6a488035
TO
27 */
28 protected $_queryParams;
29
52ddd94d 30 /**
31 * @return string
32 */
33 public function getDefaultEntity() {
34 return 'Pledge';
35 }
36
6a488035 37 /**
fe482240 38 * Are we restricting ourselves to a single contact.
6a488035 39 *
b67daa72 40 * @var bool
6a488035
TO
41 */
42 protected $_single = FALSE;
43
44 /**
fe482240 45 * Are we restricting ourselves to a single contact.
6a488035 46 *
b67daa72 47 * @var bool
6a488035
TO
48 */
49 protected $_limit = NULL;
50
6a488035 51 /**
fe482240 52 * Prefix for the controller.
c86d4e7c 53 * @var string
6a488035
TO
54 */
55 protected $_prefix = "pledge_";
56
6a488035 57 /**
fe482240 58 * Processing needed for buildForm and later.
6a488035 59 */
00be9182 60 public function preProcess() {
6a488035 61
6a488035
TO
62 $this->_actionButtonName = $this->getButtonName('next', 'action');
63
64 $this->_done = FALSE;
6a488035 65
f991bf8f 66 parent::preProcess();
6a488035 67
6a488035
TO
68 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
69 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
70 $this->_action,
71 NULL,
72 $this->_single,
73 $this->_limit,
74 $this->_context
75 );
76 $prefix = NULL;
77 if ($this->_context == 'user') {
78 $prefix = $this->_prefix;
79 }
80
81 $this->assign("{$prefix}limit", $this->_limit);
82 $this->assign("{$prefix}single", $this->_single);
83
84 $controller = new CRM_Core_Selector_Controller($selector,
85 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 86 $this->getSortID(),
6a488035
TO
87 CRM_Core_Action::VIEW,
88 $this,
89 CRM_Core_Selector_Controller::TRANSFER,
90 $prefix
91 );
92 $controller->setEmbedded(TRUE);
93 $controller->moveFromSessionToTemplate();
94
95 $this->assign('summary', $this->get('summary'));
96 }
97
98 /**
fe482240 99 * Build the form object.
6a488035 100 */
00be9182 101 public function buildQuickForm() {
3efb5b86 102 parent::buildQuickForm();
0573fd28 103 $this->addContactSearchFields();
6a488035
TO
104
105 CRM_Pledge_BAO_Query::buildSearchForm($this);
106
6a488035
TO
107 $rows = $this->get('rows');
108 if (is_array($rows)) {
6a488035 109 if (!$this->_single) {
8d36b801 110 $this->addRowSelectors($rows);
6a488035
TO
111 }
112
56c77e1c 113 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
6a488035
TO
114 }
115
6a488035
TO
116 }
117
e597fc33
DG
118 /**
119 * Get the label for the sortName field if email searching is on.
120 *
121 * (email searching is a setting under search preferences).
122 *
123 * @return string
124 */
125 protected function getSortNameLabelWithEmail() {
126 return ts('Pledger Name or Email');
127 }
128
129 /**
130 * Get the label for the sortName field if email searching is off.
131 *
132 * (email searching is a setting under search preferences).
133 *
134 * @return string
135 */
136 protected function getSortNameLabelWithOutEmail() {
137 return ts('Pledger Name');
138 }
139
0573fd28 140 /**
141 * Get the label for the tag field.
142 *
143 * We do this in a function so the 'ts' wraps the whole string to allow
144 * better translation.
145 *
146 * @return string
147 */
148 protected function getTagLabel() {
149 return ts('Pledger Tag(s)');
150 }
151
152 /**
153 * Get the label for the group field.
154 *
155 * @return string
156 */
157 protected function getGroupLabel() {
158 return ts('Pledger Group(s)');
159 }
160
161 /**
162 * Get the label for the group field.
163 *
164 * @return string
165 */
166 protected function getContactTypeLabel() {
167 return ts('Pledger Contact Type');
168 }
169
6a488035
TO
170 /**
171 * The post processing of the form gets done here.
172 *
173 * Key things done during post processing are
174 * - check for reset or next request. if present, skip post procesing.
175 * - now check if user requested running a saved search, if so, then
176 * the form values associated with the saved search are used for searching.
177 * - if user has done a submit with new values the regular post submissing is
178 * done.
179 * The processing consists of using a Selector / Controller framework for getting the
180 * search results.
6a488035 181 */
00be9182 182 public function postProcess() {
6a488035
TO
183 if ($this->_done) {
184 return;
185 }
186
187 $this->_done = TRUE;
188
1519c828 189 $this->setFormValues();
6a488035
TO
190
191 $this->fixFormValues();
192
d43b88cc
CW
193 // We don't show test records in summaries or dashboards
194 if (empty($this->_formValues['pledge_test']) && $this->_force) {
6a488035
TO
195 $this->_formValues["pledge_test"] = 0;
196 }
197
be2fb01f 198 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
3f48e02b
PJ
199 if (isset($this->_formValues[$f])) {
200 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
201 }
202 }
203
6a488035
TO
204 if (isset($this->_ssID) && empty($_POST)) {
205 // if we are editing / running a saved search and the form has not been posted
206 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
207 }
208
c94d39fd 209 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
210
211 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
212
6a488035
TO
213 $this->set('queryParams', $this->_queryParams);
214
215 $buttonName = $this->controller->getButtonName();
e341bbee 216 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
217 // check actionName and if next, then do not repeat a search, since we are going to the next page
218
219 // hack, make sure we reset the task values
95ea77ec 220 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller->resetPage($formName);
223 return;
224 }
225
6a488035
TO
226 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
227
228 $selector = new CRM_Pledge_Selector_Search($this->_queryParams,
229 $this->_action,
230 NULL,
231 $this->_single,
232 $this->_limit,
233 $this->_context
234 );
235 $selector->setKey($this->controller->_key);
236
237 $prefix = NULL;
238 if ($this->_context == 'user') {
239 $prefix = $this->_prefix;
240 }
241
242 $this->assign("{$prefix}limit", $this->_limit);
243 $this->assign("{$prefix}single", $this->_single);
244
245 $controller = new CRM_Core_Selector_Controller($selector,
246 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 247 $this->getSortID(),
6a488035
TO
248 CRM_Core_Action::VIEW,
249 $this,
250 CRM_Core_Selector_Controller::SESSION,
251 $prefix
252 );
253 $controller->setEmbedded(TRUE);
254
255 $query = &$selector->getQuery();
256 if ($this->_context == 'user') {
257 $query->setSkipPermission(TRUE);
258 }
259 $controller->run();
260 }
261
262 /**
dc195289 263 * add the rules (mainly global rules) for form.
6a488035
TO
264 * All local rules are added near the element
265 *
6a488035
TO
266 * @see valid_date
267 */
00be9182 268 public function addRules() {
be2fb01f 269 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
6a488035
TO
270 }
271
00be9182 272 public function fixFormValues() {
6a488035
TO
273 if (!$this->_force) {
274 return;
275 }
276
277 // set pledge payment related fields
167bcb5f 278 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035 279 if ($status) {
be2fb01f
CW
280 $this->_formValues['pledge_payment_status_id'] = [$status => 1];
281 $this->_defaults['pledge_payment_status_id'] = [$status => 1];
6a488035
TO
282 }
283
167bcb5f 284 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
6a488035
TO
285 if ($fromDate) {
286 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
287 $this->_formValues['pledge_payment_date_low'] = $date;
288 $this->_defaults['pledge_payment_date_low'] = $date;
289 }
290
167bcb5f 291 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
6a488035
TO
292 if ($toDate) {
293 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
294 $this->_formValues['pledge_payment_date_high'] = $date;
295 $this->_defaults['pledge_payment_date_high'] = $date;
296 }
297
298 // set pledge related fields
167bcb5f 299 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String');
6a488035 300
ab6ba136 301 if ($pledgeStatus) {
302 $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
6a488035
TO
303
304 // we need set all statuses except Cancelled
305 unset($statusValues[$pledgeStatus]);
306
be2fb01f 307 $statuses = [];
6a488035
TO
308 foreach ($statusValues as $statusId => $value) {
309 $statuses[$statusId] = 1;
310 }
311
312 $this->_formValues['pledge_status_id'] = $statuses;
313 $this->_defaults['pledge_status_id'] = $statuses;
314 }
315
1273d77c 316 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
317 if ($pledgeFromDate) {
318 list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate);
319 $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date;
320 }
321
1273d77c 322 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
323 if ($pledgeToDate) {
324 list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate);
325 $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date;
326 }
327
328 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
329 if ($cid) {
330 $cid = CRM_Utils_Type::escape($cid, 'Integer');
331 if ($cid > 0) {
332 $this->_formValues['contact_id'] = $cid;
333 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
334 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
335 'sort_name'
336 );
337 // also assign individual mode to the template
338 $this->_single = TRUE;
339 }
340 }
341 }
342
6a488035
TO
343 /**
344 * Return a descriptive name for the page, used in wizard header
345 *
346 * @return string
6a488035
TO
347 */
348 public function getTitle() {
349 return ts('Find Pledges');
350 }
96025800 351
18db97df 352 /**
353 * Set the metadata for the form.
354 *
355 * @throws \CiviCRM_API3_Exception
356 */
357 protected function setSearchMetadata() {
358 $this->addSearchFieldMetadata(['Pledge' => CRM_Pledge_BAO_Query::getSearchFieldMetadata()]);
359 $this->addSearchFieldMetadata(['PledgePayment' => CRM_Pledge_BAO_Query::getPledgePaymentSearchFieldMetadata()]);
360 }
361
6a488035 362}