copyright and version fixes
[civicrm-core.git] / CRM / Contact / Form / Search / Advanced.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
9df8a11d 11s | under the terms of the GNU Affero General Public License |
6a488035
TO
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
39
40/**
41 * advanced search, extends basic search
42 */
43class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
44
45 /**
46 * processing needed for buildForm and later
47 *
48 * @return void
49 * @access public
50 */
51 function preProcess() {
52 $this->set('searchFormName', 'Advanced');
53
54 parent::preProcess();
55 $openedPanes = CRM_Contact_BAO_Query::$_openedPanes;
56 $openedPanes = array_merge($openedPanes, $this->_openedPanes);
57 $this->assign('openedPanes', $openedPanes);
58 }
59
60 /**
61 * Build the form
62 *
63 * @access public
64 *
65 * @return void
66 */
67 function buildQuickForm() {
68 $this->set('context', 'advanced');
69
70 $this->_searchPane = CRM_Utils_Array::value('searchPane', $_GET);
71
72 $this->_searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
73 'advanced_search_options'
74 );
75
76 if (!$this->_searchPane || $this->_searchPane == 'basic') {
77 CRM_Contact_Form_Search_Criteria::basic($this);
78 }
79
80 $allPanes = array();
81 $paneNames = array(
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',
89 );
90
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()
95 );
96 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
97 $extends
98 );
99 // if no searchable fields unset panel
100 if (empty($groupDetails)) {
101 unset($paneNames[ts('Custom Fields')]);
102 }
103
104 foreach ($paneNames as $name => $type) {
105 if (!$this->_searchOptions[$type]) {
106 unset($paneNames[$name]);
107 }
108 }
109
110 $components = CRM_Core_Component::getEnabledComponents();
111
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)
117 ) {
118 $componentPanes[$name] = $component->registerAdvancedSearchPane();
119 $componentPanes[$name]['name'] = $name;
120 }
121 }
122
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'];
127 }
128
efa3a566
DS
129 $hookPanes = array();
130 CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
131 $paneNames = array_merge($paneNames, $hookPanes);
6a488035 132
efa3a566 133 $this->_paneTemplatePath = array();
6a488035 134 foreach ($paneNames as $name => $type) {
efa3a566 135 if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) {
6a488035
TO
136 continue;
137 }
138
139 $allPanes[$name] = array(
140 'url' => CRM_Utils_System::url('civicrm/contact/search/advanced',
141 "snippet=1&searchPane=$type&qfKey={$this->controller->_key}"
142 ),
143 'open' => 'false',
144 'id' => $type,
145 );
146
147 // see if we need to include this paneName in the current form
8cc574cf 148 if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) ||
6a488035
TO
149 CRM_Utils_Array::value("hidden_{$type}", $this->_formValues)
150 ) {
151 $allPanes[$name]['open'] = 'true';
152
a7488080 153 if (!empty($components[$type])) {
6a488035
TO
154 $c = $components[$type];
155 $this->add('hidden', "hidden_$type", 1);
156 $c->buildAdvancedSearchPaneForm($this);
157 $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath();
158 }
efa3a566
DS
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);
162 }
6a488035 163 else {
0e6e8724 164 CRM_Contact_Form_Search_Criteria::$type($this);
6a488035
TO
165 $template = ucfirst($type);
166 $this->_paneTemplatePath[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
167 }
168 }
169 }
170
171 $this->assign('allPanes', $allPanes);
172 if (!$this->_searchPane) {
173 parent::buildQuickForm();
174 }
175 else {
176 $this->assign('suppressForm', TRUE);
177 }
178 }
179
180 function getTemplateFileName() {
181 if (!$this->_searchPane) {
182 return parent::getTemplateFileName();
183 }
184 else {
185 if (isset($this->_paneTemplatePath[$this->_searchPane])) {
186 return $this->_paneTemplatePath[$this->_searchPane];
187 }
188 else {
189 $name = ucfirst($this->_searchPane);
190 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
191 }
192 }
193 }
194
195 /**
196 * Set the default form values
197 *
198 * @access protected
199 *
200 * @return array the default array reference
201 */
202 function setDefaultValues() {
203 $defaults = $this->_formValues;
204 $this->normalizeDefaultValues($defaults);
205
206 if ($this->_context === 'amtg') {
207 $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
208 }
6a488035
TO
209
210 $defaults['privacy_toggle'] = 1;
211
212 return $defaults;
213 }
214
215 /**
216 * The post processing of the form gets done here.
217 *
218 * Key things done during post processing are
219 * - check for reset or next request. if present, skip post procesing.
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 submissing is
223 * done.
224 * The processing consists of using a Selector / Controller framework for getting the
225 * search results.
226 *
227 * @param
228 *
229 * @return void
230 * @access public
231 */
232 function postProcess() {
233 $this->set('isAdvanced', '1');
234
235 // get user submitted values
236 // get it from controller only if form has been submitted, else preProcess has set this
237 if (!empty($_POST)) {
238 $this->_formValues = $this->controller->exportValues($this->_name);
239 $this->normalizeFormValues();
240 // FIXME: couldn't figure out a good place to do this,
241 // FIXME: so leaving this as a dependency for now
242 if (array_key_exists('contribution_amount_low', $this->_formValues)) {
243 foreach (array(
244 'contribution_amount_low', 'contribution_amount_high') as $f) {
245 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
246 }
247 }
248
249 // set the group if group is submitted
81aa6678 250 if (!empty($this->_formValues['uf_group_id'])) {
6a488035
TO
251 $this->set('id', $this->_formValues['uf_group_id']);
252 }
253 else {
254 $this->set('id', '');
255 }
256 }
257
258 // retrieve ssID values only if formValues is null, i.e. form has never been posted
259 if (empty($this->_formValues) && isset($this->_ssID)) {
260 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
261 }
262
8cc574cf 263 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
6a488035
TO
264 $this->_formValues['group'] = array($this->_groupID => 1);
265 }
266
267
268 //search for civicase
269 if (is_array($this->_formValues)) {
270 $allCases = FALSE;
271 if (array_key_exists('case_owner', $this->_formValues) &&
272 !$this->_formValues['case_owner'] &&
273 !$this->_force
274 ) {
275 foreach (array(
276 'case_type_id', 'case_status_id', 'case_deleted', 'case_tags') as $caseCriteria) {
a7488080 277 if (!empty($this->_formValues[$caseCriteria])) {
6a488035
TO
278 $allCases = TRUE;
279 $this->_formValues['case_owner'] = 1;
280 continue;
281 }
282 }
283 if ($allCases) {
284 if (CRM_Core_Permission::check('access all cases and activities')) {
285 $this->_formValues['case_owner'] = 1;
286 }
287 else {
288 $this->_formValues['case_owner'] = 2;
289 }
290 }
291 else {
292 $this->_formValues['case_owner'] = 0;
293 }
294 }
295 }
296
297 // we dont want to store the sortByCharacter in the formValue, it is more like
298 // a filter on the result set
299 // this filter is reset if we click on the search button
e166ff79 300 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
6a488035
TO
301 if (strtolower($this->_sortByCharacter) == 'all') {
302 $this->_formValues['sortByCharacter'] = NULL;
303 }
304 else {
305 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
306 }
307 }
e166ff79
CW
308 else {
309 $this->_sortByCharacter = NULL;
310 }
6a488035
TO
311
312 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
313
314 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
315 $this->_returnProperties = &$this->returnProperties();
316 parent::postProcess();
317 }
318
319 /**
320 * normalize the form values to make it look similar to the advanced form values
321 * this prevents a ton of work downstream and allows us to use the same code for
322 * multiple purposes (queries, save/edit etc)
323 *
324 * @return void
325 * @access private
326 */
327 function normalizeFormValues() {
328 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
329
330 if ($contactType && is_array($contactType)) {
331 unset($this->_formValues['contact_type']);
332 foreach ($contactType as $key => $value) {
333 $this->_formValues['contact_type'][$value] = 1;
334 }
335 }
336
337 $config = CRM_Core_Config::singleton();
338 $group = CRM_Utils_Array::value('group', $this->_formValues);
339 if ($group && is_array($group)) {
340 unset($this->_formValues['group']);
341 foreach ($group as $key => $value) {
342 $this->_formValues['group'][$value] = 1;
343 }
344 }
345
346 $tag = CRM_Utils_Array::value('contact_tags', $this->_formValues);
347 if ($tag && is_array($tag)) {
348 unset($this->_formValues['contact_tags']);
349 foreach ($tag as $key => $value) {
350 $this->_formValues['contact_tags'][$value] = 1;
351 }
352 }
1f0d8c92
PN
353
354 // CRM-13848
355 $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues);
356 if ($financialType && is_array($financialType)) {
357 unset($this->_formValues['financial_type_id']);
358 foreach($financialType as $notImportant => $typeID) {
359 $this->_formValues['financial_type_id'][$typeID] = 1;
360 }
361 }
362
6a488035
TO
363 $taglist = CRM_Utils_Array::value('contact_taglist', $this->_formValues);
364
365 if ($taglist && is_array($taglist)) {
366 unset($this->_formValues['contact_taglist']);
367 foreach ($taglist as $value) {
368 if ($value) {
369 $value = explode(',', $value);
370 foreach ($value as $tId) {
371 if (is_numeric($tId)) {
372 $this->_formValues['contact_tags'][$tId] = 1;
373 }
374 }
375 }
376 }
377 }
378
379 return;
380 }
381
382 /**
383 * normalize default values for multiselect plugins
384 *
f4bff68a
CW
385 * @param $defaults array
386 * @return array
6a488035
TO
387 * @access private
388 */
389 function normalizeDefaultValues(&$defaults) {
390 if (!is_array($defaults)) {
391 $defaults = array();
392 }
393
394 if ($this->_ssID && empty($_POST)) {
395 $fields = array('contact_type', 'group', 'contact_tags');
396
397 foreach ($fields as $field) {
398 $fieldValues = CRM_Utils_Array::value($field, $defaults);
399 if ($fieldValues && is_array($fieldValues)) {
400 $defaults[$field] = array_keys($fieldValues);
401 }
402 }
403 }
404 return $defaults;
405 }
406}
407