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