3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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', CRM_Core_DAO
::$_nullObject);
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', 'String', $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->addSortNameField();
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 $permission = CRM_Core_Permission
::getPermission();
160 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles($permission));
166 * Get the label for the sortName field if email searching is on.
168 * (email searching is a setting under search preferences).
172 protected function getSortNameLabelWithEmail() {
173 return ts('Pledger Name or Email');
177 * Get the label for the sortName field if email searching is off.
179 * (email searching is a setting under search preferences).
183 protected function getSortNameLabelWithOutEmail() {
184 return ts('Pledger Name');
188 * The post processing of the form gets done here.
190 * Key things done during post processing are
191 * - check for reset or next request. if present, skip post procesing.
192 * - now check if user requested running a saved search, if so, then
193 * the form values associated with the saved search are used for searching.
194 * - if user has done a submit with new values the regular post submissing is
196 * The processing consists of using a Selector / Controller framework for getting the
199 public function postProcess() {
206 $this->_formValues
= $this->controller
->exportValues($this->_name
);
208 $this->fixFormValues();
210 // We don't show test records in summaries or dashboards
211 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
212 $this->_formValues
["pledge_test"] = 0;
215 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
216 if (isset($this->_formValues
[$f])) {
217 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
221 if (isset($this->_ssID
) && empty($_POST)) {
222 // if we are editing / running a saved search and the form has not been posted
223 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
226 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
228 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
230 $this->set('formValues', $this->_formValues
);
231 $this->set('queryParams', $this->_queryParams
);
233 $buttonName = $this->controller
->getButtonName();
234 if ($buttonName == $this->_actionButtonName
) {
235 // check actionName and if next, then do not repeat a search, since we are going to the next page
237 // hack, make sure we reset the task values
238 $stateMachine = $this->controller
->getStateMachine();
239 $formName = $stateMachine->getTaskFormName();
240 $this->controller
->resetPage($formName);
245 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
246 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
247 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
251 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
253 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
260 $selector->setKey($this->controller
->_key
);
263 if ($this->_context
== 'user') {
264 $prefix = $this->_prefix
;
267 $this->assign("{$prefix}limit", $this->_limit
);
268 $this->assign("{$prefix}single", $this->_single
);
270 $controller = new CRM_Core_Selector_Controller($selector,
271 $this->get(CRM_Utils_Pager
::PAGE_ID
),
273 CRM_Core_Action
::VIEW
,
275 CRM_Core_Selector_Controller
::SESSION
,
278 $controller->setEmbedded(TRUE);
280 $query = &$selector->getQuery();
281 if ($this->_context
== 'user') {
282 $query->setSkipPermission(TRUE);
288 * add the rules (mainly global rules) for form.
289 * All local rules are added near the element
293 public function addRules() {
294 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
298 * Global validation rules for the form.
300 * @param array $fields
301 * Posted values of the form.
305 public static function formRule($fields) {
308 if (!empty($errors)) {
316 * Set the default form values.
320 * the default array reference
322 public function setDefaultValues() {
324 $defaults = $this->_formValues
;
328 public function fixFormValues() {
329 if (!$this->_force
) {
333 // set pledge payment related fields
334 $status = CRM_Utils_Request
::retrieve('status', 'String',
335 CRM_Core_DAO
::$_nullObject
338 $this->_formValues
['pledge_payment_status_id'] = array($status => 1);
339 $this->_defaults
['pledge_payment_status_id'] = array($status => 1);
342 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date',
343 CRM_Core_DAO
::$_nullObject
346 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
347 $this->_formValues
['pledge_payment_date_low'] = $date;
348 $this->_defaults
['pledge_payment_date_low'] = $date;
351 $toDate = CRM_Utils_Request
::retrieve('end', 'Date',
352 CRM_Core_DAO
::$_nullObject
355 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
356 $this->_formValues
['pledge_payment_date_high'] = $date;
357 $this->_defaults
['pledge_payment_date_high'] = $date;
360 // set pledge related fields
361 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String',
362 CRM_Core_DAO
::$_nullObject
365 $statusValues = CRM_Contribute_PseudoConstant
::contributionStatus();
367 // Remove status values that are only used for recurring contributions for now (Failed).
368 unset($statusValues['4']);
370 // we need set all statuses except Cancelled
371 unset($statusValues[$pledgeStatus]);
374 foreach ($statusValues as $statusId => $value) {
375 $statuses[$statusId] = 1;
378 $this->_formValues
['pledge_status_id'] = $statuses;
379 $this->_defaults
['pledge_status_id'] = $statuses;
382 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
383 CRM_Core_DAO
::$_nullObject
385 if ($pledgeFromDate) {
386 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
387 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
390 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
391 CRM_Core_DAO
::$_nullObject
394 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
395 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
398 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
400 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
402 $this->_formValues
['contact_id'] = $cid;
403 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
404 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
407 // also assign individual mode to the template
408 $this->_single
= TRUE;
416 public function getFormValues() {
421 * Return a descriptive name for the page, used in wizard header
425 public function getTitle() {
426 return ts('Find Pledges');