2014-05-14 13:24:20 +00:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
|
|
|
|
|
use Cms\Classes\Theme;
|
|
|
|
|
use System\Classes\ApplicationException;
|
|
|
|
|
use Cms\Classes\Layout;
|
|
|
|
|
use Lang;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The CMS page class.
|
|
|
|
|
*
|
|
|
|
|
* @package october\cms
|
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
|
*/
|
|
|
|
|
class Page extends CmsCompoundObject
|
|
|
|
|
{
|
|
|
|
|
protected $settingsValidationRules = [
|
|
|
|
|
'title' => 'required',
|
2014-08-21 09:46:32 +00:00
|
|
|
'url' => ['required', 'regex:/^\/[a-z0-9\/\:_\-\*\[\]\+\?\|\.]*$/i']
|
2014-05-14 13:24:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of the object and associates it with a CMS theme.
|
|
|
|
|
* @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
|
|
|
|
|
*/
|
2014-05-23 09:35:56 +00:00
|
|
|
public function __construct(Theme $theme = null)
|
2014-05-14 13:24:20 +00:00
|
|
|
{
|
|
|
|
|
parent::__construct($theme);
|
|
|
|
|
|
|
|
|
|
$this->settingsValidationMessages = [
|
|
|
|
|
'url.regex' => Lang::get('cms::lang.page.invalid_url')
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function parseSettings() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the directory name corresponding to the object type.
|
|
|
|
|
* For pages the directory name is "pages", for layouts - "layouts", etc.
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getObjectTypeDirName()
|
|
|
|
|
{
|
|
|
|
|
return 'pages';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns name of a PHP class to us a parent for the PHP class created for the object's PHP section.
|
|
|
|
|
* @return mixed Returns the class name or null.
|
|
|
|
|
*/
|
|
|
|
|
public function getCodeClassParent()
|
|
|
|
|
{
|
|
|
|
|
return '\Cms\Classes\PageCode';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of layouts available in the theme.
|
|
|
|
|
* This method is used by the form widget.
|
|
|
|
|
* @return array Returns an array of strings.
|
|
|
|
|
*/
|
|
|
|
|
public function getLayoutOptions()
|
|
|
|
|
{
|
|
|
|
|
if (!($theme = Theme::getEditTheme()))
|
|
|
|
|
throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found'));
|
|
|
|
|
|
|
|
|
|
$layouts = Layout::listInTheme($theme, true);
|
|
|
|
|
$result = [];
|
|
|
|
|
$result[null] = Lang::get('cms::lang.page.no_layout');
|
|
|
|
|
foreach ($layouts as $layout) {
|
|
|
|
|
$baseName = $layout->getBaseFileName();
|
|
|
|
|
$result[$baseName] = strlen($layout->name) ? $layout->name : $baseName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2014-07-11 08:50:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper that returns a nicer list of pages for use in dropdowns.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getNameList()
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
$pages = self::sortBy('baseFileName')->all();
|
|
|
|
|
foreach ($pages as $page) {
|
|
|
|
|
$result[$page->baseFileName] = $page->title.' ('.$page->baseFileName.')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper that makes a URL for a page in the active theme.
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-09-11 04:45:37 +00:00
|
|
|
public static function url($page, $params = [], $absolute = true)
|
2014-07-11 08:50:29 +00:00
|
|
|
{
|
2014-09-28 06:28:38 +00:00
|
|
|
/*
|
|
|
|
|
* Reuse existing controller or create a new one,
|
|
|
|
|
* assuming that the method is called not during the front-end
|
|
|
|
|
* request processing.
|
|
|
|
|
*/
|
|
|
|
|
$controller = Controller::getController();
|
|
|
|
|
if (!$controller)
|
|
|
|
|
$controller = new Controller;
|
|
|
|
|
|
2014-09-11 04:45:37 +00:00
|
|
|
return $controller->pageUrl($page, $params, true, $absolute);
|
2014-07-11 08:50:29 +00:00
|
|
|
}
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|