Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Widgets;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\MiniView\MiniView;
use Maslosoft\Staple\Helpers\SiteWalker;
use Maslosoft\Staple\Models\RequestItem;
use Maslosoft\Staple\Widgets\Vo\SubNavItem;
use Maslosoft\Staple\Widgets\Vo\SubNavSeparator;
/**
* SubNavRecursive
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class SubNavRecursive extends SubNav
{
public $baseUrl = '';
public $urlSuffix = '';
public $items = [];
/**
* How many level from top to skip
* @var int
*/
public $skipLevel = 0;
/**
* Root path
* @var string
*/
public $root = '';
/**
* Scan path
* @var string
*/
public $path = '';
public $options = [
'path' => '',
'items' => ''
];
/**
* View
* @var MiniView
*/
public $mv = null;
public function __construct($options = [])
{
if (!empty($options))
{
$this->options = array_merge($this->options, $options);
}
// Apply configuration
EmbeDi::fly()->apply($this->options, $this);
// Setup view
$this->mv = new MiniView($this);
}
/**
* Get view instance
* Used by items
* @return MiniView
*/
public function getView()
{
return $this->mv;
}
public function getItems()
{
$w = new SiteWalker($this->root);
$w->start($this->path)->scan();
$items = [];
if (!empty($this->items))
{
foreach ($this->items as $url => $title)
{
if ($title === '--')
{
$items[] = new SubNavSeparator();
}
else
{
$items[] = new SubNavItem($url, $title, $this);
}
}
}
$rootItem = $w->get();
if ($this->skipLevel > 0)
{
for ($i = -1; $i < $this->skipLevel; $i++)
{
$walkerItems = $rootItem->items;
if (!isset($walkerItems[0]))
{
continue;
}
$rootItem = $walkerItems[0];
}
}
else
{
$walkerItems = $rootItem->items;
}
$walkerItems = $this->convertItems($walkerItems);
$walkerItems = $this->sort($walkerItems);
$allItems = array_merge($items, $walkerItems);
return $allItems;
}
private function sort($items)
{
if (empty($items))
{
return $items;
}
$sorted = [];
$groups = [];
$groupId = 0;
$groups[$groupId] = [];
foreach ($items as $item)
{
if ($item instanceof SubNavSeparator)
{
$groupId++;
$groups[$groupId] = [];
continue;
}
$item->items = $this->sort($item->items);
$groups[$groupId][] = $item;
}
$lastId = count($groups) - 1;
foreach ($groups as $id => $group)
{
usort($group, [$this, 'sortGroup']);
// Do not add separator to the last group
$sep = [];
if ($id !== $lastId)
{
$sep = [
new SubNavSeparator()
];
}
$sorted = array_merge($sorted, $group, $sep);
}
return $sorted;
}
public function sortGroup($one, $two)
{
return strcmp($one->title, $two->title);
}
private function convertItems($walkerItems)
{
$items = [];
foreach ($walkerItems as $requestItem)
{
/* @var $requestItem RequestItem */
$item = new SubNavItem($this->baseUrl . $requestItem->url . $this->urlSuffix, $requestItem->title, $this);
$item->items = $this->convertItems($requestItem->items);
$items[] = $item;
}
return $items;
}
public function __toString()
{
return $this->mv->render('sub-nav-recursive/nav', ['mv' => $this->mv], true);
}
}
API documentation generated by ApiGen