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->_actionButtonName
= $this->getButtonName('next', 'action');
68 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
69 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
77 if ($this->_context
== 'user') {
78 $prefix = $this->_prefix
;
81 $this->assign("{$prefix}limit", $this->_limit
);
82 $this->assign("{$prefix}single", $this->_single
);
84 $controller = new CRM_Core_Selector_Controller($selector,
85 $this->get(CRM_Utils_Pager
::PAGE_ID
),
87 CRM_Core_Action
::VIEW
,
89 CRM_Core_Selector_Controller
::TRANSFER
,
92 $controller->setEmbedded(TRUE);
93 $controller->moveFromSessionToTemplate();
95 $this->assign('summary', $this->get('summary'));
99 * Build the form object.
101 public function buildQuickForm() {
102 parent
::buildQuickForm();
103 $this->addContactSearchFields();
105 CRM_Pledge_BAO_Query
::buildSearchForm($this);
107 $rows = $this->get('rows');
108 if (is_array($rows)) {
109 if (!$this->_single
) {
110 $this->addRowSelectors($rows);
113 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
119 * Get the label for the sortName field if email searching is on.
121 * (email searching is a setting under search preferences).
125 protected function getSortNameLabelWithEmail() {
126 return ts('Pledger Name or Email');
130 * Get the label for the sortName field if email searching is off.
132 * (email searching is a setting under search preferences).
136 protected function getSortNameLabelWithOutEmail() {
137 return ts('Pledger Name');
141 * Get the label for the tag field.
143 * We do this in a function so the 'ts' wraps the whole string to allow
144 * better translation.
148 protected function getTagLabel() {
149 return ts('Pledger Tag(s)');
153 * Get the label for the group field.
157 protected function getGroupLabel() {
158 return ts('Pledger Group(s)');
162 * Get the label for the group field.
166 protected function getContactTypeLabel() {
167 return ts('Pledger Contact Type');
171 * The post processing of the form gets done here.
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
179 * The processing consists of using a Selector / Controller framework for getting the
182 public function postProcess() {
189 $this->setFormValues();
191 $this->fixFormValues();
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;
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]);
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
);
209 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
211 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
213 $this->set('queryParams', $this->_queryParams
);
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
219 // hack, make sure we reset the task values
220 $stateMachine = $this->controller
->getStateMachine();
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller
->resetPage($formName);
226 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
228 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
235 $selector->setKey($this->controller
->_key
);
238 if ($this->_context
== 'user') {
239 $prefix = $this->_prefix
;
242 $this->assign("{$prefix}limit", $this->_limit
);
243 $this->assign("{$prefix}single", $this->_single
);
245 $controller = new CRM_Core_Selector_Controller($selector,
246 $this->get(CRM_Utils_Pager
::PAGE_ID
),
248 CRM_Core_Action
::VIEW
,
250 CRM_Core_Selector_Controller
::SESSION
,
253 $controller->setEmbedded(TRUE);
255 $query = &$selector->getQuery();
256 if ($this->_context
== 'user') {
257 $query->setSkipPermission(TRUE);
263 * add the rules (mainly global rules) for form.
264 * All local rules are added near the element
268 public function addRules() {
269 $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']);
272 public function fixFormValues() {
273 if (!$this->_force
) {
277 // set pledge payment related fields
278 $status = CRM_Utils_Request
::retrieve('status', 'String');
280 $this->_formValues
['pledge_payment_status_id'] = [$status => 1];
281 $this->_defaults
['pledge_payment_status_id'] = [$status => 1];
284 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
286 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
287 $this->_formValues
['pledge_payment_date_low'] = $date;
288 $this->_defaults
['pledge_payment_date_low'] = $date;
291 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
293 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
294 $this->_formValues
['pledge_payment_date_high'] = $date;
295 $this->_defaults
['pledge_payment_date_high'] = $date;
298 // set pledge related fields
299 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String');
302 $statusValues = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
304 // we need set all statuses except Cancelled
305 unset($statusValues[$pledgeStatus]);
308 foreach ($statusValues as $statusId => $value) {
309 $statuses[$statusId] = 1;
312 $this->_formValues
['pledge_status_id'] = $statuses;
313 $this->_defaults
['pledge_status_id'] = $statuses;
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;
322 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
324 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
325 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
328 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
330 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
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,
337 // also assign individual mode to the template
338 $this->_single
= TRUE;
344 * Return a descriptive name for the page, used in wizard header
348 public function getTitle() {
349 return ts('Find Pledges');
353 * Set the metadata for the form.
355 * @throws \CiviCRM_API3_Exception
357 protected function setSearchMetadata() {
358 $this->addSearchFieldMetadata(['Pledge' => CRM_Pledge_BAO_Query
::getSearchFieldMetadata()]);
359 $this->addSearchFieldMetadata(['PledgePayment' => CRM_Pledge_BAO_Query
::getPledgePaymentSearchFieldMetadata()]);