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