From 5d020472bbf2d57dea680d599c2f06e0bb938bcb Mon Sep 17 00:00:00 2001 From: Matthew Hailwood Date: Wed, 24 Feb 2016 12:18:44 +1300 Subject: [PATCH] Initial Commit --- .gitignore | 4 ++ README.md | 55 ++++++++++++++ _config/.gitkeep | 0 _config/config.yml | 7 ++ code/GlobalContent.php | 59 +++++++++++++++ ...obalContent_ContentControllerExtension.php | 11 +++ code/GlobalContent_ModelAdmin.php | 67 ++++++++++++++++++ composer.json | 23 ++++++ images/globe-icon.png | Bin 0 -> 1129 bytes templates/GlobalContent_ModelAdmin_Content.ss | 3 + .../GlobalContent_ModelAdmin_EditForm.ss | 65 +++++++++++++++++ templates/TablessCMSTabSet.ss | 13 ++++ 12 files changed, 307 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 _config/.gitkeep create mode 100644 _config/config.yml create mode 100644 code/GlobalContent.php create mode 100644 code/GlobalContent_ContentControllerExtension.php create mode 100644 code/GlobalContent_ModelAdmin.php create mode 100644 composer.json create mode 100755 images/globe-icon.png create mode 100644 templates/GlobalContent_ModelAdmin_Content.ss create mode 100644 templates/GlobalContent_ModelAdmin_EditForm.ss create mode 100644 templates/TablessCMSTabSet.ss diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcdbcf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +.buildpath +.project +.settings \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d97479 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# Installation Instructions # +## Composer ## +Run the following to add this module as a requirement and install it via composer. + +``` +#!bash + +composer require "webfox/silverstripe-global-content" +``` +then browse to /dev/build?flush=all + + +#Requirements# +* Silverstripe 3.2+ +* php5.4+ + +#Module Overview# +This module adds a convenient `SiteConfig` like interface for managing global content. + Useful for when you want global content but don't want to give content-authors access to `SiteConfig` + +#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 + +```php +class GlobalContentExtension extends DataExtension +{ + + protected static $db = [ + 'MyFieldName' => 'Varchar' + ]; + + public function updateCMSFields(FieldList $fields) + { + + $fields->addFieldToTab( + 'Root.Main', + TextField::create('MyFieldName', 'My field name') + ); + + } + +} +``` + +The use with permissions: +* Grant the user/role/group the `Access to 'Global Content' section` permission + +To use in templates: +* `$GlobalContent.MyFieldName` +* `<% with $GlobalContent %> {$MyFieldName} <% end_with %>` + +To use in PHP: +* `GlobalContent::inst()->MyFieldName` diff --git a/_config/.gitkeep b/_config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/_config/config.yml b/_config/config.yml new file mode 100644 index 0000000..de8360b --- /dev/null +++ b/_config/config.yml @@ -0,0 +1,7 @@ +--- +Name: silverstripe-global-content +--- + +ContentController: + extensions: + - GlobalContent_ContentControllerExtension \ No newline at end of file diff --git a/code/GlobalContent.php b/code/GlobalContent.php new file mode 100644 index 0000000..d1af9f1 --- /dev/null +++ b/code/GlobalContent.php @@ -0,0 +1,59 @@ +add(HiddenField::create('ID', 'ID', $this->ID)); + + /** @var TabSet $rootTabset */ + $rootTabset = $fields->fieldByName('Root'); + $rootTabset->setTemplate('TablessCMSTabSet'); + + return $fields; + } + + + public function requireDefaultRecords() + { + if (!static::inst()) { + /** @var self $config */ + $config = new static(); + $config->write(); + + DB::alteration_message('Created default Global Content instance', 'created'); + } + } + + public function canView($member = null) + { + return true; + } + + public function canEdit($member = null) + { + return Permission::check('CMS_ACCESS_GlobalContent_ModelAdmin', 'any', $member); + } + + public function canDelete($member = null) + { + return false; + } + + public function canCreate($member = null) + { + return false; + } + +} \ No newline at end of file diff --git a/code/GlobalContent_ContentControllerExtension.php b/code/GlobalContent_ContentControllerExtension.php new file mode 100644 index 0000000..206fbc3 --- /dev/null +++ b/code/GlobalContent_ContentControllerExtension.php @@ -0,0 +1,11 @@ +getCMSFields(); + $actions = FieldList::create( + FormAction::create('save', _t('GridFieldDetailForm.Save', 'Save')) + ->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive') + ); + + GlobalContent::inst()->extend('updateCMSActions', $actions); + + if (GlobalContent::inst()->hasMethod('getCMSValidator')) { + $validator = GlobalContent::inst()->getCMSValidator(); + } else { + $validator = RequiredFields::create(); + } + GlobalContent::inst()->extend('updateCMSValidator', $validator); + + /** @var CMSForm $form */ + $form = CMSForm::create($this, 'EditForm', $fields, $actions, $validator); + $form->loadDataFrom(GlobalContent::inst()); + $form->addExtraClass('cms-edit-form center'); + $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); + $form->setHTMLID('Form_EditForm'); + $form->setResponseNegotiator($this->getResponseNegotiator()); + $form->setAttribute('data-pjax-fragment', 'CurrentForm'); + $form->setFormAction(Controller::join_links( + $this->Link($this->sanitiseClassName($this->modelClass)), + 'EditForm' + )); + + $this->extend('updateEditForm', $form); + + return $form; + } + + public function saveSettings($data, Form $form) + { + + $globalContentInstance = GlobalContent::inst(); + + $form->saveInto($globalContentInstance); + $form->sessionMessage('Global content updated', 'good'); + + return $this->getResponseNegotiator()->respond($this->request); + } + + public function save($data, $form) + { + return parent::save($data, $form); + } + + +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..47aee0c --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "webfox/silverstripe-global-content", + "description": "Silverstripe SiteConfig like interface for global content that content-authors can access", + "keywords": [ + "webfox", + "silverstripe", + "global" + ], + "type": "silverstripe-module", + "require": { + "silverstripe/cms": "~3.2" + }, + "license": "MIT", + "authors": [ + { + "name": "Matthew Hailwood", + "email": "matthew@webfox.co.nz" + } + ], + "extra": { + "installer-name": "silverstripe-global-content" + } +} diff --git a/images/globe-icon.png b/images/globe-icon.png new file mode 100755 index 0000000000000000000000000000000000000000..6ca20c34f6db18540aa0d3ce5de68b6246351a96 GIT binary patch literal 1129 zcmeAS@N?(olHy`uVBq!ia0vp^0w65F0wkYiyFdh=fJt18-9o6W=05`ivx29KV~EA++~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0 HcmV?d00001 diff --git a/templates/GlobalContent_ModelAdmin_Content.ss b/templates/GlobalContent_ModelAdmin_Content.ss new file mode 100644 index 0000000..7362f33 --- /dev/null +++ b/templates/GlobalContent_ModelAdmin_Content.ss @@ -0,0 +1,3 @@ +
+ $EditForm +
diff --git a/templates/GlobalContent_ModelAdmin_EditForm.ss b/templates/GlobalContent_ModelAdmin_EditForm.ss new file mode 100644 index 0000000..d26bc3b --- /dev/null +++ b/templates/GlobalContent_ModelAdmin_EditForm.ss @@ -0,0 +1,65 @@ +<% if $IncludeFormTag %> +
+<% end_if %> + +
+
+ <% with $Controller %> + <% include CMSBreadcrumbs %> + <% end_with %> +
+ + <% if $Fields.hasTabset && $Fields.fieldByName('Root').Tabs.Count > 1 %> +
+
    + <% loop $Fields.fieldByName('Root').Tabs %> +
  • + $Title +
  • + <% end_loop %> +
+
+ <% end_if %> + +
+ +
+ +
+ <% if $Message %> +

$Message

+ <% else %> + + <% end_if %> + +
+ <% if $Legend %> + $Legend<% end_if %> + + <% loop $Fields %> + $FieldHolder + <% end_loop %> +
+
+
+ +
+ <% if $Actions %> +
+ <% loop $Actions %> + $Field + <% end_loop %> + <% if $Controller.LinkPreview %> + + <% _t('LeftAndMain.PreviewButton', 'Preview') %> » + + <% end_if %> +
+ <% end_if %> +
+
+ +<% if $IncludeFormTag %> +
+<% end_if %> \ No newline at end of file diff --git a/templates/TablessCMSTabSet.ss b/templates/TablessCMSTabSet.ss new file mode 100644 index 0000000..86d2629 --- /dev/null +++ b/templates/TablessCMSTabSet.ss @@ -0,0 +1,13 @@ +
+ <% loop $Tabs %> + <% if $Tabs %> + $FieldHolder + <% else %> +
+ <% loop $Fields %> + $FieldHolder + <% end_loop %> +
+ <% end_if %> + <% end_loop %> +