Merge pull request #12072 from civicrm/5.1
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / MultipleValues.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
34
35 protected $_groupTree;
36 protected $_tables;
430ae6dd 37 protected $_options;
d14ccbdc
SL
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
ddfc623e 40 protected $fieldInfo = array();
430ae6dd 41
86538308 42 /**
5a409b50 43 * Class constructor.
44 *
45 * @param array $formValues
86538308 46 */
00be9182 47 public function __construct(&$formValues) {
6a488035
TO
48 parent::__construct($formValues);
49
0b330e6d 50 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'", NULL, NULL, -1);
6a488035
TO
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(
7b99ead3 57 ts('Contact ID') => 'contact_id',
6a488035
TO
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
ddfc623e
CW
69 /**
70 * Add all the fields for chosen groups
71 */
00be9182 72 public function addColumns() {
ddfc623e 73 $this->_tables = array();
6a488035 74 foreach ($this->_groupTree as $groupID => $group) {
a7488080 75 if (empty($this->_customGroupIDs[$groupID])) {
6a488035
TO
76 continue;
77 }
78
ddfc623e
CW
79 $this->fieldInfo += $group['fields'];
80
6a488035
TO
81 // now handle all the fields
82 foreach ($group['fields'] as $fieldID => $field) {
83 $this->_columns[$field['label']] = "custom_{$field['id']}";
84 if (!array_key_exists($group['table_name'], $this->_tables)) {
85 $this->_tables[$group['table_name']] = array();
86 }
87 $this->_tables[$group['table_name']][$field['id']] = $field['column_name'];
6a488035
TO
88 }
89 }
90 }
91
86538308 92 /**
c490a46a 93 * @param CRM_Core_Form $form
86538308 94 */
00be9182 95 public function buildForm(&$form) {
6a488035 96
6a488035
TO
97 $this->setTitle('Multiple Value Custom Group Search and Export');
98
99 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
100
46b3417a 101 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements();
24431f7b 102 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, array('class' => 'crm-select2 huge'));
6a488035
TO
103
104 // add select for groups
105 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
24431f7b 106 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
6a488035
TO
107
108 // add select for tags
cd43c5e3 109 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
24431f7b 110 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
6a488035
TO
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
86538308
EM
128 /**
129 * @return null
130 */
00be9182 131 public function summary() {
6a488035
TO
132 return NULL;
133 }
134
1cd3ffa9
EM
135 /**
136 * @param int $offset
137 * @param int $rowcount
138 * @param null $sort
139 * @param bool $returnSQL
140 *
141 * @return string
142 */
00be9182 143 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
26a9b6ab
C
144 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
145 }
146
86538308
EM
147 /**
148 * @param int $offset
149 * @param int $rowcount
150 * @param null $sort
151 * @param bool $includeContactIDs
152 * @param bool $justIDs
153 *
154 * @return string
155 */
00be9182 156 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
6a488035 157 //redirect if custom group not select in search criteria
a7488080 158 if (empty($this->_formValues['custom_group'])) {
6a488035
TO
159 CRM_Core_Error::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
160 CRM_Utils_System::url('civicrm/contact/search/custom',
161 "reset=1&csid={$this->_formValues['customSearchID']}",
162 FALSE, NULL, FALSE, TRUE
163 )
164 );
165 }
166
167 if ($justIDs) {
168 $selectClause = "contact_a.id as contact_id";
26a9b6ab
C
169 $sort = "contact_a.id";
170
171 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
6a488035
TO
172 }
173 else {
174 $selectClause = "
175contact_a.id as contact_id ,
176contact_a.contact_type as contact_type,
177contact_a.sort_name as sort_name,
178";
179 }
180
181 $customClauses = array();
182 foreach ($this->_tables as $tableName => $fields) {
183 foreach ($fields as $fieldID => $fieldName) {
184 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
185 }
186 }
187 $selectClause .= implode(',', $customClauses);
188
189 return $this->sql($selectClause,
190 $offset, $rowcount, $sort,
191 $includeContactIDs, NULL
192 );
193 }
194
86538308
EM
195 /**
196 * @return string
197 */
00be9182 198 public function from() {
d14ccbdc
SL
199 $this->buildACLClause('contact_a');
200 $from = "FROM civicrm_contact contact_a {$this->_aclFrom}";
6a488035
TO
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
86538308
EM
224 /**
225 * @param bool $includeContactIDs
226 *
227 * @return string
228 */
00be9182 229 public function where($includeContactIDs = FALSE) {
353ffa53 230 $count = 1;
6a488035
TO
231 $clause = array();
232 $params = array();
353ffa53 233 $name = CRM_Utils_Array::value('sort_name',
6a488035
TO
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) {
46b3417a 249 $contactType = explode('__', $contact_type);
6a488035
TO
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 }
47b8444f 265 if ($this->_aclWhere) {
b49db103 266 $clause[] = " {$this->_aclWhere}";
47b8444f 267 }
6a488035
TO
268
269 $where = '( 1 )';
270 if (!empty($clause)) {
271 $where .= ' AND ' . implode(' AND ', $clause);
272 }
273
274 return $this->whereClause($where, $params);
275 }
276
86538308
EM
277 /**
278 * @return string
279 */
00be9182 280 public function templateFile() {
6a488035
TO
281 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
282 }
283
86538308
EM
284 /**
285 * @param $row
286 */
00be9182 287 public function alterRow(&$row) {
ddfc623e
CW
288 foreach ($row as $fieldName => &$field) {
289 if (strpos($fieldName, 'custom_') === 0) {
51a3717f 290 $field = CRM_Core_BAO_CustomField::displayValue($field, $fieldName);
6a488035
TO
291 }
292 }
293 }
294
d14ccbdc
SL
295 /**
296 * @param string $tableAlias
297 */
298 public function buildACLClause($tableAlias = 'contact') {
299 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
300 }
301
6a488035 302}