Merge pull request #21876 from mariav0/patch-14
[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->_actionButtonName = $this->getButtonName('next', 'action');
63
64 $this->_done = FALSE;
65
66 parent::preProcess();
67
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),
86 $this->getSortID(),
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 /**
99 * Build the form object.
100 */
101 public function buildQuickForm() {
102 parent::buildQuickForm();
103 $this->addContactSearchFields();
104
105 CRM_Pledge_BAO_Query::buildSearchForm($this);
106
107 $rows = $this->get('rows');
108 if (is_array($rows)) {
109 if (!$this->_single) {
110 $this->addRowSelectors($rows);
111 }
112
113 $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
114 }
115
116 }
117
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
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
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.
181 */
182 public function postProcess() {
183 if ($this->_done) {
184 return;
185 }
186
187 $this->_done = TRUE;
188
189 $this->setFormValues();
190
191 $this->fixFormValues();
192
193 // We don't show test records in summaries or dashboards
194 if (empty($this->_formValues['pledge_test']) && $this->_force) {
195 $this->_formValues["pledge_test"] = 0;
196 }
197
198 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
199 if (isset($this->_formValues[$f])) {
200 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
201 }
202 }
203
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
209 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
210
211 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
212
213 $this->set('queryParams', $this->_queryParams);
214
215 $buttonName = $this->controller->getButtonName();
216 if ($buttonName == $this->_actionButtonName) {
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
220 $stateMachine = $this->controller->getStateMachine();
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller->resetPage($formName);
223 return;
224 }
225
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),
247 $this->getSortID(),
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 /**
263 * add the rules (mainly global rules) for form.
264 * All local rules are added near the element
265 *
266 * @see valid_date
267 */
268 public function addRules() {
269 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
270 }
271
272 public function fixFormValues() {
273 if (!$this->_force) {
274 return;
275 }
276
277 // set pledge payment related fields
278 $status = CRM_Utils_Request::retrieve('status', 'String');
279 if ($status) {
280 $this->_formValues['pledge_payment_status_id'] = [$status => 1];
281 $this->_defaults['pledge_payment_status_id'] = [$status => 1];
282 }
283
284 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
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
291 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
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
299 $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String');
300
301 if ($pledgeStatus) {
302 $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
303
304 // we need set all statuses except Cancelled
305 unset($statusValues[$pledgeStatus]);
306
307 $statuses = [];
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
316 $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
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
322 $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date');
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
343 /**
344 * Return a descriptive name for the page, used in wizard header
345 *
346 * @return string
347 */
348 public function getTitle() {
349 return ts('Find Pledges');
350 }
351
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
362 }