From b3a274010e6b85ff680351834f647e56fae62064 Mon Sep 17 00:00:00 2001 From: Deltik Date: Fri, 9 Feb 2018 05:34:09 -0600 Subject: [PATCH] Refactored e_tree_model for Code Climate --- e107_handlers/admin_ui.php | 6 +++--- e107_handlers/model_class.php | 30 +++++++----------------------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index d8e1305af..e21df6156 100755 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -6167,7 +6167,7 @@ class e_admin_ui extends e_admin_controller_ui $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) @@ -6188,10 +6188,10 @@ class e_admin_ui extends e_admin_controller_ui $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(), diff --git a/e107_handlers/model_class.php b/e107_handlers/model_class.php index f0ab6f502..ba46ad2ee 100755 --- a/e107_handlers/model_class.php +++ b/e107_handlers/model_class.php @@ -3344,12 +3344,12 @@ class e_tree_model extends e_front_model 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, @@ -3387,22 +3387,6 @@ class e_tree_model extends e_front_model 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 @@ -3488,7 +3472,7 @@ class e_tree_model extends e_front_model * @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