Здравствуйте!
Есть класс:
<?php
namespace app\widgets\menu;
use ishop\App;
use ishop\Cache;
use RedUNIT\Base\Threeway;
class Menu{
protected $data;
protected $tree;
protected $menuHtml;
protected $tpl;
protected $container = 'li';
protected $class = 'menu';
protected $table = 'category';
protected $cache = 3600;
protected $cacheKey = 'ishop_menu';
protected $attrs = [];
protected $prepend = '';
public function __construct($options = []){
$this->tpl = __DIR__ . '/menu_tpl/menu.php';
$this->getOptions($options);
$this->run();
}
protected function getOptions($options){
foreach($options as $k => $v){
if(property_exists($this, $k)){
$this->$k = $v;
}
}
}
protected function run(){
$cache = Cache::instance();
$this->menuHtml = $cache->get($this->cacheKey);
if(!$this->menuHtml){
$this->data = App::$app->getProperty('cats');
if(!$this->data){
$this->data = $cats = \R::getAssoc("SELECT * FROM {$this->table}");
}
$this->tree = $this->getTree();
$this->menuHtml = $this->getMenuHtml($this->tree);
if($this->cache){
$cache->set($this->cacheKey, $this->menuHtml, $this->cache);
}
}
$this->output();
}
protected function output(){
$attrs = '';
if(!empty($this->attrs)){
foreach($this->attrs as $k => $v){
$attrs .= " $k='$v' ";
}
}
echo "<{$this->container} class='{$this->class}' $attrs>";
echo $this->prepend;
echo $this->menuHtml;
echo "</{$this->container}>";
}
protected function getTree(){
$tree = [];
$data = $this->data;
foreach ($data as $id=>&$node) {
if (!$node['parent_id']){
$tree[$id] = &$node;
}else{
$data[$node['parent_id']]['childs'][$id] = &$node;
}
}
return $tree;
}
protected function getMenuHtml($tree, $tab = ''){
$str = '';
foreach($tree as $id => $category){
$str .= $this->catToTemplate($category, $tab, $id);
}
return $str;
}
protected function catToTemplate($category, $tab, $id){
ob_start();
require $this->tpl;
return ob_get_clean();
}
}
menu.php :
<?php $parent = isset($category['childs']); ?>
<li>
<div class="link"><i class="fa fa-database"></i><?=$category['title'];?><i class="fa fa-chevron-down"></i></div>
<?php if(isset($category['childs'])): ?>
<ul class="submenu">
<?= $this->getMenuHtml($category['childs']);?>
</ul>
<?php endif; ?>
</li>
Как должна выглядеть структура меню:
<li>
<div class="link"><i class="fa fa-database"></i>Категории<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Авто</a></li>
<li><a href="#">Животные</a></li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-code"></i>Наборы<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Одежда</a></li>
</ul>
</li>
И что сейчас попадает в кэш:
<li>
<div class="link"><i class="fa fa-database"></i>Категории<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li>
<div class="link"><i class="fa fa-database"></i>Авто<i class="fa fa-chevron-down"></i></div>
</li>
<li>
<div class="link"><i class="fa fa-database"></i>Животные<i class="fa fa-chevron-down"></i></div>
</li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-database"></i>Наборы<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li>
<div class="link"><i class="fa fa-database"></i>Одежда<i class="fa fa-chevron-down"></i></div>
</li>
</ul>
</li>
Уважаемые знатоки, внимание вопрос : Как убрать из submenu все эти дивы и i, и заменить на ссылки?