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:
WidgetContentControllerExtension: SilverStripe\Widgets\Controllers\WidgetContentControllerExtension
WidgetController: SilverStripe\Widgets\Controllers\WidgetController
Widget_Controller: SilverStripe\Widgets\Controllers\Widget_Controller
Widget_Controller: SilverStripe\Widgets\Models\WidgetController
WidgetPageExtension: SilverStripe\Widgets\Extensions\WidgetPageExtension
WidgetAreaEditor: SilverStripe\Widgets\Forms\WidgetAreaEditor
Widget: SilverStripe\Widgets\Model\Widget
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:
extensions:

View File

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

View File

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

View File

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

View File

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

View File

@ -2,32 +2,36 @@
namespace SilverStripe\Widgets\Model;
use SilverStripe\Control\Controller;
use SilverStripe\ORM\ArrayList;
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.
*
* @package widgets
*/
class WidgetArea extends DataObject
{
/**
* @var array
*/
private static $has_many = array(
private static $has_many = [
"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';
/**
*
* @var string
*/
public $template = __CLASS__;
/**
@ -43,6 +47,9 @@ class WidgetArea extends DataObject
$items = $this->ItemsToRender();
if (!is_null($items)) {
foreach ($items as $widget) {
/** @var Widget $widget */
/** @var Controller $controller */
$controller = $widget->getController();
$controller->doInit();
@ -57,7 +64,7 @@ class WidgetArea extends DataObject
*/
public function Items()
{
return $this->getComponents('Widgets');
return $this->Widgets();
}
/**
@ -65,8 +72,7 @@ class WidgetArea extends DataObject
*/
public function ItemsToRender()
{
return $this->getComponents('Widgets')
->filter("Enabled", 1);
return $this->Items()->filter('Enabled', 1);
}
/**
@ -85,15 +91,4 @@ class WidgetArea extends DataObject
{
$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
namespace SilverStripe\Widgets\Controllers;
namespace SilverStripe\Widgets\Model;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Widgets\Model\Widget;
/**
* 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\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\Widgets\Controllers\WidgetController;
use SilverStripe\Widgets\Model\WidgetController;
/**
* @package widgets