3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Contact_Form_Search_Custom_MultipleValues
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_groupTree;
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
40 protected $fieldInfo = array();
45 * @param array $formValues
47 public function __construct(&$formValues) {
48 parent
::__construct($formValues);
50 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree("'Contact', 'Individual', 'Organization', 'Household'",
51 CRM_Core_DAO
::$_nullObject,
55 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
57 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
59 $this->_columns
= array(
60 ts('Contact ID') => 'contact_id',
61 ts('Contact Type') => 'contact_type',
62 ts('Name') => 'sort_name',
65 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
67 if (!empty($this->_customGroupIDs
)) {
73 * Add all the fields for chosen groups
75 public function addColumns() {
76 $this->_tables
= array();
77 foreach ($this->_groupTree
as $groupID => $group) {
78 if (empty($this->_customGroupIDs
[$groupID])) {
82 $this->fieldInfo +
= $group['fields'];
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();
90 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
96 * @param CRM_Core_Form $form
98 public function buildForm(&$form) {
100 $this->setTitle('Multiple Value Custom Group Search and Export');
102 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
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'));
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'));
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'));
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',
122 // add the checkbox for custom_groups
123 foreach ($this->_groupTree
as $groupID => $group) {
124 if ($groupID == 'info') {
127 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
134 public function summary() {
140 * @param int $rowcount
142 * @param bool $returnSQL
146 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
147 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
152 * @param int $rowcount
154 * @param bool $includeContactIDs
155 * @param bool $justIDs
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
171 $selectClause = "contact_a.id as contact_id";
172 $sort = "contact_a.id";
174 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
178 contact_a.id as contact_id ,
179 contact_a.contact_type as contact_type,
180 contact_a.sort_name as sort_name,
184 $customClauses = array();
185 foreach ($this->_tables
as $tableName => $fields) {
186 foreach ($fields as $fieldID => $fieldName) {
187 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
190 $selectClause .= implode(',', $customClauses);
192 return $this->sql($selectClause,
193 $offset, $rowcount, $sort,
194 $includeContactIDs, NULL
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 ";
210 $from .= implode(' ', $customFrom);
213 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
215 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
216 AND contact_a.id = t.entity_id)";
220 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
221 AND cgc.status = 'Added')";
228 * @param bool $includeContactIDs
232 public function where($includeContactIDs = FALSE) {
236 $name = CRM_Utils_Array
::value('sort_name',
240 if (strpos($name, '%') === FALSE) {
243 $params[$count] = array($name, 'String');
244 $clause[] = "contact_a.sort_name LIKE %{$count}";
248 $contact_type = CRM_Utils_Array
::value('contact_type',
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]'";
257 $clause[] = "contact_a.contact_type = '$contactType[0]'";
262 $clause[] = "t.tag_id = {$this->_tag}";
266 $clause[] = "cgc.group_id = {$this->_group}";
268 if ($this->_aclWhere
) {
269 $clause[] = " {$this->_aclWhere}";
273 if (!empty($clause)) {
274 $where .= ' AND ' . implode(' AND ', $clause);
277 return $this->whereClause($where, $params);
283 public function templateFile() {
284 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
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);
299 * @param string $tableAlias
301 public function buildACLClause($tableAlias = 'contact') {
302 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);