3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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.
63 protected $_prefix = "pledge_";
66 * Processing needed for buildForm and later.
68 public function preProcess() {
70 $this->_searchButtonName
= $this->getButtonName('refresh');
71 $this->_actionButtonName
= $this->getButtonName('next', 'action');
74 $this->defaults
= array();
76 // we allow the controller to set force/reset externally, useful when we are being
77 // driven by the wizard framework
79 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean');
80 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
81 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
82 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this, FALSE, 'search');
84 $this->assign("context", $this->_context
);
86 // get user submitted values
87 // get it from controller only if form has been submitted, else preProcess has set this
88 if (!empty($_POST) && !$this->controller
->isModal()) {
89 $this->_formValues
= $this->controller
->exportValues($this->_name
);
92 $this->_formValues
= $this->get('formValues');
95 if (empty($this->_formValues
)) {
96 if (isset($this->_ssID
)) {
97 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
102 $this->postProcess();
103 $this->set('force', 0);
107 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
108 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
109 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
113 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
114 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
122 if ($this->_context
== 'user') {
123 $prefix = $this->_prefix
;
126 $this->assign("{$prefix}limit", $this->_limit
);
127 $this->assign("{$prefix}single", $this->_single
);
129 $controller = new CRM_Core_Selector_Controller($selector,
130 $this->get(CRM_Utils_Pager
::PAGE_ID
),
132 CRM_Core_Action
::VIEW
,
134 CRM_Core_Selector_Controller
::TRANSFER
,
137 $controller->setEmbedded(TRUE);
138 $controller->moveFromSessionToTemplate();
140 $this->assign('summary', $this->get('summary'));
144 * Build the form object.
146 public function buildQuickForm() {
147 parent
::buildQuickForm();
148 $this->addContactSearchFields();
150 CRM_Pledge_BAO_Query
::buildSearchForm($this);
152 $rows = $this->get('rows');
153 if (is_array($rows)) {
154 if (!$this->_single
) {
155 $this->addRowSelectors($rows);
158 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
164 * Get the label for the sortName field if email searching is on.
166 * (email searching is a setting under search preferences).
170 protected function getSortNameLabelWithEmail() {
171 return ts('Pledger Name or Email');
175 * Get the label for the sortName field if email searching is off.
177 * (email searching is a setting under search preferences).
181 protected function getSortNameLabelWithOutEmail() {
182 return ts('Pledger Name');
186 * Get the label for the tag field.
188 * We do this in a function so the 'ts' wraps the whole string to allow
189 * better translation.
193 protected function getTagLabel() {
194 return ts('Pledger Tag(s)');
198 * Get the label for the group field.
202 protected function getGroupLabel() {
203 return ts('Pledger Group(s)');
207 * Get the label for the group field.
211 protected function getContactTypeLabel() {
212 return ts('Pledger Contact Type');
216 * The post processing of the form gets done here.
218 * Key things done during post processing are
219 * - check for reset or next request. if present, skip post procesing.
220 * - now check if user requested running a saved search, if so, then
221 * the form values associated with the saved search are used for searching.
222 * - if user has done a submit with new values the regular post submissing is
224 * The processing consists of using a Selector / Controller framework for getting the
227 public function postProcess() {
234 $this->_formValues
= $this->controller
->exportValues($this->_name
);
236 $this->fixFormValues();
238 // We don't show test records in summaries or dashboards
239 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
240 $this->_formValues
["pledge_test"] = 0;
243 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
244 if (isset($this->_formValues
[$f])) {
245 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
249 if (isset($this->_ssID
) && empty($_POST)) {
250 // if we are editing / running a saved search and the form has not been posted
251 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
254 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
256 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
258 $this->set('formValues', $this->_formValues
);
259 $this->set('queryParams', $this->_queryParams
);
261 $buttonName = $this->controller
->getButtonName();
262 if ($buttonName == $this->_actionButtonName
) {
263 // check actionName and if next, then do not repeat a search, since we are going to the next page
265 // hack, make sure we reset the task values
266 $stateMachine = $this->controller
->getStateMachine();
267 $formName = $stateMachine->getTaskFormName();
268 $this->controller
->resetPage($formName);
273 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
274 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
275 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
279 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
281 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
288 $selector->setKey($this->controller
->_key
);
291 if ($this->_context
== 'user') {
292 $prefix = $this->_prefix
;
295 $this->assign("{$prefix}limit", $this->_limit
);
296 $this->assign("{$prefix}single", $this->_single
);
298 $controller = new CRM_Core_Selector_Controller($selector,
299 $this->get(CRM_Utils_Pager
::PAGE_ID
),
301 CRM_Core_Action
::VIEW
,
303 CRM_Core_Selector_Controller
::SESSION
,
306 $controller->setEmbedded(TRUE);
308 $query = &$selector->getQuery();
309 if ($this->_context
== 'user') {
310 $query->setSkipPermission(TRUE);
316 * add the rules (mainly global rules) for form.
317 * All local rules are added near the element
321 public function addRules() {
322 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
326 * Global validation rules for the form.
328 * @param array $fields
329 * Posted values of the form.
333 public static function formRule($fields) {
336 if (!empty($errors)) {
344 * Set the default form values.
348 * the default array reference
350 public function setDefaultValues() {
352 $defaults = $this->_formValues
;
356 public function fixFormValues() {
357 if (!$this->_force
) {
361 // set pledge payment related fields
362 $status = CRM_Utils_Request
::retrieve('status', 'String');
364 $this->_formValues
['pledge_payment_status_id'] = array($status => 1);
365 $this->_defaults
['pledge_payment_status_id'] = array($status => 1);
368 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
370 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
371 $this->_formValues
['pledge_payment_date_low'] = $date;
372 $this->_defaults
['pledge_payment_date_low'] = $date;
375 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
377 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
378 $this->_formValues
['pledge_payment_date_high'] = $date;
379 $this->_defaults
['pledge_payment_date_high'] = $date;
382 // set pledge related fields
383 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String');
386 $statusValues = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
388 // we need set all statuses except Cancelled
389 unset($statusValues[$pledgeStatus]);
392 foreach ($statusValues as $statusId => $value) {
393 $statuses[$statusId] = 1;
396 $this->_formValues
['pledge_status_id'] = $statuses;
397 $this->_defaults
['pledge_status_id'] = $statuses;
400 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date');
401 if ($pledgeFromDate) {
402 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
403 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
406 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
408 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
409 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
412 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
414 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
416 $this->_formValues
['contact_id'] = $cid;
417 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
418 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
421 // also assign individual mode to the template
422 $this->_single
= TRUE;
430 public function getFormValues() {
435 * Return a descriptive name for the page, used in wizard header
439 public function getTitle() {
440 return ts('Find Pledges');