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-17 19:25:50 +11:00
|
|
|
* @var array List of attribute names which are not considered "settings".
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2016-03-17 19:25:50 +11:00
|
|
|
protected $purgeable = ['parsedMarkup'];
|
2014-09-29 22:58:30 -07:00
|
|
|
|
|
|
|
/**
|
2016-03-17 19:25:50 +11:00
|
|
|
* 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.
|
2014-09-29 22:58:30 -07:00
|
|
|
*/
|
2016-03-17 19:25:50 +11:00
|
|
|
public static function initCacheItem(&$item)
|
2014-09-29 22:58:30 -07:00
|
|
|
{
|
2016-03-17 19:25:50 +11:00
|
|
|
$item['parsedMarkup'] = (new static($item))->parseMarkup();
|
2014-09-29 22:58:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-17 19:25:50 +11:00
|
|
|
* Returns a default value for parsedMarkup attribute.
|
|
|
|
* @return string
|
2014-09-29 22:58:30 -07:00
|
|
|
*/
|
2016-03-17 19:25:50 +11:00
|
|
|
public function getParsedMarkupAttribute()
|
2014-09-29 22:58:30 -07:00
|
|
|
{
|
2016-03-17 19:25:50 +11:00
|
|
|
if (array_key_exists('parsedMarkup', $this->attributes)) {
|
|
|
|
return $this->attributes['parsedMarkup'];
|
|
|
|
}
|
2014-09-29 22:58:30 -07:00
|
|
|
|
2016-03-17 19:25:50 +11:00
|
|
|
return $this->attributes['parsedMarkup'] = $this->parseMarkup();
|
2014-09-29 22:58:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-17 19:25:50 +11:00
|
|
|
* Parses the content markup according to the file type.
|
|
|
|
* @return string
|
2014-09-29 22:58:30 -07:00
|
|
|
*/
|
2016-03-17 19:25:50 +11:00
|
|
|
public function parseMarkup()
|
2014-09-29 22:58:30 -07:00
|
|
|
{
|
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
|
|
|
}
|