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