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