3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This file is for Pledge search
39 class CRM_Pledge_Form_Search
extends CRM_Core_Form_Search
{
42 * Are we forced to run a search
50 * name of search button
55 protected $_searchButtonName;
58 * name of action button
63 protected $_actionButtonName;
66 * form values that we will be using
71 protected $_formValues;
74 * the params that are sent to the query
79 protected $_queryParams;
82 * have we already done this search
90 * are we restricting ourselves to a single contact
95 protected $_single = FALSE;
98 * are we restricting ourselves to a single contact
103 protected $_limit = NULL;
106 * what context are we being invoked from
111 protected $_context = NULL;
114 * prefix for the controller
117 protected $_prefix = "pledge_";
119 protected $_defaults;
122 * processing needed for buildForm and later
127 function preProcess() {
130 * set the button names
132 $this->_searchButtonName
= $this->getButtonName('refresh');
133 $this->_actionButtonName
= $this->getButtonName('next', 'action');
135 $this->_done
= FALSE;
136 $this->defaults
= array();
139 * we allow the controller to set force/reset externally, useful when we are being
140 * driven by the wizard framework
142 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
143 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
144 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
145 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
147 $this->assign("context", $this->_context
);
149 // get user submitted values
150 // get it from controller only if form has been submitted, else preProcess has set this
151 if (!empty($_POST) && !$this->controller
->isModal()) {
152 $this->_formValues
= $this->controller
->exportValues($this->_name
);
155 $this->_formValues
= $this->get('formValues');
158 if (empty($this->_formValues
)) {
159 if (isset($this->_ssID
)) {
160 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
165 $this->postProcess();
166 $this->set('force', 0);
170 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
171 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
172 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
177 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
178 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
186 if ($this->_context
== 'user') {
187 $prefix = $this->_prefix
;
190 $this->assign("{$prefix}limit", $this->_limit
);
191 $this->assign("{$prefix}single", $this->_single
);
193 $controller = new CRM_Core_Selector_Controller($selector,
194 $this->get(CRM_Utils_Pager
::PAGE_ID
),
196 CRM_Core_Action
::VIEW
,
198 CRM_Core_Selector_Controller
::TRANSFER
,
201 $controller->setEmbedded(TRUE);
202 $controller->moveFromSessionToTemplate();
204 $this->assign('summary', $this->get('summary'));
214 function buildQuickForm() {
215 parent
::buildQuickForm();
216 $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
218 CRM_Pledge_BAO_Query
::buildSearchForm($this);
221 * add form checkboxes for each row. This is needed out here to conform to QF protocol
222 * of all elements being declared in builQuickForm
224 $rows = $this->get('rows');
225 if (is_array($rows)) {
226 if (!$this->_single
) {
227 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows'));
229 foreach ($rows as $row) {
230 $this->addElement('checkbox', $row['checkbox'],
232 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
237 $total = $cancel = 0;
239 $permission = CRM_Core_Permission
::getPermission();
241 $tasks = array('' => ts('- actions -')) + CRM_Pledge_Task
::permissionedTaskTitles($permission);
243 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
244 $this->add('submit', $this->_actionButtonName
, ts('Go'),
246 'class' => 'form-submit',
248 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
252 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
253 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
254 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
260 * The post processing of the form gets done here.
262 * Key things done during post processing are
263 * - check for reset or next request. if present, skip post procesing.
264 * - now check if user requested running a saved search, if so, then
265 * the form values associated with the saved search are used for searching.
266 * - if user has done a submit with new values the regular post submissing is
268 * The processing consists of using a Selector / Controller framework for getting the
276 function postProcess() {
283 $this->_formValues
= $this->controller
->exportValues($this->_name
);
285 $this->fixFormValues();
287 // We don't show test records in summaries or dashboards
288 if (empty($this->_formValues
['pledge_test']) && $this->_force
) {
289 $this->_formValues
["pledge_test"] = 0;
292 foreach (array('pledge_amount_low', 'pledge_amount_high') as $f) {
293 if (isset($this->_formValues
[$f])) {
294 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
298 if (isset($this->_ssID
) && empty($_POST)) {
299 // if we are editing / running a saved search and the form has not been posted
300 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
303 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
305 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
307 $this->set('formValues', $this->_formValues
);
308 $this->set('queryParams', $this->_queryParams
);
310 $buttonName = $this->controller
->getButtonName();
311 if ($buttonName == $this->_actionButtonName
) {
312 // check actionName and if next, then do not repeat a search, since we are going to the next page
314 // hack, make sure we reset the task values
315 $stateMachine = $this->controller
->getStateMachine();
316 $formName = $stateMachine->getTaskFormName();
317 $this->controller
->resetPage($formName);
322 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
323 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
324 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
328 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
330 $selector = new CRM_Pledge_Selector_Search($this->_queryParams
,
337 $selector->setKey($this->controller
->_key
);
340 if ($this->_context
== 'user') {
341 $prefix = $this->_prefix
;
344 $this->assign("{$prefix}limit", $this->_limit
);
345 $this->assign("{$prefix}single", $this->_single
);
347 $controller = new CRM_Core_Selector_Controller($selector,
348 $this->get(CRM_Utils_Pager
::PAGE_ID
),
350 CRM_Core_Action
::VIEW
,
352 CRM_Core_Selector_Controller
::SESSION
,
355 $controller->setEmbedded(TRUE);
357 $query = &$selector->getQuery();
358 if ($this->_context
== 'user') {
359 $query->setSkipPermission(TRUE);
365 * This function is used to add the rules (mainly global rules) for form.
366 * All local rules are added near the element
372 function addRules() {
373 $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule'));
377 * global validation rules for the form
379 * @param array $fields posted values of the form
380 * @param array $errors list of errors to be posted back to the form
386 static function formRule($fields) {
389 if (!empty($errors)) {
397 * Set the default form values
401 * @return array the default array reference
403 function setDefaultValues() {
405 $defaults = $this->_formValues
;
409 function fixFormValues() {
410 if (!$this->_force
) {
414 // set pledge payment related fields
415 $status = CRM_Utils_Request
::retrieve('status', 'String',
416 CRM_Core_DAO
::$_nullObject
419 $this->_formValues
['pledge_payment_status_id'] = array($status => 1);
420 $this->_defaults
['pledge_payment_status_id'] = array($status => 1);
423 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date',
424 CRM_Core_DAO
::$_nullObject
427 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
428 $this->_formValues
['pledge_payment_date_low'] = $date;
429 $this->_defaults
['pledge_payment_date_low'] = $date;
432 $toDate = CRM_Utils_Request
::retrieve('end', 'Date',
433 CRM_Core_DAO
::$_nullObject
436 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
437 $this->_formValues
['pledge_payment_date_high'] = $date;
438 $this->_defaults
['pledge_payment_date_high'] = $date;
441 // set pledge related fields
442 $pledgeStatus = CRM_Utils_Request
::retrieve('pstatus', 'String',
443 CRM_Core_DAO
::$_nullObject
446 $statusValues = CRM_Contribute_PseudoConstant
::contributionStatus();
448 // Remove status values that are only used for recurring contributions for now (Failed).
449 unset($statusValues['4']);
451 // we need set all statuses except Cancelled
452 unset($statusValues[$pledgeStatus]);
455 foreach ($statusValues as $statusId => $value) {
456 $statuses[$statusId] = 1;
459 $this->_formValues
['pledge_status_id'] = $statuses;
460 $this->_defaults
['pledge_status_id'] = $statuses;
463 $pledgeFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
464 CRM_Core_DAO
::$_nullObject
466 if ($pledgeFromDate) {
467 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeFromDate);
468 $this->_formValues
['pledge_create_date_low'] = $this->_defaults
['pledge_create_date_low'] = $date;
471 $pledgeToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
472 CRM_Core_DAO
::$_nullObject
475 list($date) = CRM_Utils_Date
::setDateDefaults($pledgeToDate);
476 $this->_formValues
['pledge_create_date_high'] = $this->_defaults
['pledge_create_date_high'] = $date;
479 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
481 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
483 $this->_formValues
['contact_id'] = $cid;
484 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
485 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
488 // also assign individual mode to the template
489 $this->_single
= TRUE;
494 function getFormValues() {
499 * Return a descriptive name for the page, used in wizard header
504 public function getTitle() {
505 return ts('Find Pledges');