Merge pull request #1176 from pratik-joshi/CRM-13025
[civicrm-core.git] / CRM / Contact / Form / Search / Advanced.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 |
11 s | under the terms of the GNU Affero General Public License |
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 */
43 class 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
129 $hookPanes = array();
130 CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
131 $paneNames = array_merge($paneNames, $hookPanes);
132
133 $this->_paneTemplatePath = array();
134 foreach ($paneNames as $name => $type) {
135 if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) {
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
148 if ($this->_searchPane == $type ||
149 CRM_Utils_Array::value("hidden_{$type}", $_POST) ||
150 CRM_Utils_Array::value("hidden_{$type}", $this->_formValues)
151 ) {
152 $allPanes[$name]['open'] = 'true';
153
154 if (CRM_Utils_Array::value($type, $components)) {
155 $c = $components[$type];
156 $this->add('hidden', "hidden_$type", 1);
157 $c->buildAdvancedSearchPaneForm($this);
158 $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath();
159 }
160 else if (in_array($type, $hookPanes)) {
161 CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type);
162 CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type);
163 }
164 else {
165 CRM_Contact_Form_Search_Criteria::$type($this);
166 $template = ucfirst($type);
167 $this->_paneTemplatePath[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
168 }
169 }
170 }
171
172 $this->assign('allPanes', $allPanes);
173 if (!$this->_searchPane) {
174 parent::buildQuickForm();
175 }
176 else {
177 $this->assign('suppressForm', TRUE);
178 }
179 }
180
181 function getTemplateFileName() {
182 if (!$this->_searchPane) {
183 return parent::getTemplateFileName();
184 }
185 else {
186 if (isset($this->_paneTemplatePath[$this->_searchPane])) {
187 return $this->_paneTemplatePath[$this->_searchPane];
188 }
189 else {
190 $name = ucfirst($this->_searchPane);
191 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
192 }
193 }
194 }
195
196 /**
197 * Set the default form values
198 *
199 * @access protected
200 *
201 * @return array the default array reference
202 */
203 function setDefaultValues() {
204 $defaults = $this->_formValues;
205 $this->normalizeDefaultValues($defaults);
206
207 if ($this->_context === 'amtg') {
208 $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
209 }
210 else {
211 $defaults['task'] = CRM_Contact_Task::PRINT_CONTACTS;
212 }
213
214 $defaults['privacy_toggle'] = 1;
215
216 return $defaults;
217 }
218
219 /**
220 * The post processing of the form gets done here.
221 *
222 * Key things done during post processing are
223 * - check for reset or next request. if present, skip post procesing.
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 submissing is
227 * done.
228 * The processing consists of using a Selector / Controller framework for getting the
229 * search results.
230 *
231 * @param
232 *
233 * @return void
234 * @access public
235 */
236 function postProcess() {
237 $this->set('isAdvanced', '1');
238
239 // get user submitted values
240 // get it from controller only if form has been submitted, else preProcess has set this
241 if (!empty($_POST)) {
242 $this->_formValues = $this->controller->exportValues($this->_name);
243 $this->normalizeFormValues();
244 // FIXME: couldn't figure out a good place to do this,
245 // FIXME: so leaving this as a dependency for now
246 if (array_key_exists('contribution_amount_low', $this->_formValues)) {
247 foreach (array(
248 'contribution_amount_low', 'contribution_amount_high') as $f) {
249 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
250 }
251 }
252
253 // set the group if group is submitted
254 if (!empty($this->_formValues['uf_group_id'])) {
255 $this->set('id', $this->_formValues['uf_group_id']);
256 }
257 else {
258 $this->set('id', '');
259 }
260 }
261
262 // retrieve ssID values only if formValues is null, i.e. form has never been posted
263 if (empty($this->_formValues) && isset($this->_ssID)) {
264 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
265 }
266
267 if (isset($this->_groupID) && !CRM_Utils_Array::value('group', $this->_formValues)) {
268 $this->_formValues['group'] = array($this->_groupID => 1);
269 }
270
271
272 //search for civicase
273 if (is_array($this->_formValues)) {
274 $allCases = FALSE;
275 if (array_key_exists('case_owner', $this->_formValues) &&
276 !$this->_formValues['case_owner'] &&
277 !$this->_force
278 ) {
279 foreach (array(
280 'case_type_id', 'case_status_id', 'case_deleted', 'case_tags') as $caseCriteria) {
281 if (CRM_Utils_Array::value($caseCriteria, $this->_formValues)) {
282 $allCases = TRUE;
283 $this->_formValues['case_owner'] = 1;
284 continue;
285 }
286 }
287 if ($allCases) {
288 if (CRM_Core_Permission::check('access all cases and activities')) {
289 $this->_formValues['case_owner'] = 1;
290 }
291 else {
292 $this->_formValues['case_owner'] = 2;
293 }
294 }
295 else {
296 $this->_formValues['case_owner'] = 0;
297 }
298 }
299 }
300
301 // we dont want to store the sortByCharacter in the formValue, it is more like
302 // a filter on the result set
303 // this filter is reset if we click on the search button
304 if ($this->_sortByCharacter !== NULL
305 && empty($_POST)
306 ) {
307 if (strtolower($this->_sortByCharacter) == 'all') {
308 $this->_formValues['sortByCharacter'] = NULL;
309 }
310 else {
311 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
312 }
313 }
314
315 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
316
317 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
318 $this->_returnProperties = &$this->returnProperties();
319 parent::postProcess();
320 }
321
322 /**
323 * normalize the form values to make it look similar to the advanced form values
324 * this prevents a ton of work downstream and allows us to use the same code for
325 * multiple purposes (queries, save/edit etc)
326 *
327 * @return void
328 * @access private
329 */
330 function normalizeFormValues() {
331 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
332
333 if ($contactType && is_array($contactType)) {
334 unset($this->_formValues['contact_type']);
335 foreach ($contactType as $key => $value) {
336 $this->_formValues['contact_type'][$value] = 1;
337 }
338 }
339
340 $config = CRM_Core_Config::singleton();
341 $group = CRM_Utils_Array::value('group', $this->_formValues);
342 if ($group && is_array($group)) {
343 unset($this->_formValues['group']);
344 foreach ($group as $key => $value) {
345 $this->_formValues['group'][$value] = 1;
346 }
347 }
348
349 $tag = CRM_Utils_Array::value('contact_tags', $this->_formValues);
350 if ($tag && is_array($tag)) {
351 unset($this->_formValues['contact_tags']);
352 foreach ($tag as $key => $value) {
353 $this->_formValues['contact_tags'][$value] = 1;
354 }
355 }
356
357 $taglist = CRM_Utils_Array::value('contact_taglist', $this->_formValues);
358
359 if ($taglist && is_array($taglist)) {
360 unset($this->_formValues['contact_taglist']);
361 foreach ($taglist as $value) {
362 if ($value) {
363 $value = explode(',', $value);
364 foreach ($value as $tId) {
365 if (is_numeric($tId)) {
366 $this->_formValues['contact_tags'][$tId] = 1;
367 }
368 }
369 }
370 }
371 }
372
373 return;
374 }
375
376 /**
377 * normalize default values for multiselect plugins
378 *
379 * @return void
380 * @access private
381 */
382 function normalizeDefaultValues(&$defaults) {
383 if (!is_array($defaults)) {
384 $defaults = array();
385 }
386
387 if ($this->_ssID && empty($_POST)) {
388 $fields = array('contact_type', 'group', 'contact_tags');
389
390 foreach ($fields as $field) {
391 $fieldValues = CRM_Utils_Array::value($field, $defaults);
392 if ($fieldValues && is_array($fieldValues)) {
393 $defaults[$field] = array_keys($fieldValues);
394 }
395 }
396 }
397 return $defaults;
398 }
399 }
400