From eb7d43c876f70ecc7b612c7ba34046a14af4fa6c Mon Sep 17 00:00:00 2001 From: Naomi Guyer Date: Thu, 24 Aug 2017 15:24:08 +1200 Subject: [PATCH] FIX: Silverstripe 4 support --- README.md | 54 +-- _config.php | 5 + _config/config.yml | 4 +- code/Brand.php | 331 --------------- code/BrandAdmin.php | 18 - code/BrandExtension.php | 43 -- code/BrandSiteTreeExtension.php | 13 - composer.json | 43 +- images/palette.png | Bin 329 -> 0 bytes images/palette.svg | 11 + src/Brand.php | 413 +++++++++++++++++++ src/BrandAdmin.php | 21 + src/BrandDataObjectExtension.php | 55 +++ src/BrandSiteTreeExtension.php | 18 + templates/Adrexia/Brand/Includes/Brand.ss | 65 +++ templates/Adrexia/Brand/Includes/Favicons.ss | 25 ++ templates/Brand.ss | 70 ---- 17 files changed, 670 insertions(+), 519 deletions(-) delete mode 100644 code/Brand.php delete mode 100644 code/BrandAdmin.php delete mode 100644 code/BrandExtension.php delete mode 100644 code/BrandSiteTreeExtension.php delete mode 100755 images/palette.png create mode 100755 images/palette.svg create mode 100644 src/Brand.php create mode 100644 src/BrandAdmin.php create mode 100644 src/BrandDataObjectExtension.php create mode 100644 src/BrandSiteTreeExtension.php create mode 100644 templates/Adrexia/Brand/Includes/Brand.ss create mode 100644 templates/Adrexia/Brand/Includes/Favicons.ss delete mode 100644 templates/Brand.ss diff --git a/README.md b/README.md index 6dcb828..f6d9d2b 100644 --- a/README.md +++ b/README.md @@ -2,38 +2,40 @@ A module to assist with a website's basic branding. Allows setting of brand colours and fonts from within a Silverstripe Admin. - ## Requirements -SilverStripe 3.3 or higher +SilverStripe 4 or higher. See 3.0 branch for SilverStripe 3 support ## Installation ``composer require adrexia/brand`` ## Setup -Include the brand template in the head of you page template: -``<% include Brand %>`` - -By default this includes the fonts and the font-color. The Palette colours are for use on other page types, or for custom theming. For example, they can be used to add a restricted colourpalette to a page (with the help of the Color Palette module): +You can either just include the Favicons, and apply the colour schemes manually: +``<% include Adrexia/Brand/Favicons %>`` +Or, include the brand template in the head of you page template to get both: +``<% include Adrexia/Brand/Brand %>`` - $brand = Brand::get()->First(); +By default this includes the fonts, the font-colors, and the favicons. +The Menu colours are left for you to implement, or disable based on your site's requirements. +The Palette colours are for use on other page types, or for custom theming. For example, they can be used to add a restricted colourpalette to a page (with the help of the Color Palette module): - if($brand) { - $fields->insertAfter( - ColorPaletteField::create( - "Color", "Color", $brand->getFullPalette() - ), "Intro" - ); - } + $brand = Brand::get()->First(); -There is an extension 'BrandPageExtension' included for this purpose which adds a brand colour, a contrast colour, and an image. To enable it, add this to your _config.yml file: + if($brand) { + $fields->insertAfter( + ColorPaletteField::create( + "Color", "Color", $brand->getFullPalette() + ), "Intro" + ); + } - Page: - extensions: - - BrandExtension +There is an extension 'BrandDataObjectExtension' included for this purpose which adds a brand colour, a contrast colour, and an image. To enable it, add this to your _config.yml file: + Page: + extensions: + - Adrexia\Brand\BrandDataObjectExtension The other variables provided can be called in a template with: ``$Brand.Logo`` @@ -41,9 +43,11 @@ The other variables provided can be called in a template with: To get the hex value of a colour from a colourpalette in your template you can do something like this: - <% if $Colour %>$Brand.getHex($Color)<% else %>$Brand.getHex($Level(1).Color)<% end_if %> + <% if $Colour %>$Brand.getHex($Color)<% else %>$Brand.getHex($Level(1).Color)<% end_if %> ## Subsite support +NOTE: Subsite support isn't yet complete for silverstripe 4 + This module can work with subsites with the addition of the SubsiteModelExtension from adrexia/subsite-modeladmins (specifically, the onBeforeWrite and augmentSQL functions). You can install this via composer require, or just take the bits you need: ``composer require adrexia/subsite-modeladmins`` @@ -51,12 +55,12 @@ This module can work with subsites with the addition of the SubsiteModelExtensio ### Setup: In your yml config: - Brand: - extensions: - - SubsiteModelExtension - BrandAdmin - extensions: - - SubsiteMenuExtension + Adrexia\Brand\Brand: + extensions: + - SubsiteModelExtension + Adrexia\Brand\BrandAdmin + extensions: + - SubsiteMenuExtension The brand extension is supplied by the subsite modeladmin module. The BrandAdmin extension is straight from the subsites module, and enables the menu item in the CMS menu. diff --git a/_config.php b/_config.php index b3d9bbc..796429e 100644 --- a/_config.php +++ b/_config.php @@ -1 +1,6 @@ 'Varchar(255)', - "ContrastColour2" => 'Varchar(255)', - "ContrastColour3" => 'Varchar(255)', - "ContrastColour4" => 'Varchar(255)', - - "PaletteColour1" => 'Varchar(255)', - "PaletteColour2" => 'Varchar(255)', - "PaletteColour3" => 'Varchar(255)', - "PaletteColour4" => 'Varchar(255)', - "PaletteColour5" => 'Varchar(255)', - "PaletteColour6" => 'Varchar(255)', - - "BodyBackgroundColour" => 'Varchar(255)', - "BodyFontColour" => 'Varchar(255)', - "BodyLinkColour" => 'Varchar(255)', - "BodyLinkHoverColour" => 'Varchar(255)', - - "MenuBackgroundColour" => 'Varchar(255)', - "MenuFontColour" => 'Varchar(255)', - - "FontImportURLS" => "Text", - "HeadingFont" => "Varchar(255)", - "BodyFont" => "Varchar(255)", - "BaseFontSize" => "Int", - "MainHeadingFontSize" => "Int" - ); - - private static $has_one = array( - 'Logo' => 'Image', - 'Favicon' => 'Image' - ); - - private static $defaults = array( - "ContrastColour1" => '#333333', - "ContrastColour2" => '#29c407', - "ContrastColour3" => '#e7e7e7', - "ContrastColour4" => '#ffffff', - - "PaletteColour1" => '#600083', - "PaletteColour2" => '#ffffff', - "PaletteColour3" => '#29c407', - "PaletteColour4" => '#decafe', - "PaletteColour5" => '#e00a95', - "PaletteColour6" => '#077fc4' - ); - - public function getCMSFields() { - $this->beforeUpdateCMSFields(function ($fields) { - $fields->addFieldsToTab('Root.Palette', - LiteralField::create('ContrastColours', '

Constrast colours are usually grey scale, and include both light and dark variations

') - ); - - $fields->addFieldsToTab('Root.Palette', - ColorField::create('ContrastColour1', '1st Contrast Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('ContrastColour2', '2nd Contrast Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('ContrastColour3', '3rd Contrast Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('ContrastColour4', '4th Contrast Colour') - ); - - $fields->addFieldsToTab('Root.Palette', - LiteralField::create('PaletteColours', '

The palette defined here will be used to provide colour options on pages and objects throughout your site

') - ); - - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour1', '1st Brand Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour2', '2nd Brand Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour3', '3rd Brand Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour4', '4th Brand Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour5', '5th Brand Colour') - ); - $fields->addFieldsToTab('Root.Palette', - ColorField::create('PaletteColour6', '6th Brand Colour') - ); - - - - $fields->addFieldsToTab('Root.Colours', - LiteralField::create('BodyLit', '

Body

') - ); - - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('BodyBackgroundColour', 'Body background colour', $this->getFullPalette()) - ); - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('BodyFontColour', 'Body font colour', $this->getFullPalette()) - ); - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('BodyLinkColour', 'Link colour', $this->getFullPalette()) - ); - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('BodyLinkHoverColour', 'Link hover colour', $this->getFullPalette()) - ); - - $fields->addFieldsToTab('Root.Colours', - LiteralField::create('MenuLit', '

Menu

') - ); - - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('MenuBackgroundColour', 'Menu background colour', $this->getFullPalette()) - ); - $fields->addFieldsToTab('Root.Colours', - ColorPaletteField::create('MenuFontColour', 'Menu font colour', $this->getFullPalette()) - ); - - $fields->addFieldsToTab('Root.Fonts', $fontURLS = TextareaField::create('FontImportURLS')); - $fontURLS->setDescription('One font import statement per line. Example from google fonts: @import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,300,600|Pacifico);
Find more fonts here: https://www.google.com/fonts'); - - $fields->addFieldsToTab('Root.Fonts', $headingFont = TextField::create('HeadingFont')); - $headingFont->setDescription('e.g. Open Sans. Note: should be in the same case as the import url above'); - - $fields->addFieldsToTab('Root.Fonts', $headingFont = TextField::create('BodyFont')); - $headingFont->setDescription('e.g. Open Sans. Note: should be in the same case as the import url above'); - - $fields->addFieldsToTab('Root.Fonts', $baseFont = TextField::create('BaseFontSize')); - $baseFont->setDescription('Base Body Font size (in pixels)'); - - $fields->addFieldsToTab('Root.Fonts', $h1Font = TextField::create('MainHeadingFontSize')); - $h1Font->setDescription('Size of the page heading text (in pixels)'); - - $fields->addFieldToTab('Root.Logos', $logoField = new UploadField('Logo', 'Logo')); - $logoField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif', 'svg')); - $logoField->setConfig('allowedMaxFileNumber', 1); - $logoField->setFolderName('Uploads/brand'); - - $fields->addFieldToTab('Root.Logos', $favField = new UploadField('Favicon', 'Favicon')); - $favField->getValidator()->setAllowedExtensions(array('ico', 'png')); - $favField->setConfig('allowedMaxFileNumber', 1); - $favField->setFolderName('Uploads/brand'); - - $fields->removeByName('Main'); - }); - return parent::getCMSFields(); - } - - /** - * Returns the CMS defined contrasting colours palette. - * - * @return Array - */ - public function getContrastColours() { - return array( - "ContrastColour1" => $this->ContrastColour1, - "ContrastColour2" => $this->ContrastColour2, - "ContrastColour3" => $this->ContrastColour3, - "ContrastColour4" => $this->ContrastColour4 - ); - } - - /** - * Returns the CMS defined Brand colours palette. - * - * @return Array - */ - public function getPaletteColours() { - return array( - 'PaletteColour1' => $this->PaletteColour1, - 'PaletteColour2' => $this->PaletteColour2, - 'PaletteColour3' => $this->PaletteColour3, - 'PaletteColour4' => $this->PaletteColour4, - 'PaletteColour5' => $this->PaletteColour5, - 'PaletteColour6' => $this->PaletteColour6, - ); - } - - /** - * Returns the CMS defined colour palette. - * - * @return Array - */ - public function getFullPalette() { - return array( - "ContrastColour1" => $this->ContrastColour1, - "ContrastColour2" => $this->ContrastColour2, - "ContrastColour3" => $this->ContrastColour3, - "ContrastColour4" => $this->ContrastColour4, - 'PaletteColour1' => $this->PaletteColour1, - 'PaletteColour2' => $this->PaletteColour2, - 'PaletteColour3' => $this->PaletteColour3, - 'PaletteColour4' => $this->PaletteColour4, - 'PaletteColour5' => $this->PaletteColour5, - 'PaletteColour6' => $this->PaletteColour6 - ); - } - - /** - * Takes a Brand DB field name and returns the accociated Hex colour - * - * @param String | DB field name (eg 'PaletteColour1') - * @return String | Hex colour - */ - public function getHex($index) { - $palette = $this->getFullPalette(); - if(isset($palette[$index])) { - return $palette[$index]; - } else { - return $palette['PaletteColour1']; - } - } - - /** - * Converts a hex value into an array of rgb values (red, green, blue) - * Can be used to construct an rgba version of the colour - * And/Or do colour maths. - * - * @param $color Hex String - * @return Array - */ - public function getRGBArray($color) { - - $hex = ltrim($color, '#'); - $result = array(); - - if(strlen($hex) == 3) { - $result['r'] = hexdec(substr($hex, 0, 1)); - $result['g'] = hexdec(substr($hex, 1, 1)); - $result['b'] = hexdec(substr($hex, 2, 1)); - } - else if(strlen($hex) == 6) { - $result['r'] = hexdec(substr($hex, 0, 2)); - $result['g'] = hexdec(substr($hex, 2, 2)); - $result['b'] = hexdec(substr($hex, 4, 2)); - } - - return $result; - } - - /** - * Converts a hex value into an rgb value. Can be used in the template - * to crwate rgba colours. - * @param $color Hex String - * @return String RGB value (eg "26,35,255") - */ - public function getColorAsRGB($color) { - $result = $this->getRGBArray($color); - return implode($result, ','); - } - - /** - * Returns a brightness calculation (lower numbers are darker, higher is lighter) - * @param $color Hex String - * @return Int - */ - public function getBrightnessCalc($color) { - $rgb = $this->getRGBArray($color); - return (($rgb['r'] * 299) + ($rgb['g'] * 587) + ($rgb['b'] * 114)) / 1000; - } - - /** - * Returns a string of the current contrast of a colour (light or dark) - * Useful for calculating whether a text colour must be dark or light, when - * supplied with a background colour. If this outputs light, then we know the - * text must be a dark shade in order to get satisfactory contrast - * - * @param $color Hex String - * @return String - */ - public function getContrast($color) { - $brightness = $this->getBrightnessCalc($color); - - if ($brightness > 130) { - return 'light'; - } - return 'dark'; - } - - - public function canView($member = null) { - return Permission::check('BRAND_VIEW'); - } - - public function canEdit($member = null) { - return Permission::check('BRAND_EDIT'); - } - - public function canDelete($member = null) { - return Permission::check('BRAND_DELETE'); - } - - public function canCreate($member = null) { - if(Brand::get()) { - return false; - } - return Permission::check('BRAND_CREATE'); - } - - - /** - * Get an array of {@link Permission} definitions that this object supports - * - * @return array - */ - public function providePermissions() { - return array( - 'BRAND_VIEW' => array( - 'name' => 'View brand', - 'category' => 'Module', - ), - 'BRAND_EDIT' => array( - 'name' => 'Edit brand', - 'category' => 'Module', - ), - 'BRAND_DELETE' => array( - 'name' => 'Delete brand', - 'category' => 'Module', - ), - 'BRAND_CREATE' => array( - 'name' => 'Create brand', - 'category' => 'Module' - ) - ); - } - -} diff --git a/code/BrandAdmin.php b/code/BrandAdmin.php deleted file mode 100644 index 911fdea..0000000 --- a/code/BrandAdmin.php +++ /dev/null @@ -1,18 +0,0 @@ - 'Varchar(255)', - 'ContrastColour' => 'Varchar(255)', - ); - - private static $has_one = array( - 'SplashImage' => 'Image' - ); - - public function updateCMSFields(FieldList $fields) { - - $brand = $this->owner->getBrand(); - - if($brand) { - $fields->insertBefore( - ColorPaletteField::create( - "Colour", "Colour", $brand->getPaletteColours() - ), "Content" - ); - } - - $fields->insertBefore( - $contrast = ColorPaletteField::create( - "ContrastColour", "Contrast Colour", $brand->getContrastColours() - ), "Content" - ); - - $contrast->setDescription("Used for any text on 'Colour'"); - - $fields->insertAfter($image = UploadField::create('SplashImage', 'Splash Image'),'ContrastColour'); - - $image->setFolderName('Uploads/Splash-Images'); - } -} diff --git a/code/BrandSiteTreeExtension.php b/code/BrandSiteTreeExtension.php deleted file mode 100644 index cc4d366..0000000 --- a/code/BrandSiteTreeExtension.php +++ /dev/null @@ -1,13 +0,0 @@ -First(); - } -} diff --git a/composer.json b/composer.json index 8a491c8..2e1cec6 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,30 @@ { - "name": "adrexia/brand", - "description": "Branding and basic site theming admin for silverstripe", - "type": "silverstripe-module", - "keywords": ["silverstripe"], - "license": "BSD-3-Clause", - "authors": [ - { - "name": "Naomi Guyer", - "email": "adrexia@gmail.com" - } - ], - "require": + "name": "adrexia/brand", + "description": "Branding and basic site theming admin for silverstripe", + "type": "silverstripe-module", + "keywords": ["silverstripe"], + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Naomi Guyer", + "email": "adrexia@gmail.com" + } + ], + "require": + { + "silverstripe/framework": "^4.0", + "littlegiant/silverstripe-singleobjectadmin": "^2.0", + "ryanpotter/silverstripe-color-field": "dev-master", + "heyday/silverstripe-colorpalette": "dev-update-ss4" + }, + "repositories": [ { - "silverstripe/framework": "~3.3", - "silverstripe/cms": "~3.3", - "littlegiant/silverstripe-singleobjectadmin": "*", - "ryanpotter/silverstripe-color-field": "*", - "heyday/silverstripe-colorpalette": "dev-master" + "type": "vcs", + "url": "https://github.com/wernerkrauss/silverstripe-colorpalette.git" + }, + { + "type": "vcs", + "url": "https://github.com/adrexia/silverstripe-singleobjectadmin.git" } + ] } diff --git a/images/palette.png b/images/palette.png deleted file mode 100755 index b5ccd50ed4068204a69b13962323692ee4cc94b3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 329 zcmV-P0k-~$P)M#3fpsl;4KxB?fh*t#7{|shU;-R~B_Q}eN|2Cm!1ol` z#UoHDQ4aE!@Ufg_f&YLzu~YH#~CD2H3~KR8nb4K7R*1m!K!$Hso8H zUew|uR9Ln?xu>DXvSwkjpj1)`OHFY8qYae&Irexx0#?#c zkRYWZ$9em6VDjQGdo5}Q(ryfk#V^3BOKRH{dCLxF+TVG>`!20#L_<9 + + Brand + Artist palette + + + + + + + \ No newline at end of file diff --git a/src/Brand.php b/src/Brand.php new file mode 100644 index 0000000..e19a47b --- /dev/null +++ b/src/Brand.php @@ -0,0 +1,413 @@ + 'Varchar(255)', + "ContrastColour2" => 'Varchar(255)', + "ContrastColour3" => 'Varchar(255)', + "ContrastColour4" => 'Varchar(255)', + + "PaletteColour1" => 'Varchar(255)', + "PaletteColour2" => 'Varchar(255)', + "PaletteColour3" => 'Varchar(255)', + "PaletteColour4" => 'Varchar(255)', + "PaletteColour5" => 'Varchar(255)', + "PaletteColour6" => 'Varchar(255)', + + "BodyBackgroundColour" => 'Varchar(255)', + "BodyFontColour" => 'Varchar(255)', + "BodyLinkColour" => 'Varchar(255)', + "BodyLinkHoverColour" => 'Varchar(255)', + + "MenuBackgroundColour" => 'Varchar(255)', + "MenuFontColour" => 'Varchar(255)', + + "FontImportURLS" => "Text", + "HeadingFont" => "Varchar(255)", + "BodyFont" => "Varchar(255)", + "BaseFontSize" => "Int", + "MainHeadingFontSize" => "Int", + "FaviconTileColor" => 'Varchar(255)', + ]; + + private static $has_one = [ + 'Logo' => File::class, + 'FaviconICO' => Image::class, + 'FaviconPNG' => Image::class, + ]; + + private static $defaults = [ + "ContrastColour1" => '#111111', + "ContrastColour2" => '#333333', + "ContrastColour3" => '#e7e7e7', + "ContrastColour4" => '#ffffff', + + "PaletteColour1" => '#600083', + "PaletteColour2" => '#ffffff', + "PaletteColour3" => '#29c407', + "PaletteColour4" => '#decafe', + "PaletteColour5" => '#e00a95', + "PaletteColour6" => '#077fc4' + ]; + + private static $edit_constrast_options = true; + private static $edit_palette_options = true; + private static $edit_bodycolors = true; + private static $edit_menucolors = true; + private static $edit_fonts = true; + private static $edit_images = true; + + public function getCMSFields() { + $this->beforeUpdateCMSFields(function (FieldList $fields) { + + $this->addPalette($fields); + $this->addContrast($fields); + + $fields->addFieldToTab('Root.Colours', + LiteralField::create('ColoursLit', '

Globally applied colour schemes

') + ); + $this->addBodyColours($fields); + $this->addMenuColours($fields); + $this->addFonts($fields); + $this->addImages($fields); + + $fields->removeByName('Main'); + }); + return parent::getCMSFields(); + } + + /** + * @param FieldList $fields + */ + public function addContrast(FieldList $fields) { + if(!self::config()->edit_constrast_options) { + return; + } + $fields->addFieldsToTab('Root.Palette', [ + LiteralField::create('ContrastColours', '

Constrast colours are usually grey scale, and include both light and dark variations

'), + ColorField::create('ContrastColour1', '1st Contrast Colour'), + ColorField::create('ContrastColour2', '2nd Contrast Colour'), + ColorField::create('ContrastColour3', '3rd Contrast Colour'), + ColorField::create('ContrastColour4', '4th Contrast Colour') + ]); + } + + /** + * @param FieldList $fields + */ + public function addPalette(FieldList $fields) { + if(!self::config()->edit_palette_options) { + return; + } + + $fields->addFieldsToTab('Root.Palette', [ + LiteralField::create('PaletteColours', '

The palette defined here will be used to provide colour options on pages and objects throughout your site

'), + ColorField::create('PaletteColour1', '1st Brand Colour'), + ColorField::create('PaletteColour2', '2nd Brand Colour'), + ColorField::create('PaletteColour3', '3rd Brand Colour'), + ColorField::create('PaletteColour4', '4th Brand Colour'), + ColorField::create('PaletteColour5', '5th Brand Colour'), + ColorField::create('PaletteColour6', '6th Brand Colour') + ]); + } + + /** + * @param FieldList $fields + */ + public function addImages(FieldList $fields) { + if(!self::config()->edit_images) { + return; + } + $fields->addFieldsToTab('Root.Images', [ + $logoField = new UploadField('Logo', 'Logo'), + $favField = new UploadField('FaviconICO', 'Favicon Ico'), + $favPNGField = new UploadField('FaviconPNG', 'Favicon PNG'), + ColorPaletteField::create('FaviconTileColor', 'Favicon Tile Color (used by windows)', $this->getFullPalette()) + ]); + + $logoField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif', 'svg')); + $logoField->setFolderName('Uploads/brand'); + + $favField->getValidator()->setAllowedExtensions(array('ico')); + $favField->setFolderName('Uploads/brand'); + + $favPNGField->getValidator()->setAllowedExtensions(array('png')); + $favPNGField->setFolderName('Uploads/brand'); + $favPNGField->setRightTitle('Minimum size: 192x192, png'); + } + + /** + * @param FieldList $fields + */ + public function addFonts(FieldList $fields) { + if(!self::config()->edit_fonts) { + return; + } + + $fields->addFieldsToTab('Root.Fonts', [ + $fontURLS = TextareaField::create('FontImportURLS'), + $headingFont = TextField::create('HeadingFont'), + $bodyFont = TextField::create('BodyFont'), + $baseFont = TextField::create('BaseFontSize'), + $h1Font = TextField::create('MainHeadingFontSize') + ]); + + $fontURLS->setDescription('One font import statement per line. Example from google fonts: @import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,300,600|Pacifico);
Find more fonts here: https://www.google.com/fonts'); + $headingFont->setDescription('e.g. Open Sans. Note: should be in the same case as the import url above'); + $bodyFont->setDescription('e.g. Open Sans. Note: should be in the same case as the import url above'); + $baseFont->setDescription('Base Body Font size (in pixels)'); + $h1Font->setDescription('Size of the page heading text (in pixels)'); + } + + /** + * @param FieldList $fields + */ + public function addBodyColours(FieldList $fields) { + if(!self::config()->edit_bodycolors) { + return; + } + $fields->addFieldsToTab('Root.Colours', [ + LiteralField::create('BodyLit', '

Body

'), + ColorPaletteField::create('BodyBackgroundColour', 'Body background colour', $this->getFullPalette()), + ColorPaletteField::create('BodyFontColour', 'Body font colour', $this->getFullPalette()), + ColorPaletteField::create('BodyLinkColour', 'Link colour', $this->getFullPalette()), + ColorPaletteField::create('BodyLinkHoverColour', 'Link hover colour', $this->getFullPalette()) + ]); + } + + /** + * @param FieldList $fields + */ + public function addMenuColours(FieldList $fields) { + if(!self::config()->edit_menucolors) { + return; + } + $fields->addFieldsToTab('Root.Colours', [ + LiteralField::create('MenuLit', '

Menu

'), + ColorPaletteField::create('MenuBackgroundColour', 'Menu background colour', $this->getFullPalette()), + ColorPaletteField::create('MenuFontColour', 'Menu font colour', $this->getFullPalette()) + ]); + } + + /** + * Publish our dependent objects. + */ + public function onAfterWrite() { + + if($this->Logo() && $this->Logo()->exists()) { + $this->Logo()->publishSingle(); + } + if($this->FaviconICO() && $this->FaviconICO()->exists()) { + $this->FaviconICO()->publishSingle(); + } + if($this->FaviconPNG() && $this->FaviconPNG()->exists()) { + $this->FaviconPNG()->publishSingle(); + } + parent::onAfterWrite(); + } + + /** + * Returns the CMS defined contrasting colours palette. + * + * @return array + */ + public function getContrastColours() { + return [ + "ContrastColour1" => $this->ContrastColour1, + "ContrastColour2" => $this->ContrastColour2, + "ContrastColour3" => $this->ContrastColour3, + "ContrastColour4" => $this->ContrastColour4 + ]; + } + + /** + * Returns the CMS defined Brand colours palette. + * + * @return array + */ + public function getPaletteColours() { + return [ + 'PaletteColour1' => $this->PaletteColour1, + 'PaletteColour2' => $this->PaletteColour2, + 'PaletteColour3' => $this->PaletteColour3, + 'PaletteColour4' => $this->PaletteColour4, + 'PaletteColour5' => $this->PaletteColour5, + 'PaletteColour6' => $this->PaletteColour6, + ]; + } + + /** + * Returns the CMS defined colour palette. + * + * @return array + */ + public function getFullPalette() { + return [ + 'PaletteColour1' => $this->PaletteColour1, + 'PaletteColour2' => $this->PaletteColour2, + 'PaletteColour3' => $this->PaletteColour3, + 'PaletteColour4' => $this->PaletteColour4, + 'PaletteColour5' => $this->PaletteColour5, + 'PaletteColour6' => $this->PaletteColour6, + "ContrastColour1" => $this->ContrastColour1, + "ContrastColour2" => $this->ContrastColour2, + "ContrastColour3" => $this->ContrastColour3, + "ContrastColour4" => $this->ContrastColour4 + ]; + } + + /** + * Takes a Brand DB field name and returns the associated Hex colour + * + * @param String $index - DB field name (eg 'PaletteColour1') + * @return String - Hex colour + */ + public function getHex($index) { + $palette = $this->getFullPalette(); + if(isset($palette[$index])) { + return $palette[$index]; + } else { + return $palette['PaletteColour1']; + } + } + + /** + * Converts a hex value into an array of rgb values (red, green, blue) + * Can be used to construct an rgba version of the colour + * And/Or do colour maths. + * + * @param $color - Hex String + * @return array + */ + public function getRGBArray($color) { + + $hex = ltrim($color, '#'); + $result = []; + + if(strlen($hex) == 3) { + $result['r'] = hexdec(substr($hex, 0, 1)); + $result['g'] = hexdec(substr($hex, 1, 1)); + $result['b'] = hexdec(substr($hex, 2, 1)); + } + else if(strlen($hex) == 6) { + $result['r'] = hexdec(substr($hex, 0, 2)); + $result['g'] = hexdec(substr($hex, 2, 2)); + $result['b'] = hexdec(substr($hex, 4, 2)); + } + + return $result; + } + + /** + * Converts a hex value into an rgb value. Can be used in the template + * to crwate rgba colours. + * @param String $color - Hex String + * @return String RGB value (eg "26,35,255") + */ + public function getColorAsRGB($color) { + $result = $this->getRGBArray($color); + return implode($result, ','); + } + + /** + * Returns a brightness calculation (lower numbers are darker, higher is lighter) + * @param String $color - Hex String + * @return Int + */ + public function getBrightnessCalc($color) { + $rgb = $this->getRGBArray($color); + return (($rgb['r'] * 299) + ($rgb['g'] * 587) + ($rgb['b'] * 114)) / 1000; + } + + /** + * Returns a string of the current contrast of a colour (light or dark) + * Useful for calculating whether a text colour must be dark or light, when + * supplied with a background colour. If this outputs light, then we know the + * text must be a dark shade in order to get satisfactory contrast + * + * @param String $color - Hex String + * @return String + */ + public function getContrast($color) { + $brightness = $this->getBrightnessCalc($color); + + if ($brightness > 130) { + return 'light'; + } + return 'dark'; + } + + public function canView($member = NULL, $context = []) { + return Permission::check('BRAND_VIEW'); + } + + public function canEdit($member = NULL, $context = []) { + return Permission::check('BRAND_EDIT'); + } + + public function canDelete($member = NULL, $context = []) { + return Permission::check('BRAND_DELETE'); + } + + /** + * Limit to one brand per (sub)site + * @param null $member + * @param array $context + * @return bool|int + */ + public function canCreate($member = NULL, $context = []) { + if(Brand::get()) { + return false; + } + return Permission::check('BRAND_CREATE'); + } + + + /** + * Get an array of {@link Permission} definitions that this object supports + * + * @return array + */ + public function providePermissions() { + return array( + 'BRAND_VIEW' => array( + 'name' => 'View brand', + 'category' => 'Module', + ), + 'BRAND_EDIT' => array( + 'name' => 'Edit brand', + 'category' => 'Module', + ), + 'BRAND_DELETE' => array( + 'name' => 'Delete brand', + 'category' => 'Module', + ), + 'BRAND_CREATE' => array( + 'name' => 'Create brand', + 'category' => 'Module' + ) + ); + } + +} diff --git a/src/BrandAdmin.php b/src/BrandAdmin.php new file mode 100644 index 0000000..caa18c8 --- /dev/null +++ b/src/BrandAdmin.php @@ -0,0 +1,21 @@ + 'Varchar(255)', + 'ContrastColour' => 'Varchar(255)', + ); + + private static $has_one = array( + 'SplashImage' => Image::class + ); + + /** + * @param FieldList $fields + */ + public function updateCMSFields(FieldList $fields) { + + $brand = $this->owner->getBrand(); + + if($brand) { + $fields->insertBefore( + ColorPaletteField::create( + "Colour", "Colour", $brand->getPaletteColours() + ), "Content" + ); + + $fields->insertBefore( + $contrast = ColorPaletteField::create( + "ContrastColour", "Contrast Colour", $brand->getContrastColours() + ), "Content" + ); + + $contrast->setDescription("Used for any text on 'Colour'"); + } + + $fields->insertAfter($image = UploadField::create('SplashImage', 'Splash Image'),'ContrastColour'); + $image->setFolderName('Uploads/Splash-Images'); + } +} diff --git a/src/BrandSiteTreeExtension.php b/src/BrandSiteTreeExtension.php new file mode 100644 index 0000000..7d33877 --- /dev/null +++ b/src/BrandSiteTreeExtension.php @@ -0,0 +1,18 @@ +First(); + } +} diff --git a/templates/Adrexia/Brand/Includes/Brand.ss b/templates/Adrexia/Brand/Includes/Brand.ss new file mode 100644 index 0000000..2f74e68 --- /dev/null +++ b/templates/Adrexia/Brand/Includes/Brand.ss @@ -0,0 +1,65 @@ +<% include Adrexia/Brand/Favicons %> + +<% with $Brand %> + <% cached 'brand', $LastEdited %> + + <% end_cached %> +<% end_with %> diff --git a/templates/Adrexia/Brand/Includes/Favicons.ss b/templates/Adrexia/Brand/Includes/Favicons.ss new file mode 100644 index 0000000..3e9e706 --- /dev/null +++ b/templates/Adrexia/Brand/Includes/Favicons.ss @@ -0,0 +1,25 @@ +<% with $Brand %> + <% cached 'favicons', $LastEdited %> + <% if $FaviconICO && $FaviconICO.exists() %> + + <% end_if %> + <% if $FaviconPNG && $FaviconPNG.exists() %> + + + + + + + + + + + + + + + + + <% end_if %> + <% end_cached %> +<% end_with %> \ No newline at end of file diff --git a/templates/Brand.ss b/templates/Brand.ss deleted file mode 100644 index 93f336c..0000000 --- a/templates/Brand.ss +++ /dev/null @@ -1,70 +0,0 @@ -<% with $Brand %> - <% if $Favicon %> - - <% end_if %> - - - -<% end_with %>