phpcs - Fix error, "Visibility must be declared on method"
[civicrm-core.git] / CRM / Contact / Form / Search / Basic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Files required
38 */
39
40 /**
41 * Base Search / View form for *all* listing of multiple
42 * contacts
43 */
44 class CRM_Contact_Form_Search_Basic extends CRM_Contact_Form_Search {
45
46 /*
47 * csv - common search values
48 *
49 * @var array
50 * @access protected
51 * @static
52 */
53 static $csv = array('contact_type', 'group', 'tag');
54
55 /**
56 * Build the form object
57 *
58 * @access public
59 *
60 * @return void
61 */
62 public function buildQuickForm() {
63 // text for sort_name or email criteria
64 $config = CRM_Core_Config::singleton();
65 $label = empty($config->includeEmailInName) ? ts('Name') : ts('Name or Email');
66 $this->add('text', 'sort_name', $label);
67
68 $searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
69 'advanced_search_options'
70 );
71
72 $shortCuts = array();
73 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
74 // this is loaded onto then replace with something like '__' & test
75 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
76 if (!empty($searchOptions['contactType'])) {
77 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
78 $this->add('select', 'contact_type',
79 ts('is...'),
80 $contactTypes,
81 FALSE,
82 array('class' => 'crm-select2')
83 );
84 }
85
86 if (!empty($searchOptions['groups'])) {
87 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
88 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '&nbsp;&nbsp;', TRUE);
89
90 // add select for groups
91 $group = array('' => ts('- any group -')) + $groupHierarchy;
92 $this->add('select', 'group', ts('in'), $group, FALSE, array('class' => 'crm-select2'));
93 }
94
95 if (!empty($searchOptions['tags'])) {
96 // tag criteria
97 if (!empty($this->_tag)) {
98 $tag = array('' => ts('- any tag -')) + $this->_tag;
99 $this->add('select', 'tag', ts('with'), $tag, FALSE, array('class' => 'crm-select2'));
100 }
101 }
102
103 parent::buildQuickForm();
104 }
105
106 /**
107 * Set the default form values
108 *
109 * @access protected
110 *
111 * @return array the default array reference
112 */
113 public function setDefaultValues() {
114 $defaults = array();
115
116 $defaults['sort_name'] = CRM_Utils_Array::value('sort_name', $this->_formValues);
117 foreach (self::$csv as $v) {
118 if (!empty($this->_formValues[$v]) && is_array($this->_formValues[$v])) {
119 $tmpArray = array_keys($this->_formValues[$v]);
120 $defaults[$v] = array_pop($tmpArray);
121 }
122 else {
123 $defaults[$v] = '';
124 }
125 }
126
127 if ($this->_context === 'amtg') {
128 $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
129 }
130
131 if ($this->_context === 'smog') {
132 $defaults['group_contact_status[Added]'] = TRUE;
133 }
134
135 return $defaults;
136 }
137
138 /**
139 * Add local and global form rules
140 *
141 * @access protected
142 *
143 * @return void
144 */
145 public function addRules() {
146 $this->addFormRule(array('CRM_Contact_Form_Search_Basic', 'formRule'));
147 }
148
149 /**
150 * Processing needed for buildForm and later
151 *
152 * @return void
153 * @access public
154 */
155 public function preProcess() {
156 $this->set('searchFormName', 'Basic');
157
158 parent::preProcess();
159 }
160
161 /**
162 * @return array
163 */
164 public function &getFormValues() {
165 return $this->_formValues;
166 }
167
168 /**
169 * This method is called for processing a submitted search form
170 *
171 * @return void
172 * @access public
173 */
174 public function postProcess() {
175 $this->set('isAdvanced', '0');
176 $this->set('isSearchBuilder', '0');
177
178 // get user submitted values
179 // get it from controller only if form has been submitted, else preProcess has set this
180 if (!empty($_POST)) {
181 $this->_formValues = $this->controller->exportValues($this->_name);
182 $this->normalizeFormValues();
183 }
184
185 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
186 $this->_formValues['group'][$this->_groupID] = 1;
187 }
188 elseif (isset($this->_ssID) && empty($_POST)) {
189 // if we are editing / running a saved search and the form has not been posted
190 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
191
192 //fix for CRM-1505
193 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
194 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
195 }
196 }
197
198 // we dont want to store the sortByCharacter in the formValue, it is more like
199 // a filter on the result set
200 // this filter is reset if we click on the search button
201 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
202 if (strtolower($this->_sortByCharacter) == 'all') {
203 $this->_formValues['sortByCharacter'] = NULL;
204 }
205 else {
206 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
207 }
208 }
209 else {
210 $this->_sortByCharacter = NULL;
211 }
212
213 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
214 $this->_returnProperties = &$this->returnProperties();
215
216 parent::postProcess();
217 }
218
219 /**
220 * Normalize the form values to make it look similar to the advanced form values
221 * this prevents a ton of work downstream and allows us to use the same code for
222 * multiple purposes (queries, save/edit etc)
223 *
224 * @return void
225 * @access private
226 */
227 public function normalizeFormValues() {
228 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
229 if ($contactType && !is_array($contactType)) {
230 unset($this->_formValues['contact_type']);
231 $this->_formValues['contact_type'][$contactType] = 1;
232 }
233
234 $config = CRM_Core_Config::singleton();
235
236 $group = CRM_Utils_Array::value('group', $this->_formValues);
237 if ($group && !is_array($group)) {
238 unset($this->_formValues['group']);
239 $this->_formValues['group'][$group] = 1;
240 }
241
242 $tag = CRM_Utils_Array::value('tag', $this->_formValues);
243 if ($tag && !is_array($tag)) {
244 unset($this->_formValues['tag']);
245 $this->_formValues['tag'][$tag] = 1;
246 }
247
248 return;
249 }
250
251 /**
252 * Add a form rule for this form. If Go is pressed then we must select some checkboxes
253 * and an action
254 */
255 public static function formRule($fields) {
256 // check actionName and if next, then do not repeat a search, since we are going to the next page
257 if (array_key_exists('_qf_Search_next', $fields)) {
258 if (empty($fields['task'])) {
259 return array('task' => 'Please select a valid action.');
260 }
261
262 if (CRM_Utils_Array::value('task', $fields) == CRM_Contact_Task::SAVE_SEARCH) {
263 // dont need to check for selection of contacts for saving search
264 return TRUE;
265 }
266
267 // if the all contact option is selected, ignore the contact checkbox validation
268 if ($fields['radio_ts'] == 'ts_all') {
269 return TRUE;
270 }
271
272 foreach ($fields as $name => $dontCare) {
273 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
274 return TRUE;
275 }
276 }
277 return array('task' => 'Please select one or more checkboxes to perform the action on.');
278 }
279 return TRUE;
280 }
281
282 /**
283 * Return a descriptive name for the page, used in wizard header
284 *
285 * @return string
286 * @access public
287 */
288 /**
289 * @return string
290 */
291 public function getTitle() {
292 return ts('Find Contacts');
293 }
294 }
295