Merge pull request #163 from creative-commoners/pulls/2.0/ownership-api

API Widgets and WidgetAreas are versioned and are owned by pages
This commit is contained in:
Dylan Wagstaff 2017-12-22 11:13:55 +13:00 committed by GitHub
commit 0a3c280c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 89 additions and 109 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\Model\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,
];
/** /**
* @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,
];
/**
* @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

@ -3,14 +3,9 @@
namespace SilverStripe\Widgets\Tests; namespace SilverStripe\Widgets\Tests;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\Form;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage; use SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget; use SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget;
/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest extends FunctionalTest class WidgetControllerTest extends FunctionalTest
{ {
protected static $fixture_file = 'WidgetControllerTest.yml'; protected static $fixture_file = 'WidgetControllerTest.yml';
@ -20,11 +15,18 @@ class WidgetControllerTest extends FunctionalTest
TestWidget::class, TestWidget::class,
]; ];
protected function setUp()
{
parent::setUp();
$this->actWithPermission('ADMIN', function () {
$this->objFromFixture(TestPage::class, 'page1')->publishRecursive();
});
}
public function testWidgetFormRendering() public function testWidgetFormRendering()
{ {
$page = $this->objFromFixture(TestPage::class, 'page1'); $page = $this->objFromFixture(TestPage::class, 'page1');
$page->copyVersionToStage('Stage', 'Live');
$widget = $this->objFromFixture(TestWidget::class, 'widget1'); $widget = $this->objFromFixture(TestWidget::class, 'widget1');
$response = $this->get($page->URLSegment); $response = $this->get($page->URLSegment);
@ -40,11 +42,9 @@ class WidgetControllerTest extends FunctionalTest
public function testWidgetFormSubmission() public function testWidgetFormSubmission()
{ {
$page = $this->objFromFixture(TestPage::class, 'page1'); $page = $this->objFromFixture(TestPage::class, 'page1');
$page->copyVersionToStage('Stage', 'Live');
$widget = $this->objFromFixture(TestWidget::class, 'widget1'); $widget = $this->objFromFixture(TestWidget::class, 'widget1');
$response = $this->get($page->URLSegment); $this->get($page->URLSegment);
$response = $this->submitForm('Form_Form', null, array('TestValue' => 'Updated')); $response = $this->submitForm('Form_Form', null, array('TestValue' => 'Updated'));
$this->assertContains( $this->assertContains(

View File

@ -4,18 +4,17 @@ namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use Page; use Page;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
use SilverStripe\View\SSViewer;
use SilverStripe\Widgets\Model\WidgetArea; use SilverStripe\Widgets\Model\WidgetArea;
/**
* @package cms
* @subpackage tests
*/
class TestPage extends Page implements TestOnly class TestPage extends Page implements TestOnly
{ {
private static $table_name = 'TestPage'; private static $table_name = 'TestPage';
private static $has_one = array( private static $has_one = [
'WidgetControllerTestSidebar' => WidgetArea::class 'WidgetControllerTestSidebar' => WidgetArea::class,
); ];
private static $owns = [
'WidgetControllerTestSidebar',
];
} }

View File

@ -5,15 +5,11 @@ namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Model\Widget; use SilverStripe\Widgets\Model\Widget;
/**
* @package widgets
* @subpackage tests
*/
class TestWidget extends Widget implements TestOnly class TestWidget extends Widget implements TestOnly
{ {
private static $table_name = 'WidgetControllerTest_TestWidget'; private static $table_name = 'WidgetControllerTest_TestWidget';
private static $db = array( private static $db = [
'TestValue' => 'Text' 'TestValue' => 'Text',
); ];
} }

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