Skip to content

Commit

Permalink
Change themes in the settings from the admin without touch the themes…
Browse files Browse the repository at this point in the history
….yml file
  • Loading branch information
AlexStack committed Jul 16, 2019
1 parent e395b5c commit 3f45d3d
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
@@ -1,9 +1,10 @@
# SilverStripe All-in-one

- SilverStripe All-in-one for start a new website. You can build a basic website within 30 minutes after installed.
- Bootstrap themes included
- Bootstrap 4 themes included
- Contact Us Form included
- Custom Carousel Slider included
- Change themes in the settings from the admin without touch the themes.yml file
- Easy to custom page top logo, copyright content at the bottom of every page, page content above nav bar, reserved fields for extension, custom css file

# How to install
Expand Down
95 changes: 88 additions & 7 deletions code/extentions/CustomALLinOneConfig.php
Expand Up @@ -11,33 +11,114 @@
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\ORM\DataExtension;
use Symfony\Component\Yaml\Yaml;

class CustomALLinOneConfig extends DataExtension
{

private static $db = [
'PageFooter' => 'Text',
'PageTop' => 'Text',
'CustomCssFile' => 'Varchar'
'PageFooter' => 'HTMLText',
'PageTop' => 'HTMLText',
'CustomCssFile' => 'Varchar',
'CustomThemeName' => 'Varchar'
];

private static $has_one = [
'TopLogo' => Image::class
];
private static $owns = ['TopLogo'];


private static $defaults = [
'PageFooter' => 'PageFooter in settings @ PageFooter 2030',
'PageFooter' => '<p class="text-center">&nbsp;</p><p class="text-center">PageFooter in settings @ PageFooter 2030</p><p class="text-center">&nbsp;</p>',
'PageTop' => '',
'CustomCssFile' => 'custom.css'
'CustomCssFile' => 'custom.css',
'CustomThemeName' => 'silverstripe-custom-bootstrap4-theme'
];

public function updateCMSFields(FieldList $fields)
{
$themeList = array_map('basename', glob(BASE_PATH . "/themes/*", GLOB_ONLYDIR));
foreach ($themeList as $k=>$v) {
$newThemeList[$v] = $v;
}

$fields->addFieldsToTab("Root.Main", [

DropdownField::create('CustomThemeName', 'Choose a Theme', $newThemeList)
->setDescription('Save it then you can <a href="/?flush=1" target="_blank">click here to see the changes in the homepage</a>'),


UploadField::create('TopLogo', 'Logo display at the top navbar'),
HtmlEditorField::create('PageFooter', 'Bottom content of every page'),
HtmlEditorField::create('PageTop', 'Content above navbar'),
TextField::create('CustomCssFile', 'Custom CSS File'),
]);
TextField::create('CustomCssFile', 'Custom CSS File')
->setDescription('Please do not input .css, eg. custom.css, just input custom. main.css => main , xxx.css => xxx<br/>And this xxx.css should be located at your-ss-project/public/_resource/themes/your-theme/css/xxx.css')
]);
}

protected function changeTheme()
{
if ( isset($_POST['CustomThemeName']) && trim($_POST['CustomThemeName'])!='' ){

$themeFile = BASE_PATH . '/app/_config/theme.yml';
if ( !file_exists($themeFile) ){
$themeFile = BASE_PATH . '/mysite/_config/theme.yml';
if ( !file_exists($themeFile) ){
echo '<h2>Please make sure ' . BASE_PATH . '/app/_config/theme.yml exists</h2>';
}
}
$themeStr = file_get_contents($themeFile);

// Symfony\Component\Yaml\Yaml not support ---
// Replace it to something else first
$themeStr = preg_replace_callback(
"/\-\-\-/",
function($m) {
static $id = 0;
$id++;
return 'ThreeDashes' . $id.': dashes';
},
$themeStr);


$themeData = Yaml::parse($themeStr);

$needUpdateThemeYml = false;

// Update the first theme not contain $
for($i=0; $i< count($themeData['SilverStripe\View\SSViewer']['themes']); $i++ )
if ( strpos($themeData['SilverStripe\View\SSViewer']['themes'][$i],'$') === false && $themeData['SilverStripe\View\SSViewer']['themes'][$i] != trim($_POST['CustomThemeName']) ){
$themeData['SilverStripe\View\SSViewer']['themes'][$i] = trim($_POST['CustomThemeName']);
$needUpdateThemeYml = true;
break;
}

if ( $needUpdateThemeYml ){
$newYaml = Yaml::dump($themeData,5,2);

$newYaml = preg_replace("/ThreeDashes(\d): dashes/", "---", $newYaml);

file_put_contents($themeFile, $newYaml);

// refresh the theme for homepage
echo '<img src="/?flush=1" width=0 height=0 />';
} else {
echo '<!-- No needUpdateThemeYml -->';
}

} else {
echo '<!-- CustomThemeName not set -->';
}

}

public function onAfterWrite()
{
$this->changeTheme("silverstripe-custom-bootstrap4-theme");

// CAUTION: You are required to call the parent-function, otherwise
// SilverStripe will not execute the request.
parent::onAfterWrite();
}
}
7 changes: 1 addition & 6 deletions docs/en/using.md
@@ -1,7 +1,2 @@
# How to use it
- Create a new page in SilverStripe admin and choose page type "Custom Page with Contact Us Form"
- Choose custom page layout or use your own template xxx.ss
- Change Form Settings
- Set up Google Recaptcha
- Save & publish the page
- Details: https://github.com/AlexStack/silverstripe-custom-all-in-one/blob/master/README.md
- Details: https://github.com/AlexStack/silverstripe-all-in-one/blob/master/README.md
Binary file removed docs/images/choose-page-type.png
Binary file not shown.
Binary file removed docs/images/contact-us-form-settings.png
Binary file not shown.
Binary file removed docs/images/custom-my-own-template-file.png
Binary file not shown.
Binary file removed docs/images/extra-image-1.png
Binary file not shown.
Binary file removed docs/images/extra-image-2.png
Binary file not shown.
Binary file removed docs/images/extra-image-3.png
Binary file not shown.
Binary file removed docs/images/global-settings.png
Binary file not shown.
Binary file removed docs/images/override-form-ss.png
Binary file not shown.
Binary file removed docs/images/page-layout-001.png
Binary file not shown.
Binary file removed docs/images/page-layout-00101.png
Binary file not shown.
Binary file removed docs/images/page-layout-002.png
Binary file not shown.
Binary file removed docs/images/page-layout-003.png
Binary file not shown.
Binary file removed docs/images/page-layout-004.png
Binary file not shown.
Binary file removed docs/images/page-layout-005.png
Binary file not shown.
Binary file removed docs/images/select-page-layout.png
Binary file not shown.
Binary file removed docs/images/set-up-google-recaptcha-v3.png
Binary file not shown.
Binary file removed docs/images/set-up-google-recaptcha.png
Binary file not shown.
Binary file removed docs/images/view-contact-us-data.png
Binary file not shown.

0 comments on commit 3f45d3d

Please sign in to comment.