2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
|
|
|
use Cms\Classes\Theme;
|
2015-01-28 18:03:35 +11:00
|
|
|
use ApplicationException;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Cms\Classes\Layout;
|
|
|
|
use Lang;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The CMS page class.
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class Page extends CmsCompoundObject
|
|
|
|
{
|
2014-10-16 20:47:23 -07:00
|
|
|
/**
|
|
|
|
* @var array The API bag allows the API handler code to bind arbitrary data to the page object.
|
|
|
|
*/
|
|
|
|
public $apiBag = [];
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
protected $settingsValidationRules = [
|
|
|
|
'title' => 'required',
|
2014-11-19 12:04:55 +04:00
|
|
|
'url' => ['required', 'regex:/^\/[a-z0-9\/\:_\-\*\[\]\+\?\|\.\^\$]*$/i']
|
2014-05-14 23:24:20 +10: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 19:35:56 +10:00
|
|
|
public function __construct(Theme $theme = null)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
parent::__construct($theme);
|
|
|
|
|
|
|
|
$this->settingsValidationMessages = [
|
|
|
|
'url.regex' => Lang::get('cms::lang.page.invalid_url')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-10-11 01:22:03 +02:00
|
|
|
protected function parseSettings()
|
|
|
|
{
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2014-10-11 01:22:03 +02:00
|
|
|
if (!($theme = Theme::getEditTheme())) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found'));
|
2014-10-11 01:22:03 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$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 18:50:29 +10: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.
|
2014-10-02 15:55:55 -07:00
|
|
|
* @param mixed $page Specifies the Cms Page file name.
|
2015-03-11 19:15:54 +11:00
|
|
|
* @param array $params Route parameters to consider in the URL.
|
2014-07-11 18:50:29 +10:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-28 07:48:00 +10:00
|
|
|
public static function url($page, $params = [])
|
2014-07-11 18:50:29 +10:00
|
|
|
{
|
2014-09-27 23:28:38 -07:00
|
|
|
/*
|
2014-11-19 17:46:26 +11:00
|
|
|
* Reuse existing controller or create a new one,
|
2014-09-27 23:28:38 -07:00
|
|
|
* assuming that the method is called not during the front-end
|
|
|
|
* request processing.
|
|
|
|
*/
|
2015-03-25 19:34:09 +11:00
|
|
|
$controller = Controller::getController() ?: new Controller;
|
2014-09-27 23:28:38 -07:00
|
|
|
|
2015-03-11 19:15:54 +11:00
|
|
|
return $controller->pageUrl($page, $params, true);
|
2014-07-11 18:50:29 +10:00
|
|
|
}
|
2014-10-02 15:55:55 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the pages.menuitem.getTypeInfo event.
|
|
|
|
* Returns a menu item type information. The type information is returned as array
|
|
|
|
* with the following elements:
|
|
|
|
* - references - a list of the item type reference options. The options are returned in the
|
|
|
|
* ["key"] => "title" format for options that don't have sub-options, and in the format
|
|
|
|
* ["key"] => ["title"=>"Option title", "items"=>[...]] for options that have sub-options. Optional,
|
|
|
|
* required only if the menu item type requires references.
|
|
|
|
* - nesting - Boolean value indicating whether the item type supports nested items. Optional,
|
|
|
|
* false if omitted.
|
|
|
|
* - dynamicItems - Boolean value indicating whether the item type could generate new menu items.
|
|
|
|
* Optional, false if omitted.
|
2014-10-11 01:22:03 +02:00
|
|
|
* - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires
|
|
|
|
* a CMS page reference to resolve the item URL.
|
2014-10-02 15:55:55 -07:00
|
|
|
* @param string $type Specifies the menu item type
|
|
|
|
* @return array Returns an array
|
|
|
|
*/
|
|
|
|
public static function getMenuTypeInfo($type)
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
if ($type == 'cms-page') {
|
|
|
|
$theme = Theme::getActiveTheme();
|
|
|
|
$pages = self::listInTheme($theme, true);
|
|
|
|
|
2014-10-11 01:22:03 +02:00
|
|
|
foreach ($pages as $page) {
|
2015-05-15 20:19:11 +10:00
|
|
|
$references[$page->getBaseFileName()] = $page->title . ' ['.$page->getBaseFileName().']';
|
2014-10-11 01:22:03 +02:00
|
|
|
}
|
2014-10-02 15:55:55 -07:00
|
|
|
|
|
|
|
$result = [
|
2014-10-18 14:32:06 +11:00
|
|
|
'references' => $references,
|
|
|
|
'nesting' => false,
|
2014-10-02 15:55:55 -07:00
|
|
|
'dynamicItems' => false
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the pages.menuitem.resolveItem event.
|
|
|
|
* Returns information about a menu item. The result is an array
|
|
|
|
* with the following keys:
|
|
|
|
* - url - the menu item URL. Not required for menu item types that return all available records.
|
|
|
|
* The URL should be returned relative to the website root and include the subdirectory, if any.
|
|
|
|
* Use the URL::to() helper to generate the URLs.
|
|
|
|
* - isActive - determines whether the menu item is active. Not required for menu item types that
|
|
|
|
* return all available records.
|
|
|
|
* - items - an array of arrays with the same keys (url, isActive, items) + the title key.
|
|
|
|
* The items array should be added only if the $item's $nesting property value is TRUE.
|
|
|
|
* @param \RainLab\Pages\Classes\MenuItem $item Specifies the menu item.
|
|
|
|
* @param \Cms\Classes\Theme $theme Specifies the current theme.
|
|
|
|
* @param string $url Specifies the current page URL, normalized, in lower case
|
|
|
|
* The URL is specified relative to the website root, it includes the subdirectory name, if any.
|
|
|
|
* @return mixed Returns an array. Returns null if the item cannot be resolved.
|
|
|
|
*/
|
|
|
|
public static function resolveMenuItem($item, $url, $theme)
|
|
|
|
{
|
|
|
|
$result = null;
|
|
|
|
|
|
|
|
if ($item->type == 'cms-page') {
|
2014-10-11 01:22:03 +02:00
|
|
|
if (!$item->reference) {
|
2014-10-02 15:55:55 -07:00
|
|
|
return;
|
2014-10-11 01:22:03 +02:00
|
|
|
}
|
2014-10-02 15:55:55 -07:00
|
|
|
|
2014-11-19 17:46:26 +11:00
|
|
|
$page = self::loadCached($theme, $item->reference);
|
2015-03-25 19:34:09 +11:00
|
|
|
$controller = Controller::getController() ?: new Controller;
|
|
|
|
$pageUrl = $controller->pageUrl($item->reference, [], false);
|
2014-10-02 15:55:55 -07:00
|
|
|
|
|
|
|
$result = [];
|
|
|
|
$result['url'] = $pageUrl;
|
|
|
|
$result['isActive'] = $pageUrl == $url;
|
2014-11-19 17:46:26 +11:00
|
|
|
$result['mtime'] = $page ? $page->mtime : null;
|
2014-10-02 15:55:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|