Merge pull request #15826 from seamuslee001/dev_core_183_dedupe
[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 62 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
63 $this->_actionButtonName = $this->getButtonName('next', 'action');
64
65 $this->_done = FALSE;
6a488035 66
64ffcefd 67 $this->loadStandardSearchOptionsFromUrl();
6a488035
TO
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) {
1519c828
SL
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();
6a488035
TO
89 $this->postProcess();
90 $this->set('force', 0);
91 }
92
6a488035
TO
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),
b5c63125 111 $this->getSortID(),
6a488035
TO
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 /**
fe482240 124 * Build the form object.
6a488035 125 */
00be9182 126 public function buildQuickForm() {
3efb5b86 127 parent::buildQuickForm();
0573fd28 128 $this->addContactSearchFields();
6a488035
TO
129
130 CRM_Pledge_BAO_Query::buildSearchForm($this);
131
6a488035
TO
132 $rows = $this->get('rows');
133 if (is_array($rows)) {
6a488035 134 if (!$this->_single) {
8d36b801 135 $this->addRowSelectors($rows);
6a488035
TO
136 }
137
56c77e1c 138 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
6a488035
TO
139 }
140
6a488035
TO
141 }
142
e597fc33
DG
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
0573fd28 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
6a488035
TO
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.
6a488035 206 */
00be9182 207 public function postProcess() {
6a488035
TO
208 if ($this->_done) {
209 return;
210 }
211
212 $this->_done = TRUE;
213
1519c828 214 $this->setFormValues();
6a488035
TO
215
216 $this->fixFormValues();
217
d43b88cc
CW
218 // We don't show test records in summaries or dashboards
219 if (empty($this->_formValues['pledge_test']) && $this->_force) {
6a488035
TO
220 $this->_formValues["pledge_test"] = 0;
221 }
222
be2fb01f 223 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
3f48e02b
PJ
224 if (isset($this->_formValues[$f])) {
225 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
226 }
227 }
228
6a488035
TO
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
c94d39fd 234 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
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();
e341bbee 242 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
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
95ea77ec 246 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
247 $formName = $stateMachine->getTaskFormName();
248 $this->controller->resetPage($formName);
249 return;
250 }
251
6a488035
TO
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),
b5c63125 273 $this->getSortID(),
6a488035
TO
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 /**
dc195289 289 * add the rules (mainly global rules) for form.
6a488035
TO
290 * All local rules are added near the element
291 *
6a488035
TO
292 * @see valid_date
293 */
00be9182 294 public function addRules() {
be2fb01f 295 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
6a488035
TO
296 }
297
00be9182 298 public function fixFormValues() {
6a488035
TO
299 if (!$this->_force) {
300 return;
301 }
302
303 // set pledge payment related fields
167bcb5f 304 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035 305 if ($status) {
be2fb01f
CW
306 $this->_formValues['pledge_payment_status_id'] = [$status => 1];
307 $this->_defaults['pledge_payment_status_id'] = [$status => 1];
6a488035
TO
308 }
309
167bcb5f 310 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
6a488035
TO
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
167bcb5f 317 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
6a488035
TO
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
167bcb5f 325 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String');
6a488035 326
ab6ba136 327 if ($pledgeStatus) {
328 $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
6a488035
TO
329
330 // we need set all statuses except Cancelled
331 unset($statusValues[$pledgeStatus]);
332
be2fb01f 333 $statuses = [];
6a488035
TO
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
1273d77c 342 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
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
1273d77c 348 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
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
6a488035
TO
369 /**
370 * Return a descriptive name for the page, used in wizard header
371 *
372 * @return string
6a488035
TO
373 */
374 public function getTitle() {
375 return ts('Find Pledges');
376 }
96025800 377
18db97df 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
6a488035 388}