1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-08 23:56:58 +02:00

PR #3016: Refactored for Code Climate

Some code improvements to pass Code Climate checks that failed in e5a0935726
This commit is contained in:
Nick Liu
2018-01-28 14:42:49 -06:00
parent e5a0935726
commit 8102917903

View File

@@ -3310,7 +3310,6 @@ class e_tree_model extends e_front_model
return $this;
}
$class_name = $this->getParam('model_class', 'e_model');
// auto-load all
if(!$this->getParam('db_query') && $this->getModelTable())
{
@@ -3323,6 +3322,7 @@ class e_tree_model extends e_front_model
);
}
$class_name = $this->getParam('model_class', 'e_model');
if($this->getParam('db_query') && $class_name && class_exists($class_name))
{
$sql = e107::getDb($this->getParam('model_class', 'e_model'));
@@ -3330,7 +3330,6 @@ class e_tree_model extends e_front_model
if($sql->gen($this->getParam('db_query'), $this->getParam('db_debug') ? true : false))
{
$this->_total = is_integer($sql->total_results) ? $sql->total_results : false; //requires SQL_CALC_FOUND_ROWS in query - see db handler
$rows = self::flatTreeFromArray($sql->rows(),
$this->getParam('primary_field'),
$this->getParam('sort_parent'),
@@ -3345,20 +3344,9 @@ class e_tree_model extends e_front_model
}
$this->_onLoad($tmp)->setNode($tmp->get($this->getFieldIdName()), $tmp);
}
if(false === $this->_total && $this->getModelTable() && !$this->getParam('nocount'))
{
//SQL_CALC_FOUND_ROWS not found in the query, do one more query
// $this->_total = e107::getDb()->db_Count($this->getModelTable()); // fails with specific listQry
// Calculates correct total when using filters and search. //XXX Optimize.
$countQry = preg_replace('/(LIMIT ([\d,\s])*)$/', "", $this->getParam('db_query'));
$this->_total = e107::getDb()->gen($countQry);
}
unset($tmp);
$this->countResults($sql);
}
if($sql->getLastErrorNumber())
@@ -3410,7 +3398,9 @@ class e_tree_model extends e_front_model
array_shift($nodes);
foreach($rows as $key => $row)
{
if(intval($row[$sort_parent]) === intval($node[$primary_field]))
$rowParentID = (int) $row[$sort_parent];
$nodeID = (int) $node[$primary_field];
if($rowParentID === $nodeID)
{
$node['_children'][] = &$row;
unset($rows[$key]);
@@ -3455,6 +3445,31 @@ class e_tree_model extends e_front_model
return $flat;
}
/**
* Resiliently counts the results from the last SQL query in the given resource
*
* Sets the count in $this->_total
*
* @param resource $sql SQL resource that executed a query
* @return int Number of results from the latest query
*/
private function countResults($sql)
{
$this->_total = is_integer($sql->total_results) ? $sql->total_results : false; //requires SQL_CALC_FOUND_ROWS in query - see db handler
if(false === $this->_total && $this->getModelTable() && !$this->getParam('nocount'))
{
//SQL_CALC_FOUND_ROWS not found in the query, do one more query
// $this->_total = e107::getDb()->db_Count($this->getModelTable()); // fails with specific listQry
// Calculates correct total when using filters and search. //XXX Optimize.
$countQry = preg_replace('/(LIMIT ([\d,\s])*)$/', "", $this->getParam('db_query'));
$this->_total = e107::getDb()->gen($countQry);
}
return $this->_total;
}
/**
* Get single model instance from the collection
* @param integer $node_id