3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Contact_Form_Search_Custom_MultipleValues
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_groupTree;
40 protected $_aclFrom = NULL;
41 protected $_aclWhere = NULL;
46 public function __construct(&$formValues) {
47 parent
::__construct($formValues);
49 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree("'Contact', 'Individual', 'Organization', 'Household'",
50 CRM_Core_DAO
::$_nullObject,
54 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
56 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
58 $this->_columns
= array(
59 ts('Contact ID') => 'contact_id',
60 ts('Contact Type') => 'contact_type',
61 ts('Name') => 'sort_name',
64 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
66 if (!empty($this->_customGroupIDs
)) {
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])) {
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();
85 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
87 // also build the option array
88 $this->_options
[$field['id']] = array();
89 CRM_Core_BAO_CustomField
::buildOption($field,
90 $this->_options
[$field['id']]
97 * @param CRM_Core_Form $form
99 public function buildForm(&$form) {
102 * You can define a custom title for the search form
104 $this->setTitle('Multiple Value Custom Group Search and Export');
106 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
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'));
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'));
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'));
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',
130 // add the checkbox for custom_groups
131 foreach ($this->_groupTree
as $groupID => $group) {
132 if ($groupID == 'info') {
135 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
142 public function summary() {
148 * @param int $rowcount
150 * @param bool $returnSQL
154 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
155 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
160 * @param int $rowcount
162 * @param bool $includeContactIDs
163 * @param bool $justIDs
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
179 $selectClause = "contact_a.id as contact_id";
180 $sort = "contact_a.id";
182 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
186 contact_a.id as contact_id ,
187 contact_a.contact_type as contact_type,
188 contact_a.sort_name as sort_name,
192 $customClauses = array();
193 foreach ($this->_tables
as $tableName => $fields) {
194 foreach ($fields as $fieldID => $fieldName) {
195 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
198 $selectClause .= implode(',', $customClauses);
200 return $this->sql($selectClause,
201 $offset, $rowcount, $sort,
202 $includeContactIDs, NULL
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 ";
218 $from .= implode(' ', $customFrom);
221 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
223 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
224 AND contact_a.id = t.entity_id)";
228 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
229 AND cgc.status = 'Added')";
236 * @param bool $includeContactIDs
240 public function where($includeContactIDs = FALSE) {
244 $name = CRM_Utils_Array
::value('sort_name',
248 if (strpos($name, '%') === FALSE) {
251 $params[$count] = array($name, 'String');
252 $clause[] = "contact_a.sort_name LIKE %{$count}";
256 $contact_type = CRM_Utils_Array
::value('contact_type',
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]'";
265 $clause[] = "contact_a.contact_type = '$contactType[0]'";
270 $clause[] = "t.tag_id = {$this->_tag}";
274 $clause[] = "cgc.group_id = {$this->_group}";
276 if ($this->_aclWhere
) {
277 $clause[] = " {$this->_aclWhere}";
281 if (!empty($clause)) {
282 $where .= ' AND ' . implode(' AND ', $clause);
285 return $this->whereClause($where, $params);
291 public function templateFile() {
292 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
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')
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');
307 elseif ($row["custom_{$fieldID}"] &&
308 array_key_exists($row["custom_{$fieldID}"],
312 $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]];
315 elseif (in_array($values['attributes']['html_type'],
316 array('CheckBox', 'Multi-Select', 'AdvMulti-Select')
318 $valueSeparatedArray = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $row["custom_{$fieldID}"]));
319 foreach ($valueSeparatedArray as $val) {
320 $customVal[] = $values[$val];
322 $row["custom_{$fieldID}"] = implode(', ', $customVal);
324 elseif (in_array($values['attributes']['html_type'],
325 array('Multi-Select State/Province', 'Select State/Province')
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];
332 $row["custom_{$fieldID}"] = implode(', ', $customVal);
334 elseif (in_array($values['attributes']['html_type'],
335 array('Multi-Select Country', 'Select Country')
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'
341 foreach ($valueSeparatedArray as $val) {
342 $customVal[] = $countryNames[$val];
344 $row["custom_{$fieldID}"] = implode(', ', $customVal);
352 public function setTitle($title) {
353 CRM_Utils_System
::setTitle($title);
357 * @param string $tableAlias
359 public function buildACLClause($tableAlias = 'contact') {
360 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);