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 s | 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
41 * advanced search, extends basic search
43 class CRM_Contact_Form_Search_Advanced
extends CRM_Contact_Form_Search
{
46 * processing needed for buildForm and later
51 function preProcess() {
52 $this->set('searchFormName', 'Advanced');
55 $openedPanes = CRM_Contact_BAO_Query
::$_openedPanes;
56 $openedPanes = array_merge($openedPanes, $this->_openedPanes
);
57 $this->assign('openedPanes', $openedPanes);
67 function buildQuickForm() {
68 $this->set('context', 'advanced');
70 $this->_searchPane
= CRM_Utils_Array
::value('searchPane', $_GET);
72 $this->_searchOptions
= CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
73 'advanced_search_options'
76 if (!$this->_searchPane ||
$this->_searchPane
== 'basic') {
77 CRM_Contact_Form_Search_Criteria
::basic($this);
82 ts('Address Fields') => 'location',
83 ts('Custom Fields') => 'custom',
84 ts('Activities') => 'activity',
85 ts('Relationships') => 'relationship',
86 ts('Demographics') => 'demographics',
87 ts('Notes') => 'notes',
88 ts('Change Log') => 'changeLog',
91 //check if there are any custom data searchable fields
92 $groupDetails = array();
93 $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'),
94 CRM_Contact_BAO_ContactType
::subTypes()
96 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE,
99 // if no searchable fields unset panel
100 if (empty($groupDetails)) {
101 unset($paneNames[ts('Custom Fields')]);
104 foreach ($paneNames as $name => $type) {
105 if (!$this->_searchOptions
[$type]) {
106 unset($paneNames[$name]);
110 $components = CRM_Core_Component
::getEnabledComponents();
112 $componentPanes = array();
113 foreach ($components as $name => $component) {
114 if (in_array($name, array_keys($this->_searchOptions
)) &&
115 $this->_searchOptions
[$name] &&
116 CRM_Core_Permission
::access($component->name
)
118 $componentPanes[$name] = $component->registerAdvancedSearchPane();
119 $componentPanes[$name]['name'] = $name;
123 usort($componentPanes, array('CRM_Utils_Sort', 'cmpFunc'));
124 foreach ($componentPanes as $name => $pane) {
125 // FIXME: we should change the use of $name here to keyword
126 $paneNames[$pane['title']] = $pane['name'];
129 $hookPanes = array();
130 CRM_Contact_BAO_Query_Hook
::singleton()->registerAdvancedSearchPane($hookPanes);
131 $paneNames = array_merge($paneNames, $hookPanes);
133 $this->_paneTemplatePath
= array();
134 foreach ($paneNames as $name => $type) {
135 if (!array_key_exists($type, $this->_searchOptions
) && !in_array($type, $hookPanes)) {
139 $allPanes[$name] = array(
140 'url' => CRM_Utils_System
::url('civicrm/contact/search/advanced',
141 "snippet=1&searchPane=$type&qfKey={$this->controller->_key}"
147 // see if we need to include this paneName in the current form
148 if ($this->_searchPane
== $type ||
!empty($_POST["hidden_{$type}"]) ||
149 CRM_Utils_Array
::value("hidden_{$type}", $this->_formValues
)
151 $allPanes[$name]['open'] = 'true';
153 if (!empty($components[$type])) {
154 $c = $components[$type];
155 $this->add('hidden', "hidden_$type", 1);
156 $c->buildAdvancedSearchPaneForm($this);
157 $this->_paneTemplatePath
[$type] = $c->getAdvancedSearchPaneTemplatePath();
159 else if (in_array($type, $hookPanes)) {
160 CRM_Contact_BAO_Query_Hook
::singleton()->buildAdvancedSearchPaneForm($this, $type);
161 CRM_Contact_BAO_Query_Hook
::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath
, $type);
164 CRM_Contact_Form_Search_Criteria
::$type($this);
165 $template = ucfirst($type);
166 $this->_paneTemplatePath
[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
171 $this->assign('allPanes', $allPanes);
172 if (!$this->_searchPane
) {
173 parent
::buildQuickForm();
176 $this->assign('suppressForm', TRUE);
181 * Use the form name to create the tpl file name
189 function getTemplateFileName() {
190 if (!$this->_searchPane
) {
191 return parent
::getTemplateFileName();
194 if (isset($this->_paneTemplatePath
[$this->_searchPane
])) {
195 return $this->_paneTemplatePath
[$this->_searchPane
];
198 $name = ucfirst($this->_searchPane
);
199 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
205 * Set the default form values
209 * @return array the default array reference
211 function setDefaultValues() {
212 $defaults = $this->_formValues
;
213 $this->normalizeDefaultValues($defaults);
215 if ($this->_context
=== 'amtg') {
216 $defaults['task'] = CRM_Contact_Task
::GROUP_CONTACTS
;
219 $defaults['privacy_toggle'] = 1;
225 * The post processing of the form gets done here.
227 * Key things done during post processing are
228 * - check for reset or next request. if present, skip post procesing.
229 * - now check if user requested running a saved search, if so, then
230 * the form values associated with the saved search are used for searching.
231 * - if user has done a submit with new values the regular post submissing is
233 * The processing consists of using a Selector / Controller framework for getting the
241 function postProcess() {
242 $this->set('isAdvanced', '1');
244 // get user submitted values
245 // get it from controller only if form has been submitted, else preProcess has set this
246 if (!empty($_POST)) {
247 $this->_formValues
= $this->controller
->exportValues($this->_name
);
248 $this->normalizeFormValues();
249 // FIXME: couldn't figure out a good place to do this,
250 // FIXME: so leaving this as a dependency for now
251 if (array_key_exists('contribution_amount_low', $this->_formValues
)) {
253 'contribution_amount_low', 'contribution_amount_high') as $f) {
254 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
258 // set the group if group is submitted
259 if (!empty($this->_formValues
['uf_group_id'])) {
260 $this->set('id', $this->_formValues
['uf_group_id']);
263 $this->set('id', '');
267 // retrieve ssID values only if formValues is null, i.e. form has never been posted
268 if (empty($this->_formValues
) && isset($this->_ssID
)) {
269 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
272 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
273 $this->_formValues
['group'] = array($this->_groupID
=> 1);
277 //search for civicase
278 if (is_array($this->_formValues
)) {
280 if (array_key_exists('case_owner', $this->_formValues
) &&
281 !$this->_formValues
['case_owner'] &&
285 'case_type_id', 'case_status_id', 'case_deleted', 'case_tags') as $caseCriteria) {
286 if (!empty($this->_formValues
[$caseCriteria])) {
288 $this->_formValues
['case_owner'] = 1;
293 if (CRM_Core_Permission
::check('access all cases and activities')) {
294 $this->_formValues
['case_owner'] = 1;
297 $this->_formValues
['case_owner'] = 2;
301 $this->_formValues
['case_owner'] = 0;
304 if (array_key_exists('case_owner', $this->_formValues
) && empty($this->_formValues
['case_deleted'])) {
305 $this->_formValues
['case_deleted'] = 0;
309 // we dont want to store the sortByCharacter in the formValue, it is more like
310 // a filter on the result set
311 // this filter is reset if we click on the search button
312 if ($this->_sortByCharacter
!== NULL && empty($_POST)) {
313 if (strtolower($this->_sortByCharacter
) == 'all') {
314 $this->_formValues
['sortByCharacter'] = NULL;
317 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
321 $this->_sortByCharacter
= NULL;
324 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
326 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
327 $this->_returnProperties
= &$this->returnProperties();
328 parent
::postProcess();
332 * normalize the form values to make it look similar to the advanced form values
333 * this prevents a ton of work downstream and allows us to use the same code for
334 * multiple purposes (queries, save/edit etc)
339 function normalizeFormValues() {
340 $contactType = CRM_Utils_Array
::value('contact_type', $this->_formValues
);
342 if ($contactType && is_array($contactType)) {
343 unset($this->_formValues
['contact_type']);
344 foreach ($contactType as $key => $value) {
345 $this->_formValues
['contact_type'][$value] = 1;
349 $config = CRM_Core_Config
::singleton();
350 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
351 if ($group && is_array($group)) {
352 unset($this->_formValues
['group']);
353 foreach ($group as $key => $value) {
354 $this->_formValues
['group'][$value] = 1;
358 $tag = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
359 if ($tag && is_array($tag)) {
360 unset($this->_formValues
['contact_tags']);
361 foreach ($tag as $key => $value) {
362 $this->_formValues
['contact_tags'][$value] = 1;
367 $financialType = CRM_Utils_Array
::value('financial_type_id', $this->_formValues
);
368 if ($financialType && is_array($financialType)) {
369 unset($this->_formValues
['financial_type_id']);
370 foreach($financialType as $notImportant => $typeID) {
371 $this->_formValues
['financial_type_id'][$typeID] = 1;
375 $taglist = CRM_Utils_Array
::value('contact_taglist', $this->_formValues
);
377 if ($taglist && is_array($taglist)) {
378 unset($this->_formValues
['contact_taglist']);
379 foreach ($taglist as $value) {
381 $value = explode(',', $value);
382 foreach ($value as $tId) {
383 if (is_numeric($tId)) {
384 $this->_formValues
['contact_tags'][$tId] = 1;
395 * normalize default values for multiselect plugins
397 * @param $defaults array
401 function normalizeDefaultValues(&$defaults) {
402 if (!is_array($defaults)) {
406 if ($this->_ssID
&& empty($_POST)) {
407 $fields = array('contact_type', 'group', 'contact_tags');
409 foreach ($fields as $field) {
410 $fieldValues = CRM_Utils_Array
::value($field, $defaults);
411 if ($fieldValues && is_array($fieldValues)) {
412 $defaults[$field] = array_keys($fieldValues);