Skip to content

Commit

Permalink
Upgrade cms for new i18n backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Jan 25, 2017
1 parent 36de854 commit f2f347b
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 71 deletions.
28 changes: 9 additions & 19 deletions code/Controllers/CMSMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -425,7 +425,7 @@ public function SearchForm() {
);
$pageClasses = new DropdownField(
'q[ClassName]',
_t('CMSMain.PAGETYPEOPT', 'Page type', 'Dropdown for limiting search to a page type'),
_t('CMSMain.PAGETYPEOPT', 'Page type'),
$this->getPageTypes()
);
$pageClasses->setEmptyString(_t('CMSMain.PAGETYPEANYOPT','Any'));
Expand Down Expand Up @@ -597,7 +597,8 @@ public function PageTypes() {
$result = new ArrayList();

foreach($classes as $class) {
$instance = singleton($class);
/** @var SiteTree $instance */
$instance = singleton($class);

if($instance instanceof HiddenClass) {
continue;
Expand All @@ -608,27 +609,16 @@ public function PageTypes() {
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,
)));
}

Expand Down Expand Up @@ -931,7 +921,7 @@ public function ListViewForm() {
// 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 */
Expand Down
8 changes: 5 additions & 3 deletions code/Controllers/CMSPageEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,14 +88,14 @@ public function AddToCampaignForm($request)
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;
}
Expand All @@ -102,7 +104,7 @@ public function getAddToCampaignForm($id)
'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;
}
Expand Down
2 changes: 1 addition & 1 deletion code/Controllers/CMSPageHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public function CompareVersionsForm($versionID, $otherVersionID) {

public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE');
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs;
}

Expand Down
2 changes: 1 addition & 1 deletion code/Controllers/CMSPageSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getEditForm($id = null, $fields = null) {

public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE');
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs;
}

Expand Down
2 changes: 1 addition & 1 deletion code/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function SilverStripeNavigator() {
$dateObj = DBField::create_field('Datetime', $date);
// $dateObj->setVal($date);
return "<div id=\"SilverStripeNavigatorMessage\">" .
_t('ContentController.ARCHIVEDSITEFROM') .
_t('ContentController.ARCHIVEDSITEFROM', 'Archived site from') .
"<br>" . $dateObj->Nice() . "</div>";
}
}
Expand Down
153 changes: 112 additions & 41 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 {
Expand Down Expand Up @@ -819,7 +856,7 @@ public function setParent($item) {
*/
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;
}
Expand Down Expand Up @@ -1459,7 +1496,7 @@ public function MetaTags($includeTitle = true) {
));
}

$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,
Expand Down Expand Up @@ -1573,6 +1610,7 @@ protected function onBeforeWrite() {
// 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) {
Expand Down Expand Up @@ -1707,7 +1745,9 @@ public function validURLSegment() {
}

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
Expand Down Expand Up @@ -1959,6 +1999,7 @@ public function getCMSFields() {
->setURLPrefix($baseLink)
->setDefaultURL($this->generateURLSegment(_t(
'CMSMain.NEWPAGE',
'New {pagetype}',
array('pagetype' => $this->i18n_singular_name())
)));
$helpText = (self::config()->nested_urls && $this->Children()->count())
Expand Down Expand Up @@ -2844,18 +2885,55 @@ public function providePermissions() {
}

/**
* 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;
}

/**
Expand All @@ -2867,17 +2945,11 @@ public function i18n_singular_name() {
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;
}

Expand All @@ -2897,8 +2969,7 @@ public static function reset() {
self::$cache_permissions = array();
}

static public function on_db_reset() {
public static function on_db_reset() {
self::$cache_permissions = array();
}

}
4 changes: 2 additions & 2 deletions lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading

0 comments on commit f2f347b

Please sign in to comment.