Skip to content

Commit

Permalink
Complete overhaul (still b/c) based on silverstripe SecurityAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
hailwood committed Nov 16, 2016
1 parent 57a0047 commit af194aa
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 179 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
## Composer ##
Run the following to add this module as a requirement and install it via composer.

```bash
```
#!bash
composer require "webfox/silverstripe-global-content"
```
then browse to /dev/build?flush=all
Expand All @@ -19,17 +21,9 @@ This module adds a convenient `SiteConfig` like interface for managing global co
#Module Usage#
Too add additional fields:
* Create a `DataExtension` that gets applied to `GlobalContent`
* The extension requires an `updateFields(FieldList $fields)` method and any standard `DataExtension` properties

```yaml
GlobalContent:
extensions:
- GlobalContentExtension
```
```php5
<?php
* The extension requires an `updateCMSFields(FieldList $fields)` method and any standard `DataExtension` properties

```php
class GlobalContentExtension extends DataExtension
{

Expand All @@ -56,6 +50,12 @@ The use with permissions:
To use in templates:
* `$GlobalContent.MyFieldName`
* `<% with $GlobalContent %> {$MyFieldName} <% end_with %>`
* `$GlobalContent('MyFieldName')`

To use in PHP:
* `GlobalContent::inst()->MyFieldName`

To alter the edit for directly:
* Create a new `LeftAndMainExtension` that gets applied to `GlobalContentAdmin`
* The extension can use the `updateEditForm($form)` method to update the form **before** data is loaded.
* The extension can use the `updateEditFormData($form)` method to update the form **after** data is loaded.
6 changes: 1 addition & 5 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
Name: silverstripe-global-content
---

ContentController:
extensions:
- GlobalContent_ContentControllerExtension
---
4 changes: 0 additions & 4 deletions code/GlobalContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public function getCMSFields()
$fields = parent::getCMSFields();
$fields->add(HiddenField::create('ID', 'ID', $this->ID));

/** @var TabSet $rootTabset */
$rootTabset = $fields->fieldByName('Root');
$rootTabset->setTemplate('TablessCMSTabSet');

return $fields;
}

Expand Down
132 changes: 132 additions & 0 deletions code/GlobalContentAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

class GlobalContentAdmin extends LeftAndMain implements PermissionProvider
{
private static $url_segment = 'global-content';

private static $url_rule = '/$Action/$ID/$OtherID';

private static $menu_title = 'Global Content';

private static $tree_class = 'GlobalContent';

private static $subitem_class = 'GlobalContent';

protected static $menu_icon = 'silverstripe-global-content/images/globe-icon.png';

private static $allowed_actions = [
'EditForm',
];

public function init()
{
parent::init();
}

public function getEditForm($id = null, $fields = null)
{
$record = GlobalContent::inst();
$fields = $record->getCMSFields();
$actions = $this->getCMSActions($record);

if ($record && !$record->canView()) {
return Security::permissionFailure($this);
}

// Tab nav in CMS is rendered through separate template
/** @var TabSet $root */
$root = $fields->fieldByName('Root');
$root->setTemplate('CMSTabSet');

$form = $this->buildForm($fields, $actions);

$this->extend('updateEditForm', $form);
$form->loadDataFrom($record);
$this->extend('updateEditFormWithData', $form);

return $form;
}

public function getCMSActions(GlobalContent $record)
{
if ($record->hasMethod('getAllCMSActions')) {
$actions = $record->getAllCMSActions();
} else {
$actions = $record->getCMSActions();
// add default actions if none are defined
if (!$actions || !$actions->Count()) {
if ($record->hasMethod('canEdit') && $record->canEdit()) {
$actions->push(
FormAction::create('save', _t('CMSMain.SAVE', 'Save'))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
);
}
if ($record->hasMethod('canDelete') && $record->canDelete()) {
$actions->push(
FormAction::create('delete', _t('ModelAdmin.DELETE', 'Delete'))
->addExtraClass('ss-ui-action-destructive')
);
}
}
}

// Use <button> to allow full jQuery UI styling
$actionsFlattened = $actions->dataFields();
if ($actionsFlattened) {
foreach ($actionsFlattened as $action) {
$action->setUseButtonTag(true);
}
}

return $actions;
}

protected function buildForm(FieldList $fields, FieldList $actions){
$form = CMSForm::create(
$this,
'EditForm',
$fields,
$actions
)->setHTMLID('Form_EditForm');

$form->setResponseNegotiator($this->getResponseNegotiator());
$form->addExtraClass('cms-edit-form');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// Tab nav in CMS is rendered through separate template
if ($form->Fields()->hasTabset()) {
$form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
}
$form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses());
$form->setAttribute('data-pjax-fragment', 'CurrentForm');

return $form;
}

public function Backlink()
{
return false;
}

public function Breadcrumbs($unlinked = false)
{
return ArrayList::create([
ArrayData::create([
'Title' => singleton('GlobalContent')->i18n_singular_name(),
'Link' => $this->Link()
])
]);
}

public function providePermissions()
{
$title = _t("GlobalContentAdmin.MENUTITLE", LeftAndMain::menu_title_for_class($this->class));
return [
"CMS_ACCESS_GlobalContentAdmin" => [
'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", ['title' => $title]),
'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'),
'help' => _t('GlobalContentAdmin.ACCESS_HELP', "Allow viewing and editing data in the '{title}' section", ['title' => $title]),
]
];
}

}
17 changes: 17 additions & 0 deletions code/GlobalContentTemplateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class GlobalContentTemplateProvider implements TemplateGlobalProvider {

public static function get_template_global_variables() {
return [
'GlobalContent',
'Dump'
];
}

public static function GlobalContent($key = null){
$inst = GlobalContent::inst();
return (is_null($key) ? $inst : $inst->{$key});
}

}
11 changes: 0 additions & 11 deletions code/GlobalContent_ContentControllerExtension.php

This file was deleted.

67 changes: 0 additions & 67 deletions code/GlobalContent_ModelAdmin.php

This file was deleted.

3 changes: 0 additions & 3 deletions templates/GlobalContent_ModelAdmin_Content.ss

This file was deleted.

65 changes: 0 additions & 65 deletions templates/GlobalContent_ModelAdmin_EditForm.ss

This file was deleted.

13 changes: 0 additions & 13 deletions templates/TablessCMSTabSet.ss

This file was deleted.

0 comments on commit af194aa

Please sign in to comment.