| Conditions | 89 |
| Paths | > 20000 |
| Total Lines | 287 |
| Code Lines | 178 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 110 |
| CRAP Score | 437.1248 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 247 | 4 | function getListViewData($seed, $where, $offset=-1, $limit = -1, $filter_fields=array(),$params=array(),$id_field = 'id',$singleSelect=true) { |
|
| 248 | 4 | global $current_user; |
|
| 249 | 4 | SugarVCR::erase($seed->module_dir); |
|
| 250 | 4 | $this->seed =& $seed; |
|
| 251 | 4 | $totalCounted = empty($GLOBALS['sugar_config']['disable_count_query']); |
|
| 252 | 4 | $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'] = $seed->module_dir; |
|
| 253 | 4 | if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup'){ |
|
| 254 | 4 | $_SESSION['MAILMERGE_MODULE'] = $seed->module_dir; |
|
| 255 | } |
||
| 256 | |||
| 257 | 4 | $this->setVariableName($seed->object_name, $where, $this->listviewName); |
|
| 258 | |||
| 259 | 4 | $this->seed->id = '[SELECT_ID_LIST]'; |
|
| 260 | |||
| 261 | // if $params tell us to override all ordering |
||
| 262 | 4 | if(!empty($params['overrideOrder']) && !empty($params['orderBy'])) { |
|
| 263 | $order = $this->getOrderBy(strtolower($params['orderBy']), (empty($params['sortOrder']) ? '' : $params['sortOrder'])); // retreive from $_REQUEST |
||
| 264 | } |
||
| 265 | else { |
||
| 266 | 4 | $order = $this->getOrderBy(); // retreive from $_REQUEST |
|
| 267 | } |
||
| 268 | |||
| 269 | // still empty? try to use settings passed in $param |
||
| 270 | 4 | if(empty($order['orderBy']) && !empty($params['orderBy'])) { |
|
| 271 | $order['orderBy'] = $params['orderBy']; |
||
| 272 | $order['sortOrder'] = (empty($params['sortOrder']) ? '' : $params['sortOrder']); |
||
| 273 | } |
||
| 274 | |||
| 275 | //rrs - bug: 21788. Do not use Order by stmts with fields that are not in the query. |
||
| 276 | // Bug 22740 - Tweak this check to strip off the table name off the order by parameter. |
||
| 277 | // Samir Gandhi : Do not remove the report_cache.date_modified condition as the report list view is broken |
||
| 278 | 4 | $orderby = $order['orderBy']; |
|
| 279 | 4 | if (strpos($order['orderBy'],'.') && ($order['orderBy'] != "report_cache.date_modified")) { |
|
| 280 | $orderby = substr($order['orderBy'],strpos($order['orderBy'],'.')+1); |
||
| 281 | } |
||
| 282 | 4 | if ($orderby != 'date_entered' && !in_array($orderby, array_keys($filter_fields))) { |
|
| 283 | $order['orderBy'] = ''; |
||
| 284 | $order['sortOrder'] = ''; |
||
| 285 | } |
||
| 286 | |||
| 287 | 4 | if (empty($order['orderBy'])) { |
|
| 288 | $orderBy = ''; |
||
| 289 | } else { |
||
| 290 | 4 | $orderBy = $order['orderBy'] . ' ' . $order['sortOrder']; |
|
| 291 | //wdong, Bug 25476, fix the sorting problem of Oracle. |
||
| 292 | 4 | if (isset($params['custom_order_by_override']['ori_code']) && $order['orderBy'] == $params['custom_order_by_override']['ori_code']) |
|
| 293 | $orderBy = $params['custom_order_by_override']['custom_code'] . ' ' . $order['sortOrder']; |
||
| 294 | } |
||
| 295 | |||
| 296 | 4 | if (empty($params['skipOrderSave'])) { // don't save preferences if told so |
|
| 297 | 4 | $current_user->setPreference('listviewOrder', $order, 0, $this->var_name); // save preference |
|
| 298 | } |
||
| 299 | |||
| 300 | // If $params tells us to override for the special last_name, first_name sorting |
||
| 301 | 4 | if (!empty($params['overrideLastNameOrder']) && $order['orderBy'] == 'last_name') { |
|
| 302 | $orderBy = 'last_name '.$order['sortOrder'].', first_name '.$order['sortOrder']; |
||
| 303 | } |
||
| 304 | |||
| 305 | 4 | $ret_array = $seed->create_new_list_query($orderBy, $where, $filter_fields, $params, 0, '', true, $seed, $singleSelect); |
|
| 306 | 4 | $ret_array['inner_join'] = ''; |
|
| 307 | 4 | if (!empty($this->seed->listview_inner_join)) { |
|
| 308 | 1 | $ret_array['inner_join'] = ' ' . implode(' ', $this->seed->listview_inner_join) . ' '; |
|
| 309 | } |
||
| 310 | |||
| 311 | 4 | if(!is_array($params)) $params = array(); |
|
| 312 | 4 | if(!isset($params['custom_select'])) $params['custom_select'] = ''; |
|
| 313 | 4 | if(!isset($params['custom_from'])) $params['custom_from'] = ''; |
|
| 314 | 4 | if(!isset($params['custom_where'])) $params['custom_where'] = ''; |
|
| 315 | 4 | if(!isset($params['custom_order_by'])) $params['custom_order_by'] = ''; |
|
| 316 | 4 | $main_query = $ret_array['select'] . $params['custom_select'] . $ret_array['from'] . $params['custom_from'] . $ret_array['inner_join']. $ret_array['where'] . $params['custom_where'] . $ret_array['order_by'] . $params['custom_order_by']; |
|
| 317 | //C.L. - Fix for 23461 |
||
| 318 | 4 | if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup') { |
|
| 319 | 4 | $_SESSION['export_where'] = $ret_array['where']; |
|
| 320 | } |
||
| 321 | 4 | if($limit < -1) { |
|
| 322 | $result = $this->db->query($main_query); |
||
| 323 | } |
||
| 324 | else { |
||
| 325 | 4 | if($limit == -1) { |
|
| 326 | 3 | $limit = $this->getLimit(); |
|
| 327 | } |
||
| 328 | 4 | $dyn_offset = $this->getOffset(); |
|
| 329 | 4 | if($dyn_offset > 0 || !is_int($dyn_offset))$offset = $dyn_offset; |
|
| 330 | |||
| 331 | 4 | if(strcmp($offset, 'end') == 0){ |
|
| 332 | $totalCount = $this->getTotalCount($main_query); |
||
| 333 | $offset = (floor(($totalCount -1) / $limit)) * $limit; |
||
| 334 | } |
||
| 335 | 4 | if($this->seed->ACLAccess('ListView')) { |
|
| 336 | 4 | $result = $this->db->limitQuery($main_query, $offset, $limit + 1); |
|
| 337 | } |
||
| 338 | else { |
||
| 339 | $result = array(); |
||
| 340 | } |
||
| 341 | |||
| 342 | } |
||
| 343 | |||
| 344 | 4 | $data = array(); |
|
| 345 | |||
| 346 | 4 | $temp = clone $seed; |
|
| 347 | |||
| 348 | 4 | $rows = array(); |
|
| 349 | 4 | $count = 0; |
|
| 350 | 4 | $idIndex = array(); |
|
| 351 | 4 | $id_list = ''; |
|
| 352 | |||
| 353 | 4 | while(($row = $this->db->fetchByAssoc($result)) != null) |
|
| 354 | { |
||
| 355 | 2 | if($count < $limit) |
|
| 356 | { |
||
| 357 | 2 | $id_list .= ',\''.$row[$id_field].'\''; |
|
| 358 | 2 | $idIndex[$row[$id_field]][] = count($rows); |
|
| 359 | 2 | $rows[] = $seed->convertRow($row); |
|
| 360 | } |
||
| 361 | 2 | $count++; |
|
| 362 | } |
||
| 363 | |||
| 364 | 4 | if (!empty($id_list)) |
|
| 365 | { |
||
| 366 | 2 | $id_list = '('.substr($id_list, 1).')'; |
|
| 367 | } |
||
| 368 | |||
| 369 | 4 | SugarVCR::store($this->seed->module_dir, $main_query); |
|
| 370 | 4 | if($count != 0) { |
|
| 371 | //NOW HANDLE SECONDARY QUERIES |
||
| 372 | 2 | if(!empty($ret_array['secondary_select'])) { |
|
| 373 | $secondary_query = $ret_array['secondary_select'] . $ret_array['secondary_from'] . ' WHERE '.$this->seed->table_name.'.id IN ' .$id_list; |
||
| 374 | if(isset($ret_array['order_by'])) |
||
| 375 | { |
||
| 376 | $secondary_query .= ' ' . $ret_array['order_by']; |
||
| 377 | } |
||
| 378 | |||
| 379 | $secondary_result = $this->db->query($secondary_query); |
||
| 380 | |||
| 381 | $ref_id_count = array(); |
||
| 382 | while($row = $this->db->fetchByAssoc($secondary_result)) { |
||
| 383 | |||
| 384 | $ref_id_count[$row['ref_id']][] = true; |
||
| 385 | foreach($row as $name=>$value) { |
||
| 386 | //add it to every row with the given id |
||
| 387 | foreach($idIndex[$row['ref_id']] as $index){ |
||
| 388 | $rows[$index][$name]=$value; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | $rows_keys = array_keys($rows); |
||
| 394 | foreach($rows_keys as $key) |
||
| 395 | { |
||
| 396 | $rows[$key]['secondary_select_count'] = count($ref_id_count[$rows[$key]['ref_id']]); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | // retrieve parent names |
||
| 401 | 2 | if(!empty($filter_fields['parent_name']) && !empty($filter_fields['parent_id']) && !empty($filter_fields['parent_type'])) { |
|
| 402 | foreach($idIndex as $id => $rowIndex) { |
||
| 403 | if(!isset($post_retrieve[$rows[$rowIndex[0]]['parent_type']])) { |
||
| 404 | $post_retrieve[$rows[$rowIndex[0]]['parent_type']] = array(); |
||
| 405 | } |
||
| 406 | if(!empty($rows[$rowIndex[0]]['parent_id'])) $post_retrieve[$rows[$rowIndex[0]]['parent_type']][] = array('child_id' => $id , 'parent_id'=> $rows[$rowIndex[0]]['parent_id'], 'parent_type' => $rows[$rowIndex[0]]['parent_type'], 'type' => 'parent'); |
||
| 407 | } |
||
| 408 | if(isset($post_retrieve)) { |
||
| 409 | $parent_fields = $seed->retrieve_parent_fields($post_retrieve); |
||
| 410 | foreach($parent_fields as $child_id => $parent_data) { |
||
| 411 | //add it to every row with the given id |
||
| 412 | foreach($idIndex[$child_id] as $index){ |
||
| 413 | $rows[$index]['parent_name']= $parent_data['parent_name']; |
||
| 414 | } |
||
| 415 | } |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | 2 | $pageData = array(); |
|
| 420 | |||
| 421 | 2 | reset($rows); |
|
| 422 | 2 | while($row = current($rows)){ |
|
| 423 | |||
| 424 | 2 | $temp = clone $seed; |
|
| 425 | 2 | $dataIndex = count($data); |
|
| 426 | |||
| 427 | 2 | $temp->setupCustomFields($temp->module_dir); |
|
| 428 | 2 | $temp->loadFromRow($row); |
|
| 429 | 2 | if (empty($this->seed->assigned_user_id) && !empty($temp->assigned_user_id)) { |
|
| 430 | $this->seed->assigned_user_id = $temp->assigned_user_id; |
||
| 431 | } |
||
| 432 | 2 | if($idIndex[$row[$id_field]][0] == $dataIndex){ |
|
| 433 | 2 | $pageData['tag'][$dataIndex] = $temp->listviewACLHelper(); |
|
| 434 | }else{ |
||
| 435 | $pageData['tag'][$dataIndex] = $pageData['tag'][$idIndex[$row[$id_field]][0]]; |
||
| 436 | } |
||
| 437 | 2 | $data[$dataIndex] = $temp->get_list_view_data($filter_fields); |
|
| 438 | 2 | $detailViewAccess = $temp->ACLAccess('DetailView'); |
|
| 439 | 2 | $editViewAccess = $temp->ACLAccess('EditView'); |
|
| 440 | 2 | $pageData['rowAccess'][$dataIndex] = array('view' => $detailViewAccess, 'edit' => $editViewAccess); |
|
| 441 | 2 | $additionalDetailsAllow = $this->additionalDetails && $detailViewAccess && (file_exists( |
|
| 442 | 2 | 'modules/' . $temp->module_dir . '/metadata/additionalDetails.php' |
|
| 443 | 2 | ) || file_exists('custom/modules/' . $temp->module_dir . '/metadata/additionalDetails.php')); |
|
| 444 | 2 | $additionalDetailsEdit = $editViewAccess; |
|
| 445 | 2 | if($additionalDetailsAllow) { |
|
| 446 | 2 | if($this->additionalDetailsAjax) { |
|
| 447 | 2 | $ar = $this->getAdditionalDetailsAjax($data[$dataIndex]['ID']); |
|
| 448 | } |
||
| 449 | else { |
||
| 450 | $additionalDetailsFile = 'modules/' . $this->seed->module_dir . '/metadata/additionalDetails.php'; |
||
| 451 | if(file_exists('custom/modules/' . $this->seed->module_dir . '/metadata/additionalDetails.php')){ |
||
| 452 | $additionalDetailsFile = 'custom/modules/' . $this->seed->module_dir . '/metadata/additionalDetails.php'; |
||
| 453 | } |
||
| 454 | require_once($additionalDetailsFile); |
||
| 455 | $ar = $this->getAdditionalDetails($data[$dataIndex], |
||
| 456 | (empty($this->additionalDetailsFunction) ? 'additionalDetails' : $this->additionalDetailsFunction) . $this->seed->object_name, |
||
| 457 | $additionalDetailsEdit); |
||
| 458 | } |
||
| 459 | 2 | $pageData['additionalDetails'][$dataIndex] = $ar['string']; |
|
| 460 | 2 | $pageData['additionalDetails']['fieldToAddTo'] = $ar['fieldToAddTo']; |
|
| 461 | } |
||
| 462 | 2 | next($rows); |
|
| 463 | } |
||
| 464 | } |
||
| 465 | 4 | $nextOffset = -1; |
|
| 466 | 4 | $prevOffset = -1; |
|
| 467 | 4 | $endOffset = -1; |
|
| 468 | 4 | if($count > $limit) { |
|
| 469 | $nextOffset = $offset + $limit; |
||
| 470 | } |
||
| 471 | |||
| 472 | 4 | if($offset > 0) { |
|
| 473 | $prevOffset = $offset - $limit; |
||
| 474 | if($prevOffset < 0)$prevOffset = 0; |
||
| 475 | } |
||
| 476 | 4 | $totalCount = $count + $offset; |
|
| 477 | |||
| 478 | 4 | if( $count >= $limit && $totalCounted){ |
|
| 479 | $totalCount = $this->getTotalCount($main_query); |
||
| 480 | } |
||
| 481 | 4 | SugarVCR::recordIDs($this->seed->module_dir, array_keys($idIndex), $offset, $totalCount); |
|
| 482 | $module_names = array( |
||
| 483 | 'Prospects' => 'Targets' |
||
| 484 | 4 | ); |
|
| 485 | 4 | $endOffset = (floor(($totalCount - 1) / $limit)) * $limit; |
|
| 486 | 4 | $pageData['ordering'] = $order; |
|
| 487 | 4 | $pageData['ordering']['sortOrder'] = $this->getReverseSortOrder($pageData['ordering']['sortOrder']); |
|
| 488 | //get url parameters as an array |
||
| 489 | 4 | $pageData['queries'] = $this->generateQueries($pageData['ordering']['sortOrder'], $offset, $prevOffset, $nextOffset, $endOffset, $totalCounted); |
|
| 490 | //join url parameters from array to a string |
||
| 491 | 4 | $pageData['urls'] = $this->generateURLS($pageData['queries']); |
|
| 492 | 4 | $pageData['offsets'] = array( 'current'=>$offset, 'next'=>$nextOffset, 'prev'=>$prevOffset, 'end'=>$endOffset, 'total'=>$totalCount, 'totalCounted'=>$totalCounted); |
|
| 493 | 4 | $pageData['bean'] = array('objectName' => $seed->object_name, 'moduleDir' => $seed->module_dir, 'moduleName' => strtr($seed->module_dir, $module_names)); |
|
| 494 | 4 | $pageData['stamp'] = $this->stamp; |
|
| 495 | 4 | $pageData['access'] = array('view' => $this->seed->ACLAccess('DetailView'), 'edit' => $this->seed->ACLAccess('EditView')); |
|
| 496 | 4 | $pageData['idIndex'] = $idIndex; |
|
| 497 | 4 | if(!$this->seed->ACLAccess('ListView')) { |
|
| 498 | $pageData['error'] = 'ACL restricted access'; |
||
| 499 | } |
||
| 500 | |||
| 501 | 4 | $queryString = ''; |
|
| 502 | |||
| 503 | 4 | if( isset($_REQUEST["searchFormTab"]) && $_REQUEST["searchFormTab"] == "advanced_search" || |
|
| 504 | 4 | isset($_REQUEST["type_basic"]) && (count($_REQUEST["type_basic"] > 1) || $_REQUEST["type_basic"][0] != "") || |
|
| 505 | 4 | isset($_REQUEST["module"]) && $_REQUEST["module"] == "MergeRecords") |
|
| 506 | { |
||
| 507 | $queryString = "-advanced_search"; |
||
| 508 | } |
||
| 509 | 4 | else if (isset($_REQUEST["searchFormTab"]) && $_REQUEST["searchFormTab"] == "basic_search") |
|
| 510 | { |
||
| 511 | if($seed->module_dir == "Reports") $searchMetaData = SearchFormReports::retrieveReportsSearchDefs(); |
||
| 512 | else $searchMetaData = SearchForm::retrieveSearchDefs($seed->module_dir); |
||
| 513 | |||
| 514 | $basicSearchFields = array(); |
||
| 515 | |||
| 516 | if( isset($searchMetaData['searchdefs']) && isset($searchMetaData['searchdefs'][$seed->module_dir]['layout']['basic_search']) ) |
||
| 517 | $basicSearchFields = $searchMetaData['searchdefs'][$seed->module_dir]['layout']['basic_search']; |
||
| 518 | |||
| 519 | foreach( $basicSearchFields as $basicSearchField) |
||
| 520 | { |
||
| 521 | $field_name = (is_array($basicSearchField) && isset($basicSearchField['name'])) ? $basicSearchField['name'] : $basicSearchField; |
||
| 522 | $field_name .= "_basic"; |
||
| 523 | if( isset($_REQUEST[$field_name]) && ( !is_array($basicSearchField) || !isset($basicSearchField['type']) || $basicSearchField['type'] == 'text' || $basicSearchField['type'] == 'name') ) |
||
| 524 | { |
||
| 525 | // Ensure the encoding is UTF-8 |
||
| 526 | $queryString = htmlentities($_REQUEST[$field_name], null, 'UTF-8'); |
||
| 527 | break; |
||
| 528 | } |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | 4 | return array('data'=>$data , 'pageData'=>$pageData, 'query' => $queryString); |
|
| 533 | } |
||
| 534 | |||
| 666 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.