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