API Widgets and WidgetAreas are versioned and are owned by pages

This commit is contained in:
Robbie Averill 2017-12-19 14:20:20 +13:00
parent a0b1f9b1cf
commit e1a0469c46
9 changed files with 68 additions and 83 deletions

View File

@ -1,16 +1,7 @@
mappings: mappings:
WidgetContentControllerExtension: SilverStripe\Widgets\Controllers\WidgetContentControllerExtension WidgetContentControllerExtension: SilverStripe\Widgets\Controllers\WidgetContentControllerExtension
WidgetController: SilverStripe\Widgets\Controllers\WidgetController Widget_Controller: SilverStripe\Widgets\Models\WidgetController
Widget_Controller: SilverStripe\Widgets\Controllers\Widget_Controller
WidgetPageExtension: SilverStripe\Widgets\Extensions\WidgetPageExtension WidgetPageExtension: SilverStripe\Widgets\Extensions\WidgetPageExtension
WidgetAreaEditor: SilverStripe\Widgets\Forms\WidgetAreaEditor WidgetAreaEditor: SilverStripe\Widgets\Forms\WidgetAreaEditor
Widget: SilverStripe\Widgets\Model\Widget Widget: SilverStripe\Widgets\Model\Widget
WidgetArea: SilverStripe\Widgets\Model\WidgetArea WidgetArea: SilverStripe\Widgets\Model\WidgetArea
WidgetAreaEditorTest: SilverStripe\Widgets\Tests\WidgetAreaEditorTest
WidgetAreaEditorTest_FakePage: SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage
WidgetAreaEditorTest_TestWidget: SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget
WidgetControllerTest: SilverStripe\Widgets\Tests\WidgetControllerTest
WidgetControllerTest_Widget: SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget
WidgetControllerTest_WidgetController: SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidgetController
WidgetControllerTestPage: SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage
WidgetControllerTestPage_Controller: SilverStripe\Widgets\Tests\WidgetControllerTest\TestPageController

View File

@ -1,5 +1,5 @@
--- ---
Name: contentcontrollerurlhandler Name: widgetscontentcontrollerurlhandler
--- ---
SilverStripe\CMS\Controllers\ContentController: SilverStripe\CMS\Controllers\ContentController:
extensions: extensions:

View File

@ -1,3 +1,6 @@
---
Name: widgetsroutes
---
SilverStripe\Control\Director: SilverStripe\Control\Director:
rules: rules:
'WidgetController//$Action/$ID/$OtherID': 'SilverStripe\Widgets\Controllers\WidgetController' 'WidgetController//$Action/$ID/$OtherID': 'SilverStripe\Widgets\Controllers\WidgetController'

View File

@ -12,7 +12,8 @@
"require": { "require": {
"silverstripe/vendor-plugin": "^1.0", "silverstripe/vendor-plugin": "^1.0",
"silverstripe/framework": "^4.0", "silverstripe/framework": "^4.0",
"silverstripe/cms": "^4.0" "silverstripe/cms": "^4.0",
"silverstripe/versioned": "^1.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7", "phpunit/phpunit": "^5.7",

View File

@ -19,17 +19,25 @@ use SilverStripe\Widgets\Model\WidgetArea;
*/ */
class WidgetPageExtension extends DataExtension class WidgetPageExtension extends DataExtension
{ {
private static $db = array( private static $db = [
'InheritSideBar' => 'Boolean', 'InheritSideBar' => 'Boolean',
); ];
private static $defaults = array( private static $defaults = [
'InheritSideBar' => true 'InheritSideBar' => true
); ];
private static $has_one = array( private static $has_one = [
'SideBar' => WidgetArea::class 'SideBar' => WidgetArea::class,
); ];
private static $owns = [
'SideBar',
];
private static $cascade_deletes = [
'SideBar',
];
public function updateCMSFields(FieldList $fields) public function updateCMSFields(FieldList $fields)
{ {

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Widgets\Model; namespace SilverStripe\Widgets\Model;
use Exception; use Exception;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
@ -10,6 +11,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/** /**
* Widgets let CMS authors drag and drop small pieces of functionality into * Widgets let CMS authors drag and drop small pieces of functionality into
@ -23,42 +25,27 @@ use SilverStripe\ORM\DataObject;
*/ */
class Widget extends DataObject class Widget extends DataObject
{ {
/** private static $db = [
* @var array
*/
private static $db = array(
"Title" => "Varchar(255)", "Title" => "Varchar(255)",
"Sort" => "Int", "Sort" => "Int",
"Enabled" => "Boolean", "Enabled" => "Boolean",
); ];
/** private static $defaults = [
* @var array
*/
private static $defaults = array(
'Enabled' => true, 'Enabled' => true,
); ];
/** private static $casting = [
* @var array
*/
private static $casting = array(
'CMSTitle' => 'Text', 'CMSTitle' => 'Text',
'Description' => 'Text', 'Description' => 'Text',
); ];
private static $only_available_in = array(); private static $only_available_in = [];
/** private static $has_one = [
* @var array
*/
private static $has_one = array(
"Parent" => WidgetArea::class, "Parent" => WidgetArea::class,
); ];
/**
* @var string
*/
private static $default_sort = "\"Sort\""; private static $default_sort = "\"Sort\"";
/** /**
@ -76,18 +63,16 @@ class Widget extends DataObject
*/ */
private static $description = "Description of what this widget does."; private static $description = "Description of what this widget does.";
/** private static $summary_fields = [
* @var array
*/
private static $summary_fields = array(
'CMSTitle' => 'Title' 'CMSTitle' => 'Title'
); ];
/**
* @var string
*/
private static $table_name = 'Widget'; private static $table_name = 'Widget';
private static $extensions = [
Versioned::class . '.versioned',
];
/** /**
* @var WidgetController * @var WidgetController
*/ */
@ -267,10 +252,13 @@ class Widget extends DataObject
} }
if (!class_exists($controllerClass)) { if (!class_exists($controllerClass)) {
throw new Exception('Could not find controller class for ' . $controllerClass); throw new Exception('Could not find controller class for ' . static::class);
} }
$this->controller = Injector::inst()->create($controllerClass, $this); $this->controller = Injector::inst()->create($controllerClass, $this);
if (Injector::inst()->has(HTTPRequest::class)) {
$this->controller->setRequest(Injector::inst()->get(HTTPRequest::class));
}
return $this->controller; return $this->controller;
} }

View File

@ -2,32 +2,36 @@
namespace SilverStripe\Widgets\Model; namespace SilverStripe\Widgets\Model;
use SilverStripe\Control\Controller;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\SS_List;
use SilverStripe\Versioned\Versioned;
/** /**
* Represents a set of widgets shown on a page. * Represents a set of widgets shown on a page.
*
* @package widgets
*/ */
class WidgetArea extends DataObject class WidgetArea extends DataObject
{ {
/** private static $has_many = [
* @var array
*/
private static $has_many = array(
"Widgets" => Widget::class "Widgets" => Widget::class
); ];
private static $owns = [
'Widgets',
];
private static $cascade_deletes = [
'Widgets',
];
private static $extensions = [
Versioned::class . '.versioned',
];
/**
* @var string
*/
private static $table_name = 'WidgetArea'; private static $table_name = 'WidgetArea';
/**
*
* @var string
*/
public $template = __CLASS__; public $template = __CLASS__;
/** /**
@ -43,6 +47,9 @@ class WidgetArea extends DataObject
$items = $this->ItemsToRender(); $items = $this->ItemsToRender();
if (!is_null($items)) { if (!is_null($items)) {
foreach ($items as $widget) { foreach ($items as $widget) {
/** @var Widget $widget */
/** @var Controller $controller */
$controller = $widget->getController(); $controller = $widget->getController();
$controller->doInit(); $controller->doInit();
@ -57,7 +64,7 @@ class WidgetArea extends DataObject
*/ */
public function Items() public function Items()
{ {
return $this->getComponents('Widgets'); return $this->Widgets();
} }
/** /**
@ -65,8 +72,7 @@ class WidgetArea extends DataObject
*/ */
public function ItemsToRender() public function ItemsToRender()
{ {
return $this->getComponents('Widgets') return $this->Items()->filter('Enabled', 1);
->filter("Enabled", 1);
} }
/** /**
@ -85,15 +91,4 @@ class WidgetArea extends DataObject
{ {
$this->template = $template; $this->template = $template;
} }
/**
* Delete all connected Widgets when this WidgetArea gets deleted
*/
public function onBeforeDelete()
{
parent::onBeforeDelete();
foreach ($this->Widgets() as $widget) {
$widget->delete();
}
}
} }

View File

@ -1,12 +1,11 @@
<?php <?php
namespace SilverStripe\Widgets\Controllers; namespace SilverStripe\Widgets\Model;
use SilverStripe\Admin\LeftAndMain; use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Widgets\Model\Widget;
/** /**
* Optional controller for every widget which has its own logic, e.g. in forms. * Optional controller for every widget which has its own logic, e.g. in forms.

View File

@ -7,7 +7,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction; use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Widgets\Controllers\WidgetController; use SilverStripe\Widgets\Model\WidgetController;
/** /**
* @package widgets * @package widgets