Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Nov 15, 2016
0 parents commit 8483eb2
Show file tree
Hide file tree
Showing 35 changed files with 2,455 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/docs export-ignore
3 changes: 3 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<FilesMatch "\.(php|php3|php4|php5|phtml|inc)$">
Deny from all
</FilesMatch>
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) 2016, Zauberfisch - www.zauberfisch.at
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# SilverStripe PageBuilder module

SilverStripe module to allow for modular, block based, content in SilverStripe

**WARNING: this module is still work in progress and may not be suitable for production yet.**

**Major API changes are more than likely**

## Maintainer Contact

* Zauberfisch <[email protected]>

## Requirements

* silverstripe/framework >=3.5
* zauberfisch/silverstripe-serialized-dataobject >=1.0

## Installation

* `composer require "zauberfisch/silverstripe-page-builder"`
* Optional: install suggested packages

composer require "zauberfisch/silverstripe-page-builder-basic-blocks"
* rebuild manifest (flush)
* Unfortunately a bug in SilverStripe current breaks the HTMLEditorField file management
inside a sub controller within the CMS, to work around this bug the following work around is required:

In your project `.htaccess` replace `RewriteRule .* framework/main.php?url=%1 [QSA]` with
`RewriteRule .* index.php?url=%1 [QSA]` and create an `index.php` file (you can replace the default
SilverStripe file if your server supports mod_rewrite) with the following content:

<?php

if (isset($_GET['ID'])) {
# redirect as workaround for inset File dialog in the PageBuilder
# which is broken because ?ID=<$fileID> is mistaken for a Page ID by LeftAndMain
# lets replace ID with FileID which is then converted back in PageBuilder
$match = true;
$url = $_GET['url'];
foreach(['PageBuilder', 'EditorToolbar', 'viewfile'] as $str) {
if (strpos($url, $str) === FALSE) {
$match = false;
break;
}
}
if ($match) {
$id = $_GET['ID'];
$_GET['url'] = str_replace(['?ID=','&ID='],['?FileID=','&FileID='], $url);
$_REQUEST['FileID'] = $id;
$_GET['FileID'] = $id;
unset($_REQUEST['ID']);
unset($_GET['ID']);
}
}

chdir('framework');
require 'framework/main.php';

## Documentation

Add the PageBuilder DB Field and FormField to any DataObject, for example Page:

class Page extends SiteTree {
private static $db = [
'PageBuilder' => 'PageBuilder_DBField',
];
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', [
new PageBuilder_Field('PageBuilder', $this->fieldLabel('PageBuilder')),
]);
return $fields;
}
}

Now, in your template, you can access $PageBuilder and output the content:

<% if $PageBuilder %>
<div class="my-content-blocks">
$PageBuilder.Value
</div>
<% end_if %>

See [docs/example.md](docs/example.md) for example project files with some basic frontend.

However, in the bare installation this module does not provide any content types.
It just provides the abstract classes and form fields.
Just like with SilverStripe DataObjects it is up to the individual developer and
other modules to define the desired data types.
A good starting point with some basic block types can be found in the
[basic blocks module](https://packagist.org/packages/zauberfisch/silverstripe-page-builder-basic-blocks).

Documentation for creating custom blocks has not been written yet, please refer
to the [basic blocks module](https://packagist.org/packages/zauberfisch/silverstripe-page-builder-basic-blocks),
they should serve as good examples.
4 changes: 4 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

define('PAGE_BUILDER_DIR', basename(__DIR__));
define('PAGE_BUILDER_PATH', BASE_PATH . '/' . PAGE_BUILDER_DIR);
7 changes: 7 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Name: PageBuilder
After: 'framework/*','cms/*'
---
LeftAndMain:
extensions:
- 'PageBuilder_LeftAndMainExtension'
29 changes: 29 additions & 0 deletions code/HtmlEditorField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* no underscore because it would break routing
*/
class PageBuilderHtmlEditorField_Toolbar extends HtmlEditorField_Toolbar {
private static $allowed_actions = ['viewfile'];

/**
* @param SS_HTTPRequest $request
* @return HTMLText
* @throws SS_HTTPResponse_Exception
*/
public function viewfile($request) {
$data = [
$request->getVars(),
$request->postVars(),
];
foreach($data as &$arr) {
if (isset($arr['FileID'])) {
$arr['ID'] = $arr['FileID'];
unset($arr['FileID']);
}
}
$request = new SS_HTTPRequest($request->httpMethod(), $request->getURL(), $data[0], $data[1], $request->getBody());
return parent::viewfile($request);
}

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

/**
* Helper class to render templates with ->setAttribute() values
* @author zauberfisch
*/
class PageBuilder_CompositeField extends CompositeField {
}
7 changes: 7 additions & 0 deletions code/PageBuilder_DBField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

class PageBuilder_DBField extends SerializedDBFieldHasOne {
public function nullValue() {
return new PageBuilder_Value_Block_BlockGroup_Base();
}
}
187 changes: 187 additions & 0 deletions code/PageBuilder_Field/PageBuilder_Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

/**
* @author zauberfisch
*/
class PageBuilder_Field extends FormField {
public function __construct($name, $title = null, $value = null) {
parent::__construct($name, $title, $value);
$this->addExtraClass('stacked');
}

public function setValue($val) {
if (is_a($val, 'PageBuilder_DBField')) {
$this->value = $val;
} else {
$this->value = new PageBuilder_DBField();
$this->value->setValue('', null, true);
if ($val) {
// value is an array after form submission, lets turn it into an object
if (is_array($val)) {
$this->value->setValue($this->createValueFromArray($val));
} elseif (is_string($val)) {
$this->value->setValue($val);
} else {
throw new Exception('unexpected value');
}
}
}
return $this;
}

public function Value() {
$return = parent::Value();
if (!$return) {
$this->setValue(null);
$return = parent::Value();
}
return $return;
}

public function createValueFromArray($array) {
$baseUID = '';
$base = null;
$blocksByParent = [];
foreach ($array as $uid => $blockArray) {
if (isset($blockArray['ClassName'])) {
if (!in_array($blockArray['ClassName'], ClassInfo::subclassesFor('PageBuilder_Value_Block'))) {
throw new Exception(sprintf('Invalid class name "%s"', $blockArray['ClassName']));
}
/** @var PageBuilder_Value_Block $block */
$block = new $blockArray['ClassName']();
$block->update($blockArray);
//$blocks[$uid] = $block;
$parentUID = $blockArray['BlockParent'];
if (!$parentUID) {
$baseUID = $uid;
$base = $block;
} else {
if (!isset($blocksByParent[$parentUID])) {
$blocksByParent[$parentUID] = [];
}
$blocksByParent[$parentUID][$blockArray['BlockPosition']] = [
'UID' => $uid,
'Block' => $block,
];
}
}
}
if (!$base) {
throw new Exception('PageBuilder is missing the base block');
}
/**
* @param PageBuilder_Value_Block_BlockGroup $parent
* @param array $blocks
* @return PageBuilder_Value_Block_BlockGroup
*/
$nestBlock = function ($parent, $blocks) use ($blocksByParent, &$nestBlock) {
ksort($blocks);
$_blocks = [];
foreach ($blocks as $childBlock) {
//$childBlock['Block']->setMaxWidthDesktop($parent->getWidthDesktop());
//$childBlock['Block']->setMaxWidthTablet($parent->getWidthTablet());
$childBlock['Block']->setWidthDesktop(min(
$childBlock['Block']->getWidthDesktop(),
$parent->getWidthDesktop()
));
$childBlock['Block']->setWidthTablet(min(
$childBlock['Block']->getWidthTablet(),
$parent->getWidthTablet()
));
if (isset($blocksByParent[$childBlock['UID']])) {
$_blocks[] = $nestBlock($childBlock['Block'], $blocksByParent[$childBlock['UID']]);
} else {
$_blocks[] = $childBlock['Block'];
}
}
$parent->setBlocks(new SerializedDataList($_blocks));
return $parent;
};
if (isset($blocksByParent[$baseUID])) {
$base = $nestBlock($base, $blocksByParent[$baseUID]);
}
return $base;
}

public function getAttributes() {
return $this->attributes;
}

public function FieldHolder($properties = []) {
$this->addExtraClass('PageBuilder_Field');
$this->setAttribute('data-name', $this->getName());
$this->setAttribute('data-grid-mode', 'desktop');
$this->setAttribute('data-config', json_encode([
'urls' => [
'add' => $this->Link('handleBlock/add'),
'edit-content-element' => $this->Link('handleContentElement'),
//'group' => [
// 'create' => $this->Link('handleGroup/createNew'),
// 'createFromTemplate' => $this->Link('handleGroup/createFromTemplate'),
////'createFromTemplate' => $this->Link('newFromTemplate'),
//],
//'block' => [
// 'create' => $this->Link('handleBlock'),
// 'edit' => $this->Link('handleBlock/edit'),
//],
],
]));
return parent::FieldHolder($properties);
}

public function Field($properties = []) {
$val = $this->Value();
if ($val && is_a($val->getValue(), 'PageBuilder_Value_Block_BlockGroup_Base')) {
/** @var PageBuilder_Value_Block_BlockGroup_Base $baseBlock */
$baseBlock = $val->getValue();
return $baseBlock->getPageBuilderFields($this->getName());
//foreach ($val->getValue() as $i => $group) {
// /** @var PageBuilder_Value_BlockGroup_Grid $group */
// $groupsFields[] = $group->getPageBuilderGridFields($this->getName(), $i);
//}
}
throw new Exception('PageBuilder_Field does not have a base block');
//return new LiteralField($this->getName(), '');

//return (new PageBuilder_CompositeField([
// (new PageBuilder_CompositeField($groupsFields))
// ->addExtraClass('PageBuilder_Field-BlockGroups'),
// (new FormAction(sprintf('%s[groups][%s]', $this->getName(), 'add'), _t('PageBuilder_Field.BlockGroup-Add', 'add new block group')))
// ->setUseButtonTag(true)
// ->addExtraClass('PageBuilder_Field-BlockGroup-Add')
// ->addExtraClass('font-icon-plus'),
//]))
// ->addExtraClass('PageBuilder_Field')
// ->addExtraClass('PageBuilder_Field_HasMany')
// ->setAttribute('data-config', json_encode([
// 'urls' => [
// 'group' => [
// 'create' => $this->Link('handleGroup/createNew'),
// 'createFromTemplate' => $this->Link('handleGroup/createFromTemplate'),
// //'createFromTemplate' => $this->Link('newFromTemplate'),
// ],
// 'block' => [
// 'create' => $this->Link('handleBlock'),
// 'edit' => $this->Link('handleBlock/edit'),
// ],
// ],
// ]));
}

public function saveInto(DataObjectInterface $record) {
$record->{$this->name} = $this->Value();
}

private static $allowed_actions = [
'handleBlock',
'handleContentElement',
];

public function handleBlock(SS_HTTPRequest $r) {
return new PageBuilder_Field_Handler_Block($this);
}

public function handleContentElement(SS_HTTPRequest $r) {
return new PageBuilder_Field_Handler_ContentElement($this);
}
}
Loading

0 comments on commit 8483eb2

Please sign in to comment.