CRM-20091 - Specify return params from getTree
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / MultipleValues.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33 class 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;
37 protected $_options;
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
40 protected $fieldInfo = array();
41
42 /**
43 * Class constructor.
44 *
45 * @param array $formValues
46 */
47 public function __construct(&$formValues) {
48 parent::__construct($formValues);
49
50 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'", NULL, NULL, -1);
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 /**
70 * Add all the fields for chosen groups
71 */
72 public function addColumns() {
73 $this->_tables = array();
74 foreach ($this->_groupTree as $groupID => $group) {
75 if (empty($this->_customGroupIDs[$groupID])) {
76 continue;
77 }
78
79 $this->fieldInfo += $group['fields'];
80
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'];
88 }
89 }
90 }
91
92 /**
93 * @param CRM_Core_Form $form
94 */
95 public function buildForm(&$form) {
96
97 $this->setTitle('Multiple Value Custom Group Search and Export');
98
99 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
100
101 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements();
102 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, array('class' => 'crm-select2 huge'));
103
104 // add select for groups
105 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
106 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
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, array('class' => 'crm-select2 huge'));
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 /**
129 * @return null
130 */
131 public function summary() {
132 return NULL;
133 }
134
135 /**
136 * @param int $offset
137 * @param int $rowcount
138 * @param null $sort
139 * @param bool $returnSQL
140 *
141 * @return string
142 */
143 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
144 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
145 }
146
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 */
156 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
157 //redirect if custom group not select in search criteria
158 if (empty($this->_formValues['custom_group'])) {
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";
169 $sort = "contact_a.id";
170
171 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
172 }
173 else {
174 $selectClause = "
175 contact_a.id as contact_id ,
176 contact_a.contact_type as contact_type,
177 contact_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
195 /**
196 * @return string
197 */
198 public function from() {
199 $this->buildACLClause('contact_a');
200 $from = "FROM civicrm_contact contact_a {$this->_aclFrom}";
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 public 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('__', $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 if ($this->_aclWhere) {
266 $clause[] = " {$this->_aclWhere}";
267 }
268
269 $where = '( 1 )';
270 if (!empty($clause)) {
271 $where .= ' AND ' . implode(' AND ', $clause);
272 }
273
274 return $this->whereClause($where, $params);
275 }
276
277 /**
278 * @return string
279 */
280 public function templateFile() {
281 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
282 }
283
284 /**
285 * @param $row
286 */
287 public function alterRow(&$row) {
288 foreach ($row as $fieldName => &$field) {
289 if (strpos($fieldName, 'custom_') === 0) {
290 $field = CRM_Core_BAO_CustomField::displayValue($field, $fieldName);
291 }
292 }
293 }
294
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
302 }