Refactored out of CRM_Core_PseudoConstant: IMProvider(), pcm(), preferredCommunicatio...
[civicrm-core.git] / CRM / Contact / Form / Search / Criteria.php
CommitLineData
6a488035
TO
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 | 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 */
35class CRM_Contact_Form_Search_Criteria {
36
37 static function basic(&$form) {
38 $form->addElement('hidden', 'hidden_basic', 1);
39
40 if ($form->_searchOptions['contactType']) {
41 // add checkboxes for contact type
42 $contact_type = array();
43 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements();
44
45 if ($contactTypes) {
46 $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE,
47 array('id' => 'contact_type', 'multiple' => 'multiple', 'title' => ts('- select -'))
48 );
49 }
50 }
51
52 if ($form->_searchOptions['groups']) {
53 // multiselect for groups
54 if ($form->_group) {
55 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
f828fa2c 56 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($form->_group, NULL, '&nbsp;&nbsp;', TRUE);
6a488035
TO
57
58 $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE,
59 array('id' => 'group', 'multiple' => 'multiple', 'title' => ts('- select -'))
60 );
61 $groupOptions = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type');
62 $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE,
63 array('id' => 'group_type', 'multiple' => 'multiple', 'title' => ts('- select -'))
64 );
65 $form->add('hidden','group_search_selected','group');
66 }
67 }
68
69 if ($form->_searchOptions['tags']) {
70 // multiselect for categories
71 $contactTags = CRM_Core_BAO_Tag::getTags();
72
73 if ($contactTags) {
74 $form->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
75 array('id' => 'contact_tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
76 );
77 }
78
79 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
80 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE, TRUE);
f828fa2c 81
6a488035
TO
82 $used_for = CRM_Core_OptionGroup::values('tag_used_for');
83 $tagsTypes = array();
84 $showAllTagTypes = false;
85 foreach ($used_for as $key => $value) {
86 //check tags for every type and find if there are any defined
87 $tags = CRM_Core_BAO_Tag::getTagsUsedFor($key, FALSE, TRUE, NULL);
88 // check if there are tags other than contact type, if no - keep checkbox hidden on adv search
89 // we will hide searching contact by attachments tags until it will be implemented in core
90 if (count($tags) && $key != 'civicrm_file' && $key != 'civicrm_contact') {
91 //if tags exists then add type to display in adv search form help text
f828fa2c 92 $tagsTypes[] = ts($value);
6a488035
TO
93 $showAllTagTypes = true;
94 }
95 }
96 $tagTypesText = implode(" or ", $tagsTypes);
97 if ($showAllTagTypes) {
98 $form->add('checkbox', 'all_tag_types', ts('Include tags used for %1', array(1 => $tagTypesText)));
99 $form->add('hidden','tag_types_text', $tagTypesText);
100 }
101 }
102
103 // add text box for last name, first name, street name, city
104 $form->addElement('text', 'sort_name', ts('Find...'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
105
106 // add text box for last name, first name, street name, city
107 $form->add('text', 'email', ts('Contact Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
108
109 //added contact source
110 $form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'contact_source'));
111
112 //added job title
113 $form->addElement('text', 'job_title', ts('Job Title'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'job_title'));
114
115
116 //added internal ID
c8ef633b 117 $form->addElement('text', 'contact_id', ts('Contact ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'id'));
6a488035
TO
118
119 //added external ID
120 $form->addElement('text', 'external_identifier', ts('External ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier'));
121
122 if (CRM_Core_Permission::check('access deleted contacts') and CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL)) {
123 $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
124 }
125
126 // add checkbox for cms users only
127 $form->addYesNo('uf_user', ts('CMS User?'));
128
129 // tag all search
130 $form->add('text', 'tag_search', ts('All Tags'));
131
132 // add search profiles
133
134 // FIXME: This is probably a part of profiles - need to be
135 // FIXME: eradicated from here when profiles are reworked.
136 $types = array('Participant', 'Contribution', 'Membership');
137
138 // get component profiles
139 $componentProfiles = array();
140 $componentProfiles = CRM_Core_BAO_UFGroup::getProfiles($types);
141
142 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Search Profile', 1);
143 $accessibleUfGroups = CRM_Core_Permission::ufGroup(CRM_Core_Permission::VIEW);
144
145 $searchProfiles = array();
146 foreach ($ufGroups as $key => $var) {
147 if (!array_key_exists($key, $componentProfiles) && in_array($key, $accessibleUfGroups)) {
148 $searchProfiles[$key] = $var['title'];
149 }
150 }
151
152 $form->addElement('select',
153 'uf_group_id',
154 ts('Search Views'),
155 array(
156 '0' => ts('- default view -')) + $searchProfiles
157 );
158
159 $componentModes = CRM_Contact_Form_Search::getModeSelect();
160
161 // unset contributions or participants if user does not have
162 // permission on them
163 if (!CRM_Core_Permission::access('CiviContribute')) {
164 unset($componentModes['2']);
165 }
166
167 if (!CRM_Core_Permission::access('CiviEvent')) {
168 unset($componentModes['3']);
169 }
170
171 if (!CRM_Core_Permission::access('CiviMember')) {
172 unset($componentModes['5']);
173 }
174
175 if (!CRM_Core_Permission::check('view all activities')) {
176 unset($componentModes['4']);
177 }
178
179 if (count($componentModes) > 1) {
180 $form->addElement('select',
181 'component_mode',
182 ts('Display Results As'),
183 $componentModes
184 );
185 }
186
187 $form->addElement('select',
188 'operator',
189 ts('Search Operator'),
190 array('AND' => ts('AND'),
191 'OR' => ts('OR'),
192 )
193 );
194
195 // add the option to display relationships
196 $rTypes = CRM_Core_PseudoConstant::relationshipType();
197 $rSelect = array('' => ts('- Select Relationship Type-'));
198 foreach ($rTypes as $rid => $rValue) {
199 if ($rValue['label_a_b'] == $rValue['label_b_a']) {
200 $rSelect[$rid] = $rValue['label_a_b'];
201 }
202 else {
203 $rSelect["{$rid}_a_b"] = $rValue['label_a_b'];
204 $rSelect["{$rid}_b_a"] = $rValue['label_b_a'];
205 }
206 }
207
208 $form->addElement('select',
209 'display_relationship_type',
210 ts('Display Results as Relationship'),
211 $rSelect
212 );
213
214 // checkboxes for DO NOT phone, email, mail
215 // we take labels from SelectValues
216 $t = CRM_Core_SelectValues::privacy();
217 $form->add('select',
218 'privacy_options',
219 ts('Privacy'),
220 $t,
221 FALSE,
222 array(
223 'id' => 'privacy_options',
224 'multiple' => 'multiple',
225 'title' => ts('- select -'),
226 )
227 );
228
229 $form->addElement('select',
230 'privacy_operator',
231 ts('Operator'),
232 array('OR' => ts('OR'),
233 'AND' => ts('AND'),
234 )
235 );
236
237 $options = array(
238 1 => ts('Exclude'),
239 2 => ts('Include by Privacy Option(s)'),
240 );
241 $form->addRadio('privacy_toggle', ts('Privacy Options'), $options);
242
243 // preferred communication method
e7e657f0 244 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
6a488035
TO
245
246 $commPreff = array();
247 foreach ($comm as $k => $v) {
248 $commPreff[] = $form->createElement('advcheckbox', $k, NULL, $v);
249 }
250
251 $onHold[] = $form->createElement('advcheckbox', 'on_hold', NULL, ts(''));
252 $form->addGroup($onHold, 'email_on_hold', ts('Email On Hold'));
253
254 $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method'));
255
256 //CRM-6138 Preferred Language
257 $langPreff = CRM_Core_PseudoConstant::languages();
258 $form->add('select', 'preferred_language', ts('Preferred Language'), array('' => ts('- any -')) + $langPreff);
259
260 // Phone search
261 $form->addElement('text', 'phone_numeric', ts('Phone Number'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'));
b2b0530a 262 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
b4f964d9 263 $phoneType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
6a488035
TO
264 $form->add('select', 'phone_location_type_id', ts('Phone Location'), array('' => ts('- any -')) + $locationType);
265 $form->add('select', 'phone_phone_type_id', ts('Phone Type'), array('' => ts('- any -')) + $phoneType);
266 }
267
268
269 static function location(&$form) {
270 // Build location criteria based on _submitValues if
271 // available; otherwise, use $form->_formValues.
272 $formValues = $form->_submitValues;
273
274 if (empty($formValues) && !empty($form->_formValues)) {
275 $formValues = $form->_formValues;
276 }
277
278 $form->addElement('hidden', 'hidden_location', 1);
279
280 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
281 'address_options', TRUE, NULL, TRUE
282 );
283
284 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
285
286 $elements = array(
287 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL, NULL),
288 'city' => array(ts('City'), $attributes['city'], NULL, NULL),
289 'postal_code' => array(ts('Zip / Postal Code'), $attributes['postal_code'], NULL, NULL),
290 'county' => array(ts('County'), $attributes['county_id'], 'county', TRUE),
291 'state_province' => array(ts('State / Province'), $attributes['state_province_id'], 'stateProvince', TRUE),
292 'country' => array(ts('Country'), $attributes['country_id'], 'country', FALSE),
293 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL, NULL),
294 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL, NULL),
295 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL, NULL),
296 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL),
297 );
298
299 $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', $addressOptions, 0);
300 $form->assign('parseStreetAddress', $parseStreetAddress);
301 foreach ($elements as $name => $v) {
302 list($title, $attributes, $select, $multiSelect) = $v;
303
304 if (in_array($name,
305 array('street_number', 'street_name', 'street_unit')
306 )) {
307 if (!$parseStreetAddress) {
308 continue;
309 }
310 }
311 elseif (!$addressOptions[$name]) {
312 continue;
313 }
314
315 if (!$attributes) {
316 $attributes = $attributes[$name];
317 }
318
319 if ($select) {
6a488035
TO
320 $stateCountryMap[] = array(
321 'state_province' => 'state_province',
322 'country' => 'country',
323 'county' => 'county',
324 );
325 if ($select == 'stateProvince') {
b2891982 326 if (CRM_Utils_Array::value('country', $formValues)) {
6a488035
TO
327 $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($formValues['country']);
328 }
329 else {
330 //if not setdefault any country
331 $selectElements = array('' => ts('- any -')) + CRM_Core_PseudoConstant::$select();
332 }
333 $element = $form->addElement('select', $name, $title, $selectElements);
334 }
335 elseif ($select == 'country') {
6a488035
TO
336 $selectElements = array('' => ts('- any -')) + CRM_Core_PseudoConstant::$select();
337 $element = $form->addElement('select', $name, $title, $selectElements);
338 }
339 elseif ($select == 'county') {
340 if ( array_key_exists('state_province', $formValues) && !CRM_Utils_System::isNull($formValues['state_province'])) {
341 $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::countyForState($formValues['state_province']);
342 }
343 else {
344 $selectElements = array('' => ts('- any -'));
345 }
346 $element = $form->addElement('select', $name, $title, $selectElements);
347 }
348 else {
349 $selectElements = array('' => ts('- any -')) + CRM_Core_PseudoConstant::$select();
350 $element = $form->addElement('select', $name, $title, $selectElements);
351 }
352 if ($multiSelect) {
353 $element->setMultiple(TRUE);
354 }
355 }
356 else {
357 $form->addElement('text', $name, $title, $attributes);
358 }
359
360 if ($addressOptions['postal_code']) {
361 $form->addElement('text', 'postal_code_low', ts('Range-From'),
362 CRM_Utils_Array::value('postal_code', $attributes)
363 );
364 $form->addElement('text', 'postal_code_high', ts('To'),
365 CRM_Utils_Array::value('postal_code', $attributes)
366 );
367 }
368 }
369
2f1714c3 370 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
c8ef633b 371
6a488035
TO
372 // extend addresses with proximity search
373 $form->addElement('text', 'prox_distance', ts('Find contacts within'));
374 $form->addElement('select', 'prox_distance_unit', NULL, array('miles' => ts('Miles'), 'kilos' => ts('Kilometers')));
375
376 // is there another form rule that does decimals besides money ? ...
377 $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric');
378
6a488035
TO
379 $worldRegions = array('' => ts('- any region -')) + CRM_Core_PseudoConstant::worldRegion();
380 $form->addElement('select', 'world_region', ts('World Region'), $worldRegions);
381
382 // checkboxes for location type
383 $location_type = array();
b2b0530a 384 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
385 foreach ($locationType as $locationTypeID => $locationTypeName) {
386 $location_type[] = $form->createElement('checkbox', $locationTypeID, NULL, $locationTypeName);
387 }
388 $form->addGroup($location_type, 'location_type', ts('Location Types'), '&nbsp;');
389
390 // custom data extending addresses -
391 $extends = array('Address');
392 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
393 if ($groupDetails) {
394 $form->assign('addressGroupTree', $groupDetails);
395 foreach ($groupDetails as $group) {
396 foreach ($group['fields'] as $field) {
397 $elementName = 'custom_' . $field['id'];
398 CRM_Core_BAO_CustomField::addQuickFormElement($form,
399 $elementName,
400 $field['id'],
401 FALSE, FALSE, TRUE
402 );
403 }
404 }
405 }
406 }
407
408 static function activity(&$form) {
409 $form->add('hidden', 'hidden_activity', 1);
410 CRM_Activity_BAO_Query::buildSearchForm($form);
411 }
412
413 static function changeLog(&$form) {
414 $form->add('hidden', 'hidden_changeLog', 1);
415
416 // block for change log
417 $form->addElement('text', 'changed_by', ts('Modified By'), NULL);
418
419 $dates = array(1 => ts('Added'), 2 => ts('Modified'));
420 $form->addRadio('log_date', NULL, $dates, NULL, '<br />');
421
422 CRM_Core_Form_Date::buildDateRange($form, 'log_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
423 }
424
425 static function task(&$form) {
426 $form->add('hidden', 'hidden_task', 1);
427 }
428
429 static function relationship(&$form) {
430 $form->add('hidden', 'hidden_relationship', 1);
431
432 $allRelationshipType = array();
433 $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
434 $form->addElement('select', 'relation_type_id', ts('Relationship Type'), array('' => ts('- select -')) + $allRelationshipType);
435 $form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
436 $relStatusOption = array(ts('Active '), ts('Inactive '), ts('All'));
437 $form->addRadio('relation_status', ts('Relationship Status'), $relStatusOption);
438 $form->setDefaults(array('relation_status' => 0));
439
440 //add the target group
441 if ($form->_group) {
442 $form->add('select', 'relation_target_group', ts('Target Contact(s) in Group'), $form->_group, FALSE,
443 array('id' => 'relation_target_group', 'multiple' => 'multiple', 'title' => ts('- select -'))
444 );
445 }
446
447 // Add reltionship dates
448 CRM_Core_Form_Date::buildDateRange($form, 'relation_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE);
449
450 // add all the custom searchable fields
451 $relationship = array('Relationship');
452 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $relationship);
453 if ($groupDetails) {
454 $form->assign('relationshipGroupTree', $groupDetails);
455 foreach ($groupDetails as $group) {
456 foreach ($group['fields'] as $field) {
457 $fieldId = $field['id'];
458 $elementName = 'custom_' . $fieldId;
459 CRM_Core_BAO_CustomField::addQuickFormElement($form,
460 $elementName,
461 $fieldId,
462 FALSE, FALSE, TRUE
463 );
464 }
465 }
466 }
467 }
468
469 static function demographics(&$form) {
470 $form->add('hidden', 'hidden_demographics', 1);
471 // radio button for gender
472 $genderOptions = array();
26cf88b5 473 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
6a488035
TO
474 foreach ($gender as $key => $var) {
475 $genderOptions[$key] = $form->createElement('radio', NULL,
476 ts('Gender'), $var, $key,
477 array('id' => "civicrm_gender_{$var}_{$key}")
478 );
479 }
9df8a11d 480 $form->addGroup($genderOptions, 'gender_id', ts('Gender'));
6a488035
TO
481
482 CRM_Core_Form_Date::buildDateRange($form, 'birth_date', 1, '_low', '_high', ts('From'), FALSE, FALSE, 'birth');
483
484 CRM_Core_Form_Date::buildDateRange($form, 'deceased_date', 1, '_low', '_high', ts('From'), FALSE, FALSE, 'birth');
485
486
487 // radio button for is_deceased
488 $form->addYesNo( 'is_deceased', ts('Deceased'));
489 }
490
491 static function notes(&$form) {
492 $form->add('hidden', 'hidden_notes', 1);
493
494 $options = array(
495 2 => ts('Body Only'),
496 3 => ts('Subject Only'),
497 6 => ts('Both'),
498 );
499 $form->addRadio('note_option', '', $options);
500
501 $form->addElement('text', 'note', ts('Note Text'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
502
503 $form->setDefaults(array('note_option' => 6));
504 }
505
506 /**
507 * Generate the custom Data Fields based
508 * on the is_searchable
509 *
510 * @access private
511 *
512 * @return void
513 */
514 static function custom(&$form) {
515 $form->add('hidden', 'hidden_custom', 1);
516 $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'),
517 CRM_Contact_BAO_ContactType::subTypes()
518 );
519 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
520 $extends
521 );
522
523 $form->assign('groupTree', $groupDetails);
524
525 foreach ($groupDetails as $key => $group) {
526 $_groupTitle[$key] = $group['name'];
527 CRM_Core_ShowHideBlocks::links($form, $group['name'], '', '');
528
529 $groupId = $group['id'];
530 foreach ($group['fields'] as $field) {
531 $fieldId = $field['id'];
532 $elementName = 'custom_' . $fieldId;
533
534 CRM_Core_BAO_CustomField::addQuickFormElement($form,
535 $elementName,
536 $fieldId,
537 FALSE, FALSE, TRUE
538 );
539 }
540 }
541
542 //TODO: validate for only one state if prox_distance isset
543 }
544
545 static function CiviCase(&$form) {
546 //Looks like obsolete code, since CiviCase is a component, but might be used by HRD
547 $form->add('hidden', 'hidden_CiviCase', 1);
548 CRM_Case_BAO_Query::buildSearchForm($form);
549 }
550}
551