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'", NULL, NULL, -1);
52 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
54 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
56 $this->_columns
= array(
57 ts('Contact ID') => 'contact_id',
58 ts('Contact Type') => 'contact_type',
59 ts('Name') => 'sort_name',
62 $this->_customGroupIDs
= CRM_Utils_Array
::value('custom_group', $formValues);
64 if (!empty($this->_customGroupIDs
)) {
70 * Add all the fields for chosen groups
72 public function addColumns() {
73 $this->_tables
= array();
74 foreach ($this->_groupTree
as $groupID => $group) {
75 if (empty($this->_customGroupIDs
[$groupID])) {
79 $this->fieldInfo +
= $group['fields'];
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();
87 $this->_tables
[$group['table_name']][$field['id']] = $field['column_name'];
93 * @param CRM_Core_Form $form
95 public function buildForm(&$form) {
97 $this->setTitle('Multiple Value Custom Group Search and Export');
99 $form->add('text', 'sort_name', ts('Contact Name'), TRUE);
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'));
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'));
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'));
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',
119 // add the checkbox for custom_groups
120 foreach ($this->_groupTree
as $groupID => $group) {
121 if ($groupID == 'info') {
124 $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']);
131 public function summary() {
137 * @param int $rowcount
139 * @param bool $returnSQL
143 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
144 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
149 * @param int $rowcount
151 * @param bool $includeContactIDs
152 * @param bool $justIDs
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
168 $selectClause = "contact_a.id as contact_id";
169 $sort = "contact_a.id";
171 return $this->sql($selectClause, $offset, $rowcount, $sort, $includeContactIDs, NULL);
175 contact_a.id as contact_id ,
176 contact_a.contact_type as contact_type,
177 contact_a.sort_name as sort_name,
181 $customClauses = array();
182 foreach ($this->_tables
as $tableName => $fields) {
183 foreach ($fields as $fieldID => $fieldName) {
184 $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}";
187 $selectClause .= implode(',', $customClauses);
189 return $this->sql($selectClause,
190 $offset, $rowcount, $sort,
191 $includeContactIDs, NULL
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 ";
207 $from .= implode(' ', $customFrom);
210 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
212 $from .= " LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact'
213 AND contact_a.id = t.entity_id)";
217 $from .= " LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id
218 AND cgc.status = 'Added')";
225 * @param bool $includeContactIDs
229 public function where($includeContactIDs = FALSE) {
233 $name = CRM_Utils_Array
::value('sort_name',
237 if (strpos($name, '%') === FALSE) {
240 $params[$count] = array($name, 'String');
241 $clause[] = "contact_a.sort_name LIKE %{$count}";
245 $contact_type = CRM_Utils_Array
::value('contact_type',
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]'";
254 $clause[] = "contact_a.contact_type = '$contactType[0]'";
259 $clause[] = "t.tag_id = {$this->_tag}";
263 $clause[] = "cgc.group_id = {$this->_group}";
265 if ($this->_aclWhere
) {
266 $clause[] = " {$this->_aclWhere}";
270 if (!empty($clause)) {
271 $where .= ' AND ' . implode(' AND ', $clause);
274 return $this->whereClause($where, $params);
280 public function templateFile() {
281 return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
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);
296 * @param string $tableAlias
298 public function buildACLClause($tableAlias = 'contact') {
299 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);