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
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.
50 public function preProcess() {
51 $this->set('searchFormName', 'Advanced');
54 $openedPanes = CRM_Contact_BAO_Query
::$_openedPanes;
55 $openedPanes = array_merge($openedPanes, $this->_openedPanes
);
56 $this->assign('openedPanes', $openedPanes);
60 * Build the form object.
65 public function buildQuickForm() {
66 $this->set('context', 'advanced');
68 $this->_searchPane
= CRM_Utils_Array
::value('searchPane', $_GET);
70 $this->_searchOptions
= CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
71 'advanced_search_options'
74 if (!$this->_searchPane ||
$this->_searchPane
== 'basic') {
75 CRM_Contact_Form_Search_Criteria
::basic($this);
80 ts('Address Fields') => 'location',
81 ts('Custom Fields') => 'custom',
82 ts('Activities') => 'activity',
83 ts('Relationships') => 'relationship',
84 ts('Demographics') => 'demographics',
85 ts('Notes') => 'notes',
86 ts('Change Log') => 'changeLog',
89 //check if there are any custom data searchable fields
90 $groupDetails = array();
91 $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'),
92 CRM_Contact_BAO_ContactType
::subTypes()
94 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE,
97 // if no searchable fields unset panel
98 if (empty($groupDetails)) {
99 unset($paneNames[ts('Custom Fields')]);
102 foreach ($paneNames as $name => $type) {
103 if (!$this->_searchOptions
[$type]) {
104 unset($paneNames[$name]);
108 $components = CRM_Core_Component
::getEnabledComponents();
110 $componentPanes = array();
111 foreach ($components as $name => $component) {
112 if (in_array($name, array_keys($this->_searchOptions
)) &&
113 $this->_searchOptions
[$name] &&
114 CRM_Core_Permission
::access($component->name
)
116 $componentPanes[$name] = $component->registerAdvancedSearchPane();
117 $componentPanes[$name]['name'] = $name;
121 usort($componentPanes, array('CRM_Utils_Sort', 'cmpFunc'));
122 foreach ($componentPanes as $name => $pane) {
123 // FIXME: we should change the use of $name here to keyword
124 $paneNames[$pane['title']] = $pane['name'];
127 $hookPanes = array();
128 CRM_Contact_BAO_Query_Hook
::singleton()->registerAdvancedSearchPane($hookPanes);
129 $paneNames = array_merge($paneNames, $hookPanes);
131 $this->_paneTemplatePath
= array();
132 foreach ($paneNames as $name => $type) {
133 if (!array_key_exists($type, $this->_searchOptions
) && !in_array($type, $hookPanes)) {
137 $allPanes[$name] = array(
138 'url' => CRM_Utils_System
::url('civicrm/contact/search/advanced',
139 "snippet=1&searchPane=$type&qfKey={$this->controller->_key}"
145 // see if we need to include this paneName in the current form
146 if ($this->_searchPane
== $type ||
!empty($_POST["hidden_{$type}"]) ||
147 CRM_Utils_Array
::value("hidden_{$type}", $this->_formValues
)
149 $allPanes[$name]['open'] = 'true';
151 if (!empty($components[$type])) {
152 $c = $components[$type];
153 $this->add('hidden', "hidden_$type", 1);
154 $c->buildAdvancedSearchPaneForm($this);
155 $this->_paneTemplatePath
[$type] = $c->getAdvancedSearchPaneTemplatePath();
157 elseif (in_array($type, $hookPanes)) {
158 CRM_Contact_BAO_Query_Hook
::singleton()->buildAdvancedSearchPaneForm($this, $type);
159 CRM_Contact_BAO_Query_Hook
::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath
, $type);
162 CRM_Contact_Form_Search_Criteria
::$type($this);
163 $template = ucfirst($type);
164 $this->_paneTemplatePath
[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
169 $this->assign('allPanes', $allPanes);
170 if (!$this->_searchPane
) {
171 parent
::buildQuickForm();
174 $this->assign('suppressForm', TRUE);
179 * Use the form name to create the tpl file name.
186 public function getTemplateFileName() {
187 if (!$this->_searchPane
) {
188 return parent
::getTemplateFileName();
191 if (isset($this->_paneTemplatePath
[$this->_searchPane
])) {
192 return $this->_paneTemplatePath
[$this->_searchPane
];
195 $name = ucfirst($this->_searchPane
);
196 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
202 * Set the default form values.
206 * the default array reference
208 public function setDefaultValues() {
209 $defaults = $this->_formValues
;
210 $this->normalizeDefaultValues($defaults);
212 if ($this->_context
=== 'amtg') {
213 $defaults['task'] = CRM_Contact_Task
::GROUP_CONTACTS
;
216 $defaults['privacy_toggle'] = 1;
217 $defaults['operator'] = 'AND';
223 * The post processing of the form gets done here.
225 * Key things done during post processing are
226 * - check for reset or next request. if present, skip post procesing.
227 * - now check if user requested running a saved search, if so, then
228 * the form values associated with the saved search are used for searching.
229 * - if user has done a submit with new values the regular post submissing is
231 * The processing consists of using a Selector / Controller framework for getting the
238 public function postProcess() {
239 $this->set('isAdvanced', '1');
241 // get user submitted values
242 // get it from controller only if form has been submitted, else preProcess has set this
243 if (!empty($_POST)) {
244 $this->_formValues
= $this->controller
->exportValues($this->_name
);
245 $this->normalizeFormValues();
246 // FIXME: couldn't figure out a good place to do this,
247 // FIXME: so leaving this as a dependency for now
248 if (array_key_exists('contribution_amount_low', $this->_formValues
)) {
250 'contribution_amount_low',
251 'contribution_amount_high',
253 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
257 // set the group if group is submitted
258 if (!empty($this->_formValues
['uf_group_id'])) {
259 $this->set('id', $this->_formValues
['uf_group_id']);
262 $this->set('id', '');
266 // retrieve ssID values only if formValues is null, i.e. form has never been posted
267 if (empty($this->_formValues
) && isset($this->_ssID
)) {
268 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
271 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
272 $this->_formValues
['group'] = array($this->_groupID
=> 1);
275 //search for civicase
276 if (is_array($this->_formValues
)) {
278 if (array_key_exists('case_owner', $this->_formValues
) &&
279 !$this->_formValues
['case_owner'] &&
287 ) as $caseCriteria) {
288 if (!empty($this->_formValues
[$caseCriteria])) {
290 $this->_formValues
['case_owner'] = 1;
295 if (CRM_Core_Permission
::check('access all cases and activities')) {
296 $this->_formValues
['case_owner'] = 1;
299 $this->_formValues
['case_owner'] = 2;
303 $this->_formValues
['case_owner'] = 0;
306 if (array_key_exists('case_owner', $this->_formValues
) && empty($this->_formValues
['case_deleted'])) {
307 $this->_formValues
['case_deleted'] = 0;
311 // we dont want to store the sortByCharacter in the formValue, it is more like
312 // a filter on the result set
313 // this filter is reset if we click on the search button
314 if ($this->_sortByCharacter
!== NULL && empty($_POST)) {
315 if (strtolower($this->_sortByCharacter
) == 'all') {
316 $this->_formValues
['sortByCharacter'] = NULL;
319 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
323 $this->_sortByCharacter
= NULL;
326 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
328 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
329 $this->_returnProperties
= &$this->returnProperties();
330 parent
::postProcess();
334 * Normalize the form values to make it look similar to the advanced form values
335 * this prevents a ton of work downstream and allows us to use the same code for
336 * multiple purposes (queries, save/edit etc)
340 public function normalizeFormValues() {
341 $contactType = CRM_Utils_Array
::value('contact_type', $this->_formValues
);
343 if ($contactType && is_array($contactType)) {
344 unset($this->_formValues
['contact_type']);
345 foreach ($contactType as $key => $value) {
346 $this->_formValues
['contact_type'][$value] = 1;
350 $config = CRM_Core_Config
::singleton();
351 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
352 if ($group && is_array($group)) {
353 unset($this->_formValues
['group']);
354 foreach ($group as $key => $value) {
355 $this->_formValues
['group'][$value] = 1;
359 $tag = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
360 if ($tag && is_array($tag)) {
361 unset($this->_formValues
['contact_tags']);
362 foreach ($tag as $key => $value) {
363 $this->_formValues
['contact_tags'][$value] = 1;
367 $specialParams = array(
369 'contribution_soft_credit_type_id',
370 'contribution_status',
371 'contribution_status_id',
372 'contribution_source',
373 'membership_type_id',
374 'membership_status_id',
375 'participant_status_id',
376 'contribution_trxn_id',
380 'contribution_page_id',
381 'contribution_product_id',
382 'payment_instrument_id',
384 foreach ($specialParams as $element) {
385 $value = CRM_Utils_Array
::value($element, $this->_formValues
);
387 if (is_array($value)) {
388 if ($element == 'status_id') {
389 unset($this->_formValues
[$element]);
390 $element = 'activity_' . $element;
392 $this->_formValues
[$element] = array('IN' => $value);
395 $this->_formValues
[$element] = array('LIKE' => "%$value%");
400 $taglist = CRM_Utils_Array
::value('contact_taglist', $this->_formValues
);
402 if ($taglist && is_array($taglist)) {
403 unset($this->_formValues
['contact_taglist']);
404 foreach ($taglist as $value) {
406 $value = explode(',', $value);
407 foreach ($value as $tId) {
408 if (is_numeric($tId)) {
409 $this->_formValues
['contact_tags'][$tId] = 1;
418 * Normalize default values for multiselect plugins.
420 * @param array $defaults
423 public function normalizeDefaultValues(&$defaults) {
424 if (!is_array($defaults)) {
428 if ($this->_ssID
&& empty($_POST)) {
429 $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id');
431 foreach ($defaults as $element => $value) {
432 if (!empty($value) && is_array($value)) {
433 if (in_array($element, $specialFields)) {
434 $element = str_replace('member_membership_type_id', 'membership_type_id', $element);
435 $element = str_replace('member_status_id', 'membership_status_id', $element);
436 $defaults[$element] = array_keys($value);
438 // As per the OK (Operator as Key) value format, value array may contain key
439 // as an operator so to ensure the default is always set actual value
440 elseif (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
441 $defaults[$element] = CRM_Utils_Array
::value(key($value), $value);
442 if (is_string($defaults[$element])) {
443 $defaults[$element] = str_replace("%", '', $defaults[$element]);