Merge https://github.com/civicrm/civicrm-core/pull/6276
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Basic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Search_Custom_Basic extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
36
37 protected $_query;
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
40
41 /**
42 * @param $formValues
43 */
44 /**
45 * @param $formValues
46 */
47 public function __construct(&$formValues) {
48 parent::__construct($formValues);
49
50 $this->normalize();
51 $this->_columns = array(
52 '' => 'contact_type',
53 ts('Name') => 'sort_name',
54 ts('Address') => 'street_address',
55 ts('City') => 'city',
56 ts('State') => 'state_province',
57 ts('Postal') => 'postal_code',
58 ts('Country') => 'country',
59 ts('Email') => 'email',
60 ts('Phone') => 'phone',
61 );
62
63 $params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
64 $returnProperties = array();
65 $returnProperties['contact_sub_type'] = 1;
66
67 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
68 'address_options', TRUE, NULL, TRUE
69 );
70
71 foreach ($this->_columns as $name => $field) {
72 if (in_array($field, array(
73 'street_address',
74 'city',
75 'state_province',
76 'postal_code',
77 'country',
78 )) && empty($addressOptions[$field])
79 ) {
80 unset($this->_columns[$name]);
81 continue;
82 }
83 $returnProperties[$field] = 1;
84 }
85
86 $this->_query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
87 FALSE, FALSE, 1, FALSE, FALSE
88 );
89 }
90
91 /**
92 * Normalize the form values to make it look similar to the advanced form values
93 * this prevents a ton of work downstream and allows us to use the same code for
94 * multiple purposes (queries, save/edit etc)
95 *
96 * @return void
97 */
98 public function normalize() {
99 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
100 if ($contactType && !is_array($contactType)) {
101 unset($this->_formValues['contact_type']);
102 $this->_formValues['contact_type'][$contactType] = 1;
103 }
104
105 $group = CRM_Utils_Array::value('group', $this->_formValues);
106 if ($group && !is_array($group)) {
107 unset($this->_formValues['group']);
108 $this->_formValues['group'][$group] = 1;
109 }
110
111 $tag = CRM_Utils_Array::value('tag', $this->_formValues);
112 if ($tag && !is_array($tag)) {
113 unset($this->_formValues['tag']);
114 $this->_formValues['tag'][$tag] = 1;
115 }
116
117 return NULL;
118 }
119
120 /**
121 * @param CRM_Core_Form $form
122 */
123 public function buildForm(&$form) {
124 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
125 // this is loaded onto then replace with something like '__' & test
126 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
127 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
128 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, FALSE, array('class' => 'crm-select2 huge'));
129
130 // add select for groups
131 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup();
132 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
133
134 // add select for categories
135 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
136 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
137
138 // text for sort_name
139 $form->add('text', 'sort_name', ts('Name'));
140
141 $form->assign('elements', array('sort_name', 'contact_type', 'group', 'tag'));
142 }
143
144 /**
145 * @return CRM_Contact_DAO_Contact
146 */
147 public function count() {
148 return $this->_query->searchQuery(0, 0, NULL, TRUE);
149 }
150
151 /**
152 * @param int $offset
153 * @param int $rowCount
154 * @param null $sort
155 * @param bool $includeContactIDs
156 * @param bool $justIDs
157 *
158 * @return CRM_Contact_DAO_Contact
159 */
160 public function all(
161 $offset = 0,
162 $rowCount = 0,
163 $sort = NULL,
164 $includeContactIDs = FALSE,
165 $justIDs = FALSE
166 ) {
167 return $this->_query->searchQuery(
168 $offset,
169 $rowCount,
170 $sort,
171 FALSE,
172 $includeContactIDs,
173 FALSE,
174 $justIDs,
175 TRUE
176 );
177 }
178
179 /**
180 * @return string
181 */
182 public function from() {
183 $this->buildACLClause('contact_a');
184 $from = $this->_query->_fromClause;
185 $from .= "{$this->_aclFrom}";
186 return $from;
187 }
188
189 /**
190 * @param bool $includeContactIDs
191 *
192 * @return string|void
193 */
194 public function where($includeContactIDs = FALSE) {
195 if ($whereClause = $this->_query->whereClause()) {
196 if ($this->_aclWhere) {
197 $whereClause .= " AND {$this->_aclWhere}";
198 }
199 return $whereClause;
200 }
201 return ' (1) ';
202 }
203
204 /**
205 * @return string
206 */
207 public function templateFile() {
208 return 'CRM/Contact/Form/Search/Basic.tpl';
209 }
210
211 /**
212 * @return CRM_Contact_BAO_Query
213 */
214 public function getQueryObj() {
215 return $this->_query;
216 }
217
218 /**
219 * @param string $tableAlias
220 */
221 public function buildACLClause($tableAlias = 'contact') {
222 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
223 }
224
225 }