3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This file is for Pledge search
39 class CRM_Pledge_Form_Search
extends CRM_Core_Form_Search
{
42 * The params that are sent to the query.
46 protected $_queryParams;
49 * Are we restricting ourselves to a single contact.
53 protected $_single = FALSE;
56 * Are we restricting ourselves to a single contact.
60 protected $_limit = NULL;
63 * Prefix for the controller.
65 protected $_prefix = "pledge_";
68 * Processing needed for buildForm and later.
70 public function preProcess() {
73 * set the button names
75 $this->_searchButtonName
= $this->getButtonName('refresh');
76 $this->_actionButtonName
= $this->getButtonName('next', 'action');
79 $this->defaults
= array();
82 * we allow the controller to set force/reset externally, useful when we are being
83 * driven by the wizard framework
85 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
86 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
87 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
88 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
90 $this->assign("context", $this->_context
);
92 // get user submitted values
93 // get it from controller only if form has been submitted, else preProcess has set this
94 if (!empty($_POST) && !$this->controller
->isModal()) {
95 $this->_formValues
= $this->controller
->exportValues($this->_name
);
98 $this->_formValues
= $this->get('formValues');
101 if (empty($this->_formValues
)) {
102 if (isset($this->_ssID
)) {
103 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
108 $this->postProcess();
109 $this->set('force', 0);
113 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
114 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
115 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
119 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
120 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
128 if ($this->_context
== 'user') {
129 $prefix = $this->_prefix
;
132 $this->assign("{$prefix}limit", $this->_limit
);
133 $this->assign("{$prefix}single", $this->_single
);
135 $controller = new CRM_Core_Selector_Controller($selector,
136 $this->get(CRM_Utils_Pager
::PAGE_ID
),
138 CRM_Core_Action
::VIEW
,
140 CRM_Core_Selector_Controller
::TRANSFER
,
143 $controller->setEmbedded(TRUE);
144 $controller->moveFromSessionToTemplate();
146 $this->assign('summary', $this->get('summary'));
150 * Build the form object.
155 public function buildQuickForm() {
156 parent
::buildQuickForm();
157 $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
159 CRM_Pledge_BAO_Query
::buildSearchForm($this);
161 $rows = $this->get('rows');
162 if (is_array($rows)) {
163 if (!$this->_single
) {
164 $this->addRowSelectors($rows);
167 $permission = CRM_Core_Permission
::getPermission();
169 $this->addTaskMenu(CRM_Pledge_Task
::permissionedTaskTitles($permission));
175 * The post processing of the form gets done here.
177 * Key things done during post processing are
178 * - check for reset or next request. if present, skip post procesing.
179 * - now check if user requested running a saved search, if so, then
180 * the form values associated with the saved search are used for searching.
181 * - if user has done a submit with new values the regular post submissing is
183 * The processing consists of using a Selector / Controller framework for getting the
190 public function postProcess() {
197 $this->_formValues
= $this->controller
->exportValues($this->_name
);
199 $this->fixFormValues();
201 // We don't show test records in summaries or dashboards
202 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
203 $this->_formValues
["pledge_test"] = 0;
206 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
207 if (isset($this->_formValues
[$f])) {
208 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
212 if (isset($this->_ssID
) && empty($_POST)) {
213 // if we are editing / running a saved search and the form has not been posted
214 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
217 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
219 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
221 $this->set('formValues', $this->_formValues
);
222 $this->set('queryParams', $this->_queryParams
);
224 $buttonName = $this->controller
->getButtonName();
225 if ($buttonName == $this->_actionButtonName
) {
226 // check actionName and if next, then do not repeat a search, since we are going to the next page
228 // hack, make sure we reset the task values
229 $stateMachine = $this->controller
->getStateMachine();
230 $formName = $stateMachine->getTaskFormName();
231 $this->controller
->resetPage($formName);
236 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
237 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
238 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
242 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
244 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
251 $selector->setKey($this->controller
->_key
);
254 if ($this->_context
== 'user') {
255 $prefix = $this->_prefix
;
258 $this->assign("{$prefix}limit", $this->_limit
);
259 $this->assign("{$prefix}single", $this->_single
);
261 $controller = new CRM_Core_Selector_Controller($selector,
262 $this->get(CRM_Utils_Pager
::PAGE_ID
),
264 CRM_Core_Action
::VIEW
,
266 CRM_Core_Selector_Controller
::SESSION
,
269 $controller->setEmbedded(TRUE);
271 $query = &$selector->getQuery();
272 if ($this->_context
== 'user') {
273 $query->setSkipPermission(TRUE);
279 * add the rules (mainly global rules) for form.
280 * All local rules are added near the element
285 public function addRules() {
286 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
290 * Global validation rules for the form.
292 * @param array $fields
293 * Posted values of the form.
297 public static function formRule($fields) {
300 if (!empty($errors)) {
308 * Set the default form values.
312 * the default array reference
314 public function setDefaultValues() {
316 $defaults = $this->_formValues
;
320 public function fixFormValues() {
321 if (!$this->_force
) {
325 // set pledge payment related fields
326 $status = CRM_Utils_Request
::retrieve('status', 'String',
327 CRM_Core_DAO
::$_nullObject
330 $this->_formValues
['pledge_payment_status_id'] = array($status => 1);
331 $this->_defaults
['pledge_payment_status_id'] = array($status => 1);
334 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date',
335 CRM_Core_DAO
::$_nullObject
338 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
339 $this->_formValues
['pledge_payment_date_low'] = $date;
340 $this->_defaults
['pledge_payment_date_low'] = $date;
343 $toDate = CRM_Utils_Request
::retrieve('end', 'Date',
344 CRM_Core_DAO
::$_nullObject
347 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
348 $this->_formValues
['pledge_payment_date_high'] = $date;
349 $this->_defaults
['pledge_payment_date_high'] = $date;
352 // set pledge related fields
353 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String',
354 CRM_Core_DAO
::$_nullObject
357 $statusValues = CRM_Contribute_PseudoConstant
::contributionStatus();
359 // Remove status values that are only used for recurring contributions for now (Failed).
360 unset($statusValues['4']);
362 // we need set all statuses except Cancelled
363 unset($statusValues[$pledgeStatus]);
366 foreach ($statusValues as $statusId => $value) {
367 $statuses[$statusId] = 1;
370 $this->_formValues
['pledge_status_id'] = $statuses;
371 $this->_defaults
['pledge_status_id'] = $statuses;
374 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
375 CRM_Core_DAO
::$_nullObject
377 if ($pledgeFromDate) {
378 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
379 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
382 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
383 CRM_Core_DAO
::$_nullObject
386 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
387 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
390 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
392 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
394 $this->_formValues
['contact_id'] = $cid;
395 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
396 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
399 // also assign individual mode to the template
400 $this->_single
= TRUE;
408 public function getFormValues() {
413 * Return a descriptive name for the page, used in wizard header
417 public function getTitle() {
418 return ts('Find Pledges');