Merge pull request #12010 from JMAConsulting/dev-core-70
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Basic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33 class CRM_Contact_Form_Search_Custom_Basic extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
34
35 protected $_query;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
38
39 /**
40 * Class constructor.
41 *
42 * @param array $formValues
43 */
44 public function __construct(&$formValues) {
45 parent::__construct($formValues);
46
47 $this->_columns = array(
48 '' => 'contact_type',
49 ts('Name') => 'sort_name',
50 ts('Address') => 'street_address',
51 ts('City') => 'city',
52 ts('State') => 'state_province',
53 ts('Postal') => 'postal_code',
54 ts('Country') => 'country',
55 ts('Email') => 'email',
56 ts('Phone') => 'phone',
57 );
58
59 $params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
60 $returnProperties = array();
61 $returnProperties['contact_sub_type'] = 1;
62
63 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
64 'address_options', TRUE, NULL, TRUE
65 );
66
67 foreach ($this->_columns as $name => $field) {
68 if (in_array($field, array(
69 'street_address',
70 'city',
71 'state_province',
72 'postal_code',
73 'country',
74 )) && empty($addressOptions[$field])
75 ) {
76 unset($this->_columns[$name]);
77 continue;
78 }
79 $returnProperties[$field] = 1;
80 }
81
82 $this->_query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
83 FALSE, FALSE, 1, FALSE, FALSE
84 );
85 }
86
87 /**
88 * @param CRM_Core_Form $form
89 */
90 public function buildForm(&$form) {
91 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements();
92 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, FALSE, array('class' => 'crm-select2 huge'));
93
94 // add select for groups
95 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup();
96 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
97
98 // add select for categories
99 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
100 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
101
102 // text for sort_name
103 $form->add('text', 'sort_name', ts('Name'));
104
105 $form->assign('elements', array('sort_name', 'contact_type', 'group', 'tag'));
106 }
107
108 /**
109 * @return CRM_Contact_DAO_Contact
110 */
111 public function count() {
112 return $this->_query->searchQuery(0, 0, NULL, TRUE);
113 }
114
115 /**
116 * @param int $offset
117 * @param int $rowCount
118 * @param null $sort
119 * @param bool $includeContactIDs
120 * @param bool $justIDs
121 *
122 * @return CRM_Contact_DAO_Contact
123 */
124 public function all(
125 $offset = 0,
126 $rowCount = 0,
127 $sort = NULL,
128 $includeContactIDs = FALSE,
129 $justIDs = FALSE
130 ) {
131 return $this->_query->searchQuery(
132 $offset,
133 $rowCount,
134 $sort,
135 FALSE,
136 $includeContactIDs,
137 FALSE,
138 $justIDs,
139 TRUE
140 );
141 }
142
143 /**
144 * @return string
145 */
146 public function from() {
147 $this->buildACLClause('contact_a');
148 $from = $this->_query->_fromClause;
149 $from .= "{$this->_aclFrom}";
150 return $from;
151 }
152
153 /**
154 * @param bool $includeContactIDs
155 *
156 * @return string|void
157 */
158 public function where($includeContactIDs = FALSE) {
159 if ($whereClause = $this->_query->whereClause()) {
160 if ($this->_aclWhere) {
161 $whereClause .= " AND {$this->_aclWhere}";
162 }
163 return $whereClause;
164 }
165 return ' (1) ';
166 }
167
168 /**
169 * @return string
170 */
171 public function templateFile() {
172 return 'CRM/Contact/Form/Search/Basic.tpl';
173 }
174
175 /**
176 * @return CRM_Contact_BAO_Query
177 */
178 public function getQueryObj() {
179 return $this->_query;
180 }
181
182 /**
183 * @param string $tableAlias
184 */
185 public function buildACLClause($tableAlias = 'contact') {
186 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
187 }
188
189 }