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