3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is for Pledge search
21 class CRM_Pledge_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
33 public function getDefaultEntity() {
38 * Are we restricting ourselves to a single contact.
42 protected $_single = FALSE;
45 * Are we restricting ourselves to a single contact.
49 protected $_limit = NULL;
52 * Prefix for the controller.
55 protected $_prefix = "pledge_";
58 * Processing needed for buildForm and later.
60 public function preProcess() {
62 $this->_searchButtonName
= $this->getButtonName('refresh');
63 $this->_actionButtonName
= $this->getButtonName('next', 'action');
67 $this->loadStandardSearchOptionsFromUrl();
69 // get user submitted values
70 // get it from controller only if form has been submitted, else preProcess has set this
71 if (!empty($_POST) && !$this->controller
->isModal()) {
72 $this->_formValues
= $this->controller
->exportValues($this->_name
);
75 $this->_formValues
= $this->get('formValues');
78 if (empty($this->_formValues
)) {
79 if (isset($this->_ssID
)) {
80 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
85 // pledge related dates
86 $this->addSearchFieldMetadata(['Pledge' => CRM_Pledge_BAO_Query
::getSearchFieldMetadata()]);
87 $this->addSearchFieldMetadata(['PledgePayment' => CRM_Pledge_BAO_Query
::getPledgePaymentSearchFieldMetadata()]);
88 $this->addFormFieldsFromMetadata();
90 $this->set('force', 0);
93 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
94 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
102 if ($this->_context
== 'user') {
103 $prefix = $this->_prefix
;
106 $this->assign("{$prefix}limit", $this->_limit
);
107 $this->assign("{$prefix}single", $this->_single
);
109 $controller = new CRM_Core_Selector_Controller($selector,
110 $this->get(CRM_Utils_Pager
::PAGE_ID
),
112 CRM_Core_Action
::VIEW
,
114 CRM_Core_Selector_Controller
::TRANSFER
,
117 $controller->setEmbedded(TRUE);
118 $controller->moveFromSessionToTemplate();
120 $this->assign('summary', $this->get('summary'));
124 * Build the form object.
126 public function buildQuickForm() {
127 parent
::buildQuickForm();
128 $this->addContactSearchFields();
130 CRM_Pledge_BAO_Query
::buildSearchForm($this);
132 $rows = $this->get('rows');
133 if (is_array($rows)) {
134 if (!$this->_single
) {
135 $this->addRowSelectors($rows);
138 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
144 * Get the label for the sortName field if email searching is on.
146 * (email searching is a setting under search preferences).
150 protected function getSortNameLabelWithEmail() {
151 return ts('Pledger Name or Email');
155 * Get the label for the sortName field if email searching is off.
157 * (email searching is a setting under search preferences).
161 protected function getSortNameLabelWithOutEmail() {
162 return ts('Pledger Name');
166 * Get the label for the tag field.
168 * We do this in a function so the 'ts' wraps the whole string to allow
169 * better translation.
173 protected function getTagLabel() {
174 return ts('Pledger Tag(s)');
178 * Get the label for the group field.
182 protected function getGroupLabel() {
183 return ts('Pledger Group(s)');
187 * Get the label for the group field.
191 protected function getContactTypeLabel() {
192 return ts('Pledger Contact Type');
196 * The post processing of the form gets done here.
198 * Key things done during post processing are
199 * - check for reset or next request. if present, skip post procesing.
200 * - now check if user requested running a saved search, if so, then
201 * the form values associated with the saved search are used for searching.
202 * - if user has done a submit with new values the regular post submissing is
204 * The processing consists of using a Selector / Controller framework for getting the
207 public function postProcess() {
214 $this->setFormValues();
216 $this->fixFormValues();
218 // We don't show test records in summaries or dashboards
219 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
220 $this->_formValues
["pledge_test"] = 0;
223 foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) {
224 if (isset($this->_formValues
[$f])) {
225 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
229 if (isset($this->_ssID
) && empty($_POST)) {
230 // if we are editing / running a saved search and the form has not been posted
231 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
234 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
236 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
238 $this->set('formValues', $this->_formValues
);
239 $this->set('queryParams', $this->_queryParams
);
241 $buttonName = $this->controller
->getButtonName();
242 if ($buttonName == $this->_actionButtonName
) {
243 // check actionName and if next, then do not repeat a search, since we are going to the next page
245 // hack, make sure we reset the task values
246 $stateMachine = $this->controller
->getStateMachine();
247 $formName = $stateMachine->getTaskFormName();
248 $this->controller
->resetPage($formName);
252 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
254 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
261 $selector->setKey($this->controller
->_key
);
264 if ($this->_context
== 'user') {
265 $prefix = $this->_prefix
;
268 $this->assign("{$prefix}limit", $this->_limit
);
269 $this->assign("{$prefix}single", $this->_single
);
271 $controller = new CRM_Core_Selector_Controller($selector,
272 $this->get(CRM_Utils_Pager
::PAGE_ID
),
274 CRM_Core_Action
::VIEW
,
276 CRM_Core_Selector_Controller
::SESSION
,
279 $controller->setEmbedded(TRUE);
281 $query = &$selector->getQuery();
282 if ($this->_context
== 'user') {
283 $query->setSkipPermission(TRUE);
289 * add the rules (mainly global rules) for form.
290 * All local rules are added near the element
294 public function addRules() {
295 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
298 public function fixFormValues() {
299 if (!$this->_force
) {
303 // set pledge payment related fields
304 $status = CRM_Utils_Request
::retrieve('status', 'String');
306 $this->_formValues
['pledge_payment_status_id'] = [$status => 1];
307 $this->_defaults
['pledge_payment_status_id'] = [$status => 1];
310 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
312 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
313 $this->_formValues
['pledge_payment_date_low'] = $date;
314 $this->_defaults
['pledge_payment_date_low'] = $date;
317 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
319 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
320 $this->_formValues
['pledge_payment_date_high'] = $date;
321 $this->_defaults
['pledge_payment_date_high'] = $date;
324 // set pledge related fields
325 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String');
328 $statusValues = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
330 // we need set all statuses except Cancelled
331 unset($statusValues[$pledgeStatus]);
334 foreach ($statusValues as $statusId => $value) {
335 $statuses[$statusId] = 1;
338 $this->_formValues
['pledge_status_id'] = $statuses;
339 $this->_defaults
['pledge_status_id'] = $statuses;
342 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date');
343 if ($pledgeFromDate) {
344 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
345 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
348 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
350 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
351 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
354 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
356 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
358 $this->_formValues
['contact_id'] = $cid;
359 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
360 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
363 // also assign individual mode to the template
364 $this->_single
= TRUE;
370 * Return a descriptive name for the page, used in wizard header
374 public function getTitle() {
375 return ts('Find Pledges');
379 * Set the metadata for the form.
381 * @throws \CiviCRM_API3_Exception
383 protected function setSearchMetadata() {
384 $this->addSearchFieldMetadata(['Pledge' => CRM_Pledge_BAO_Query
::getSearchFieldMetadata()]);
385 $this->addSearchFieldMetadata(['PledgePayment' => CRM_Pledge_BAO_Query
::getPledgePaymentSearchFieldMetadata()]);