Merge pull request #9616 from ErichBSchulz/feature/drupal_boot_no_exit
[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'",
51 CRM_Core_DAO::$_nullObject,
52 NULL, -1
53 );
54
55 $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
56
57 $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
58
59 $this->_columns = array(
60 ts('Contact ID') => 'contact_id',
61 ts('Contact Type') => 'contact_type',
62 ts('Name') => 'sort_name',
63 );
64
65 $this->_customGroupIDs = CRM_Utils_Array::value('custom_group', $formValues);
66
67 if (!empty($this->_customGroupIDs)) {
68 $this->addColumns();
69 }
70 }
71
72 /**
73 * Add all the fields for chosen groups
74 */
75 public function addColumns() {
76 $this->_tables = array();
77 foreach ($this->_groupTree as $groupID => $group) {
78 if (empty($this->_customGroupIDs[$groupID])) {
79 continue;
80 }
81
82 $this->fieldInfo += $group['fields'];
83
84 // now handle all the fields
85 foreach ($group['fields'] as $fieldID => $field) {
86 $this->_columns[$field['label']] = "custom_{$field['id']}";
87 if (!array_key_exists($group['table_name'], $this->_tables)) {
88 $this->_tables[$group['table_name']] = array();
89 }
90 $this->_tables[$group['table_name']][$field['id']] = $field['column_name'];
91 }
92 }
93 }
94
95 /**
96 * @param CRM_Core_Form $form
97 */
98 public function buildForm(&$form) {
99
100 $this->setTitle('Multiple Value Custom Group Search and Export');
101
102 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
103
104 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements();
105 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, array('class' => 'crm-select2 huge'));
106
107 // add select for groups
108 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
109 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
110
111 // add select for tags
112 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
113 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
114
115 if (empty($this->_groupTree)) {
116 CRM_Core_Error::statusBounce(ts("Atleast one Custom Group must be present, for Custom Group search."),
117 CRM_Utils_System::url('civicrm/contact/search/custom/list',
118 'reset=1'
119 )
120 );
121 }
122 // add the checkbox for custom_groups
123 foreach ($this->_groupTree as $groupID => $group) {
124 if ($groupID == 'info') {
125 continue;
126 }
127 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
128 }
129 }
130
131 /**
132 * @return null
133 */
134 public function summary() {
135 return NULL;
136 }
137
138 /**
139 * @param int $offset
140 * @param int $rowcount
141 * @param null $sort
142 * @param bool $returnSQL
143 *
144 * @return string
145 */
146 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
147 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
148 }
149
150 /**
151 * @param int $offset
152 * @param int $rowcount
153 * @param null $sort
154 * @param bool $includeContactIDs
155 * @param bool $justIDs
156 *
157 * @return string
158 */
159 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
160 //redirect if custom group not select in search criteria
161 if (empty($this->_formValues['custom_group'])) {
162 CRM_Core_Error::statusBounce(ts("You must select at least one Custom Group as a search criteria."),
163 CRM_Utils_System::url('civicrm/contact/search/custom',
164 "reset=1&csid={$this->_formValues['customSearchID']}",
165 FALSE, NULL, FALSE, TRUE
166 )
167 );
168 }
169
170 if ($justIDs) {
171 $selectClause = "contact_a.id as contact_id";
172 $sort = "contact_a.id";
173
174 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
175 }
176 else {
177 $selectClause = "
178 contact_a.id as contact_id ,
179 contact_a.contact_type as contact_type,
180 contact_a.sort_name as sort_name,
181 ";
182 }
183
184 $customClauses = array();
185 foreach ($this->_tables as $tableName => $fields) {
186 foreach ($fields as $fieldID => $fieldName) {
187 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
188 }
189 }
190 $selectClause .= implode(',', $customClauses);
191
192 return $this->sql($selectClause,
193 $offset, $rowcount, $sort,
194 $includeContactIDs, NULL
195 );
196 }
197
198 /**
199 * @return string
200 */
201 public function from() {
202 $this->buildACLClause('contact_a');
203 $from = "FROM civicrm_contact contact_a {$this->_aclFrom}";
204 $customFrom = array();
205 // lets do an INNER JOIN so we get only relevant values rather than all values
206 if (!empty($this->_tables)) {
207 foreach ($this->_tables as $tableName => $fields) {
208 $customFrom[] = " INNER JOIN $tableName ON {$tableName}.entity_id = contact_a.id ";
209 }
210 $from .= implode(' ', $customFrom);
211 }
212
213 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
214 if ($this->_tag) {
215 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
216 AND contact_a.id = t.entity_id)";
217 }
218
219 if ($this->_group) {
220 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
221 AND cgc.status = 'Added')";
222 }
223
224 return $from;
225 }
226
227 /**
228 * @param bool $includeContactIDs
229 *
230 * @return string
231 */
232 public function where($includeContactIDs = FALSE) {
233 $count = 1;
234 $clause = array();
235 $params = array();
236 $name = CRM_Utils_Array::value('sort_name',
237 $this->_formValues
238 );
239 if ($name != NULL) {
240 if (strpos($name, '%') === FALSE) {
241 $name = "%{$name}%";
242 }
243 $params[$count] = array($name, 'String');
244 $clause[] = "contact_a.sort_name LIKE %{$count}";
245 $count++;
246 }
247
248 $contact_type = CRM_Utils_Array::value('contact_type',
249 $this->_formValues
250 );
251 if ($contact_type != NULL) {
252 $contactType = explode('__', $contact_type);
253 if (count($contactType) > 1) {
254 $clause[] = "contact_a.contact_type = '$contactType[0]' AND contact_a.contact_sub_type = '$contactType[1]'";
255 }
256 else {
257 $clause[] = "contact_a.contact_type = '$contactType[0]'";
258 }
259 }
260
261 if ($this->_tag) {
262 $clause[] = "t.tag_id = {$this->_tag}";
263 }
264
265 if ($this->_group) {
266 $clause[] = "cgc.group_id = {$this->_group}";
267 }
268 if ($this->_aclWhere) {
269 $clause[] = " {$this->_aclWhere}";
270 }
271
272 $where = '( 1 )';
273 if (!empty($clause)) {
274 $where .= ' AND ' . implode(' AND ', $clause);
275 }
276
277 return $this->whereClause($where, $params);
278 }
279
280 /**
281 * @return string
282 */
283 public function templateFile() {
284 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
285 }
286
287 /**
288 * @param $row
289 */
290 public function alterRow(&$row) {
291 foreach ($row as $fieldName => &$field) {
292 if (strpos($fieldName, 'custom_') === 0) {
293 $field = CRM_Core_BAO_CustomField::displayValue($field, $fieldName);
294 }
295 }
296 }
297
298 /**
299 * @param string $tableAlias
300 */
301 public function buildACLClause($tableAlias = 'contact') {
302 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
303 }
304
305 }