Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hailwood committed Feb 23, 2016
0 parents commit 5d02047
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.buildpath
.project
.settings
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
Empty file added _config/.gitkeep
Empty file.
7 changes: 7 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Name: silverstripe-global-content
---

ContentController:
extensions:
- GlobalContent_ContentControllerExtension
59 changes: 59 additions & 0 deletions code/GlobalContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

class GlobalContent extends DataObject
{
/**
* Return the GlobalContent instance
*
* @return self
*/
public static function inst()
{
return static::get_one(__CLASS__);
}

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


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

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

class GlobalContent_ContentControllerExtension extends Extension
{

public function getGlobalContent()
{
return GlobalContent::inst();
}

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

class GlobalContent_ModelAdmin extends ModelAdmin
{
private static $managed_models = ['GlobalContent'];

private static $url_segment = 'global-content';

private static $menu_title = 'Global Content';

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

protected static $tree_class = 'GlobalContent';

public function getEditForm($id = null, $fields = null)
{
$fields = GlobalContent::inst()->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);
}


}
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"extra": {
"installer-name": "silverstripe-global-content"
}
}
Binary file added images/globe-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions templates/GlobalContent_ModelAdmin_Content.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="cms-content cms-tabset center $BaseCSSClasses" data-layout-type="border" data-pjax-fragment="Content" data-blerg="glerg">
$EditForm
</div>
65 changes: 65 additions & 0 deletions templates/GlobalContent_ModelAdmin_EditForm.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<% if $IncludeFormTag %>
<form $FormAttributes data-layout-type="border">
<% end_if %>

<div class="cms-content-header north">
<div class="cms-content-header-info">
<% with $Controller %>
<% include CMSBreadcrumbs %>
<% end_with %>
</div>

<% if $Fields.hasTabset && $Fields.fieldByName('Root').Tabs.Count > 1 %>
<div class="cms-content-header-tabs cms-tabset-nav-primary ss-ui-tabs-nav">
<ul>
<% loop $Fields.fieldByName('Root').Tabs %>
<li class="tab-$ClassName <% if $extraClass %>$extraClass<% end_if %>">
<a href="#$id" class="cms-panel-link" title="Form_EditForm">$Title</a>
</li>
<% end_loop %>
</ul>
</div>
<% end_if %>

</div>

<div class="cms-content-fields center ui-widget-content" data-layout-type="border">

<div class="cms-content-fields center cms-panel-padded">
<% if $Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>

<fieldset>
<% if $Legend %>
<legend>$Legend</legend><% end_if %>

<% loop $Fields %>
$FieldHolder
<% end_loop %>
<div class="clear"><!-- --></div>
</fieldset>
</div>

<div class="cms-content-actions south">
<% if $Actions %>
<div class="Actions">
<% loop $Actions %>
$Field
<% end_loop %>
<% if $Controller.LinkPreview %>
<a href="$Controller.LinkPreview" class="cms-preview-toggle-link ss-ui-button"
data-icon="preview">
<% _t('LeftAndMain.PreviewButton', 'Preview') %> &raquo;
</a>
<% end_if %>
</div>
<% end_if %>
</div>
</div>

<% if $IncludeFormTag %>
</form>
<% end_if %>
13 changes: 13 additions & 0 deletions templates/TablessCMSTabSet.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div $AttributesHTML>
<% loop $Tabs %>
<% if $Tabs %>
$FieldHolder
<% else %>
<div $AttributesHTML>
<% loop $Fields %>
$FieldHolder
<% end_loop %>
</div>
<% end_if %>
<% end_loop %>
</div>

0 comments on commit 5d02047

Please sign in to comment.