Fix spelling and capitalization of Contact ID and External ID
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / MultipleValues.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 /**
42 * @param $formValues
43 */
44 function __construct(&$formValues) {
45 parent::__construct($formValues);
46
47 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'",
48 CRM_Core_DAO::$_nullObject,
49 NULL, -1
50 );
51
52 $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
53
54 $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
55
56 $this->_columns = array(
57 ts('Contact ID') => 'contact_id',
58 ts('Contact Type') => 'contact_type',
59 ts('Name') => 'sort_name',
60 );
61
62 $this->_customGroupIDs = CRM_Utils_Array::value('custom_group', $formValues);
63
64 if (!empty($this->_customGroupIDs)) {
65 $this->addColumns();
66 }
67 }
68
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])) {
74 continue;
75 }
76
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();
82 }
83 $this->_tables[$group['table_name']][$field['id']] = $field['column_name'];
84
85 // also build the option array
86 $this->_options[$field['id']] = array();
87 CRM_Core_BAO_CustomField::buildOption($field,
88 $this->_options[$field['id']]
89 );
90 }
91 }
92 }
93
94 /**
95 * @param $form
96 */
97 function buildForm(&$form) {
98
99 /**
100 * You can define a custom title for the search form
101 */
102 $this->setTitle('Multiple Value Custom Group Search and Export');
103
104 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
105
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'));
112
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'));
116
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'));
120
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',
124 'reset=1'
125 )
126 );
127 }
128 // add the checkbox for custom_groups
129 foreach ($this->_groupTree as $groupID => $group) {
130 if ($groupID == 'info') {
131 continue;
132 }
133 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
134 }
135 }
136
137 /**
138 * @return null
139 */
140 function summary() {
141 return NULL;
142 }
143
144 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
145 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
146 }
147
148 /**
149 * @param int $offset
150 * @param int $rowcount
151 * @param null $sort
152 * @param bool $includeContactIDs
153 * @param bool $justIDs
154 *
155 * @return string
156 */
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
164 )
165 );
166 }
167
168 if ($justIDs) {
169 $selectClause = "contact_a.id as contact_id";
170 $sort = "contact_a.id";
171
172 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
173 }
174 else {
175 $selectClause = "
176 contact_a.id as contact_id ,
177 contact_a.contact_type as contact_type,
178 contact_a.sort_name as sort_name,
179 ";
180 }
181
182 $customClauses = array();
183 foreach ($this->_tables as $tableName => $fields) {
184 foreach ($fields as $fieldID => $fieldName) {
185 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
186 }
187 }
188 $selectClause .= implode(',', $customClauses);
189
190 return $this->sql($selectClause,
191 $offset, $rowcount, $sort,
192 $includeContactIDs, NULL
193 );
194 }
195
196 /**
197 * @return string
198 */
199 function from() {
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 ";
206 }
207 $from .= implode(' ', $customFrom);
208 }
209
210 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
211 if ($this->_tag) {
212 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
213 AND contact_a.id = t.entity_id)";
214 }
215
216 if ($this->_group) {
217 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
218 AND cgc.status = 'Added')";
219 }
220
221 return $from;
222 }
223
224 /**
225 * @param bool $includeContactIDs
226 *
227 * @return string
228 */
229 function where($includeContactIDs = FALSE) {
230 $count = 1;
231 $clause = array();
232 $params = array();
233 $name = CRM_Utils_Array::value('sort_name',
234 $this->_formValues
235 );
236 if ($name != NULL) {
237 if (strpos($name, '%') === FALSE) {
238 $name = "%{$name}%";
239 }
240 $params[$count] = array($name, 'String');
241 $clause[] = "contact_a.sort_name LIKE %{$count}";
242 $count++;
243 }
244
245 $contact_type = CRM_Utils_Array::value('contact_type',
246 $this->_formValues
247 );
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]'";
252 }
253 else {
254 $clause[] = "contact_a.contact_type = '$contactType[0]'";
255 }
256 }
257
258 if ($this->_tag) {
259 $clause[] = "t.tag_id = {$this->_tag}";
260 }
261
262 if ($this->_group) {
263 $clause[] = "cgc.group_id = {$this->_group}";
264 }
265
266 $where = '( 1 )';
267 if (!empty($clause)) {
268 $where .= ' AND ' . implode(' AND ', $clause);
269 }
270
271 return $this->whereClause($where, $params);
272 }
273
274 /**
275 * @return string
276 */
277 function templateFile() {
278 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
279 }
280
281 /**
282 * @return array
283 */
284 function setDefaultValues() {
285 return array();
286 }
287
288 /**
289 * @param $row
290 */
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')
296 )) {
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');
299 }
300 elseif ($row["custom_{$fieldID}"] &&
301 array_key_exists($row["custom_{$fieldID}"],
302 $values
303 )
304 ) {
305 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
306 }
307 }
308 elseif (in_array($values['attributes']['html_type'],
309 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
310 )) {
311 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"]));
312 foreach ($valueSeparatedArray as $val) {
313 $customVal[] = $values[$val];
314 }
315 $row["custom_{$fieldID}"] = implode(', ', $customVal);
316 }
317 elseif (in_array($values['attributes']['html_type'],
318 array('Multi-Select State/Province', 'Select State/Province')
319 )) {
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];
324 }
325 $row["custom_{$fieldID}"] = implode(', ', $customVal);
326 }
327 elseif (in_array($values['attributes']['html_type'],
328 array('Multi-Select Country', 'Select Country')
329 )) {
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'
333 );
334 foreach ($valueSeparatedArray as $val) {
335 $customVal[] = $countryNames[$val];
336 }
337 $row["custom_{$fieldID}"] = implode(', ', $customVal);
338 }
339 }
340 }
341
342 /**
343 * @param $title
344 */
345 function setTitle($title) {
346 CRM_Utils_System::setTitle($title);
347 }
348 }
349