winter/modules/cms/classes/Content.php

86 lines
2.2 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Cms\Classes;
use File;
2015-01-28 18:03:35 +11:00
use Markdown;
2014-05-14 23:24:20 +10:00
/**
* The CMS content file class.
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class Content extends CmsCompoundObject
2014-05-14 23:24:20 +10:00
{
/**
* @var string The container name associated with the model, eg: pages.
*/
protected $dirName = 'content';
2014-05-14 23:24:20 +10:00
/**
* @var array Allowable file extensions.
*/
protected $allowedExtensions = ['htm', 'txt', 'md'];
2014-05-14 23:24:20 +10:00
/**
* @var string Contains the parsed markup.
2014-05-14 23:24:20 +10:00
*/
public $parsedMarkup = null;
/**
* Loads the object from a file.
* @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
* @param string $fileName Specifies the file name, with the extension.
* The file name can contain only alphanumeric symbols, dashes and dots.
* @return boolean Returns true if the object was successfully loaded. Otherwise returns false.
*/
public static function load($theme, $fileName)
{
2014-10-11 01:22:03 +02:00
if ($obj = parent::load($theme, $fileName)) {
2014-10-09 17:14:29 +11:00
$obj->parsedMarkup = $obj->parseMarkup();
2014-10-11 01:22:03 +02:00
}
return $obj;
}
/**
* Initializes the object properties from the cached data.
* @param array $cached The cached data array.
*/
protected function initFromCache($cached)
{
parent::initFromCache($cached);
2014-10-09 17:14:29 +11:00
$this->parsedMarkup = array_key_exists('parsed-markup', $cached)
? $cached['parsed-markup']
: $this->parseMarkup($this->markup);
}
/**
* Initializes a cache item.
* @param array &$item The cached item array.
*/
protected function initCacheItem(&$item)
{
parent::initCacheItem($item);
$item['parsed-markup'] = $this->parsedMarkup;
}
protected function parseMarkup()
{
2015-08-05 00:00:43 +02:00
$extension = strtolower(File::extension($this->fileName));
2015-08-05 00:00:43 +02:00
switch ($extension) {
case 'txt':
$result = htmlspecialchars($this->markup);
break;
case 'md':
$result = Markdown::parse($this->markup);
break;
default:
$result = $this->markup;
2014-10-11 01:22:03 +02:00
}
return $result;
}
2014-05-14 23:24:20 +10:00
}