Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | * This class if for search builder processing | |
38 | */ | |
39 | class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { | |
40 | ||
41 | /** | |
42 | * number of columns in where | |
43 | * | |
44 | * @var int | |
45 | * @access public | |
46 | */ | |
47 | public $_columnCount; | |
48 | ||
49 | /** | |
50 | * number of blocks to be shown | |
51 | * | |
52 | * @var int | |
53 | * @access public | |
54 | */ | |
55 | public $_blockCount; | |
56 | ||
57 | /** | |
58 | * Function to actually build the form | |
59 | * | |
60 | * @return None | |
61 | * @access public | |
62 | */ | |
63 | public function preProcess() { | |
64 | $this->set('searchFormName', 'Builder'); | |
65 | ||
66 | $this->set('context', 'builder'); | |
67 | parent::preProcess(); | |
68 | ||
69 | // Get the block count | |
70 | $this->_blockCount = $this->get('blockCount'); | |
71 | // Initialize new form | |
72 | if (!$this->_blockCount) { | |
73 | $this->_blockCount = 4; | |
74 | $this->set('newBlock', 1); | |
75 | } | |
76 | ||
77 | //get the column count | |
78 | $this->_columnCount = $this->get('columnCount'); | |
79 | ||
80 | for ($i = 1; $i < $this->_blockCount; $i++) { | |
81 | if (empty($this->_columnCount[$i])) { | |
82 | $this->_columnCount[$i] = 5; | |
83 | } | |
84 | } | |
85 | ||
86 | $this->_loadedMappingId = $this->get('savedMapping'); | |
87 | ||
88 | if ($this->get('showSearchForm')) { | |
89 | $this->assign('showSearchForm', TRUE); | |
90 | } | |
91 | else { | |
92 | $this->assign('showSearchForm', FALSE); | |
93 | } | |
94 | } | |
95 | ||
96 | public function buildQuickForm() { | |
97 | $fields = self::fields(); | |
98 | // Get fields of type date | |
99 | // FIXME: This is a hack until our fields contain this meta-data | |
100 | $dateFields = array(); | |
101 | foreach ($fields as $name => $field) { | |
102 | if (strpos($name, '_date') || CRM_Utils_Array::value('data_type', $field) == 'Date') { | |
103 | $dateFields[] = $name; | |
104 | } | |
105 | } | |
106 | // Add javascript | |
107 | CRM_Core_Resources::singleton() | |
108 | ->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Search/Builder.js') | |
109 | ->addSetting(array( | |
110 | 'searchBuilder' => array( | |
111 | // Index of newly added/expanded block (1-based index) | |
112 | 'newBlock' => $this->get('newBlock'), | |
113 | 'dateFields' => $dateFields, | |
114 | 'fieldOptions' => self::fieldOptions(), | |
115 | ), | |
116 | )); | |
117 | //get the saved search mapping id | |
118 | $mappingId = NULL; | |
119 | if ($this->_ssID) { | |
120 | $mappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id'); | |
121 | } | |
122 | ||
123 | CRM_Core_BAO_Mapping::buildMappingForm($this, 'Search Builder', $mappingId, $this->_columnCount, $this->_blockCount); | |
124 | ||
125 | parent::buildQuickForm(); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Add local and global form rules | |
130 | * | |
131 | * @access protected | |
132 | * | |
133 | * @return void | |
134 | */ | |
135 | function addRules() { | |
136 | $this->addFormRule(array('CRM_Contact_Form_Search_Builder', 'formRule'), $this); | |
137 | } | |
138 | ||
139 | /** | |
140 | * global validation rules for the form | |
141 | * | |
142 | * @param array $fields posted values of the form | |
143 | * | |
144 | * @return array list of errors to be posted back to the form | |
145 | * @static | |
146 | * @access public | |
147 | */ | |
148 | static function formRule($values, $files, $self) { | |
149 | if (CRM_Utils_Array::value('addMore', $values) || CRM_Utils_Array::value('addBlock', $values)) { | |
150 | return TRUE; | |
151 | } | |
152 | $fields = self::fields(); | |
153 | $fld = CRM_Core_BAO_Mapping::formattedFields($values, TRUE); | |
154 | ||
155 | $errorMsg = array(); | |
156 | foreach ($fld as $k => $v) { | |
157 | if (!$v[1]) { | |
158 | $errorMsg["operator[$v[3]][$v[4]]"] = ts("Please enter the operator."); | |
159 | } | |
160 | else { | |
161 | // CRM-10338 | |
162 | $v[2] = self::checkArrayKeyEmpty($v[2]); | |
163 | ||
164 | if (in_array($v[1], array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) && | |
165 | !empty($v[2])) { | |
166 | $errorMsg["value[$v[3]][$v[4]]"] = ts('Please clear your value if you want to use %1 operator.', array(1 => $v[1])); | |
167 | } | |
168 | elseif (($v[0] == 'group' || $v[0] == 'tag') && !empty($v[2])) { | |
169 | $grpId = array_keys($v[2]); | |
170 | if (!key($v[2])) { | |
171 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
172 | } | |
173 | ||
174 | if (count($grpId) > 1) { | |
175 | if ($v[1] != 'IN' && $v[1] != 'NOT IN') { | |
176 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
177 | } | |
178 | foreach ($grpId as $val) { | |
179 | $error = CRM_Utils_Type::validate($val, 'Integer', FALSE); | |
180 | if ($error != $val) { | |
181 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter valid value."); | |
182 | break; | |
183 | } | |
184 | } | |
185 | } | |
186 | else { | |
187 | $error = CRM_Utils_Type::validate($grpId[0], 'Integer', FALSE); | |
188 | if ($error != $grpId[0]) { | |
189 | $errorMsg["value[$v[3]][$v[4]]"] = ts('Please enter valid %1 id.', array(1 => $v[0])); | |
190 | } | |
191 | } | |
192 | } | |
193 | elseif (substr($v[0], 0, 7) === 'do_not_' or substr($v[0], 0, 3) === 'is_') { | |
194 | if (isset($v[2])) { | |
195 | $v2 = array($v[2]); | |
196 | if (!isset($v[2])) { | |
197 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
198 | } | |
199 | ||
200 | $error = CRM_Utils_Type::validate($v2[0], 'Integer', FALSE); | |
201 | if ($error != $v2[0]) { | |
202 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
203 | } | |
204 | } | |
205 | else { | |
206 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
207 | } | |
208 | } | |
209 | else { | |
210 | if (substr($v[0], 0, 7) == 'custom_') { | |
211 | $type = $fields[$v[0]]['data_type']; | |
212 | ||
213 | // hack to handle custom data of type state and country | |
214 | if (in_array($type, array( | |
215 | 'Country', 'StateProvince'))) { | |
216 | $type = "Integer"; | |
217 | } | |
218 | } | |
219 | else { | |
220 | $fldName = $v[0]; | |
221 | // FIXME: no idea at this point what to do with this, | |
222 | // FIXME: but definitely needs fixing. | |
223 | if (substr($v[0], 0, 13) == 'contribution_') { | |
224 | $fldName = substr($v[0], 13); | |
225 | } | |
226 | ||
227 | $fldValue = CRM_Utils_Array::value($fldName, $fields); | |
228 | $fldType = CRM_Utils_Array::value('type', $fldValue); | |
229 | $type = CRM_Utils_Type::typeToString($fldType); | |
230 | // Check Empty values for Integer Or Boolean Or Date type For operators other than IS NULL and IS NOT NULL. | |
231 | if (!in_array($v[1], | |
232 | array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) { | |
233 | if ((($type == 'Int' || $type == 'Boolean') && !trim($v[2])) && $v[2] != '0') { | |
234 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
235 | } | |
236 | elseif ($type == 'Date' && !trim($v[2])) { | |
237 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | if ($type && empty($errorMsg)) { | |
243 | // check for valid format while using IN Operator | |
244 | if ($v[1] == 'IN') { | |
245 | $inVal = trim($v[2]); | |
246 | //checking for format to avoid db errors | |
247 | if ($type == 'Int') { | |
248 | if (!preg_match('/^[(]([A-Za-z0-9\,]+)[)]$/', $inVal)) { | |
249 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); | |
250 | } | |
251 | } | |
252 | else { | |
253 | if (!(substr($inVal, 0, 1) == '(' && substr($inVal, -1, 1) == ')') && !preg_match('/^[(]([A-Za-z0-9åäöÅÄÖüÜœŒæÆøØ\,\s]+)[)]$/', $inVal)) { | |
254 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); | |
255 | } | |
256 | } | |
257 | ||
258 | // Validate each value in parenthesis to avoid db errors | |
259 | if (empty($errorMsg)) { | |
260 | $parenValues = array(); | |
261 | $parenValues = explode(',', trim($inVal, "(..)")); | |
262 | foreach ($parenValues as $val) { | |
263 | $val = trim($val); | |
264 | if (!$val && $val != '0') { | |
265 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter the values correctly."); | |
266 | } | |
267 | if (empty($errorMsg)) { | |
268 | $error = CRM_Utils_Type::validate($val, $type, FALSE); | |
269 | if ($error != $val) { | |
270 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
271 | } | |
272 | } | |
273 | } | |
274 | } | |
275 | } | |
276 | elseif (trim($v[2])) { | |
277 | //else check value for rest of the Operators | |
278 | $error = CRM_Utils_Type::validate($v[2], $type, FALSE); | |
279 | if ($error != $v[2]) { | |
280 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
281 | } | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | if (!empty($errorMsg)) { | |
289 | $self->set('showSearchForm', TRUE); | |
290 | $self->assign('rows', NULL); | |
291 | return $errorMsg; | |
292 | } | |
293 | ||
294 | return TRUE; | |
295 | } | |
296 | ||
297 | public function normalizeFormValues() {} | |
298 | ||
299 | public function &convertFormValues(&$formValues) { | |
300 | return CRM_Core_BAO_Mapping::formattedFields($formValues); | |
301 | } | |
302 | ||
303 | public function &returnProperties() { | |
304 | return CRM_Core_BAO_Mapping::returnProperties($this->_formValues); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Process the uploaded file | |
309 | * | |
310 | * @return void | |
311 | * @access public | |
312 | */ | |
313 | public function postProcess() { | |
314 | $this->set('isAdvanced', '2'); | |
315 | $this->set('isSearchBuilder', '1'); | |
316 | $this->set('showSearchForm', FALSE); | |
317 | ||
318 | $params = $this->controller->exportValues($this->_name); | |
319 | if (!empty($params)) { | |
320 | // Add another block | |
321 | if (!empty($params['addBlock'])) { | |
322 | $this->set('newBlock', $this->_blockCount); | |
323 | $this->_blockCount += 3; | |
324 | $this->set('blockCount', $this->_blockCount); | |
325 | $this->set('showSearchForm', TRUE); | |
326 | return; | |
327 | } | |
328 | // Add another field | |
329 | $addMore = CRM_Utils_Array::value('addMore', $params); | |
330 | for ($x = 1; $x <= $this->_blockCount; $x++) { | |
331 | if (!empty($addMore[$x])) { | |
332 | $this->set('newBlock', $x); | |
333 | $this->_columnCount[$x] = $this->_columnCount[$x] + 5; | |
334 | $this->set('columnCount', $this->_columnCount); | |
335 | $this->set('showSearchForm', TRUE); | |
336 | return; | |
337 | } | |
338 | } | |
339 | $this->set('newBlock', NULL); | |
340 | $checkEmpty = NULL; | |
341 | foreach ($params['mapper'] as $key => $value) { | |
342 | foreach ($value as $k => $v) { | |
343 | if ($v[0]) { | |
344 | $checkEmpty++; | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
349 | if (!$checkEmpty) { | |
350 | $this->set('newBlock', 1); | |
351 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/search/builder', '_qf_Builder_display=true')); | |
352 | } | |
353 | } | |
354 | ||
355 | // get user submitted values | |
356 | // get it from controller only if form has been submitted, else preProcess has set this | |
357 | if (!empty($_POST)) { | |
358 | $this->_formValues = $this->controller->exportValues($this->_name); | |
359 | ||
360 | // set the group if group is submitted | |
361 | if (CRM_Utils_Array::value('uf_group_id', $this->_formValues)) { | |
362 | $this->set('id', $this->_formValues['uf_group_id']); | |
363 | } | |
364 | else { | |
365 | $this->set('id', ''); | |
366 | } | |
367 | } | |
368 | ||
369 | // we dont want to store the sortByCharacter in the formValue, it is more like | |
370 | // a filter on the result set | |
371 | // this filter is reset if we click on the search button | |
372 | if ($this->_sortByCharacter !== NULL && empty($_POST)) { | |
373 | if (strtolower($this->_sortByCharacter) == 'all') { | |
374 | $this->_formValues['sortByCharacter'] = NULL; | |
375 | } | |
376 | else { | |
377 | $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; | |
378 | } | |
379 | } | |
380 | ||
381 | $this->_params = &$this->convertFormValues($this->_formValues); | |
382 | $this->_returnProperties = &$this->returnProperties(); | |
383 | ||
384 | // CRM-10338 check if value is empty array | |
385 | foreach ($this->_params as $k => $v) { | |
386 | $this->_params[$k][2] = self::checkArrayKeyEmpty($v[2]); | |
387 | } | |
388 | ||
389 | parent::postProcess(); | |
390 | } | |
391 | ||
392 | static function fields() { | |
393 | return array_merge( | |
394 | CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE), | |
395 | CRM_Core_Component::getQueryFields(), | |
396 | CRM_Activity_BAO_Activity::exportableFields() | |
397 | ); | |
398 | } | |
399 | ||
400 | /** | |
401 | * CRM-9434 Hackish function to fetch fields with options. | |
402 | * FIXME: When our core fields contain reliable metadata this will be much simpler. | |
403 | * @return array: (string => string) key: field_name value: api entity name | |
404 | * Note: options are fetched via ajax using the api "getoptions" method | |
405 | */ | |
406 | static function fieldOptions() { | |
407 | // Hack to add options not retrieved by getfields | |
408 | // This list could go on and on, but it would be better to fix getfields | |
409 | $options = array( | |
410 | 'group' => 'contact', | |
411 | 'tag' => 'contact', | |
412 | 'country' => 'contact', | |
413 | 'state_province' => 'contact', | |
414 | 'gender' => 'contact', | |
415 | 'world_region' => 'contact', | |
416 | 'individual_prefix' => 'contact', | |
417 | 'individual_suffix' => 'contact', | |
418 | 'preferred_communication_method' => 'contact', | |
419 | 'preferred_language' => 'contact', | |
420 | 'on_hold' => 'yesno', | |
421 | 'is_bulkmail' => 'yesno', | |
422 | 'activity_type' => 'activity', | |
423 | 'activity_status' => 'activity', | |
424 | 'financial_type' => 'contribution', | |
425 | 'contribution_page_id' => 'contribution', | |
426 | 'contribution_status' => 'contribution', | |
427 | 'payment_instrument' => 'contribution', | |
428 | 'membership_status' => 'membership', | |
429 | 'membership_type' => 'membership', | |
430 | ); | |
431 | $entities = array('contact', 'activity', 'participant', 'pledge', 'member', 'contribution'); | |
432 | foreach ($entities as $entity) { | |
433 | $fields = civicrm_api($entity, 'getfields', array('version' => 3)); | |
434 | foreach ($fields['values'] as $field => $info) { | |
435 | if (!empty($info['options']) || !empty($info['pseudoconstant']) || !empty($info['option_group_id'])) { | |
436 | $options[$field] = $entity; | |
437 | if (substr($field, -3) == '_id') { | |
438 | $options[substr($field, 0, -3)] = $entity; | |
439 | } | |
440 | } | |
441 | elseif (in_array(substr($field, 0, 3), array('is_', 'do_')) || CRM_Utils_Array::value('data_type', $info) == 'Boolean') { | |
442 | $options[$field] = 'yesno'; | |
443 | if ($entity != 'contact') { | |
444 | $options[$entity . '_' . $field] = 'yesno'; | |
445 | } | |
446 | } | |
447 | elseif (strpos($field, '_is_')) { | |
448 | $options[$field] = 'yesno'; | |
449 | } | |
450 | } | |
451 | } | |
452 | return $options; | |
453 | } | |
454 | ||
455 | /** | |
456 | * CRM-10338 | |
457 | * tags and groups use array keys for selection list. | |
458 | * if using IS NULL/NOT NULL, an array with no array key is created | |
459 | * convert that to simple null so processing can proceed | |
460 | */ | |
461 | static function checkArrayKeyEmpty($val) { | |
462 | if (is_array($val)) { | |
463 | $v2empty = true; | |
464 | foreach ($val as $vk => $vv) { | |
465 | if (!empty($vk)) { | |
466 | $v2empty = false; | |
467 | } | |
468 | } | |
469 | if ($v2empty) { | |
470 | $val = null; | |
471 | } | |
472 | } | |
473 | return $val; | |
474 | } | |
475 | } | |
476 |