2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
2014-09-29 22:58:30 -07:00
|
|
|
use File;
|
2015-01-28 18:03:35 +11:00
|
|
|
use Markdown;
|
2014-09-29 22:58:30 -07:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* The CMS content file class.
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2014-08-22 18:45:05 +11:00
|
|
|
class Content extends CmsCompoundObject
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2016-03-12 15:32:16 +11:00
|
|
|
/**
|
|
|
|
* @var string The container name associated with the model, eg: pages.
|
|
|
|
*/
|
|
|
|
protected $dirName = 'content';
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-09-29 22:58:30 -07:00
|
|
|
/**
|
2016-03-12 15:32:16 +11:00
|
|
|
* @var array Allowable file extensions.
|
2014-09-29 22:58:30 -07:00
|
|
|
*/
|
2016-03-12 15:32:16 +11:00
|
|
|
protected $allowedExtensions = ['htm', 'txt', 'md'];
|
2014-09-29 22:58:30 -07:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2016-03-12 15:32:16 +11:00
|
|
|
* @var string Contains the parsed markup.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2016-03-12 15:32:16 +11:00
|
|
|
public $parsedMarkup = null;
|
2014-09-29 22:58:30 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2014-09-29 22:58:30 -07: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);
|
2014-09-29 22:58:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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));
|
2014-09-29 22:58:30 -07:00
|
|
|
|
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
|
|
|
}
|
2014-09-29 22:58:30 -07:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|