3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
35 * Advanced search, extends basic search.
37 class CRM_Contact_Form_Search_Advanced
extends CRM_Contact_Form_Search
{
40 * Processing needed for buildForm and later.
42 public function preProcess() {
43 $this->set('searchFormName', 'Advanced');
46 $openedPanes = CRM_Contact_BAO_Query
::$_openedPanes;
47 $openedPanes = array_merge($openedPanes, $this->_openedPanes
);
48 $this->assign('openedPanes', $openedPanes);
52 * Build the form object.
54 public function buildQuickForm() {
55 $this->set('context', 'advanced');
57 $this->_searchPane
= CRM_Utils_Array
::value('searchPane', $_GET);
59 $this->_searchOptions
= CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
60 'advanced_search_options'
63 if (!$this->_searchPane ||
$this->_searchPane
== 'basic') {
64 CRM_Contact_Form_Search_Criteria
::basic($this);
69 ts('Address Fields') => 'location',
70 ts('Custom Fields') => 'custom',
71 ts('Activities') => 'activity',
72 ts('Relationships') => 'relationship',
73 ts('Demographics') => 'demographics',
74 ts('Notes') => 'notes',
75 ts('Change Log') => 'changeLog',
78 //check if there are any custom data searchable fields
79 $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'),
80 CRM_Contact_BAO_ContactType
::subTypes()
82 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE,
85 // if no searchable fields unset panel
86 if (empty($groupDetails)) {
87 unset($paneNames[ts('Custom Fields')]);
90 foreach ($paneNames as $name => $type) {
91 if (!$this->_searchOptions
[$type]) {
92 unset($paneNames[$name]);
96 $components = CRM_Core_Component
::getEnabledComponents();
98 $componentPanes = array();
99 foreach ($components as $name => $component) {
100 if (in_array($name, array_keys($this->_searchOptions
)) &&
101 $this->_searchOptions
[$name] &&
102 CRM_Core_Permission
::access($component->name
)
104 $componentPanes[$name] = $component->registerAdvancedSearchPane();
105 $componentPanes[$name]['name'] = $name;
109 usort($componentPanes, array('CRM_Utils_Sort', 'cmpFunc'));
110 foreach ($componentPanes as $name => $pane) {
111 // FIXME: we should change the use of $name here to keyword
112 $paneNames[$pane['title']] = $pane['name'];
115 $hookPanes = array();
116 CRM_Contact_BAO_Query_Hook
::singleton()->registerAdvancedSearchPane($hookPanes);
117 $paneNames = array_merge($paneNames, $hookPanes);
119 $this->_paneTemplatePath
= array();
120 foreach ($paneNames as $name => $type) {
121 if (!array_key_exists($type, $this->_searchOptions
) && !in_array($type, $hookPanes)) {
125 $allPanes[$name] = array(
126 'url' => CRM_Utils_System
::url('civicrm/contact/search/advanced',
127 "snippet=1&searchPane=$type&qfKey={$this->controller->_key}"
133 // see if we need to include this paneName in the current form
134 if ($this->_searchPane
== $type ||
!empty($_POST["hidden_{$type}"]) ||
135 CRM_Utils_Array
::value("hidden_{$type}", $this->_formValues
)
137 $allPanes[$name]['open'] = 'true';
139 if (!empty($components[$type])) {
140 $c = $components[$type];
141 $this->add('hidden', "hidden_$type", 1);
142 $c->buildAdvancedSearchPaneForm($this);
143 $this->_paneTemplatePath
[$type] = $c->getAdvancedSearchPaneTemplatePath();
145 elseif (in_array($type, $hookPanes)) {
146 CRM_Contact_BAO_Query_Hook
::singleton()->buildAdvancedSearchPaneForm($this, $type);
147 CRM_Contact_BAO_Query_Hook
::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath
, $type);
150 CRM_Contact_Form_Search_Criteria
::$type($this);
151 $template = ucfirst($type);
152 $this->_paneTemplatePath
[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
157 $this->assign('allPanes', $allPanes);
158 if (!$this->_searchPane
) {
159 parent
::buildQuickForm();
162 $this->assign('suppressForm', TRUE);
167 * Use the form name to create the tpl file name.
174 public function getTemplateFileName() {
175 if (!$this->_searchPane
) {
176 return parent
::getTemplateFileName();
179 if (isset($this->_paneTemplatePath
[$this->_searchPane
])) {
180 return $this->_paneTemplatePath
[$this->_searchPane
];
183 $name = ucfirst($this->_searchPane
);
184 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
190 * Set the default form values.
194 * the default array reference
196 public function setDefaultValues() {
197 // Set ssID for unit tests.
198 if (empty($this->_ssID
)) {
199 $this->_ssID
= $this->get('ssID');
202 $defaults = array_merge($this->_formValues
, array(
203 'privacy_toggle' => 1,
206 $this->normalizeDefaultValues($defaults);
208 if ($this->_context
=== 'amtg') {
209 $defaults['task'] = CRM_Contact_Task
::GROUP_ADD
;
216 * The post processing of the form gets done here.
218 * Key things done during post processing are
219 * - check for reset or next request. if present, skip post processing.
220 * - now check if user requested running a saved search, if so, then
221 * the form values associated with the saved search are used for searching.
222 * - if user has done a submit with new values the regular post submitting is
224 * The processing consists of using a Selector / Controller framework for getting the
227 public function postProcess() {
228 $this->set('isAdvanced', '1');
230 // get user submitted values
231 // get it from controller only if form has been submitted, else preProcess has set this
232 if (!empty($_POST)) {
233 $this->_formValues
= $this->controller
->exportValues($this->_name
);
234 $this->normalizeFormValues();
235 // FIXME: couldn't figure out a good place to do this,
236 // FIXME: so leaving this as a dependency for now
237 if (array_key_exists('contribution_amount_low', $this->_formValues
)) {
239 'contribution_amount_low',
240 'contribution_amount_high',
242 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
246 // set the group if group is submitted
247 if (!empty($this->_formValues
['uf_group_id'])) {
248 $this->set('id', $this->_formValues
['uf_group_id']);
251 $this->set('id', '');
255 // retrieve ssID values only if formValues is null, i.e. form has never been posted
256 if (empty($this->_formValues
) && isset($this->_ssID
)) {
257 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
260 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
261 $this->_formValues
['group'] = array($this->_groupID
=> 1);
264 //search for civicase
265 if (is_array($this->_formValues
)) {
267 if (array_key_exists('case_owner', $this->_formValues
) &&
268 !$this->_formValues
['case_owner'] &&
276 ) as $caseCriteria) {
277 if (!empty($this->_formValues
[$caseCriteria])) {
279 $this->_formValues
['case_owner'] = 1;
284 if (CRM_Core_Permission
::check('access all cases and activities')) {
285 $this->_formValues
['case_owner'] = 1;
288 $this->_formValues
['case_owner'] = 2;
292 $this->_formValues
['case_owner'] = 0;
295 if (array_key_exists('case_owner', $this->_formValues
) && empty($this->_formValues
['case_deleted'])) {
296 $this->_formValues
['case_deleted'] = 0;
300 // we dont want to store the sortByCharacter in the formValue, it is more like
301 // a filter on the result set
302 // this filter is reset if we click on the search button
303 if ($this->_sortByCharacter
!== NULL && empty($_POST)) {
304 if (strtolower($this->_sortByCharacter
) == 'all') {
305 $this->_formValues
['sortByCharacter'] = NULL;
308 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
312 $this->_sortByCharacter
= NULL;
315 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
317 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
318 $this->_returnProperties
= &$this->returnProperties();
319 parent
::postProcess();
323 * Normalize the form values to make it look similar to the advanced form values.
325 * This prevents a ton of work downstream and allows us to use the same code for
326 * multiple purposes (queries, save/edit etc)
328 public function normalizeFormValues() {
329 $contactType = CRM_Utils_Array
::value('contact_type', $this->_formValues
);
331 if ($contactType && is_array($contactType)) {
332 unset($this->_formValues
['contact_type']);
333 foreach ($contactType as $key => $value) {
334 $this->_formValues
['contact_type'][$value] = 1;
338 $config = CRM_Core_Config
::singleton();
339 $specialParams = array(
341 'contribution_soft_credit_type_id',
342 'contribution_status',
343 'contribution_status_id',
344 'contribution_source',
345 'membership_status_id',
346 'participant_status_id',
347 'contribution_trxn_id',
353 'contribution_page_id',
354 'contribution_product_id',
355 'payment_instrument_id',
358 'preferred_communication_method',
360 $changeNames = array(
361 'status_id' => 'activity_status_id',
362 'priority_id' => 'activity_priority_id',
364 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams, $changeNames);
366 $taglist = CRM_Utils_Array
::value('contact_taglist', $this->_formValues
);
368 if ($taglist && is_array($taglist)) {
369 unset($this->_formValues
['contact_taglist']);
370 foreach ($taglist as $value) {
372 $value = explode(',', $value);
373 foreach ($value as $tId) {
374 if (is_numeric($tId)) {
375 $this->_formValues
['contact_tags'][] = $tId;
384 * Normalize default values for multiselect plugins.
386 * @param array $defaults
390 public function normalizeDefaultValues(&$defaults) {
391 if (!is_array($defaults)) {
394 $this->loadDefaultCountryBasedOnState($defaults);
395 if ($this->_ssID
&& empty($_POST)) {
396 $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
));
400 * CRM-18656 - reverse the normalisation of 'contact_taglist' done in
401 * self::normalizeFormValues(). Remove tagset tags from the default
402 * 'contact_tags' and put them in 'contact_taglist[N]' where N is the
405 if (isset($defaults['contact_tags'])) {
406 foreach ($defaults['contact_tags'] as $key => $tagId) {
407 if (!is_array($tagId)) {
408 $parentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $tagId, 'parent_id');
409 $element = "contact_taglist[$parentId]";
410 if ($this->elementExists($element)) {
411 // This tag is a tagset
412 unset($defaults['contact_tags'][$key]);
413 if (!isset($defaults[$element])) {
414 $defaults[$element] = array();
416 $defaults[$element][] = $tagId;
420 if (empty($defaults['contact_tags'])) {
421 unset($defaults['contact_tags']);
429 * Set the default country for the form.
431 * For performance reasons country might be removed from the form CRM-18125
432 * but we need to include it in our defaults or the state will not be visible.
434 * @param array $defaults
436 public function loadDefaultCountryBasedOnState(&$defaults) {
437 if (!empty($defaults['state_province'])) {
438 $defaults['country'] = CRM_Core_DAO
::singleValueQuery(
439 "SELECT country_id FROM civicrm_state_province
441 array(1 => array($defaults['state_province'][0], 'Integer'))