commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Contact / Form / Search / Custom / MultipleValues.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 protected $_aclFrom = NULL;
41 protected $_aclWhere = NULL;
42
43 /**
44 * @param $formValues
45 */
46 public function __construct(&$formValues) {
47 parent::__construct($formValues);
48
49 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'",
50 CRM_Core_DAO::$_nullObject,
51 NULL, -1
52 );
53
54 $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
55
56 $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
57
58 $this->_columns = array(
59 ts('Contact ID') => 'contact_id',
60 ts('Contact Type') => 'contact_type',
61 ts('Name') => 'sort_name',
62 );
63
64 $this->_customGroupIDs = CRM_Utils_Array::value('custom_group', $formValues);
65
66 if (!empty($this->_customGroupIDs)) {
67 $this->addColumns();
68 }
69 }
70
71 public 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])) {
76 continue;
77 }
78
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();
84 }
85 $this->_tables[$group['table_name']][$field['id']] = $field['column_name'];
86
87 // also build the option array
88 $this->_options[$field['id']] = array();
89 CRM_Core_BAO_CustomField::buildOption($field,
90 $this->_options[$field['id']]
91 );
92 }
93 }
94 }
95
96 /**
97 * @param CRM_Core_Form $form
98 */
99 public function buildForm(&$form) {
100
101 /**
102 * You can define a custom title for the search form
103 */
104 $this->setTitle('Multiple Value Custom Group Search and Export');
105
106 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
107
108 // add select for contact type
109 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
110 // this is loaded onto then replace with something like '__' & test
111 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
112 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
113 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, array('class' => 'crm-select2 huge'));
114
115 // add select for groups
116 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
117 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
118
119 // add select for tags
120 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
121 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
122
123 if (empty($this->_groupTree)) {
124 CRM_Core_Error::statusBounce(ts("Atleast one Custom Group must be present, for Custom Group search."),
125 CRM_Utils_System::url('civicrm/contact/search/custom/list',
126 'reset=1'
127 )
128 );
129 }
130 // add the checkbox for custom_groups
131 foreach ($this->_groupTree as $groupID => $group) {
132 if ($groupID == 'info') {
133 continue;
134 }
135 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
136 }
137 }
138
139 /**
140 * @return null
141 */
142 public function summary() {
143 return NULL;
144 }
145
146 /**
147 * @param int $offset
148 * @param int $rowcount
149 * @param null $sort
150 * @param bool $returnSQL
151 *
152 * @return string
153 */
154 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
155 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
156 }
157
158 /**
159 * @param int $offset
160 * @param int $rowcount
161 * @param null $sort
162 * @param bool $includeContactIDs
163 * @param bool $justIDs
164 *
165 * @return string
166 */
167 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
168 //redirect if custom group not select in search criteria
169 if (empty($this->_formValues['custom_group'])) {
170 CRM_Core_Error::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
171 CRM_Utils_System::url('civicrm/contact/search/custom',
172 "reset=1&csid={$this->_formValues['customSearchID']}",
173 FALSE, NULL, FALSE, TRUE
174 )
175 );
176 }
177
178 if ($justIDs) {
179 $selectClause = "contact_a.id as contact_id";
180 $sort = "contact_a.id";
181
182 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
183 }
184 else {
185 $selectClause = "
186 contact_a.id as contact_id ,
187 contact_a.contact_type as contact_type,
188 contact_a.sort_name as sort_name,
189 ";
190 }
191
192 $customClauses = array();
193 foreach ($this->_tables as $tableName => $fields) {
194 foreach ($fields as $fieldID => $fieldName) {
195 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
196 }
197 }
198 $selectClause .= implode(',', $customClauses);
199
200 return $this->sql($selectClause,
201 $offset, $rowcount, $sort,
202 $includeContactIDs, NULL
203 );
204 }
205
206 /**
207 * @return string
208 */
209 public function from() {
210 $this->buildACLClause('contact_a');
211 $from = "FROM civicrm_contact contact_a {$this->_aclFrom}";
212 $customFrom = array();
213 // lets do an INNER JOIN so we get only relevant values rather than all values
214 if (!empty($this->_tables)) {
215 foreach ($this->_tables as $tableName => $fields) {
216 $customFrom[] = " INNER JOIN $tableName ON {$tableName}.entity_id = contact_a.id ";
217 }
218 $from .= implode(' ', $customFrom);
219 }
220
221 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
222 if ($this->_tag) {
223 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
224 AND contact_a.id = t.entity_id)";
225 }
226
227 if ($this->_group) {
228 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
229 AND cgc.status = 'Added')";
230 }
231
232 return $from;
233 }
234
235 /**
236 * @param bool $includeContactIDs
237 *
238 * @return string
239 */
240 public function where($includeContactIDs = FALSE) {
241 $count = 1;
242 $clause = array();
243 $params = array();
244 $name = CRM_Utils_Array::value('sort_name',
245 $this->_formValues
246 );
247 if ($name != NULL) {
248 if (strpos($name, '%') === FALSE) {
249 $name = "%{$name}%";
250 }
251 $params[$count] = array($name, 'String');
252 $clause[] = "contact_a.sort_name LIKE %{$count}";
253 $count++;
254 }
255
256 $contact_type = CRM_Utils_Array::value('contact_type',
257 $this->_formValues
258 );
259 if ($contact_type != NULL) {
260 $contactType = explode(CRM_Core_DAO::VALUE_SEPARATOR, $contact_type);
261 if (count($contactType) > 1) {
262 $clause[] = "contact_a.contact_type = '$contactType[0]' AND contact_a.contact_sub_type = '$contactType[1]'";
263 }
264 else {
265 $clause[] = "contact_a.contact_type = '$contactType[0]'";
266 }
267 }
268
269 if ($this->_tag) {
270 $clause[] = "t.tag_id = {$this->_tag}";
271 }
272
273 if ($this->_group) {
274 $clause[] = "cgc.group_id = {$this->_group}";
275 }
276 if ($this->_aclWhere) {
277 $clause[] = " {$this->_aclWhere}";
278 }
279
280 $where = '( 1 )';
281 if (!empty($clause)) {
282 $where .= ' AND ' . implode(' AND ', $clause);
283 }
284
285 return $this->whereClause($where, $params);
286 }
287
288 /**
289 * @return string
290 */
291 public function templateFile() {
292 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
293 }
294
295 /**
296 * @param $row
297 */
298 public function alterRow(&$row) {
299 foreach ($this->_options as $fieldID => $values) {
300 $customVal = $valueSeparatedArray = array();
301 if (in_array($values['attributes']['html_type'],
302 array('Radio', 'Select', 'Autocomplete-Select')
303 )) {
304 if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) {
305 $row["custom_{$fieldID}"] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', (int) $row["custom_{$fieldID}"], 'display_name');
306 }
307 elseif ($row["custom_{$fieldID}"] &&
308 array_key_exists($row["custom_{$fieldID}"],
309 $values
310 )
311 ) {
312 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
313 }
314 }
315 elseif (in_array($values['attributes']['html_type'],
316 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
317 )) {
318 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"]));
319 foreach ($valueSeparatedArray as $val) {
320 $customVal[] = $values[$val];
321 }
322 $row["custom_{$fieldID}"] = implode(', ', $customVal);
323 }
324 elseif (in_array($values['attributes']['html_type'],
325 array('Multi-Select State/Province', 'Select State/Province')
326 )) {
327 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"]));
328 $stateName = CRM_Core_PseudoConstant::stateProvince();
329 foreach ($valueSeparatedArray as $val) {
330 $customVal[] = $stateName[$val];
331 }
332 $row["custom_{$fieldID}"] = implode(', ', $customVal);
333 }
334 elseif (in_array($values['attributes']['html_type'],
335 array('Multi-Select Country', 'Select Country')
336 )) {
337 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"]));
338 CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country',
339 TRUE, 'name', 'is_active'
340 );
341 foreach ($valueSeparatedArray as $val) {
342 $customVal[] = $countryNames[$val];
343 }
344 $row["custom_{$fieldID}"] = implode(', ', $customVal);
345 }
346 }
347 }
348
349 /**
350 * @param $title
351 */
352 public function setTitle($title) {
353 CRM_Utils_System::setTitle($title);
354 }
355
356 /**
357 * @param string $tableAlias
358 */
359 public function buildACLClause($tableAlias = 'contact') {
360 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
361 }
362
363 }