3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
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;
63 * Prefix for the controller.
65 protected $_prefix = "contribute_";
68 * Processing needed for buildForm and later.
70 public function preProcess() {
71 $this->set('searchFormName', 'Search');
73 $this->_searchButtonName
= $this->getButtonName('refresh');
74 $this->_actionButtonName
= $this->getButtonName('next', 'action');
77 // @todo - is this an error - $this->_defaults is used.
78 $this->defaults
= array();
81 * we allow the controller to set force/reset externally, useful when we are being
82 * 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
95 $this->_formValues
= $this->controller
->exportValues($this->_name
);
98 $this->_formValues
= $this->get('formValues');
102 $memberShipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
103 if (isset($memberShipId)) {
104 $this->_formValues
['contribution_membership_id'] = $memberShipId;
106 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
107 if (isset($participantId)) {
108 $this->_formValues
['contribution_participant_id'] = $participantId;
112 $this->postProcess();
113 $this->set('force', 0);
117 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
118 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
119 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
123 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 1);
124 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
132 if ($this->_context
== 'user') {
133 $prefix = $this->_prefix
;
136 $this->assign("{$prefix}limit", $this->_limit
);
137 $this->assign("{$prefix}single", $this->_single
);
139 $controller = new CRM_Core_Selector_Controller($selector,
140 $this->get(CRM_Utils_Pager
::PAGE_ID
),
142 CRM_Core_Action
::VIEW
,
144 CRM_Core_Selector_Controller
::TRANSFER
,
148 $controller->setEmbedded(TRUE);
149 $controller->moveFromSessionToTemplate();
151 $this->assign('contributionSummary', $this->get('summary'));
159 public function setDefaultValues() {
160 if (empty($this->_defaults
['contribution_status'])) {
161 $this->_defaults
['contribution_status'][1] = 1;
163 return $this->_defaults
;
167 * Build the form object.
169 public function buildQuickForm() {
170 parent
::buildQuickForm();
171 // text for sort_name
172 $this->addElement('text',
174 ts('Contributor Name or Email'),
175 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact',
180 $this->_group
= CRM_Core_PseudoConstant
::nestedGroup();
182 // multiselect for groups
184 $this->add('select', 'group', ts('Groups'), $this->_group
, FALSE,
185 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
189 // multiselect for tags
190 $contactTags = CRM_Core_BAO_Tag
::getTags();
193 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
194 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
198 CRM_Contribute_BAO_Query
::buildSearchForm($this);
200 $rows = $this->get('rows');
201 if (is_array($rows)) {
202 if (!$this->_single
) {
203 $this->addRowSelectors($rows);
206 $permission = CRM_Core_Permission
::getPermission();
208 $queryParams = $this->get('queryParams');
209 $softCreditFiltering = FALSE;
210 if (!empty($queryParams)) {
211 $softCreditFiltering = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
213 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $softCreditFiltering);
214 $this->addTaskMenu($tasks);
220 * The post processing of the form gets done here.
222 * Key things done during post processing are
223 * - check for reset or next request. if present, skip post processing.
224 * - now check if user requested running a saved search, if so, then
225 * the form values associated with the saved search are used for searching.
226 * - if user has done a submit with new values the regular post submission is
228 * The processing consists of using a Selector / Controller framework for getting the
231 public function postProcess() {
238 if (!empty($_POST)) {
239 $this->_formValues
= $this->controller
->exportValues($this->_name
);
242 $this->fixFormValues();
244 // We don't show test records in summaries or dashboards
245 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
== 'dashboard') {
246 $this->_formValues
["contribution_test"] = 0;
250 'contribution_amount_low',
251 'contribution_amount_high',
253 if (isset($this->_formValues
[$f])) {
254 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
258 $config = CRM_Core_Config
::singleton();
259 if (!empty($_POST)) {
260 $specialParams = array(
262 'contribution_soft_credit_type_id',
263 'contribution_status_id',
264 'contribution_source',
265 'contribution_trxn_id',
266 'contribution_page_id',
267 'contribution_product_id',
269 'payment_instrument_id',
271 foreach ($specialParams as $element) {
272 $value = CRM_Utils_Array
::value($element, $this->_formValues
);
274 if (is_array($value)) {
275 $this->_formValues
[$element] = array('IN' => $value);
278 $this->_formValues
[$element] = array('LIKE' => "%$value%");
283 $tags = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
284 if ($tags && !is_array($tags)) {
285 unset($this->_formValues
['contact_tags']);
286 $this->_formValues
['contact_tags'][$tags] = 1;
289 if ($tags && is_array($tags)) {
290 unset($this->_formValues
['contact_tags']);
291 foreach ($tags as $notImportant => $tagID) {
292 $this->_formValues
['contact_tags'][$tagID] = 1;
296 if (!$config->groupTree
) {
297 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
298 if ($group && !is_array($group)) {
299 unset($this->_formValues
['group']);
300 $this->_formValues
['group'][$group] = 1;
303 if ($group && is_array($group)) {
304 unset($this->_formValues
['group']);
305 foreach ($group as $notImportant => $groupID) {
306 $this->_formValues
['group'][$groupID] = 1;
312 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
314 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
316 $this->set('formValues', $this->_formValues
);
317 $this->set('queryParams', $this->_queryParams
);
319 $buttonName = $this->controller
->getButtonName();
320 if ($buttonName == $this->_actionButtonName
) {
321 // check actionName and if next, then do not repeat a search, since we are going to the next page
323 // hack, make sure we reset the task values
324 $stateMachine = $this->controller
->getStateMachine();
325 $formName = $stateMachine->getTaskFormName();
326 $this->controller
->resetPage($formName);
331 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
332 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
333 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
337 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
338 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
345 $selector->setKey($this->controller
->_key
);
348 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
349 $prefix = $this->_prefix
;
352 $controller = new CRM_Core_Selector_Controller($selector,
353 $this->get(CRM_Utils_Pager
::PAGE_ID
),
355 CRM_Core_Action
::VIEW
,
357 CRM_Core_Selector_Controller
::SESSION
,
360 $controller->setEmbedded(TRUE);
362 $query = &$selector->getQuery();
363 if ($this->_context
== 'user') {
364 $query->setSkipPermission(TRUE);
366 $summary = &$query->summaryContribution($this->_context
);
367 $this->set('summary', $summary);
368 $this->assign('contributionSummary', $summary);
373 * Use values from $_GET if force is set to TRUE.
375 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
377 public function fixFormValues() {
378 if (!$this->_force
) {
382 $status = CRM_Utils_Request
::retrieve('status', 'String',
383 CRM_Core_DAO
::$_nullObject
386 $this->_formValues
['contribution_status_id'] = array($status => 1);
387 $this->_defaults
['contribution_status_id'] = array($status => 1);
390 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
393 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
395 $this->_formValues
['contact_id'] = $cid;
396 // @todo - why do we retrieve these when they are not used?
397 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
398 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
401 // also assign individual mode to the template
402 $this->_single
= TRUE;
406 $lowDate = CRM_Utils_Request
::retrieve('start', 'Timestamp',
407 CRM_Core_DAO
::$_nullObject
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',
416 CRM_Core_DAO
::$_nullObject
419 $highDate = CRM_Utils_Type
::escape($highDate, 'Timestamp');
420 $date = CRM_Utils_Date
::setDateDefaults($highDate);
421 $this->_formValues
['contribution_date_high'] = $this->_defaults
['contribution_date_high'] = $date[0];
424 if ($highDate ||
$lowDate) {
425 //set the Choose Date Range value
426 $this->_formValues
['contribution_date_relative'] = 0;
429 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
433 $test = CRM_Utils_Request
::retrieve('test', 'Boolean',
434 CRM_Core_DAO
::$_nullObject
437 $test = CRM_Utils_Type
::escape($test, 'Boolean');
438 $this->_formValues
['contribution_test'] = $test;
441 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
443 $this->_formValues
['contribution_recur_id'] = $recur;
444 $this->_formValues
['contribution_recurring'] = 1;
447 //check for contribution page id.
448 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
449 if ($contribPageId) {
450 $this->_formValues
['contribution_page_id'] = $contribPageId;
453 //give values to default.
454 $this->_defaults
= $this->_formValues
;
458 * Return a descriptive name for the page, used in wizard header.
462 public function getTitle() {
463 return ts('Find Contributions');