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 * Advanced search, extends basic search.
37 class CRM_Contribute_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 = "contribute_";
66 * Processing needed for buildForm and later.
68 public function preProcess() {
69 $this->set('searchFormName', 'Search');
71 $this->_searchButtonName
= $this->getButtonName('refresh');
72 $this->_actionButtonName
= $this->getButtonName('next', 'action');
75 // @todo - is this an error - $this->_defaults is used.
76 $this->defaults
= array();
79 * we allow the controller to set force/reset externally, useful when we are being
80 * driven by the wizard framework
83 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean');
84 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
85 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
86 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
88 $this->assign("context", $this->_context
);
90 // get user submitted values
91 // get it from controller only if form has been submitted, else preProcess has set this
93 $this->_formValues
= $this->controller
->exportValues($this->_name
);
96 $this->_formValues
= $this->get('formValues');
100 $memberShipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
101 if (isset($memberShipId)) {
102 $this->_formValues
['contribution_membership_id'] = $memberShipId;
104 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
105 if (isset($participantId)) {
106 $this->_formValues
['contribution_participant_id'] = $participantId;
110 $this->postProcess();
111 $this->set('force', 0);
115 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
116 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
117 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
121 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
122 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
130 if ($this->_context
== 'user') {
131 $prefix = $this->_prefix
;
134 $this->assign("{$prefix}limit", $this->_limit
);
135 $this->assign("{$prefix}single", $this->_single
);
137 $controller = new CRM_Core_Selector_Controller($selector,
138 $this->get(CRM_Utils_Pager
::PAGE_ID
),
140 CRM_Core_Action
::VIEW
,
142 CRM_Core_Selector_Controller
::TRANSFER
,
146 $controller->setEmbedded(TRUE);
147 $controller->moveFromSessionToTemplate();
149 $this->assign('contributionSummary', $this->get('summary'));
157 public function setDefaultValues() {
158 if (empty($this->_defaults
['contribution_status'])) {
159 $this->_defaults
['contribution_status'][1] = 1;
161 return $this->_defaults
;
165 * Build the form object.
167 public function buildQuickForm() {
168 parent
::buildQuickForm();
169 $this->addSortNameField();
171 $this->_group
= CRM_Core_PseudoConstant
::nestedGroup();
173 // multiselect for groups
175 $this->add('select', 'group', ts('Groups'), $this->_group
, FALSE,
176 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
180 // multiselect for tags
181 $contactTags = CRM_Core_BAO_Tag
::getTags();
184 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
185 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
189 CRM_Contribute_BAO_Query
::buildSearchForm($this);
191 $rows = $this->get('rows');
192 if (is_array($rows)) {
193 if (!$this->_single
) {
194 $this->addRowSelectors($rows);
197 $permission = CRM_Core_Permission
::getPermission();
199 $queryParams = $this->get('queryParams');
200 $softCreditFiltering = FALSE;
201 if (!empty($queryParams)) {
202 $softCreditFiltering = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
204 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $softCreditFiltering);
205 $this->addTaskMenu($tasks);
211 * Get the label for the sortName field if email searching is on.
213 * (email searching is a setting under search preferences).
217 protected function getSortNameLabelWithEmail() {
218 return ts('Contributor Name or Email');
222 * Get the label for the sortName field if email searching is off.
224 * (email searching is a setting under search preferences).
228 protected function getSortNameLabelWithOutEmail() {
229 return ts('Contributor Name');
233 * The post processing of the form gets done here.
235 * Key things done during post processing are
236 * - check for reset or next request. if present, skip post processing.
237 * - now check if user requested running a saved search, if so, then
238 * the form values associated with the saved search are used for searching.
239 * - if user has done a submit with new values the regular post submission is
241 * The processing consists of using a Selector / Controller framework for getting the
244 public function postProcess() {
251 if (!empty($_POST)) {
252 $this->_formValues
= $this->controller
->exportValues($this->_name
);
255 $this->fixFormValues();
257 // We don't show test records in summaries or dashboards
258 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
== 'dashboard') {
259 $this->_formValues
["contribution_test"] = 0;
263 'contribution_amount_low',
264 'contribution_amount_high',
266 if (isset($this->_formValues
[$f])) {
267 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
271 $config = CRM_Core_Config
::singleton();
272 if (!empty($_POST)) {
273 $specialParams = array(
275 'contribution_soft_credit_type_id',
276 'contribution_status_id',
277 'contribution_source',
278 'contribution_trxn_id',
279 'contribution_page_id',
280 'contribution_product_id',
282 'payment_instrument_id',
283 'contribution_batch_id',
285 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
287 $tags = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
288 if ($tags && !is_array($tags)) {
289 unset($this->_formValues
['contact_tags']);
290 $this->_formValues
['contact_tags'][$tags] = 1;
293 if ($tags && is_array($tags)) {
294 unset($this->_formValues
['contact_tags']);
295 foreach ($tags as $notImportant => $tagID) {
296 $this->_formValues
['contact_tags'][$tagID] = 1;
300 if (!defined('CIVICRM_GROUPTREE')) {
301 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
302 if ($group && !is_array($group)) {
303 unset($this->_formValues
['group']);
304 $this->_formValues
['group'][$group] = 1;
307 if ($group && is_array($group)) {
308 unset($this->_formValues
['group']);
309 foreach ($group as $notImportant => $groupID) {
310 $this->_formValues
['group'][$groupID] = 1;
316 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
318 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
320 $this->set('formValues', $this->_formValues
);
321 $this->set('queryParams', $this->_queryParams
);
323 $buttonName = $this->controller
->getButtonName();
324 if ($buttonName == $this->_actionButtonName
) {
325 // check actionName and if next, then do not repeat a search, since we are going to the next page
327 // hack, make sure we reset the task values
328 $stateMachine = $this->controller
->getStateMachine();
329 $formName = $stateMachine->getTaskFormName();
330 $this->controller
->resetPage($formName);
335 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
336 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
337 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
341 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
342 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
349 $selector->setKey($this->controller
->_key
);
352 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
353 $prefix = $this->_prefix
;
356 $controller = new CRM_Core_Selector_Controller($selector,
357 $this->get(CRM_Utils_Pager
::PAGE_ID
),
359 CRM_Core_Action
::VIEW
,
361 CRM_Core_Selector_Controller
::SESSION
,
364 $controller->setEmbedded(TRUE);
366 $query = &$selector->getQuery();
367 if ($this->_context
== 'user') {
368 $query->setSkipPermission(TRUE);
370 $summary = &$query->summaryContribution($this->_context
);
371 $this->set('summary', $summary);
372 $this->assign('contributionSummary', $summary);
377 * Use values from $_GET if force is set to TRUE.
379 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
381 public function fixFormValues() {
382 if (!$this->_force
) {
386 $status = CRM_Utils_Request
::retrieve('status', 'String');
388 $this->_formValues
['contribution_status_id'] = array($status => 1);
389 $this->_defaults
['contribution_status_id'] = array($status => 1);
392 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
395 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
397 $this->_formValues
['contact_id'] = $cid;
398 // @todo - why do we retrieve these when they are not used?
399 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
400 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
403 // also assign individual mode to the template
404 $this->_single
= TRUE;
408 $lowDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
410 $lowDate = CRM_Utils_Type
::escape($lowDate, 'Timestamp');
411 $date = CRM_Utils_Date
::setDateDefaults($lowDate);
412 $this->_formValues
['contribution_date_low'] = $this->_defaults
['contribution_date_low'] = $date[0];
415 $highDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
417 $highDate = CRM_Utils_Type
::escape($highDate, 'Timestamp');
418 $date = CRM_Utils_Date
::setDateDefaults($highDate);
419 $this->_formValues
['contribution_date_high'] = $this->_defaults
['contribution_date_high'] = $date[0];
422 if ($highDate ||
$lowDate) {
423 //set the Choose Date Range value
424 $this->_formValues
['contribution_date_relative'] = 0;
427 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
431 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
433 $test = CRM_Utils_Type
::escape($test, 'Boolean');
434 $this->_formValues
['contribution_test'] = $test;
437 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
439 $this->_formValues
['contribution_recur_id'] = $recur;
440 $this->_formValues
['contribution_recurring'] = 1;
443 //check for contribution page id.
444 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
445 if ($contribPageId) {
446 $this->_formValues
['contribution_page_id'] = $contribPageId;
449 //give values to default.
450 $this->_defaults
= $this->_formValues
;
454 * Return a descriptive name for the page, used in wizard header.
458 public function getTitle() {
459 return ts('Find Contributions');