Skip to content

Commit

Permalink
Refactored e_tree_model for Code Climate
Browse files Browse the repository at this point in the history
  • Loading branch information
Deltik committed Feb 9, 2018
1 parent 2167a69 commit b3a2740
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
6 changes: 3 additions & 3 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -6167,7 +6167,7 @@ public function _setModel()
$this->_model = new e_admin_model();
$this->_model->setModelTable($this->table)
->setFieldIdName($this->pid)
->setUrl($this->url)
->setUrl($this->url)
->setValidationRules($this->validationRules)
->setDbTypes($this->fieldTypes)
->setFieldInputTypes($this->fieldInputTypes)
Expand All @@ -6188,10 +6188,10 @@ public function _setTreeModel()
$this->_tree_model = new e_admin_tree_model();
$this->_tree_model->setModelTable($this->table)
->setFieldIdName($this->pid)
->setUrl($this->url)
->setUrl($this->url)
->setMessageStackName('admin_ui_tree_'.$this->table)
->setParams(array('model_class' => 'e_admin_model',
'model_message_stack' => 'admin_ui_model_'.$this->table ,
'model_message_stack' => 'admin_ui_model_'.$this->table,
'db_query' => $this->listQry,
// Information necessary for PHP-based tree sort
'sort_parent' => $this->getSortParent(),
Expand Down
30 changes: 7 additions & 23 deletions e107_handlers/model_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3344,12 +3344,12 @@ public function load($force = false)

if($sql->gen($this->getParam('db_query'), $this->getParam('db_debug') ? true : false))
{
$rows = self::flatTreeFromArray($sql->rows(),
$this->getParam('primary_field'),
$this->getParam('sort_parent'),
$this->getParam('sort_field'),
$this->getParam('sort_order')
);
$rows_tree = self::arrayToTree($sql->rows,
$this->getParam('primary_field'),
$this->getParam('sort_parent'));
$rows = self::flattenTree($rows_tree,
$this->getParam('sort_field'),
$this->getParam('sort_order'));

// Simulated pagination
$rows = array_splice($rows,
Expand Down Expand Up @@ -3387,22 +3387,6 @@ public function load($force = false)
return $this;
}

/**
* Depth-first sort a relational array with a parent field and a sort order field
* @param array $rows Relational array with a parent field and a sort order field
* @param string $primary_field The field name of the primary key (matches children to parents)
* @param string $sort_parent The field name whose value is the parent ID
* @param mixed $sort_field The field name (string) or field names (array) whose value
* is or values are the sort order in the current tree node
* @param int $sort_order Desired sorting direction: 1 if ascending, -1 if descending
* @return array Input array sorted depth-first as if it were a tree
*/
private static function flatTreeFromArray($rows, $primary_field, $sort_parent, $sort_field, $sort_order)
{
$rows_tree = self::arrayToTree($rows, $primary_field, $sort_parent);
return self::flattenTree($rows_tree, $sort_field, $sort_order);
}

/**
* Converts a relational array with a parent field and a sort order field to a tree
* @param array $rows Relational array with a parent field and a sort order field
Expand Down Expand Up @@ -3488,7 +3472,7 @@ private static function flattenTree($tree, $sort_field = null, $sort_order = 1,
* @param array $row1 Associative array to compare to $row2
* @param array $row2 Associative array to compare to $row1
* @param mixed $sort_field Key (string) or keys (array) to compare
the values of in both $row1 and $row2
* the values of in both $row1 and $row2
* @param int $sort_order -1 to reverse the sorting order or 1 to keep the order as ascending
* @return int -1 if $row1 is less than $row2
* 0 if $row1 is equal to $row2
Expand Down

0 comments on commit b3a2740

Please sign in to comment.