3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contact_Form_Search_Custom_MultipleValues
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_groupTree;
41 function __construct(&$formValues) {
42 parent
::__construct($formValues);
44 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree("'Contact', 'Individual', 'Organization', 'Household'",
45 CRM_Core_DAO
::$_nullObject,
49 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
51 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
53 $this->_columns
= array(
54 ts('Contact Id') => 'contact_id',
55 ts('Contact Type') => 'contact_type',
56 ts('Name') => 'sort_name',
59 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
61 if (!empty($this->_customGroupIDs
)) {
66 function addColumns() {
67 // add all the fields for chosen groups
68 $this->_tables
= $this->_options
= array();
69 foreach ($this->_groupTree
as $groupID => $group) {
70 if (!CRM_Utils_Array
::value($groupID, $this->_customGroupIDs
)) {
74 // now handle all the fields
75 foreach ($group['fields'] as $fieldID => $field) {
76 $this->_columns
[$field['label']] = "custom_{$field['id']}";
77 if (!array_key_exists($group['table_name'], $this->_tables
)) {
78 $this->_tables
[$group['table_name']] = array();
80 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
82 // also build the option array
83 $this->_options
[$field['id']] = array();
84 CRM_Core_BAO_CustomField
::buildOption($field,
85 $this->_options
[$field['id']]
91 function buildForm(&$form) {
94 * You can define a custom title for the search form
96 $this->setTitle('Multiple Value Custom Group Search and Export');
98 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
100 // add select for contact type
101 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType
::getSelectElements();
102 $form->add('select', 'contact_type', ts('Find...'), $contactTypes);
104 // add select for groups
105 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::group();
106 $form->addElement('select', 'group', ts('in'), $group);
108 // add select for tags
109 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
110 $form->addElement('select', 'tag', ts('Tagged'), $tag);
112 if (empty($this->_groupTree
)) {
113 CRM_Core_Error
::statusBounce(ts("Atleast one Custom Group must be present, for Custom Group search."),
114 CRM_Utils_System
::url('civicrm/contact/search/custom/list',
119 // add the checkbox for custom_groups
120 foreach ($this->_groupTree
as $groupID => $group) {
121 if ($groupID == 'info') {
124 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
132 function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
133 //redirect if custom group not select in search criteria
134 if (!CRM_Utils_Array
::value('custom_group', $this->_formValues
)) {
135 CRM_Core_Error
::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
136 CRM_Utils_System
::url('civicrm/contact/search/custom',
137 "reset=1&csid={$this->_formValues['customSearchID']}",
138 FALSE, NULL, FALSE, TRUE
144 $selectClause = "contact_a.id as contact_id";
148 contact_a.id as contact_id ,
149 contact_a.contact_type as contact_type,
150 contact_a.sort_name as sort_name,
154 $customClauses = array();
155 foreach ($this->_tables
as $tableName => $fields) {
156 foreach ($fields as $fieldID => $fieldName) {
157 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
160 $selectClause .= implode(',', $customClauses);
162 return $this->sql($selectClause,
163 $offset, $rowcount, $sort,
164 $includeContactIDs, NULL
169 $from = "FROM civicrm_contact contact_a";
170 $customFrom = array();
171 // lets do an INNER JOIN so we get only relevant values rather than all values
172 if (!empty($this->_tables
)) {
173 foreach ($this->_tables
as $tableName => $fields) {
174 $customFrom[] = " INNER JOIN $tableName ON {$tableName}.entity_id = contact_a.id ";
176 $from .= implode(' ', $customFrom);
179 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
181 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
182 AND contact_a.id = t.entity_id)";
186 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
187 AND cgc.status = 'Added')";
193 function where($includeContactIDs = FALSE) {
197 $name = CRM_Utils_Array
::value('sort_name',
201 if (strpos($name, '%') === FALSE) {
204 $params[$count] = array($name, 'String');
205 $clause[] = "contact_a.sort_name LIKE %{$count}";
209 $contact_type = CRM_Utils_Array
::value('contact_type',
212 if ($contact_type != NULL) {
213 $contactType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $contact_type);
214 if (count($contactType) > 1) {
215 $clause[] = "contact_a.contact_type = '$contactType[0]' AND contact_a.contact_sub_type = '$contactType[1]'";
218 $clause[] = "contact_a.contact_type = '$contactType[0]'";
223 $clause[] = "t.tag_id = {$this->_tag}";
227 $clause[] = "cgc.group_id = {$this->_group}";
231 if (!empty($clause)) {
232 $where .= ' AND ' . implode(' AND ', $clause);
235 return $this->whereClause($where, $params);
238 function templateFile() {
239 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
242 function setDefaultValues() {
246 function alterRow(&$row) {
247 foreach ($this->_options
as $fieldID => $values) {
248 $customVal = $valueSeparatedArray = array();
249 if (in_array($values['attributes']['html_type'],
250 array('Radio', 'Select', 'Autocomplete-Select')
252 if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) {
253 $row["custom_{$fieldID}"] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', (int)$row["custom_{$fieldID}"], 'display_name');
255 elseif ($row["custom_{$fieldID}"] &&
256 array_key_exists($row["custom_{$fieldID}"],
260 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
263 elseif (in_array($values['attributes']['html_type'],
264 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
266 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
267 foreach ($valueSeparatedArray as $val) {
268 $customVal[] = $values[$val];
270 $row["custom_{$fieldID}"] = implode(', ', $customVal);
272 elseif (in_array($values['attributes']['html_type'],
273 array('Multi-Select State/Province', 'Select State/Province')
275 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
276 $stateName = CRM_Core_PseudoConstant
::stateProvince();
277 foreach ($valueSeparatedArray as $val) {
278 $customVal[] = $stateName[$val];
280 $row["custom_{$fieldID}"] = implode(', ', $customVal);
282 elseif (in_array($values['attributes']['html_type'],
283 array('Multi-Select Country', 'Select Country')
285 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
286 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country',
287 TRUE, 'name', 'is_active'
289 foreach ($valueSeparatedArray as $val) {
290 $customVal[] = $countryNames[$val];
292 $row["custom_{$fieldID}"] = implode(', ', $customVal);
297 function setTitle($title) {
298 CRM_Utils_System
::setTitle($title);