3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This file is for Pledge search
37 class CRM_Pledge_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
64 protected $_prefix = "pledge_";
67 * Processing needed for buildForm and later.
69 public function preProcess() {
71 $this->_searchButtonName
= $this->getButtonName('refresh');
72 $this->_actionButtonName
= $this->getButtonName('next', 'action');
76 $this->loadStandardSearchOptionsFromUrl();
78 // get user submitted values
79 // get it from controller only if form has been submitted, else preProcess has set this
80 if (!empty($_POST) && !$this->controller
->isModal()) {
81 $this->_formValues
= $this->controller
->exportValues($this->_name
);
84 $this->_formValues
= $this->get('formValues');
87 if (empty($this->_formValues
)) {
88 if (isset($this->_ssID
)) {
89 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
95 $this->set('force', 0);
99 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
100 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
101 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
105 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
106 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
114 if ($this->_context
== 'user') {
115 $prefix = $this->_prefix
;
118 $this->assign("{$prefix}limit", $this->_limit
);
119 $this->assign("{$prefix}single", $this->_single
);
121 $controller = new CRM_Core_Selector_Controller($selector,
122 $this->get(CRM_Utils_Pager
::PAGE_ID
),
124 CRM_Core_Action
::VIEW
,
126 CRM_Core_Selector_Controller
::TRANSFER
,
129 $controller->setEmbedded(TRUE);
130 $controller->moveFromSessionToTemplate();
132 $this->assign('summary', $this->get('summary'));
136 * Build the form object.
138 public function buildQuickForm() {
139 parent
::buildQuickForm();
140 $this->addContactSearchFields();
142 CRM_Pledge_BAO_Query
::buildSearchForm($this);
144 $rows = $this->get('rows');
145 if (is_array($rows)) {
146 if (!$this->_single
) {
147 $this->addRowSelectors($rows);
150 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
156 * Get the label for the sortName field if email searching is on.
158 * (email searching is a setting under search preferences).
162 protected function getSortNameLabelWithEmail() {
163 return ts('Pledger Name or Email');
167 * Get the label for the sortName field if email searching is off.
169 * (email searching is a setting under search preferences).
173 protected function getSortNameLabelWithOutEmail() {
174 return ts('Pledger Name');
178 * Get the label for the tag field.
180 * We do this in a function so the 'ts' wraps the whole string to allow
181 * better translation.
185 protected function getTagLabel() {
186 return ts('Pledger Tag(s)');
190 * Get the label for the group field.
194 protected function getGroupLabel() {
195 return ts('Pledger Group(s)');
199 * Get the label for the group field.
203 protected function getContactTypeLabel() {
204 return ts('Pledger Contact Type');
208 * The post processing of the form gets done here.
210 * Key things done during post processing are
211 * - check for reset or next request. if present, skip post procesing.
212 * - now check if user requested running a saved search, if so, then
213 * the form values associated with the saved search are used for searching.
214 * - if user has done a submit with new values the regular post submissing is
216 * The processing consists of using a Selector / Controller framework for getting the
219 public function postProcess() {
226 $this->_formValues
= $this->controller
->exportValues($this->_name
);
228 $this->fixFormValues();
230 // We don't show test records in summaries or dashboards
231 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
232 $this->_formValues
["pledge_test"] = 0;
235 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
236 if (isset($this->_formValues
[$f])) {
237 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
241 if (isset($this->_ssID
) && empty($_POST)) {
242 // if we are editing / running a saved search and the form has not been posted
243 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
246 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
248 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
250 $this->set('formValues', $this->_formValues
);
251 $this->set('queryParams', $this->_queryParams
);
253 $buttonName = $this->controller
->getButtonName();
254 if ($buttonName == $this->_actionButtonName
) {
255 // check actionName and if next, then do not repeat a search, since we are going to the next page
257 // hack, make sure we reset the task values
258 $stateMachine = $this->controller
->getStateMachine();
259 $formName = $stateMachine->getTaskFormName();
260 $this->controller
->resetPage($formName);
265 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
266 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
267 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
271 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
273 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
280 $selector->setKey($this->controller
->_key
);
283 if ($this->_context
== 'user') {
284 $prefix = $this->_prefix
;
287 $this->assign("{$prefix}limit", $this->_limit
);
288 $this->assign("{$prefix}single", $this->_single
);
290 $controller = new CRM_Core_Selector_Controller($selector,
291 $this->get(CRM_Utils_Pager
::PAGE_ID
),
293 CRM_Core_Action
::VIEW
,
295 CRM_Core_Selector_Controller
::SESSION
,
298 $controller->setEmbedded(TRUE);
300 $query = &$selector->getQuery();
301 if ($this->_context
== 'user') {
302 $query->setSkipPermission(TRUE);
308 * add the rules (mainly global rules) for form.
309 * All local rules are added near the element
313 public function addRules() {
314 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
318 * Set the default form values.
322 * the default array reference
324 public function setDefaultValues() {
326 $defaults = $this->_formValues
;
330 public function fixFormValues() {
331 if (!$this->_force
) {
335 // set pledge payment related fields
336 $status = CRM_Utils_Request
::retrieve('status', 'String');
338 $this->_formValues
['pledge_payment_status_id'] = [$status => 1];
339 $this->_defaults
['pledge_payment_status_id'] = [$status => 1];
342 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
344 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
345 $this->_formValues
['pledge_payment_date_low'] = $date;
346 $this->_defaults
['pledge_payment_date_low'] = $date;
349 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
351 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
352 $this->_formValues
['pledge_payment_date_high'] = $date;
353 $this->_defaults
['pledge_payment_date_high'] = $date;
356 // set pledge related fields
357 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String');
360 $statusValues = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
362 // we need set all statuses except Cancelled
363 unset($statusValues[$pledgeStatus]);
366 foreach ($statusValues as $statusId => $value) {
367 $statuses[$statusId] = 1;
370 $this->_formValues
['pledge_status_id'] = $statuses;
371 $this->_defaults
['pledge_status_id'] = $statuses;
374 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date');
375 if ($pledgeFromDate) {
376 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
377 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
380 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
382 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
383 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
386 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
388 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
390 $this->_formValues
['contact_id'] = $cid;
391 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
392 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
395 // also assign individual mode to the template
396 $this->_single
= TRUE;
404 public function getFormValues() {
409 * Return a descriptive name for the page, used in wizard header
413 public function getTitle() {
414 return ts('Find Pledges');