3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 require_once 'CRM/Contact/Form/Search/Custom/Base.php';
39 * Class org_civicrm_search_multivalue
41 class org_civicrm_search_multivalue
extends CRM_Contact_Form_Search_Custom_BaseimplementsCRM_Contact_Form_Search_Interface
{
43 protected $_groupTree;
50 function __construct(&$formValues) {
51 parent
::__construct($formValues);
53 require_once 'CRM/Core/BAO/CustomGroup.php';
54 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree("'Contact', 'Individual', 'Organization', 'Household'",
55 CRM_Core_DAO
::$_nullObject,
59 $this->_columns
= array(ts('Contact ID') => 'contact_id',
60 ts('Contact Type') => 'contact_type',
61 ts('Name') => 'sort_name',
64 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
66 if (!empty($this->_customGroupIDs
)) {
71 function addColumns() {
72 // add all the fields for chosen groups
73 $this->_tables
= $this->_options
= array();
74 foreach ($this->_groupTree
as $groupID => $group) {
75 if (empty($this->_customGroupIDs
[$groupID])) {
79 // now handle all the fields
80 foreach ($group['fields'] as $fieldID => $field) {
81 $this->_columns
[$field['label']] = "custom_{$field['id']}";
82 if (!array_key_exists($group['table_name'], $this->_tables
)) {
83 $this->_tables
[$group['table_name']] = array();
85 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
87 // also build the option array
88 $this->_options
[$field['id']] = array();
89 CRM_Core_BAO_CustomField
::buildOption($field,
90 $this->_options
[$field['id']]
97 * @param CRM_Core_Form $form
99 function buildForm(&$form) {
102 * You can define a custom title for the search form
104 $this->setTitle('Multiple Value Custom Group Search and Export');
111 if (empty($this->_groupTree
)) {
112 CRM_Core_Error
::statusBounce(ts("At least one Custom Group must be present, for Custom Group search."),
113 CRM_Utils_System
::url('civicrm/contact/search/custom/list',
118 // add the checkbox for custom_groups
119 foreach ($this->_groupTree
as $groupID => $group) {
120 if ($groupID == 'info') {
123 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
136 * @param int $rowcount
138 * @param bool $includeContactIDs
142 function all($offset = 0, $rowcount = 0, $sort = NULL,
143 $includeContactIDs = FALSE
145 //redirect if custom group not select in search criteria
146 if (empty($this->_formValues
['custom_group'])) {
147 CRM_Core_Error
::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
148 CRM_Utils_System
::url('civicrm/contact/search/custom',
149 "reset=1&csid={$this->_formValues['customSearchID']}",
150 FALSE, NULL, FALSE, TRUE
155 contact_a.id as contact_id ,
156 contact_a.contact_type as contact_type,
157 contact_a.sort_name as sort_name,
160 $customClauses = array();
161 foreach ($this->_tables
as $tableName => $fields) {
162 foreach ($fields as $fieldID => $fieldName) {
163 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
166 $selectClause .= implode(',', $customClauses);
168 return $this->sql($selectClause,
169 $offset, $rowcount, $sort,
170 $includeContactIDs, NULL
178 $from = "FROM civicrm_contact contact_a";
179 $customFrom = array();
180 if (!empty($this->_tables
)) {
181 foreach ($this->_tables
as $tableName => $fields) {
182 $customFrom[] = " LEFT JOIN $tableName ON {$tableName}.entity_id = contact_a.id ";
184 return $from . implode(' ', $customFrom);
190 * @param bool $includeContactIDs
194 function where($includeContactIDs = FALSE) {
198 $name = CRM_Utils_Array
::value('sort_name',
202 if (strpos($name, '%') === FALSE) {
205 $params[$count] = array($name, 'String');
206 $clause[] = "contact_a.sort_name LIKE %{$count}";
211 if (!empty($clause)) {
212 $where .= ' AND ' . implode(' AND ', $clause);
215 return $this->whereClause($where, $params);
221 function templateFile() {
222 return 'MultipleValues.tpl';
228 function setDefaultValues() {
235 function alterRow(&$row) {
236 foreach ($this->_options
as $fieldID => $values) {
237 $customVal = $valueSeparatedArray = array();
238 if (in_array($values['attributes']['html_type'],
239 array('Radio', 'Select', 'Autocomplete-Select')
241 if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) {
242 $row["custom_{$fieldID}"] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', (int)$row["custom_{$fieldID}"], 'display_name');
244 elseif ($row["custom_{$fieldID}"] &&
245 array_key_exists($row["custom_{$fieldID}"],
249 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
252 elseif (in_array($values['attributes']['html_type'],
253 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
255 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
256 foreach ($valueSeparatedArray as $val) {
257 $customVal[] = $values[$val];
259 $row["custom_{$fieldID}"] = implode(', ', $customVal);
261 elseif (in_array($values['attributes']['html_type'],
262 array('Multi-Select State/Province', 'Select State/Province')
264 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
265 $stateName = CRM_Core_PseudoConstant
::stateProvince();
266 foreach ($valueSeparatedArray as $val) {
267 $customVal[] = $stateName[$val];
269 $row["custom_{$fieldID}"] = implode(', ', $customVal);
271 elseif (in_array($values['attributes']['html_type'],
272 array('Multi-Select Country', 'Select Country')
274 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
275 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country',
276 TRUE, 'name', 'is_active'
278 foreach ($valueSeparatedArray as $val) {
279 $customVal[] = $countryNames[$val];
281 $row["custom_{$fieldID}"] = implode(', ', $customVal);
289 function setTitle($title) {
290 CRM_Utils_System
::setTitle($title);