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 | 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
35 class CRM_Contact_Form_Search_Custom_MultipleValues
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_groupTree;
44 function __construct(&$formValues) {
45 parent
::__construct($formValues);
47 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree("'Contact', 'Individual', 'Organization', 'Household'",
48 CRM_Core_DAO
::$_nullObject,
52 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
54 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
56 $this->_columns
= array(
57 ts('Contact ID') => 'contact_id',
58 ts('Contact Type') => 'contact_type',
59 ts('Name') => 'sort_name',
62 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
64 if (!empty($this->_customGroupIDs
)) {
69 function addColumns() {
70 // add all the fields for chosen groups
71 $this->_tables
= $this->_options
= array();
72 foreach ($this->_groupTree
as $groupID => $group) {
73 if (empty($this->_customGroupIDs
[$groupID])) {
77 // now handle all the fields
78 foreach ($group['fields'] as $fieldID => $field) {
79 $this->_columns
[$field['label']] = "custom_{$field['id']}";
80 if (!array_key_exists($group['table_name'], $this->_tables
)) {
81 $this->_tables
[$group['table_name']] = array();
83 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
85 // also build the option array
86 $this->_options
[$field['id']] = array();
87 CRM_Core_BAO_CustomField
::buildOption($field,
88 $this->_options
[$field['id']]
97 function buildForm(&$form) {
100 * You can define a custom title for the search form
102 $this->setTitle('Multiple Value Custom Group Search and Export');
104 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
106 // add select for contact type
107 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
108 // this is loaded onto then replace with something like '__' & test
109 $separator = CRM_Core_DAO
::VALUE_SEPARATOR
;
110 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, $separator);
111 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, array('class' => 'crm-select2 huge'));
113 // add select for groups
114 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::group();
115 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
117 // add select for tags
118 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
119 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
121 if (empty($this->_groupTree
)) {
122 CRM_Core_Error
::statusBounce(ts("Atleast one Custom Group must be present, for Custom Group search."),
123 CRM_Utils_System
::url('civicrm/contact/search/custom/list',
128 // add the checkbox for custom_groups
129 foreach ($this->_groupTree
as $groupID => $group) {
130 if ($groupID == 'info') {
133 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
144 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
145 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
150 * @param int $rowcount
152 * @param bool $includeContactIDs
153 * @param bool $justIDs
157 function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
158 //redirect if custom group not select in search criteria
159 if (empty($this->_formValues
['custom_group'])) {
160 CRM_Core_Error
::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
161 CRM_Utils_System
::url('civicrm/contact/search/custom',
162 "reset=1&csid={$this->_formValues['customSearchID']}",
163 FALSE, NULL, FALSE, TRUE
169 $selectClause = "contact_a.id as contact_id";
170 $sort = "contact_a.id";
172 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
176 contact_a.id as contact_id ,
177 contact_a.contact_type as contact_type,
178 contact_a.sort_name as sort_name,
182 $customClauses = array();
183 foreach ($this->_tables
as $tableName => $fields) {
184 foreach ($fields as $fieldID => $fieldName) {
185 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
188 $selectClause .= implode(',', $customClauses);
190 return $this->sql($selectClause,
191 $offset, $rowcount, $sort,
192 $includeContactIDs, NULL
200 $from = "FROM civicrm_contact contact_a";
201 $customFrom = array();
202 // lets do an INNER JOIN so we get only relevant values rather than all values
203 if (!empty($this->_tables
)) {
204 foreach ($this->_tables
as $tableName => $fields) {
205 $customFrom[] = " INNER JOIN $tableName ON {$tableName}.entity_id = contact_a.id ";
207 $from .= implode(' ', $customFrom);
210 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
212 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
213 AND contact_a.id = t.entity_id)";
217 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
218 AND cgc.status = 'Added')";
225 * @param bool $includeContactIDs
229 function where($includeContactIDs = FALSE) {
233 $name = CRM_Utils_Array
::value('sort_name',
237 if (strpos($name, '%') === FALSE) {
240 $params[$count] = array($name, 'String');
241 $clause[] = "contact_a.sort_name LIKE %{$count}";
245 $contact_type = CRM_Utils_Array
::value('contact_type',
248 if ($contact_type != NULL) {
249 $contactType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $contact_type);
250 if (count($contactType) > 1) {
251 $clause[] = "contact_a.contact_type = '$contactType[0]' AND contact_a.contact_sub_type = '$contactType[1]'";
254 $clause[] = "contact_a.contact_type = '$contactType[0]'";
259 $clause[] = "t.tag_id = {$this->_tag}";
263 $clause[] = "cgc.group_id = {$this->_group}";
267 if (!empty($clause)) {
268 $where .= ' AND ' . implode(' AND ', $clause);
271 return $this->whereClause($where, $params);
277 function templateFile() {
278 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
284 function setDefaultValues() {
291 function alterRow(&$row) {
292 foreach ($this->_options
as $fieldID => $values) {
293 $customVal = $valueSeparatedArray = array();
294 if (in_array($values['attributes']['html_type'],
295 array('Radio', 'Select', 'Autocomplete-Select')
297 if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) {
298 $row["custom_{$fieldID}"] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', (int)$row["custom_{$fieldID}"], 'display_name');
300 elseif ($row["custom_{$fieldID}"] &&
301 array_key_exists($row["custom_{$fieldID}"],
305 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
308 elseif (in_array($values['attributes']['html_type'],
309 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
311 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
312 foreach ($valueSeparatedArray as $val) {
313 $customVal[] = $values[$val];
315 $row["custom_{$fieldID}"] = implode(', ', $customVal);
317 elseif (in_array($values['attributes']['html_type'],
318 array('Multi-Select State/Province', 'Select State/Province')
320 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
321 $stateName = CRM_Core_PseudoConstant
::stateProvince();
322 foreach ($valueSeparatedArray as $val) {
323 $customVal[] = $stateName[$val];
325 $row["custom_{$fieldID}"] = implode(', ', $customVal);
327 elseif (in_array($values['attributes']['html_type'],
328 array('Multi-Select Country', 'Select Country')
330 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
331 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country',
332 TRUE, 'name', 'is_active'
334 foreach ($valueSeparatedArray as $val) {
335 $customVal[] = $countryNames[$val];
337 $row["custom_{$fieldID}"] = implode(', ', $customVal);
345 function setTitle($title) {
346 CRM_Utils_System
::setTitle($title);