| Conditions | 68 |
| Paths | > 20000 |
| Total Lines | 275 |
| Code Lines | 136 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 4692 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 274 | protected function _performSearch($query, $modules, $offset = -1, $limit = 20) |
||
| 275 | { |
||
| 276 | if(empty($query)) return array(); |
||
| 277 | $primary_module=''; |
||
| 278 | $results = array(); |
||
| 279 | require_once 'include/SearchForm/SearchForm2.php' ; |
||
| 280 | $where = ''; |
||
| 281 | $searchEmail = preg_match('/^([^%]|%)*@([^%]|%)*$/', $query); |
||
| 282 | |||
| 283 | // bug49650 - strip out asterisks from query in case |
||
| 284 | // user thinks asterisk is a wildcard value |
||
| 285 | $query = str_replace( '*' , '' , $query ); |
||
| 286 | |||
| 287 | $limit = !empty($GLOBALS['sugar_config']['max_spotresults_initial']) ? $GLOBALS['sugar_config']['max_spotresults_initial'] : 5; |
||
| 288 | if($offset !== -1){ |
||
| 289 | $limit = !empty($GLOBALS['sugar_config']['max_spotresults_more']) ? $GLOBALS['sugar_config']['max_spotresults_more'] : 20; |
||
| 290 | } |
||
| 291 | $totalCounted = empty($GLOBALS['sugar_config']['disable_count_query']); |
||
| 292 | |||
| 293 | |||
| 294 | foreach($modules as $moduleName) |
||
| 295 | { |
||
| 296 | if (empty($primary_module)) |
||
| 297 | { |
||
| 298 | $primary_module=$moduleName; |
||
| 299 | } |
||
| 300 | |||
| 301 | $searchFields = SugarSpot::getSearchFields($moduleName); |
||
| 302 | |||
| 303 | if (empty($searchFields[$moduleName])) |
||
| 304 | { |
||
| 305 | continue; |
||
| 306 | } |
||
| 307 | |||
| 308 | $class = $GLOBALS['beanList'][$moduleName]; |
||
| 309 | $return_fields = array(); |
||
| 310 | $seed = new $class(); |
||
| 311 | if(!$seed->ACLAccess('ListView')) continue; |
||
| 312 | |||
| 313 | if ($class == 'aCase') |
||
| 314 | { |
||
| 315 | $class = 'Case'; |
||
| 316 | } |
||
| 317 | |||
| 318 | foreach($searchFields[$moduleName] as $k=>$v) |
||
| 319 | { |
||
| 320 | $keep = false; |
||
| 321 | $searchFields[$moduleName][$k]['value'] = $query; |
||
| 322 | if(!empty($searchFields[$moduleName][$k]['force_unifiedsearch'])) |
||
| 323 | { |
||
| 324 | continue; |
||
| 325 | } |
||
| 326 | |||
| 327 | if(!empty($GLOBALS['dictionary'][$class]['unified_search'])){ |
||
| 328 | |||
| 329 | if(empty($GLOBALS['dictionary'][$class]['fields'][$k]['unified_search'])){ |
||
| 330 | |||
| 331 | if(isset($searchFields[$moduleName][$k]['db_field'])) |
||
| 332 | { |
||
| 333 | foreach($searchFields[$moduleName][$k]['db_field'] as $field) |
||
| 334 | { |
||
| 335 | if(!empty($GLOBALS['dictionary'][$class]['fields'][$field]['unified_search'])) |
||
| 336 | { |
||
| 337 | if(isset($GLOBALS['dictionary'][$class]['fields'][$field]['type'])) |
||
| 338 | { |
||
| 339 | if(!$this->filterSearchType($GLOBALS['dictionary'][$class]['fields'][$field]['type'], $query)) |
||
| 340 | { |
||
| 341 | unset($searchFields[$moduleName][$k]); |
||
| 342 | continue; |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $keep = true; |
||
| 347 | } |
||
| 348 | } //foreach |
||
| 349 | } |
||
| 350 | # Bug 42961 Spot search for custom fields |
||
| 351 | if (!$keep && (isset($v['force_unifiedsearch']) == false || $v['force_unifiedsearch'] != true)) |
||
|
|
|||
| 352 | { |
||
| 353 | if(strpos($k,'email') === false || !$searchEmail) { |
||
| 354 | unset($searchFields[$moduleName][$k]); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | }else{ |
||
| 358 | if($GLOBALS['dictionary'][$class]['fields'][$k]['type'] == 'int' && !is_numeric($query)) { |
||
| 359 | unset($searchFields[$moduleName][$k]); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | }else if(empty($GLOBALS['dictionary'][$class]['fields'][$k]) ){ |
||
| 363 | //If module did not have unified_search defined, then check the exception for an email search before we unset |
||
| 364 | if(strpos($k,'email') === false || !$searchEmail) |
||
| 365 | { |
||
| 366 | unset($searchFields[$moduleName][$k]); |
||
| 367 | } |
||
| 368 | }else if(!$this->filterSearchType($GLOBALS['dictionary'][$class]['fields'][$k]['type'], $query)){ |
||
| 369 | unset($searchFields[$moduleName][$k]); |
||
| 370 | } |
||
| 371 | } //foreach |
||
| 372 | |||
| 373 | //If no search field criteria matched then continue to next module |
||
| 374 | if (empty($searchFields[$moduleName])) |
||
| 375 | { |
||
| 376 | continue; |
||
| 377 | } |
||
| 378 | |||
| 379 | if (empty($searchFields[$moduleName])) continue; |
||
| 380 | |||
| 381 | if(isset($seed->field_defs['name'])) |
||
| 382 | { |
||
| 383 | $return_fields['name'] = $seed->field_defs['name']; |
||
| 384 | } |
||
| 385 | |||
| 386 | foreach($seed->field_defs as $k => $v) |
||
| 387 | { |
||
| 388 | if(isset($seed->field_defs[$k]['type']) && ($seed->field_defs[$k]['type'] == 'name') && !isset($return_fields[$k])) |
||
| 389 | { |
||
| 390 | $return_fields[$k] = $seed->field_defs[$k]; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | if(!isset($return_fields['name'])) |
||
| 395 | { |
||
| 396 | // if we couldn't find any name fields, try search fields that have name in it |
||
| 397 | foreach($searchFields[$moduleName] as $k => $v) |
||
| 398 | { |
||
| 399 | if(strpos($k, 'name') != -1 && isset($seed->field_defs[$k]) && !isset($seed->field_defs[$k]['source'])) |
||
| 400 | { |
||
| 401 | $return_fields[$k] = $seed->field_defs[$k]; |
||
| 402 | break; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | if(!isset($return_fields['name'])) |
||
| 408 | { |
||
| 409 | // last resort - any fields that have 'name' in their name |
||
| 410 | foreach($seed->field_defs as $k => $v) |
||
| 411 | { |
||
| 412 | if(strpos($k, 'name') != -1 && isset($seed->field_defs[$k]) |
||
| 413 | && !isset($seed->field_defs[$k]['source'])) { |
||
| 414 | $return_fields[$k] = $seed->field_defs[$k]; |
||
| 415 | break; |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | if(!isset($return_fields['name'])) |
||
| 421 | { |
||
| 422 | // FAIL: couldn't find id & name for the module |
||
| 423 | $GLOBALS['log']->error("Unable to find name for module $moduleName"); |
||
| 424 | continue; |
||
| 425 | } |
||
| 426 | |||
| 427 | if(isset($return_fields['name']['fields'])) |
||
| 428 | { |
||
| 429 | // some names are composite |
||
| 430 | foreach($return_fields['name']['fields'] as $field) |
||
| 431 | { |
||
| 432 | $return_fields[$field] = $seed->field_defs[$field]; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | |||
| 437 | $searchForm = new SearchForm ( $seed, $moduleName ) ; |
||
| 438 | $searchForm->setup (array ( $moduleName => array() ) , $searchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ; |
||
| 439 | $where_clauses = $searchForm->generateSearchWhere() ; |
||
| 440 | |||
| 441 | if(empty($where_clauses)) |
||
| 442 | { |
||
| 443 | continue; |
||
| 444 | } |
||
| 445 | if(count($where_clauses) > 1) |
||
| 446 | { |
||
| 447 | $query_parts = array(); |
||
| 448 | |||
| 449 | $ret_array_start = $seed->create_new_list_query('', '', $return_fields, array(), 0, '', true, $seed, true); |
||
| 450 | $search_keys = array_keys($searchFields[$moduleName]); |
||
| 451 | |||
| 452 | foreach($where_clauses as $n => $clause) |
||
| 453 | { |
||
| 454 | $allfields = $return_fields; |
||
| 455 | $skey = $search_keys[$n]; |
||
| 456 | if(isset($seed->field_defs[$skey])) |
||
| 457 | { |
||
| 458 | // Joins for foreign fields aren't produced unless the field is in result, hence the merge |
||
| 459 | $allfields[$skey] = $seed->field_defs[$skey]; |
||
| 460 | } |
||
| 461 | $ret_array = $seed->create_new_list_query('', $clause, $allfields, array(), 0, '', true, $seed, true); |
||
| 462 | $query_parts[] = $ret_array_start['select'] . $ret_array['from'] . $ret_array['where'] . $ret_array['order_by']; |
||
| 463 | } |
||
| 464 | $main_query = "(".join(") UNION (", $query_parts).")"; |
||
| 465 | } |
||
| 466 | else |
||
| 467 | { |
||
| 468 | foreach($searchFields[$moduleName] as $k=>$v) |
||
| 469 | { |
||
| 470 | if(isset($seed->field_defs[$k])) |
||
| 471 | { |
||
| 472 | $return_fields[$k] = $seed->field_defs[$k]; |
||
| 473 | } |
||
| 474 | } |
||
| 475 | $ret_array = $seed->create_new_list_query('', $where_clauses[0], $return_fields, array(), 0, '', true, $seed, true); |
||
| 476 | $main_query = $ret_array['select'] . $ret_array['from'] . $ret_array['where'] . $ret_array['order_by']; |
||
| 477 | } |
||
| 478 | |||
| 479 | $totalCount = null; |
||
| 480 | if($limit < -1) |
||
| 481 | { |
||
| 482 | $result = $seed->db->query($main_query); |
||
| 483 | } |
||
| 484 | else |
||
| 485 | { |
||
| 486 | if($limit == -1) |
||
| 487 | { |
||
| 488 | $limit = $GLOBALS['sugar_config']['list_max_entries_per_page']; |
||
| 489 | } |
||
| 490 | |||
| 491 | if($offset == 'end') |
||
| 492 | { |
||
| 493 | $totalCount = $this->_getCount($seed, $main_query); |
||
| 494 | if($totalCount) |
||
| 495 | { |
||
| 496 | $offset = (floor(($totalCount -1) / $limit)) * $limit; |
||
| 497 | } else |
||
| 498 | { |
||
| 499 | $offset = 0; |
||
| 500 | } |
||
| 501 | } |
||
| 502 | $result = $seed->db->limitQuery($main_query, $offset, $limit + 1); |
||
| 503 | } |
||
| 504 | |||
| 505 | $data = array(); |
||
| 506 | $count = 0; |
||
| 507 | while($count < $limit && ($row = $seed->db->fetchByAssoc($result))) |
||
| 508 | { |
||
| 509 | $temp = clone $seed; |
||
| 510 | $temp->setupCustomFields($temp->module_dir); |
||
| 511 | $temp->loadFromRow($row); |
||
| 512 | $data[] = $temp->get_list_view_data($return_fields); |
||
| 513 | $count++; |
||
| 514 | } |
||
| 515 | |||
| 516 | $nextOffset = -1; |
||
| 517 | $prevOffset = -1; |
||
| 518 | $endOffset = -1; |
||
| 519 | |||
| 520 | if($count >= $limit) |
||
| 521 | { |
||
| 522 | $nextOffset = $offset + $limit; |
||
| 523 | } |
||
| 524 | |||
| 525 | if($offset > 0) |
||
| 526 | { |
||
| 527 | $prevOffset = $offset - $limit; |
||
| 528 | if($prevOffset < 0) $prevOffset = 0; |
||
| 529 | } |
||
| 530 | |||
| 531 | if( $count >= $limit && $totalCounted) |
||
| 532 | { |
||
| 533 | if(!isset($totalCount)) |
||
| 534 | { |
||
| 535 | $totalCount = $this->_getCount($seed, $main_query); |
||
| 536 | } |
||
| 537 | } else |
||
| 538 | { |
||
| 539 | $totalCount = $count + $offset; |
||
| 540 | } |
||
| 541 | |||
| 542 | $pageData['offsets'] = array( 'current'=>$offset, 'next'=>$nextOffset, 'prev'=>$prevOffset, 'end'=>$endOffset, 'total'=>$totalCount, 'totalCounted'=>$totalCounted); |
||
| 543 | $pageData['bean'] = array('objectName' => $seed->object_name, 'moduleDir' => $seed->module_dir); |
||
| 544 | |||
| 545 | $results[$moduleName] = array("data" => $data, "pageData" => $pageData); |
||
| 546 | } |
||
| 547 | return $results; |
||
| 548 | } |
||
| 549 | |||
| 620 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.