Skip to content

Commit

Permalink
Merge pull request #163 from creative-commoners/pulls/2.0/ownership-api
Browse files Browse the repository at this point in the history
 API Widgets and WidgetAreas are versioned and are owned by pages
  • Loading branch information
NightJar authored Dec 21, 2017
2 parents a0b1f9b + 8583b89 commit 0a3c280
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 109 deletions.
11 changes: 1 addition & 10 deletions .upgrade.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion _config/contentcontroller-url-handler.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Name: contentcontrollerurlhandler
Name: widgetscontentcontrollerurlhandler
---
SilverStripe\CMS\Controllers\ContentController:
extensions:
Expand Down
5 changes: 4 additions & 1 deletion _config/routes.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
Name: widgetsroutes
---
SilverStripe\Control\Director:
rules:
'WidgetController//$Action/$ID/$OtherID': 'SilverStripe\Widgets\Controllers\WidgetController'
'WidgetController//$Action/$ID/$OtherID': 'SilverStripe\Widgets\Model\WidgetController'
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 15 additions & 7 deletions src/Extensions/WidgetPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@
*/
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)
{
Expand Down
54 changes: 21 additions & 33 deletions src/Model/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace SilverStripe\Widgets\Model;

use Exception;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
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
Expand All @@ -23,42 +25,27 @@
*/
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\"";

/**
Expand All @@ -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,
];

/**
* @var WidgetController
*/
Expand Down Expand Up @@ -267,10 +252,13 @@ public function getController()
}

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;
}
Expand Down
51 changes: 23 additions & 28 deletions src/Model/WidgetArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
* @var string
*/
private static $table_name = 'WidgetArea';

/**
*
* @var string
*/
public $template = __CLASS__;

/**
Expand All @@ -43,6 +47,9 @@ public function WidgetControllers()
$items = $this->ItemsToRender();
if (!is_null($items)) {
foreach ($items as $widget) {
/** @var Widget $widget */

/** @var Controller $controller */
$controller = $widget->getController();

$controller->doInit();
Expand All @@ -57,16 +64,15 @@ public function WidgetControllers()
*/
public function Items()
{
return $this->getComponents('Widgets');
return $this->Widgets();
}

/**
* @return HasManyList
*/
public function ItemsToRender()
{
return $this->getComponents('Widgets')
->filter("Enabled", 1);
return $this->Items()->filter('Enabled', 1);
}

/**
Expand All @@ -85,15 +91,4 @@ public function setTemplate($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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
20 changes: 10 additions & 10 deletions tests/WidgetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
namespace SilverStripe\Widgets\Tests;

use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\Form;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget;

/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest extends FunctionalTest
{
protected static $fixture_file = 'WidgetControllerTest.yml';
Expand All @@ -20,11 +15,18 @@ class WidgetControllerTest extends FunctionalTest
TestWidget::class,
];

protected function setUp()
{
parent::setUp();

$this->actWithPermission('ADMIN', function () {
$this->objFromFixture(TestPage::class, 'page1')->publishRecursive();
});
}

public function testWidgetFormRendering()
{
$page = $this->objFromFixture(TestPage::class, 'page1');
$page->copyVersionToStage('Stage', 'Live');

$widget = $this->objFromFixture(TestWidget::class, 'widget1');

$response = $this->get($page->URLSegment);
Expand All @@ -40,11 +42,9 @@ public function testWidgetFormRendering()
public function testWidgetFormSubmission()
{
$page = $this->objFromFixture(TestPage::class, 'page1');
$page->copyVersionToStage('Stage', 'Live');

$widget = $this->objFromFixture(TestWidget::class, 'widget1');

$response = $this->get($page->URLSegment);
$this->get($page->URLSegment);
$response = $this->submitForm('Form_Form', null, array('TestValue' => 'Updated'));

$this->assertContains(
Expand Down
15 changes: 7 additions & 8 deletions tests/WidgetControllerTest/TestPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use Page;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\SSViewer;
use SilverStripe\Widgets\Model\WidgetArea;

/**
* @package cms
* @subpackage tests
*/
class TestPage extends Page implements TestOnly
{
private static $table_name = 'TestPage';

private static $has_one = array(
'WidgetControllerTestSidebar' => WidgetArea::class
);
private static $has_one = [
'WidgetControllerTestSidebar' => WidgetArea::class,
];

private static $owns = [
'WidgetControllerTestSidebar',
];
}
10 changes: 3 additions & 7 deletions tests/WidgetControllerTest/TestWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Model\Widget;

/**
* @package widgets
* @subpackage tests
*/
class TestWidget extends Widget implements TestOnly
{
private static $table_name = 'WidgetControllerTest_TestWidget';

private static $db = array(
'TestValue' => 'Text'
);
private static $db = [
'TestValue' => 'Text',
];
}
2 changes: 1 addition & 1 deletion tests/WidgetControllerTest/TestWidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\Widgets\Controllers\WidgetController;
use SilverStripe\Widgets\Model\WidgetController;

/**
* @package widgets
Expand Down

0 comments on commit 0a3c280

Please sign in to comment.