From fa2b87172c9bbfefe276d21441ec7533d7e0e508 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 23 Jan 2017 15:11:49 +1300 Subject: [PATCH] Upgrade cms for new i18n backend --- code/Controllers/CMSMain.php | 26 +-- code/Controllers/CMSPageEditController.php | 8 +- code/Controllers/CMSPageHistoryController.php | 2 +- .../Controllers/CMSPageSettingsController.php | 2 +- code/Controllers/ContentController.php | 2 +- code/Model/SiteTree.php | 153 +++++++++++++----- lang/de.yml | 4 +- lang/en.yml | 16 +- tests/model/SiteTreeTest.php | 6 +- 9 files changed, 148 insertions(+), 71 deletions(-) diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index 14fd43c7..8a7f5840 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -85,7 +85,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr private static $menu_title = 'Edit Page'; - private static $menu_icon_class = 'font-icon-sitemap'; + private static $menu_icon_class = 'font-icon-sitemap'; private static $menu_priority = 10; @@ -597,8 +597,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $result = new ArrayList(); foreach($classes as $class) { - $instance = singleton($class); - + $instance = SiteTree::singleton($class); if($instance instanceof HiddenClass) { continue; } @@ -608,27 +607,16 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr continue; } - $addAction = $instance->i18n_singular_name(); - - // Get description (convert 'Page' to 'SiteTree' for correct localization lookups) - $i18nClass = ($class == 'Page') ? 'SilverStripe\\CMS\\Model\\SiteTree' : $class; - $description = _t($i18nClass . '.DESCRIPTION'); - - if(!$description) { - $description = $instance->uninherited('description'); - } - - if($class == 'Page' && !$description) { - $description = SiteTree::singleton()->uninherited('description'); - } + $singularName = $instance->i18n_singular_name(); + $description = $instance->i18n_description(); $result->push(new ArrayData(array( 'ClassName' => $class, - 'AddAction' => $addAction, + 'AddAction' => $singularName, 'Description' => $description, // TODO Sprite support 'IconURL' => $instance->stat('icon'), - 'Title' => singleton($class)->i18n_singular_name(), + 'Title' => $singularName, ))); } @@ -931,7 +919,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Don't allow navigating into children nodes on filtered lists $fields = array( 'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), - 'singular_name' => _t('SiteTree.PAGETYPE'), + 'singular_name' => _t('SiteTree.PAGETYPE', 'Page Type'), 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'), ); /** @var GridFieldSortableHeader $sortableHeader */ diff --git a/code/Controllers/CMSPageEditController.php b/code/Controllers/CMSPageEditController.php index 359943db..930ebb66 100644 --- a/code/Controllers/CMSPageEditController.php +++ b/code/Controllers/CMSPageEditController.php @@ -2,7 +2,9 @@ namespace SilverStripe\CMS\Controllers; +use Page; use SilverStripe\Admin\AddToCampaignHandler; +use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; @@ -86,14 +88,14 @@ class CMSPageEditController extends CMSMain { public function getAddToCampaignForm($id) { // Get record-specific fields - $record = \Page::get()->byID($id); + $record = SiteTree::get()->byID($id); if (!$record) { $this->httpError(404, _t( 'AssetAdmin.ErrorNotFound', 'That {Type} couldn\'t be found', '', - ['Type' => _t('SiteTree.SINGULARNAME')] + ['Type' => Page::singleton()->i18n_singular_name()] )); return null; } @@ -102,7 +104,7 @@ class CMSPageEditController extends CMSMain { 'AssetAdmin.ErrorItemPermissionDenied', 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', '', - ['ObjectTitle' => _t('SiteTree.SINGULARNAME')] + ['ObjectTitle' => Page::singleton()->i18n_singular_name()] )); return null; } diff --git a/code/Controllers/CMSPageHistoryController.php b/code/Controllers/CMSPageHistoryController.php index bc6b2989..7ff4e44f 100644 --- a/code/Controllers/CMSPageHistoryController.php +++ b/code/Controllers/CMSPageHistoryController.php @@ -447,7 +447,7 @@ class CMSPageHistoryController extends CMSMain { public function Breadcrumbs($unlinked = false) { $crumbs = parent::Breadcrumbs($unlinked); - $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE'); + $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages'); return $crumbs; } diff --git a/code/Controllers/CMSPageSettingsController.php b/code/Controllers/CMSPageSettingsController.php index 77a077e1..f210030e 100644 --- a/code/Controllers/CMSPageSettingsController.php +++ b/code/Controllers/CMSPageSettingsController.php @@ -20,7 +20,7 @@ class CMSPageSettingsController extends CMSMain { public function Breadcrumbs($unlinked = false) { $crumbs = parent::Breadcrumbs($unlinked); - $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE'); + $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages'); return $crumbs; } diff --git a/code/Controllers/ContentController.php b/code/Controllers/ContentController.php index 14f0f985..b9a3344f 100644 --- a/code/Controllers/ContentController.php +++ b/code/Controllers/ContentController.php @@ -355,7 +355,7 @@ HTML; $dateObj = DBField::create_field('Datetime', $date); // $dateObj->setVal($date); return "
" . - _t('ContentController.ARCHIVEDSITEFROM') . + _t('ContentController.ARCHIVEDSITEFROM', 'Archived site from') . "
" . $dateObj->Nice() . "
"; } } diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 94541366..9ba7327a 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -10,8 +10,10 @@ use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\CMS\Controllers\ModelAsController; use SilverStripe\CMS\Controllers\RootURLController; use SilverStripe\CMS\Forms\SiteTreeURLSegmentField; +use SilverStripe\Control\ContentNegotiator; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; +use SilverStripe\Control\RequestHandler; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; @@ -106,7 +108,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid * @config * @var array */ - private static $allowed_children = array("SilverStripe\\CMS\\Model\\SiteTree"); + private static $allowed_children = [ + self::class + ]; /** * The default child class for this page. @@ -185,8 +189,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid ); private static $many_many = array( - "ViewerGroups" => "SilverStripe\\Security\\Group", - "EditorGroups" => "SilverStripe\\Security\\Group", + "ViewerGroups" => Group::class, + "EditorGroups" => Group::class, ); private static $has_many = array( @@ -241,18 +245,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ private static $icon = null; - /** - * @config - * @var string Description of the class functionality, typically shown to a user - * when selecting which page type to create. Translated through {@link provideI18nEntities()}. - */ - private static $description = 'Generic content page'; - - private static $extensions = array( - 'SilverStripe\\ORM\\Hierarchy\\Hierarchy', - 'SilverStripe\\ORM\\Versioning\\Versioned', - "SilverStripe\\CMS\\Model\\SiteTreeLinkTracking" - ); + private static $extensions = [ + Hierarchy::class, + Versioned::class, + SiteTreeLinkTracking::class, + ]; private static $searchable_fields = array( 'Title', @@ -302,6 +299,46 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid protected $_cache_statusFlags = null; + /** + * Plural form for SiteTree / Page classes. Not inherited by subclasses. + * + * @config + * @var string + */ + private static $base_plural_name = 'Pages'; + + /** + * Plural form for SiteTree / Page classes. Not inherited by subclasses. + * + * @config + * @var string + */ + private static $base_singular_name = 'Page'; + + /** + * Description of the class functionality, typically shown to a user + * when selecting which page type to create. Translated through {@link provideI18nEntities()}. + * + * @see SiteTree::description() + * @see SiteTree::i18n_description() + * + * @config + * @var string + */ + private static $description = null; + + /** + * Description for Page and SiteTree classes, but not inherited by subclasses. + * override SiteTree::$description in subclasses instead. + * + * @see SiteTree::description() + * @see SiteTree::i18n_description() + * + * @config + * @var string + */ + private static $base_description = 'Generic content page'; + /** * Fetches the {@link SiteTree} object that maps to a link. * @@ -315,7 +352,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid * @param bool $cache True (default) to use caching, false to force a fresh search from the database * @return SiteTree */ - static public function get_by_link($link, $cache = true) { + public static function get_by_link($link, $cache = true) { if(trim($link, '/')) { $link = trim(Director::makeRelative($link), '/'); } else { @@ -819,7 +856,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ public function getParent() { if ($parentID = $this->getField("ParentID")) { - return DataObject::get_by_id("SilverStripe\\CMS\\Model\\SiteTree", $parentID); + return DataObject::get_by_id(self::class, $parentID); } return null; } @@ -1459,7 +1496,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid )); } - $charset = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'encoding'); + $charset = ContentNegotiator::config()->get('encoding'); $tags[] = FormField::create_tag('meta', array( 'http-equiv' => 'Content-Type', 'content' => 'text/html; charset=' . $charset, @@ -1573,6 +1610,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid // If there is no URLSegment set, generate one from Title $defaultSegment = $this->generateURLSegment(_t( 'CMSMain.NEWPAGE', + 'New {pagetype}', array('pagetype' => $this->i18n_singular_name()) )); if((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) { @@ -1707,7 +1745,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } if(!self::config()->nested_urls || !$this->ParentID) { - if(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, 'SilverStripe\\Control\\RequestHandler')) return false; + if(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, RequestHandler::class)) { + return false; + } } // Filters by url, id, and parent @@ -1959,6 +1999,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid ->setURLPrefix($baseLink) ->setDefaultURL($this->generateURLSegment(_t( 'CMSMain.NEWPAGE', + 'New {pagetype}', array('pagetype' => $this->i18n_singular_name()) ))); $helpText = (self::config()->nested_urls && $this->Children()->count()) @@ -2844,18 +2885,55 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } /** - * Return the translated Singular name. + * Default singular name for page / sitetree * * @return string */ - public function i18n_singular_name() { - // Convert 'Page' to 'SiteTree' for correct localization lookups - /** @skipUpgrade */ - // @todo When we namespace translations, change 'SiteTree' to FQN of the class - $class = (static::class == 'Page' || static::class === self::class) - ? 'SiteTree' - : static::class; - return _t($class.'.SINGULARNAME', $this->singular_name()); + public function singular_name() { + $base = in_array(static::class, [Page::class, self::class]); + if ($base) { + return $this->stat('base_singular_name'); + } + return parent::singular_name(); + } + + /** + * Default plural name for page / sitetree + * + * @return string + */ + public function plural_name() { + $base = in_array(static::class, [Page::class, self::class]); + if ($base) { + return $this->stat('base_plural_name'); + } + return parent::plural_name(); + } + + /** + * Get description for this page + * + * @return string|null + */ + public function description() { + $base = in_array(static::class, [Page::class, self::class]); + if ($base) { + return $this->stat('base_description'); + } + return $this->stat('description'); + } + + /** + * Get localised description for this page + * + * @return string|null + */ + public function i18n_description() { + $description = $this->description(); + if ($description) { + return _t(static::class.'.DESCRIPTION', $description); + } + return null; } /** @@ -2867,17 +2945,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid public function provideI18nEntities() { $entities = parent::provideI18nEntities(); - if(isset($entities['Page.SINGULARNAME'])) $entities['Page.SINGULARNAME'][3] = CMS_DIR; - if(isset($entities['Page.PLURALNAME'])) $entities['Page.PLURALNAME'][3] = CMS_DIR; - - $entities[static::class . '.DESCRIPTION'] = array( - $this->stat('description'), - 'Description of the page type (shown in the "add page" dialog)' - ); - - $entities['SiteTree.SINGULARNAME'][0] = 'Page'; - $entities['SiteTree.PLURALNAME'][0] = 'Pages'; - + // Add optional description + $description = $this->description(); + if ($description) { + $entities[static::class . '.DESCRIPTION'] = $description; + } return $entities; } @@ -2897,8 +2969,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid self::$cache_permissions = array(); } - static public function on_db_reset() { + public static function on_db_reset() { self::$cache_permissions = array(); } - } diff --git a/lang/de.yml b/lang/de.yml index 26f7708b..d9c66f9b 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -314,8 +314,8 @@ de: SINGULARNAME: Weiterleitungsseite SilverStripe\CMS\Model\SiteTree: DESCRIPTION: 'Allgemeine Inhaltsseite' - PLURALNAME: Seitenbäume - SINGULARNAME: Seitenbaum + PLURALNAME: Seiten + SINGULARNAME: Seite SilverStripe\CMS\Model\VirtualPage: DESCRIPTION: 'Zeigt den Inhalt einer anderen Seite an' PLURALNAME: 'Virtuelle Seiten' diff --git a/lang/en.yml b/lang/en.yml index 59265584..3baca161 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -338,18 +338,30 @@ en: SilverStripe\CMS\Model\ErrorPage: DESCRIPTION: 'Custom content for different error cases (e.g. "Page not found")' PLURALNAME: 'Error Pages' + PLURALS: + one: 'An Error Page' + other: '{count} Error Pages' SINGULARNAME: 'Error Page' SilverStripe\CMS\Model\RedirectorPage: DESCRIPTION: 'Redirects to an internal page or an external URL' PLURALNAME: 'Redirector Pages' + PLURALS: + one: 'A Redirector Page' + other: '{count} Redirector Pages' SINGULARNAME: 'Redirector Page' SilverStripe\CMS\Model\SiteTree: DESCRIPTION: 'Generic content page' - PLURALNAME: 'Site Trees' - SINGULARNAME: 'Site Tree' + PLURALNAME: Pages + PLURALS: + one: 'A Page' + other: '{count} Pages' + SINGULARNAME: Page SilverStripe\CMS\Model\VirtualPage: DESCRIPTION: 'Displays the content of another page' PLURALNAME: 'Virtual Pages' + PLURALS: + one: 'A Virtual Page' + other: '{count} Virtual Pages' SINGULARNAME: 'Virtual Page' SiteConfig: DEFAULTTHEME: '(Use default theme)' diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index 224b6f6e..6971b0bc 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -790,6 +790,7 @@ class SiteTreeTest extends SapphireTest { $sitetree = new SiteTree(); $sitetree->Title = _t( 'CMSMain.NEWPAGE', + 'New {pagetype}', array('pagetype' => $sitetree->i18n_singular_name()) ); $sitetree->write(); @@ -817,10 +818,13 @@ class SiteTreeTest extends SapphireTest { $sitetree = new SiteTree(); $sitetree->Title = _t( 'CMSMain.NEWPAGE', + 'New {pagetype}', array('pagetype' => $sitetree->i18n_singular_name()) ); $sitetree->write(); - $this->assertEquals($sitetree->URLSegment, 'neue-seite', + $this->assertEquals( + 'neue-seite', + $sitetree->URLSegment, 'Sets based on default title on first save' );