2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
|
|
|
use Todaymade\Daux\DauxHelper;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
abstract class Entry
|
|
|
|
{
|
2015-07-17 23:38:06 +02:00
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $title;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $name;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var Content */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $index_page;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var Content */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $first_page;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $uri;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $local_path;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var integer */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $last_modified;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var array */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $parents;
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parents
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function __construct($path = '', $parents = array())
|
|
|
|
{
|
2015-07-17 23:38:06 +02:00
|
|
|
$this->setPath($path);
|
|
|
|
$this->setParents($parents);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getUri()
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
2015-07-16 11:08:16 +02:00
|
|
|
public function setUri($uri)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-16 11:08:16 +02:00
|
|
|
$this->uri = $uri;
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return Content
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getIndexPage()
|
|
|
|
{
|
|
|
|
return $this->index_page;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param Content $index_page
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function setIndexPage($index_page)
|
|
|
|
{
|
|
|
|
$this->index_page = $index_page;
|
|
|
|
}
|
|
|
|
|
2015-04-27 12:47:10 +02:00
|
|
|
/**
|
2015-07-18 01:14:44 +02:00
|
|
|
* @return Content|false
|
2015-04-27 12:47:10 +02:00
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getFirstPage()
|
|
|
|
{
|
|
|
|
if ($this->first_page) {
|
|
|
|
return $this->first_page;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 01:14:44 +02:00
|
|
|
if (!$this instanceof Directory) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
// First we try to find a real page
|
|
|
|
foreach ($this->getEntries() as $node) {
|
2015-07-18 01:14:44 +02:00
|
|
|
if ($node instanceof Content) {
|
|
|
|
if (!count($node->getParents()) && $node->title == 'index') {
|
|
|
|
//the homepage should not count as first page
|
|
|
|
continue;
|
2015-04-27 12:47:10 +02:00
|
|
|
}
|
2015-07-18 01:14:44 +02:00
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
$this->setFirstPage($node);
|
2015-07-18 01:14:44 +02:00
|
|
|
return $node;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2015-07-18 01:14:44 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
// If we can't find one we check in the sub-directories
|
|
|
|
foreach ($this->getEntries() as $node) {
|
2015-07-18 01:14:44 +02:00
|
|
|
if ($node instanceof Directory && $page = $node->getFirstPage()) {
|
2015-07-18 14:37:18 +02:00
|
|
|
$this->setFirstPage($page);
|
2015-07-18 01:14:44 +02:00
|
|
|
return $page;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 01:14:44 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param Content $first_page
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function setFirstPage($first_page)
|
|
|
|
{
|
|
|
|
$this->first_page = $first_page;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getParents()
|
|
|
|
{
|
|
|
|
return $this->parents;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param array $parents
|
|
|
|
*/
|
|
|
|
public function setParents($parents)
|
|
|
|
{
|
|
|
|
$this->parents = $parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->local_path;
|
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public function setPath($path)
|
|
|
|
{
|
2015-07-16 11:08:16 +02:00
|
|
|
if (!isset($path) || $path == '' || !file_exists($path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->local_path = $path;
|
|
|
|
$this->last_modified = filemtime($path);
|
|
|
|
$this->name = DauxHelper::pathinfo($path)['filename'];
|
|
|
|
$this->title = $this->getTitleInternal($this->name);
|
|
|
|
$this->uri = $this->getUrlInternal($this->getFilename($path));
|
|
|
|
$this->index_page = false;
|
2015-07-17 23:38:06 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-07-16 11:08:16 +02:00
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
$url = '';
|
|
|
|
foreach ($this->parents as $node) {
|
|
|
|
$url .= $node->uri . '/';
|
|
|
|
}
|
|
|
|
$url .= $this->uri;
|
|
|
|
return $url;
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
protected function getFilename($file)
|
|
|
|
{
|
|
|
|
$parts = explode('/', $file);
|
|
|
|
return end($parts);
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
protected function getTitleInternal($filename)
|
|
|
|
{
|
|
|
|
$filename = explode('_', $filename);
|
|
|
|
if ($filename[0] == '' || is_numeric($filename[0])) {
|
|
|
|
unset($filename[0]);
|
|
|
|
} else {
|
|
|
|
$t = $filename[0];
|
|
|
|
if ($t[0] == '-') {
|
|
|
|
$filename[0] = substr($t, 1);
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
$filename = implode(' ', $filename);
|
|
|
|
return $filename;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
protected function getUrlInternal($filename)
|
|
|
|
{
|
|
|
|
$filename = explode('_', $filename);
|
|
|
|
if ($filename[0] == '' || is_numeric($filename[0])) {
|
|
|
|
unset($filename[0]);
|
|
|
|
} else {
|
|
|
|
$t = $filename[0];
|
|
|
|
if ($t[0] == '-') {
|
|
|
|
$filename[0] = substr($t, 1);
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
$filename = implode('_', $filename);
|
|
|
|
return $filename;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|