2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
use ArrayIterator;
|
|
|
|
use RuntimeException;
|
2015-08-16 22:24:49 +02:00
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
class Directory extends Entry implements \ArrayAccess, \IteratorAggregate
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 23:13:02 +02:00
|
|
|
/** @var Entry[] */
|
2015-07-18 14:37:18 +02:00
|
|
|
protected $children = [];
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
/** @var Content */
|
|
|
|
protected $first_page;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
public function sort()
|
|
|
|
{
|
2015-10-28 00:01:41 +01:00
|
|
|
// Separate the values into buckets to sort them separately
|
|
|
|
$buckets = [
|
2017-11-08 21:49:30 +01:00
|
|
|
'up_numeric' => [],
|
|
|
|
'up' => [],
|
2015-10-28 00:18:29 +01:00
|
|
|
'index' => [],
|
2015-10-28 00:01:41 +01:00
|
|
|
'numeric' => [],
|
|
|
|
'normal' => [],
|
|
|
|
'down_numeric' => [],
|
|
|
|
'down' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($this->children as $key => $entry) {
|
|
|
|
$name = $entry->getName();
|
|
|
|
|
2015-10-28 00:18:29 +01:00
|
|
|
if ($name == 'index' || $name == '_index') {
|
|
|
|
$buckets['index'][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-21 09:11:53 +01:00
|
|
|
if (!$name) continue;
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($name[0] == '-') {
|
2015-10-28 00:01:41 +01:00
|
|
|
if (is_numeric($name[1])) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$exploded = explode('_', $name);
|
2015-10-28 00:01:41 +01:00
|
|
|
$buckets['down_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$buckets['down'][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:49:30 +01:00
|
|
|
if ($name[0] == '+') {
|
|
|
|
if (is_numeric($name[1])) {
|
|
|
|
$exploded = explode('_', $name);
|
|
|
|
$buckets['up_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$buckets['up'][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:01:41 +01:00
|
|
|
if (is_numeric($name[0])) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$exploded = explode('_', $name);
|
2015-10-28 00:01:41 +01:00
|
|
|
$buckets['numeric'][abs($exploded[0])][$key] = $entry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$buckets['normal'][$key] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
$final = [];
|
|
|
|
foreach ($buckets as $name => $bucket) {
|
2017-11-08 21:49:30 +01:00
|
|
|
if (substr($name, -7) == 'numeric') {
|
2015-10-28 00:01:41 +01:00
|
|
|
ksort($bucket);
|
|
|
|
foreach ($bucket as $sub_bucket) {
|
|
|
|
$final = $this->sortBucket($sub_bucket, $final);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$final = $this->sortBucket($bucket, $final);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->children = $final;
|
|
|
|
}
|
|
|
|
|
2015-11-06 22:44:34 +01:00
|
|
|
private function sortBucket($bucket, $final)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
uasort($bucket, function (Entry $a, Entry $b) {
|
2015-10-28 00:01:41 +01:00
|
|
|
return strcasecmp($a->getName(), $b->getName());
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($bucket as $key => $value) {
|
|
|
|
$final[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $final;
|
2015-07-18 14:37:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/**
|
|
|
|
* @return Entry[]
|
|
|
|
*/
|
2015-07-18 14:37:18 +02:00
|
|
|
public function getEntries()
|
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addChild(Entry $entry)
|
|
|
|
{
|
|
|
|
$this->children[$entry->getUri()] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeChild(Entry $entry)
|
|
|
|
{
|
|
|
|
unset($this->children[$entry->getUri()]);
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
/**
|
|
|
|
* @return \Todaymade\Daux\Config
|
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
if (!$this->parent) {
|
2016-07-27 21:32:51 +02:00
|
|
|
throw new \RuntimeException('Could not retrieve configuration. Are you sure that your tree has a Root ?');
|
2015-07-18 23:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parent->getConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Content|null
|
|
|
|
*/
|
|
|
|
public function getIndexPage()
|
|
|
|
{
|
|
|
|
$index_key = $this->getConfig()['index_key'];
|
|
|
|
|
|
|
|
if (isset($this->children[$index_key])) {
|
|
|
|
return $this->children[$index_key];
|
|
|
|
}
|
|
|
|
|
2016-08-02 23:39:57 +02:00
|
|
|
if ($this->getConfig()->shouldInheritIndex() && $first_page = $this->seekFirstPage()) {
|
2015-08-16 22:24:49 +02:00
|
|
|
return $first_page;
|
2015-07-21 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-07-21 20:34:21 +02:00
|
|
|
/**
|
|
|
|
* Seek the first available page from descendants
|
|
|
|
* @return Content|null
|
|
|
|
*/
|
2015-08-16 22:26:00 +02:00
|
|
|
public function seekFirstPage()
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($this instanceof self) {
|
2015-08-16 22:26:00 +02:00
|
|
|
$index_key = $this->getConfig()['index_key'];
|
|
|
|
if (isset($this->children[$index_key])) {
|
|
|
|
return $this->children[$index_key];
|
|
|
|
}
|
|
|
|
foreach ($this->children as $node_key => $node) {
|
|
|
|
if ($node instanceof Content) {
|
|
|
|
return $node;
|
|
|
|
}
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($node instanceof self
|
2015-08-16 22:26:00 +02:00
|
|
|
&& strpos($node->getUri(), '.') !== 0
|
2016-07-27 21:32:51 +02:00
|
|
|
&& $childNode = $node->seekFirstPage()) {
|
2015-08-16 22:26:00 +02:00
|
|
|
return $childNode;
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 20:34:21 +02:00
|
|
|
}
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-08-16 22:26:00 +02:00
|
|
|
return null;
|
2015-07-21 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
/**
|
2015-07-20 15:59:52 +02:00
|
|
|
* @return Content|null
|
2015-07-18 23:13:02 +02:00
|
|
|
*/
|
|
|
|
public function getFirstPage()
|
|
|
|
{
|
|
|
|
if ($this->first_page) {
|
|
|
|
return $this->first_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we try to find a real page
|
|
|
|
foreach ($this->getEntries() as $node) {
|
|
|
|
if ($node instanceof Content) {
|
2015-07-19 01:16:04 +02:00
|
|
|
if ($this instanceof Root && $this->getIndexPage() == $node) {
|
|
|
|
// The homepage should not count as first page
|
2015-07-18 23:13:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setFirstPage($node);
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't find one we check in the sub-directories
|
|
|
|
foreach ($this->getEntries() as $node) {
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($node instanceof self && $page = $node->getFirstPage()) {
|
2015-07-18 23:13:02 +02:00
|
|
|
$this->setFirstPage($page);
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
return null;
|
2015-07-18 23:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Content $first_page
|
|
|
|
*/
|
|
|
|
public function setFirstPage($first_page)
|
|
|
|
{
|
|
|
|
$this->first_page = $first_page;
|
|
|
|
}
|
|
|
|
|
2015-07-29 08:30:41 +02:00
|
|
|
/**
|
|
|
|
* Used when creating the navigation.
|
|
|
|
* Hides folders without showable content
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasContent()
|
|
|
|
{
|
|
|
|
foreach ($this->getEntries() as $node) {
|
|
|
|
if ($node instanceof Content) {
|
|
|
|
return true;
|
2016-07-27 21:32:51 +02:00
|
|
|
} elseif ($node instanceof self) {
|
2015-07-29 08:30:41 +02:00
|
|
|
if ($node->hasContent()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
public function dump()
|
|
|
|
{
|
|
|
|
$dump = parent::dump();
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
$dump['index'] = $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '';
|
|
|
|
$dump['first'] = $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '';
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
foreach ($this->getEntries() as $entry) {
|
|
|
|
$dump['children'][] = $entry->dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dump;
|
|
|
|
}
|
2016-07-04 20:33:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether a offset exists
|
|
|
|
* @param mixed $offset An offset to check for.
|
2016-07-27 21:32:51 +02:00
|
|
|
* @return bool true on success or false on failure.
|
2016-07-04 20:33:44 +02:00
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return array_key_exists($offset, $this->children);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Offset to retrieve
|
|
|
|
* @param mixed $offset The offset to retrieve.
|
|
|
|
* @return Entry Can return all value types.
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return $this->children[$offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Offset to set
|
|
|
|
* @param mixed $offset The offset to assign the value to.
|
|
|
|
* @param Entry $value The value to set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
if (!$value instanceof Entry) {
|
2016-07-27 21:32:51 +02:00
|
|
|
throw new RuntimeException('The value is not of type Entry');
|
2016-07-04 20:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->addChild($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Offset to unset
|
|
|
|
* @param string $offset the offset to unset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
unset($this->children[$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this->children);
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|