Skip to content

Commit

Permalink
update content blocks to many_many
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Mc Cormack committed Dec 3, 2017
1 parent a921ae8 commit f4b4137
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 394 deletions.
5 changes: 1 addition & 4 deletions _config/blockpage.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
Name: cyber-duck block page config
---
CyberDuck\BlockPage\Model\ContentBlock:
extensions:
- SilverStripe\Versioned\Versioned
---
4 changes: 3 additions & 1 deletion _config/cms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ SilverStripe\Admin\LeftAndMain:
extra_requirements_css:
- resources/cyber-duck/silverstripe-block-page/assets/css/block-page.css
extra_requirements_javascript:
- resources/cyber-duck/silverstripe-block-page/assets/js/block-page.js
- resources/cyber-duck/silverstripe-block-page/assets/js/block-page.js
admin_themes:
- 'silverstripe-block-page'
4 changes: 4 additions & 0 deletions assets/scss/block-page.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#Form_ItemEditForm_BlockType {
max-width: 1200px;
}

#Form_ItemEditForm_BlockType_Holder {
.form__field-holder {
flex: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,79 @@

namespace CyberDuck\BlockPage\Action;

use Page;
use CyberDuck\BlockPage\Model\ContentBlock;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\VersionedGridFieldItemRequest;

class GridFieldVersionedContentBlockItemRequest extends VersionedGridFieldItemRequest
{
private static $allowed_actions = [
'doSelect',
'doSelection',
'ItemEditForm'
];

public function ItemEditForm()
{
$form = parent::ItemEditForm();
$actions = $form->Actions();

if($this->record->getAction() == 'new') {
// remove all actions and add the create action
foreach($actions as $action) {
$actions->remove($action);
}
$button = FormAction::create('doSelect');
$button->setTitle('Create')
->setUseButtonTag(true)
->setAttribute('data-icon', 'accept')
->addExtraClass('btn action btn-primary font-icon-plus');
$actions->unshift($button);
} else {
$button = FormAction::create('doSelection');
$button->setTitle('New Block')
->setAttribute('data-icon', 'accept');
$actions->unshift($button);
// set fields
$fields = $this->record->getCMSFields();
$fields->setForm($form);

$form->fields()->removeByName('Root');
$form->fields()->push(TabSet::create('Root', Tab::create('Main')));
$form->fields()->addFieldsToTab('Root.Main', $fields);

$form->loadDataFrom($this->record);
}
return $form;
}

public function doSelect()
{
$request = Controller::curr()->getRequest();

$class = $request->postVar('ContentBlock');

$block = $class::create();
$block->write();

$page = DataObject::get_by_id(Page::class, $request->postVar('PageID'));
$page->ContentBlocks()->add($block);

return Controller::curr()->redirect(Controller::join_links($this->gridField->Link('item'), $block->ID, 'edit'));
}

public function doSelection($data, Form $form)
{
return Controller::curr()->redirect(Controller::join_links($this->gridField->Link('item'), 'new'));
}
}
/*
use SilverStripe\Control\Controller;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
Expand All @@ -10,39 +83,11 @@
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\Versioned;
/**
* CreateBlock_ItemRequest
*
* Request handler for creating new blocks in the CMS
*
* @package silverstripe-block-page
* @license MIT License https://github.com/cyber-duck/silverstripe-block-page/blob/master/LICENSE
* @author <[email protected]>
**/
class CreateBlock_ItemRequest extends GridFieldDetailForm_ItemRequest
{
/**
* Allowe CMS block request actions
*
* @since version 1.0.0
*
* @config array $allowed_actions
**/
private static $allowed_actions = [
'ItemEditForm',
'doCreate',
'doPublish',
'doUnpublish',
'doArchive'
'ItemEditForm'
];

/**
* CMS record form method
*
* @since version 1.0.0
*
* @return object
**/
public function ItemEditForm()
{
$form = parent::ItemEditForm();
Expand Down Expand Up @@ -131,17 +176,6 @@ public function ItemEditForm()
}
return $form;
}

/**
* Handles the block create request and redirects to the new record
*
* @since version 1.0.0
*
* @param array $data
* @param object $form
*
* @return void
**/
public function doCreate($data, Form $form)
{
$request = Controller::curr()->getRequest();
Expand All @@ -156,16 +190,6 @@ public function doCreate($data, Form $form)
return Controller::curr()->redirect(Controller::join_links($this->gridField->Link('item'), $block->ID, 'edit'));
}
/**
* Handles the publish action
*
* @since version 4.0.0
*
* @param array $data
* @param object $form
*
* @return void
**/
public function doPublish($data, Form $form)
{
$record = $this->getRecord();
Expand All @@ -182,16 +206,6 @@ public function doPublish($data, Form $form)
return $this->redirectAfterSave(false);
}
/**
* Handles the unpublish action
*
* @since version 4.0.0
*
* @param array $data
* @param object $form
*
* @return void
**/
public function doUnpublish($data, $form)
{
$record = $this->getRecord();
Expand All @@ -207,16 +221,6 @@ public function doUnpublish($data, $form)
return $this->redirectAfterSave(false);
}
/**
* Set form action message
*
* @since version 4.0.0
*
* @param object $form
* @param string $message
*
* @return void
**/
protected function setFormMessage($form, $message)
{
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
Expand All @@ -227,32 +231,12 @@ protected function setFormMessage($form, $message)
}
}
/**
* Redirects to block selection screen
*
* @since version 1.0.0
*
* @param array $data
* @param object $form
*
* @return void
**/
public function doAddBlock($data, Form $form)
{
return Controller::curr()->redirect(Controller::join_links($this->gridField->Link('item'), 'new'));
}
/**
* Get the current record action
*
* @since version 1.0.0
*
* @return string
**/
private function getAction()
{
$path = explode('/', Controller::curr()->getRequest()->getURL());
return array_pop($path);
}
}
*/
41 changes: 41 additions & 0 deletions src/Action/GridFieldVersionedDeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* GridFieldVersionedDeleteAction
*
* Overrides GridFieldDeleteAction to unlink a versioned relation (ContentBlock)
* from a versioned DataObject (Page)
*
* @package silverstripe-block-page
* @license MIT License https://github.com/cyber-duck/silverstripe-block-page/blob/master/LICENSE
* @author <[email protected]>
**/
namespace CyberDuck\BlockPage\Action;

use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Versioned\Versioned;

class GridFieldVersionedDeleteAction extends GridFieldDeleteAction
{
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
// remove from live
if ($actionName == 'deleterecord' || $actionName == 'unlinkrelation') {
$item = $gridField->getList()->byID($arguments['RecordID']);
if (!$item) {
return;
}
if ($actionName == 'unlinkrelation') {
if (!$item->canEdit()) {
throw new ValidationException(
_t(__CLASS__.'.EditPermissionsFailure', "No permission to unlink record")
);
}
$item->PageContentBlock->deleteFromStage(Versioned::LIVE);
}
}
// remove from stage
parent::handleAction($gridField, $actionName, $arguments, $data);
}
}
Empty file added src/Admin/BlockAdmin.php
Empty file.
Loading

0 comments on commit f4b4137

Please sign in to comment.