winter/modules/cms/classes/Content.php

74 lines
1.8 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 array List of attribute names which are not considered "settings".
2014-05-14 23:24:20 +10:00
*/
protected $purgeable = ['parsedMarkup'];
/**
* Initializes the object properties from the cached data. The extra data
* set here becomes available as attributes set on the model after fetch.
2016-03-30 18:17:18 +02:00
* @param array $item The cached data array.
*/
public static function initCacheItem(&$item)
{
$item['parsedMarkup'] = (new static($item))->parseMarkup();
}
/**
* Returns a default value for parsedMarkup attribute.
* @return string
*/
public function getParsedMarkupAttribute()
{
if (array_key_exists('parsedMarkup', $this->attributes)) {
return $this->attributes['parsedMarkup'];
}
return $this->attributes['parsedMarkup'] = $this->parseMarkup();
}
/**
* Parses the content markup according to the file type.
* @return string
*/
public 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
}