From 90673d2ae03c46c5f89163a852b6eebfacf40932 Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 19 Apr 2017 16:35:10 -0700 Subject: [PATCH] MySQL Class: New method selectTree(); Support for custom navigation depth. Download-categories sitelink/nav function added. --- e107_core/templates/navigation_template.php | 4 +- e107_handlers/mysql_class.php | 121 ++++++++++++++++++++ e107_handlers/sitelinks_class.php | 2 +- e107_plugins/download/e_sitelink.php | 87 ++++++++++++++ 4 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 e107_plugins/download/e_sitelink.php diff --git a/e107_core/templates/navigation_template.php b/e107_core/templates/navigation_template.php index 087a6a99d..0d358bb5e 100644 --- a/e107_core/templates/navigation_template.php +++ b/e107_core/templates/navigation_template.php @@ -59,14 +59,14 @@ $NAVIGATION_TEMPLATE['main']['submenu_start'] = ' // Sub menu Link $NAVIGATION_TEMPLATE['main']['submenu_item'] = ' -
  • +
  • '; // Sub menu Link - active state $NAVIGATION_TEMPLATE['main']['submenu_item_active'] = ' - '; diff --git a/e107_handlers/mysql_class.php b/e107_handlers/mysql_class.php index 22dbb1770..8bc4e939d 100644 --- a/e107_handlers/mysql_class.php +++ b/e107_handlers/mysql_class.php @@ -2024,6 +2024,127 @@ class e_db_mysql return $this->retrieve($qry); } + + + /** + * Return a sorted list of parent/child tree with an optional ordering field. + * @param string $table Name of table (without the prefix) + * @param string $parent Name of the parent field + * @param string $pid Name of the primary id + * @param string $where (Optional ) where condition. + * @param string $order Name of the order field. + * @todo Add extra params to each procedure so we only need 2 of them site-wide. + * @return boolean | integer with the addition of _treesort and _depth fields in the results. + */ + public function selectTree($table, $parent, $pid, $where=null, $order=null) + { + + if(empty($table) || empty($parent) || empty($pid)) + { + $this->mySQLlastErrText = "missing variables in sql->categories()"; + return false; + } + + $sql = "DROP FUNCTION IF EXISTS `getDepth` ;"; + + $this->gen($sql); + + $sql = " + CREATE FUNCTION `getDepth` (project_id INT) RETURNS int + BEGIN + DECLARE depth INT; + SET depth=1; + + WHILE project_id > 0 DO + + SELECT IFNULL(".$parent.",-1) + INTO project_id + FROM ( SELECT ".$parent." FROM `#".$table."` WHERE ".$pid." = project_id) AS t; + + IF project_id > 0 THEN + SET depth = depth + 1; + END IF; + + END WHILE; + + RETURN depth; + + END + ; + "; + + + $this->gen($sql); + + $sql = "DROP FUNCTION IF EXISTS `getTreeSort`;"; + + $this->gen($sql); + + $sql = " + CREATE FUNCTION getTreeSort(incid INT) + RETURNS CHAR(255) + BEGIN + SET @parentstr = CONVERT(incid, CHAR); + SET @parent = -1; + label1: WHILE @parent != 0 DO + SET @parent = (SELECT ".$parent." FROM `#".$table."` WHERE ".$pid." =incid); + SET @order = (SELECT ".$order." FROM `#".$table."` WHERE ".$pid." =incid); + SET @parentstr = CONCAT(if(@parent = 0,'',@parent), LPAD(@order,4,0), @parentstr); + SET incid = @parent; + END WHILE label1; + + RETURN @parentstr; + END + ; + + "; + + + $this->gen($sql); + + $qry = "SELECT SQL_CALC_FOUND_ROWS *, getTreeSort(".$pid.") as _treesort, getDepth(".$pid.") as _depth FROM `#".$table."` "; + + if($where !== null) + { + $qry .= " WHERE ".$where; + } + + + if($order === null) + { + $qry .= " ORDER BY _treesort"; + } + else + { + $qry .= " ORDER BY ".$order; + } + + return $this->gen($qry); + + + } + + + + + + + + + + + + + + + + + + + + + + diff --git a/e107_handlers/sitelinks_class.php b/e107_handlers/sitelinks_class.php index 2da22c574..df552503b 100644 --- a/e107_handlers/sitelinks_class.php +++ b/e107_handlers/sitelinks_class.php @@ -1863,7 +1863,7 @@ class navigation_shortcodes extends e_shortcode function sc_link_depth($parm='') { - return $this->depth; + return isset($this->var['link_depth']) ? intval($this->var['link_depth']) : $this->depth; } diff --git a/e107_plugins/download/e_sitelink.php b/e107_plugins/download/e_sitelink.php new file mode 100644 index 000000000..d31edce48 --- /dev/null +++ b/e107_plugins/download/e_sitelink.php @@ -0,0 +1,87 @@ + "Download Categories", + 'function' => "myCategories" + ); + + + + return $links; + } + + + + function myCategories() + { + $sql = e107::getDb(); + $tp = e107::getParser(); + $sublinks = array(); + + // $sql->select("download_category","*","download_category_id != '' "); + + $where = "download_category_class IN (".USERCLASS_LIST.")"; + + $sql->selectTree('download_category','download_category_parent', 'download_category_id', $where, 'download_category_order'); + + while($row = $sql->fetch()) + { + + + if(empty($row['download_category_name'])) + { + continue; + } + + $sublinks[] = array( + 'link_name' => $tp->toHtml($row['download_category_name'],'','TITLE'), + 'link_url' => e107::url('download', 'category', $row), + 'link_description' => '', + 'link_button' => $row['download_category_icon'], + 'link_category' => '', + 'link_order' => '', + 'link_parent' => '', + 'link_open' => '', + 'link_class' => e_UC_PUBLIC, + 'link_depth' => $row['_depth'] + ); + } + + + e107::getDebug()->log($sublinks); + return $sublinks; + + } + +}