Merge pull request #1020 from lcdservices/CRM-12814
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / MultipleValues.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 | 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 class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
36
37 protected $_groupTree;
38 protected $_tables;
39 protected $_options;
40
41 function __construct(&$formValues) {
42 parent::__construct($formValues);
43
44 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'",
45 CRM_Core_DAO::$_nullObject,
46 NULL, -1
47 );
48
49 $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
50
51 $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
52
53 $this->_columns = array(
54 ts('Contact Id') => 'contact_id',
55 ts('Contact Type') => 'contact_type',
56 ts('Name') => 'sort_name',
57 );
58
59 $this->_customGroupIDs = CRM_Utils_Array::value('custom_group', $formValues);
60
61 if (!empty($this->_customGroupIDs)) {
62 $this->addColumns();
63 }
64 }
65
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)) {
71 continue;
72 }
73
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();
79 }
80 $this->_tables[$group['table_name']][$field['id']] = $field['column_name'];
81
82 // also build the option array
83 $this->_options[$field['id']] = array();
84 CRM_Core_BAO_CustomField::buildOption($field,
85 $this->_options[$field['id']]
86 );
87 }
88 }
89 }
90
91 function buildForm(&$form) {
92
93 /**
94 * You can define a custom title for the search form
95 */
96 $this->setTitle('Multiple Value Custom Group Search and Export');
97
98 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
99
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);
103
104 // add select for groups
105 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
106 $form->addElement('select', 'group', ts('in'), $group);
107
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);
111
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',
115 'reset=1'
116 )
117 );
118 }
119 // add the checkbox for custom_groups
120 foreach ($this->_groupTree as $groupID => $group) {
121 if ($groupID == 'info') {
122 continue;
123 }
124 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
125 }
126 }
127
128 function summary() {
129 return NULL;
130 }
131
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
139 )
140 );
141 }
142
143 if ($justIDs) {
144 $selectClause = "contact_a.id as contact_id";
145 }
146 else {
147 $selectClause = "
148 contact_a.id as contact_id ,
149 contact_a.contact_type as contact_type,
150 contact_a.sort_name as sort_name,
151 ";
152 }
153
154 $customClauses = array();
155 foreach ($this->_tables as $tableName => $fields) {
156 foreach ($fields as $fieldID => $fieldName) {
157 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
158 }
159 }
160 $selectClause .= implode(',', $customClauses);
161
162 return $this->sql($selectClause,
163 $offset, $rowcount, $sort,
164 $includeContactIDs, NULL
165 );
166 }
167
168 function from() {
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 ";
175 }
176 $from .= implode(' ', $customFrom);
177 }
178
179 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
180 if ($this->_tag) {
181 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
182 AND contact_a.id = t.entity_id)";
183 }
184
185 if ($this->_group) {
186 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
187 AND cgc.status = 'Added')";
188 }
189
190 return $from;
191 }
192
193 function where($includeContactIDs = FALSE) {
194 $count = 1;
195 $clause = array();
196 $params = array();
197 $name = CRM_Utils_Array::value('sort_name',
198 $this->_formValues
199 );
200 if ($name != NULL) {
201 if (strpos($name, '%') === FALSE) {
202 $name = "%{$name}%";
203 }
204 $params[$count] = array($name, 'String');
205 $clause[] = "contact_a.sort_name LIKE %{$count}";
206 $count++;
207 }
208
209 $contact_type = CRM_Utils_Array::value('contact_type',
210 $this->_formValues
211 );
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]'";
216 }
217 else {
218 $clause[] = "contact_a.contact_type = '$contactType[0]'";
219 }
220 }
221
222 if ($this->_tag) {
223 $clause[] = "t.tag_id = {$this->_tag}";
224 }
225
226 if ($this->_group) {
227 $clause[] = "cgc.group_id = {$this->_group}";
228 }
229
230 $where = '( 1 )';
231 if (!empty($clause)) {
232 $where .= ' AND ' . implode(' AND ', $clause);
233 }
234
235 return $this->whereClause($where, $params);
236 }
237
238 function templateFile() {
239 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
240 }
241
242 function setDefaultValues() {
243 return array();
244 }
245
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')
251 )) {
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');
254 }
255 elseif ($row["custom_{$fieldID}"] &&
256 array_key_exists($row["custom_{$fieldID}"],
257 $values
258 )
259 ) {
260 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
261 }
262 }
263 elseif (in_array($values['attributes']['html_type'],
264 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
265 )) {
266 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"]));
267 foreach ($valueSeparatedArray as $val) {
268 $customVal[] = $values[$val];
269 }
270 $row["custom_{$fieldID}"] = implode(', ', $customVal);
271 }
272 elseif (in_array($values['attributes']['html_type'],
273 array('Multi-Select State/Province', 'Select State/Province')
274 )) {
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];
279 }
280 $row["custom_{$fieldID}"] = implode(', ', $customVal);
281 }
282 elseif (in_array($values['attributes']['html_type'],
283 array('Multi-Select Country', 'Select Country')
284 )) {
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'
288 );
289 foreach ($valueSeparatedArray as $val) {
290 $customVal[] = $countryNames[$val];
291 }
292 $row["custom_{$fieldID}"] = implode(', ', $customVal);
293 }
294 }
295 }
296
297 function setTitle($title) {
298 CRM_Utils_System::setTitle($title);
299 }
300 }
301