Commit | Line | Data |
---|---|---|
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 | 37 | class 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) { | |
101 | $this->postProcess(); | |
102 | $this->set('force', 0); | |
103 | } | |
104 | ||
105 | $sortID = NULL; | |
106 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
107 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
108 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
109 | ); | |
110 | } | |
111 | ||
6a488035 TO |
112 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
113 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, | |
114 | $this->_action, | |
115 | NULL, | |
116 | $this->_single, | |
117 | $this->_limit, | |
118 | $this->_context | |
119 | ); | |
120 | $prefix = NULL; | |
121 | if ($this->_context == 'user') { | |
122 | $prefix = $this->_prefix; | |
123 | } | |
124 | ||
125 | $this->assign("{$prefix}limit", $this->_limit); | |
126 | $this->assign("{$prefix}single", $this->_single); | |
127 | ||
128 | $controller = new CRM_Core_Selector_Controller($selector, | |
129 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
130 | $sortID, | |
131 | CRM_Core_Action::VIEW, | |
132 | $this, | |
133 | CRM_Core_Selector_Controller::TRANSFER, | |
134 | $prefix | |
135 | ); | |
136 | $controller->setEmbedded(TRUE); | |
137 | $controller->moveFromSessionToTemplate(); | |
138 | ||
139 | $this->assign('summary', $this->get('summary')); | |
140 | } | |
141 | ||
142 | /** | |
fe482240 | 143 | * Build the form object. |
6a488035 | 144 | */ |
00be9182 | 145 | public function buildQuickForm() { |
3efb5b86 | 146 | parent::buildQuickForm(); |
0573fd28 | 147 | $this->addContactSearchFields(); |
6a488035 TO |
148 | |
149 | CRM_Pledge_BAO_Query::buildSearchForm($this); | |
150 | ||
6a488035 TO |
151 | $rows = $this->get('rows'); |
152 | if (is_array($rows)) { | |
6a488035 | 153 | if (!$this->_single) { |
8d36b801 | 154 | $this->addRowSelectors($rows); |
6a488035 TO |
155 | } |
156 | ||
56c77e1c | 157 | $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
6a488035 TO |
158 | } |
159 | ||
6a488035 TO |
160 | } |
161 | ||
e597fc33 DG |
162 | /** |
163 | * Get the label for the sortName field if email searching is on. | |
164 | * | |
165 | * (email searching is a setting under search preferences). | |
166 | * | |
167 | * @return string | |
168 | */ | |
169 | protected function getSortNameLabelWithEmail() { | |
170 | return ts('Pledger Name or Email'); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Get the label for the sortName field if email searching is off. | |
175 | * | |
176 | * (email searching is a setting under search preferences). | |
177 | * | |
178 | * @return string | |
179 | */ | |
180 | protected function getSortNameLabelWithOutEmail() { | |
181 | return ts('Pledger Name'); | |
182 | } | |
183 | ||
0573fd28 | 184 | /** |
185 | * Get the label for the tag field. | |
186 | * | |
187 | * We do this in a function so the 'ts' wraps the whole string to allow | |
188 | * better translation. | |
189 | * | |
190 | * @return string | |
191 | */ | |
192 | protected function getTagLabel() { | |
193 | return ts('Pledger Tag(s)'); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get the label for the group field. | |
198 | * | |
199 | * @return string | |
200 | */ | |
201 | protected function getGroupLabel() { | |
202 | return ts('Pledger Group(s)'); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Get the label for the group field. | |
207 | * | |
208 | * @return string | |
209 | */ | |
210 | protected function getContactTypeLabel() { | |
211 | return ts('Pledger Contact Type'); | |
212 | } | |
213 | ||
6a488035 TO |
214 | /** |
215 | * The post processing of the form gets done here. | |
216 | * | |
217 | * Key things done during post processing are | |
218 | * - check for reset or next request. if present, skip post procesing. | |
219 | * - now check if user requested running a saved search, if so, then | |
220 | * the form values associated with the saved search are used for searching. | |
221 | * - if user has done a submit with new values the regular post submissing is | |
222 | * done. | |
223 | * The processing consists of using a Selector / Controller framework for getting the | |
224 | * search results. | |
6a488035 | 225 | */ |
00be9182 | 226 | public function postProcess() { |
6a488035 TO |
227 | if ($this->_done) { |
228 | return; | |
229 | } | |
230 | ||
231 | $this->_done = TRUE; | |
232 | ||
233 | $this->_formValues = $this->controller->exportValues($this->_name); | |
234 | ||
235 | $this->fixFormValues(); | |
236 | ||
d43b88cc CW |
237 | // We don't show test records in summaries or dashboards |
238 | if (empty($this->_formValues['pledge_test']) && $this->_force) { | |
6a488035 TO |
239 | $this->_formValues["pledge_test"] = 0; |
240 | } | |
241 | ||
be2fb01f | 242 | foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) { |
3f48e02b PJ |
243 | if (isset($this->_formValues[$f])) { |
244 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); | |
245 | } | |
246 | } | |
247 | ||
6a488035 TO |
248 | if (isset($this->_ssID) && empty($_POST)) { |
249 | // if we are editing / running a saved search and the form has not been posted | |
250 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
251 | } | |
252 | ||
c94d39fd | 253 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 TO |
254 | |
255 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
256 | ||
257 | $this->set('formValues', $this->_formValues); | |
258 | $this->set('queryParams', $this->_queryParams); | |
259 | ||
260 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 261 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
262 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
263 | ||
264 | // hack, make sure we reset the task values | |
95ea77ec | 265 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
266 | $formName = $stateMachine->getTaskFormName(); |
267 | $this->controller->resetPage($formName); | |
268 | return; | |
269 | } | |
270 | ||
271 | $sortID = NULL; | |
272 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
273 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
274 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
275 | ); | |
276 | } | |
277 | ||
278 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
279 | ||
280 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, | |
281 | $this->_action, | |
282 | NULL, | |
283 | $this->_single, | |
284 | $this->_limit, | |
285 | $this->_context | |
286 | ); | |
287 | $selector->setKey($this->controller->_key); | |
288 | ||
289 | $prefix = NULL; | |
290 | if ($this->_context == 'user') { | |
291 | $prefix = $this->_prefix; | |
292 | } | |
293 | ||
294 | $this->assign("{$prefix}limit", $this->_limit); | |
295 | $this->assign("{$prefix}single", $this->_single); | |
296 | ||
297 | $controller = new CRM_Core_Selector_Controller($selector, | |
298 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
299 | $sortID, | |
300 | CRM_Core_Action::VIEW, | |
301 | $this, | |
302 | CRM_Core_Selector_Controller::SESSION, | |
303 | $prefix | |
304 | ); | |
305 | $controller->setEmbedded(TRUE); | |
306 | ||
307 | $query = &$selector->getQuery(); | |
308 | if ($this->_context == 'user') { | |
309 | $query->setSkipPermission(TRUE); | |
310 | } | |
311 | $controller->run(); | |
312 | } | |
313 | ||
314 | /** | |
dc195289 | 315 | * add the rules (mainly global rules) for form. |
6a488035 TO |
316 | * All local rules are added near the element |
317 | * | |
6a488035 TO |
318 | * @see valid_date |
319 | */ | |
00be9182 | 320 | public function addRules() { |
be2fb01f | 321 | $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']); |
6a488035 TO |
322 | } |
323 | ||
6a488035 | 324 | /** |
fe482240 | 325 | * Set the default form values. |
6a488035 | 326 | * |
6a488035 | 327 | * |
a6c01b45 CW |
328 | * @return array |
329 | * the default array reference | |
6a488035 | 330 | */ |
00be9182 | 331 | public function setDefaultValues() { |
be2fb01f | 332 | $defaults = []; |
6a488035 TO |
333 | $defaults = $this->_formValues; |
334 | return $defaults; | |
335 | } | |
336 | ||
00be9182 | 337 | public function fixFormValues() { |
6a488035 TO |
338 | if (!$this->_force) { |
339 | return; | |
340 | } | |
341 | ||
342 | // set pledge payment related fields | |
167bcb5f | 343 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
6a488035 | 344 | if ($status) { |
be2fb01f CW |
345 | $this->_formValues['pledge_payment_status_id'] = [$status => 1]; |
346 | $this->_defaults['pledge_payment_status_id'] = [$status => 1]; | |
6a488035 TO |
347 | } |
348 | ||
167bcb5f | 349 | $fromDate = CRM_Utils_Request::retrieve('start', 'Date'); |
6a488035 TO |
350 | if ($fromDate) { |
351 | list($date) = CRM_Utils_Date::setDateDefaults($fromDate); | |
352 | $this->_formValues['pledge_payment_date_low'] = $date; | |
353 | $this->_defaults['pledge_payment_date_low'] = $date; | |
354 | } | |
355 | ||
167bcb5f | 356 | $toDate = CRM_Utils_Request::retrieve('end', 'Date'); |
6a488035 TO |
357 | if ($toDate) { |
358 | list($date) = CRM_Utils_Date::setDateDefaults($toDate); | |
359 | $this->_formValues['pledge_payment_date_high'] = $date; | |
360 | $this->_defaults['pledge_payment_date_high'] = $date; | |
361 | } | |
362 | ||
363 | // set pledge related fields | |
167bcb5f | 364 | $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String'); |
6a488035 | 365 | |
ab6ba136 | 366 | if ($pledgeStatus) { |
367 | $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); | |
6a488035 TO |
368 | |
369 | // we need set all statuses except Cancelled | |
370 | unset($statusValues[$pledgeStatus]); | |
371 | ||
be2fb01f | 372 | $statuses = []; |
6a488035 TO |
373 | foreach ($statusValues as $statusId => $value) { |
374 | $statuses[$statusId] = 1; | |
375 | } | |
376 | ||
377 | $this->_formValues['pledge_status_id'] = $statuses; | |
378 | $this->_defaults['pledge_status_id'] = $statuses; | |
379 | } | |
380 | ||
1273d77c | 381 | $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date'); |
6a488035 TO |
382 | if ($pledgeFromDate) { |
383 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate); | |
384 | $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date; | |
385 | } | |
386 | ||
1273d77c | 387 | $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date'); |
6a488035 TO |
388 | if ($pledgeToDate) { |
389 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate); | |
390 | $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date; | |
391 | } | |
392 | ||
393 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
394 | if ($cid) { | |
395 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
396 | if ($cid > 0) { | |
397 | $this->_formValues['contact_id'] = $cid; | |
398 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); | |
399 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, | |
400 | 'sort_name' | |
401 | ); | |
402 | // also assign individual mode to the template | |
403 | $this->_single = TRUE; | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
ffd93213 EM |
408 | /** |
409 | * @return null | |
410 | */ | |
00be9182 | 411 | public function getFormValues() { |
6a488035 TO |
412 | return NULL; |
413 | } | |
414 | ||
415 | /** | |
416 | * Return a descriptive name for the page, used in wizard header | |
417 | * | |
418 | * @return string | |
6a488035 TO |
419 | */ |
420 | public function getTitle() { | |
421 | return ts('Find Pledges'); | |
422 | } | |
96025800 | 423 | |
6a488035 | 424 | } |