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