daux.io/libs/Tree/Entry.php

182 lignes
4.0 KiB
PHP
Brut Vue normale Historique

<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\DauxHelper;
2015-04-23 00:32:30 +02:00
abstract class Entry
{
protected $title;
protected $name;
protected $index_page;
protected $first_page;
protected $uri;
protected $local_path;
protected $last_modified;
protected $parents;
public function __construct($path = '', $parents = array())
{
2015-07-16 11:08:16 +02:00
$this->setPath($path);
$this->setParents($parents);
2015-04-23 00:32:30 +02:00
}
2015-07-16 11:08:16 +02:00
2015-04-23 00:32:30 +02:00
public function getName()
{
return $this->name;
}
2015-07-16 11:08:16 +02:00
public function setName($name)
{
$this->name = $name;
}
2015-04-23 00:32:30 +02:00
public function getUri()
{
return $this->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
}
public function getIndexPage()
{
return $this->index_page;
}
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
/**
* @return Entry
*/
2015-04-23 00:32:30 +02:00
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
if ($this instanceof Directory) {
foreach ($this->value as $node) {
2015-04-27 16:47:14 +02:00
if ($node instanceof Content) {
if (!count($node->getParents()) && $node->title == 'index') {
//the homepage should not count as first page
continue;
}
$this->first_page = $node;
return $node;
}
}
foreach ($this->value as $node) {
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
return $page;
}
}
}
2015-04-23 00:32:30 +02:00
return false;
}
2015-04-23 00:32:30 +02:00
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
2015-04-23 00:32:30 +02:00
public function getTitle()
{
return $this->title;
}
2015-07-16 11:08:16 +02:00
public function setTitle($title)
{
$this->title = $title;
}
2015-04-23 00:32:30 +02:00
public function getParents()
{
return $this->parents;
}
2015-07-16 11:08:16 +02:00
public function setParents($parents)
{
$this->parents = $parents;
}
2015-04-23 00:32:30 +02:00
public function getPath()
{
return $this->local_path;
}
2015-07-16 11:08:16 +02:00
public function setPath($path)
{
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-04-23 00:32:30 +02:00
public function write($content)
{
if (!is_writable($this->local_path)) {
return false;
}
2015-04-23 00:32:30 +02:00
file_put_contents($this->local_path, $content);
return true;
}
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
protected function getFilename($file)
{
$parts = explode('/', $file);
return end($parts);
}
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-23 00:32:30 +02:00
$filename = implode(' ', $filename);
return $filename;
}
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-23 00:32:30 +02:00
$filename = implode('_', $filename);
return $filename;
}
2015-04-23 00:32:30 +02:00
}