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;
49 public function getDefaultEntity() {
54 * Are we restricting ourselves to a single contact.
58 protected $_single = FALSE;
61 * Are we restricting ourselves to a single contact.
65 protected $_limit = NULL;
68 * Prefix for the controller.
71 protected $_prefix = "pledge_";
74 * Processing needed for buildForm and later.
76 public function preProcess() {
78 $this->_searchButtonName
= $this->getButtonName('refresh');
79 $this->_actionButtonName
= $this->getButtonName('next', 'action');
83 $this->loadStandardSearchOptionsFromUrl();
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
);
91 $this->_formValues
= $this->get('formValues');
94 if (empty($this->_formValues
)) {
95 if (isset($this->_ssID
)) {
96 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
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);
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
)
116 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
117 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
125 if ($this->_context
== 'user') {
126 $prefix = $this->_prefix
;
129 $this->assign("{$prefix}limit", $this->_limit
);
130 $this->assign("{$prefix}single", $this->_single
);
132 $controller = new CRM_Core_Selector_Controller($selector,
133 $this->get(CRM_Utils_Pager
::PAGE_ID
),
135 CRM_Core_Action
::VIEW
,
137 CRM_Core_Selector_Controller
::TRANSFER
,
140 $controller->setEmbedded(TRUE);
141 $controller->moveFromSessionToTemplate();
143 $this->assign('summary', $this->get('summary'));
147 * Build the form object.
149 public function buildQuickForm() {
150 parent
::buildQuickForm();
151 $this->addContactSearchFields();
153 CRM_Pledge_BAO_Query
::buildSearchForm($this);
155 $rows = $this->get('rows');
156 if (is_array($rows)) {
157 if (!$this->_single
) {
158 $this->addRowSelectors($rows);
161 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
167 * Get the label for the sortName field if email searching is on.
169 * (email searching is a setting under search preferences).
173 protected function getSortNameLabelWithEmail() {
174 return ts('Pledger Name or Email');
178 * Get the label for the sortName field if email searching is off.
180 * (email searching is a setting under search preferences).
184 protected function getSortNameLabelWithOutEmail() {
185 return ts('Pledger Name');
189 * Get the label for the tag field.
191 * We do this in a function so the 'ts' wraps the whole string to allow
192 * better translation.
196 protected function getTagLabel() {
197 return ts('Pledger Tag(s)');
201 * Get the label for the group field.
205 protected function getGroupLabel() {
206 return ts('Pledger Group(s)');
210 * Get the label for the group field.
214 protected function getContactTypeLabel() {
215 return ts('Pledger Contact Type');
219 * The post processing of the form gets done here.
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
227 * The processing consists of using a Selector / Controller framework for getting the
230 public function postProcess() {
237 $this->setFormValues();
239 $this->fixFormValues();
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;
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]);
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
);
257 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
259 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
261 $this->set('formValues', $this->_formValues
);
262 $this->set('queryParams', $this->_queryParams
);
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
268 // hack, make sure we reset the task values
269 $stateMachine = $this->controller
->getStateMachine();
270 $formName = $stateMachine->getTaskFormName();
271 $this->controller
->resetPage($formName);
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
)
282 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
284 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
291 $selector->setKey($this->controller
->_key
);
294 if ($this->_context
== 'user') {
295 $prefix = $this->_prefix
;
298 $this->assign("{$prefix}limit", $this->_limit
);
299 $this->assign("{$prefix}single", $this->_single
);
301 $controller = new CRM_Core_Selector_Controller($selector,
302 $this->get(CRM_Utils_Pager
::PAGE_ID
),
304 CRM_Core_Action
::VIEW
,
306 CRM_Core_Selector_Controller
::SESSION
,
309 $controller->setEmbedded(TRUE);
311 $query = &$selector->getQuery();
312 if ($this->_context
== 'user') {
313 $query->setSkipPermission(TRUE);
319 * add the rules (mainly global rules) for form.
320 * All local rules are added near the element
324 public function addRules() {
325 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
328 public function fixFormValues() {
329 if (!$this->_force
) {
333 // set pledge payment related fields
334 $status = CRM_Utils_Request
::retrieve('status', 'String');
336 $this->_formValues
['pledge_payment_status_id'] = [$status => 1];
337 $this->_defaults
['pledge_payment_status_id'] = [$status => 1];
340 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
342 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
343 $this->_formValues
['pledge_payment_date_low'] = $date;
344 $this->_defaults
['pledge_payment_date_low'] = $date;
347 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
349 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
350 $this->_formValues
['pledge_payment_date_high'] = $date;
351 $this->_defaults
['pledge_payment_date_high'] = $date;
354 // set pledge related fields
355 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String');
358 $statusValues = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
360 // we need set all statuses except Cancelled
361 unset($statusValues[$pledgeStatus]);
364 foreach ($statusValues as $statusId => $value) {
365 $statuses[$statusId] = 1;
368 $this->_formValues
['pledge_status_id'] = $statuses;
369 $this->_defaults
['pledge_status_id'] = $statuses;
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;
378 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
380 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
381 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
384 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
386 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
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,
393 // also assign individual mode to the template
394 $this->_single
= TRUE;
402 public function getFormValues() {
407 * Return a descriptive name for the page, used in wizard header
411 public function getTitle() {
412 return ts('Find Pledges');