Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-11-18 17:08:21 +13:00
parent d66a641934
commit 130e9d06a1
9 changed files with 1233 additions and 1177 deletions

View File

@ -4,62 +4,65 @@
*
* @package widgets
*/
class WidgetContentControllerExtension extends Extension {
class WidgetContentControllerExtension extends Extension
{
/**
*
* @var array
*/
private static $allowed_actions = array(
'handleWidget'
);
/**
* Handles widgets attached to a page through one or more {@link WidgetArea}
* elements.
*
* Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier.
*
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
*
* @return RequestHandler
*/
public function handleWidget()
{
$SQL_id = $this->owner->getRequest()->param('ID');
if (!$SQL_id) {
return false;
}
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne();
if (!$hasOnes) {
return false;
}
/**
*
* @var array
*/
private static $allowed_actions = array(
'handleWidget'
);
/**
* Handles widgets attached to a page through one or more {@link WidgetArea}
* elements.
*
* Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier.
*
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
*
* @return RequestHandler
*/
public function handleWidget() {
$SQL_id = $this->owner->getRequest()->param('ID');
if(!$SQL_id) return false;
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne();
if(!$hasOnes) {
return false;
}
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName;
}
}
foreach($hasOnes as $hasOneName => $hasOneClass) {
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName;
}
}
// find widget
$widget = null;
// find widget
$widget = null;
foreach ($widgetAreaRelations as $widgetAreaRelation) {
if ($widget) {
break;
}
foreach($widgetAreaRelations as $widgetAreaRelation) {
if($widget) {
break;
}
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id)
->First();
}
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id)
->First();
}
if(!$widget) {
user_error('No widget found', E_USER_ERROR);
}
return $widget->getController();
}
if (!$widget) {
user_error('No widget found', E_USER_ERROR);
}
return $widget->getController();
}
}

View File

@ -18,103 +18,109 @@
*
* @package widgets
*/
class WidgetController extends Controller {
/**
* @var Widget
*/
protected $widget;
class WidgetController extends Controller
{
/**
* @var Widget
*/
protected $widget;
/**
* @var array
*/
private static $allowed_actions = array(
'editablesegment'
);
/**
* @var array
*/
private static $allowed_actions = array(
'editablesegment'
);
/**
* @param Widget $widget
*/
public function __construct($widget = null) {
if($widget) {
$this->widget = $widget;
$this->failover = $widget;
}
parent::__construct();
}
/**
* @param Widget $widget
*/
public function __construct($widget = null)
{
if ($widget) {
$this->widget = $widget;
$this->failover = $widget;
}
parent::__construct();
}
/**
* @param string $action
* @return string
*/
public function Link($action = null) {
$id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action);
if($page = Director::get_current_page()) {
return $page->Link($segment);
}
return Controller::curr()->Link($segment);
}
/**
* @return Widget
*/
public function getWidget() {
return $this->widget;
}
/**
* Overloaded from {@link Widget->Content()} to allow for controller / form
* linking.
*
* @return string HTML
*/
public function Content() {
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
}
/**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* form linking.
*
* @return string HTML
*/
public function WidgetHolder() {
return $this->renderWith("WidgetHolder");
}
/**
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* to render a administrator-view of the widget. It is assumed that this
* view contains form elements which are submitted and saved through
* {@link WidgetAreaEditor} within the CMS interface.
*
* @return string HTML
*/
public function editablesegment() {
$className = $this->urlParams['ID'];
if (class_exists('Translatable') && Member::currentUserID()) {
// set current locale based on logged in user's locale
$locale = Member::currentUser()->Locale;
i18n::set_locale($locale);
}
if(class_exists($className) && is_subclass_of($className, 'Widget')) {
$obj = new $className();
return $obj->EditableSegment();
} else {
user_error("Bad widget class: $className", E_USER_WARNING);
return "Bad widget class name given";
}
}
/**
* @param string $action
* @return string
*/
public function Link($action = null)
{
$id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action);
if ($page = Director::get_current_page()) {
return $page->Link($segment);
}
return Controller::curr()->Link($segment);
}
/**
* @return Widget
*/
public function getWidget()
{
return $this->widget;
}
/**
* Overloaded from {@link Widget->Content()} to allow for controller / form
* linking.
*
* @return string HTML
*/
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
}
/**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* form linking.
*
* @return string HTML
*/
public function WidgetHolder()
{
return $this->renderWith("WidgetHolder");
}
/**
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* to render a administrator-view of the widget. It is assumed that this
* view contains form elements which are submitted and saved through
* {@link WidgetAreaEditor} within the CMS interface.
*
* @return string HTML
*/
public function editablesegment()
{
$className = $this->urlParams['ID'];
if (class_exists('Translatable') && Member::currentUserID()) {
// set current locale based on logged in user's locale
$locale = Member::currentUser()->Locale;
i18n::set_locale($locale);
}
if (class_exists($className) && is_subclass_of($className, 'Widget')) {
$obj = new $className();
return $obj->EditableSegment();
} else {
user_error("Bad widget class: $className", E_USER_WARNING);
return "Bad widget class name given";
}
}
}
/**
* @deprecated Use WidgetController
* @package widgets
*/
class Widget_Controller extends WidgetController {
class Widget_Controller extends WidgetController
{
}

View File

@ -8,70 +8,72 @@
* feel free to create your own relationships, naming conventions, etc.
* without using this class.
*/
class WidgetPageExtension extends DataExtension {
class WidgetPageExtension extends DataExtension
{
private static $db = array(
'InheritSideBar' => 'Boolean',
);
private static $db = array(
'InheritSideBar' => 'Boolean',
);
private static $defaults = array(
'InheritSideBar' => true
);
private static $defaults = array(
'InheritSideBar' => true
);
private static $has_one = array(
'SideBar' => 'WidgetArea'
);
private static $has_one = array(
'SideBar' => 'WidgetArea'
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab(
"Root.Widgets",
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
);
$fields->addFieldToTab(
"Root.Widgets",
new WidgetAreaEditor("SideBar")
);
}
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab(
"Root.Widgets",
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
);
$fields->addFieldToTab(
"Root.Widgets",
new WidgetAreaEditor("SideBar")
);
}
/**
* @return WidgetArea
*/
public function SideBarView()
{
if (
$this->owner->InheritSideBar
&& ($parent = $this->owner->getParent())
&& $parent->hasMethod('SideBarView')
) {
return $parent->SideBarView();
} elseif ($this->owner->SideBar()->exists()) {
return $this->owner->SideBar();
}
}
public function onBeforeDuplicate($duplicatePage)
{
if ($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
/**
* @return WidgetArea
*/
public function SideBarView() {
if(
$this->owner->InheritSideBar
&& ($parent = $this->owner->getParent())
&& $parent->hasMethod('SideBarView')
) {
return $parent->SideBarView();
} elseif($this->owner->SideBar()->exists()){
return $this->owner->SideBar();
}
}
public function onBeforeDuplicate($duplicatePage) {
if($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
foreach ($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
foreach($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
}
return $duplicatePage;
}
/**
* Support Translatable so that we don't link WidgetAreas across translations
*/
public function onTranslatableCreate() {
//reset the sidebar ID
$this->owner->SideBarID = 0;
}
return $duplicatePage;
}
/**
* Support Translatable so that we don't link WidgetAreas across translations
*/
public function onTranslatableCreate()
{
//reset the sidebar ID
$this->owner->SideBarID = 0;
}
}

View File

@ -5,174 +5,181 @@
*
* @package widgets
*/
class WidgetAreaEditor extends FormField {
class WidgetAreaEditor extends FormField
{
/**
* @param string $name
* @param array $widgetClasses
* @param int $maxWidgets
*/
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0)
{
$this->MaxWidgets = $maxWidgets;
$this->widgetClasses = $widgetClasses;
parent::__construct($name);
}
/**
* @param string $name
* @param array $widgetClasses
* @param int $maxWidgets
*/
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) {
$this->MaxWidgets = $maxWidgets;
$this->widgetClasses = $widgetClasses;
parent::__construct($name);
}
/**
* @param array $properties
*
* @return string - HTML
*/
public function FieldHolder($properties = array())
{
Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
/**
* @param array $properties
*
* @return string - HTML
*/
public function FieldHolder($properties = array()) {
Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
return $this->renderWith("WidgetAreaEditor");
}
return $this->renderWith("WidgetAreaEditor");
}
/**
*
* @return ArrayList
*/
public function AvailableWidgets()
{
$widgets= new ArrayList();
/**
*
* @return ArrayList
*/
public function AvailableWidgets() {
$widgets= new ArrayList();
foreach ($this->widgetClasses as $widgetClass) {
$classes = ClassInfo::subclassesFor($widgetClass);
foreach($this->widgetClasses as $widgetClass) {
$classes = ClassInfo::subclassesFor($widgetClass);
if (isset($classes['Widget'])) {
unset($classes['Widget']);
} elseif (isset($classes[0]) && $classes[0] == 'Widget') {
unset($classes[0]);
}
foreach ($classes as $class) {
$available = Config::inst()->get($class, 'only_available_in');
if (!empty($available) && is_array($available)) {
if (in_array($this->Name, $available)) {
$widgets->push(singleton($class));
}
} else {
$widgets->push(singleton($class));
}
}
}
return $widgets;
}
if (isset($classes['Widget'])) {
unset($classes['Widget']);
}
else if (isset($classes[0]) && $classes[0] == 'Widget') {
unset($classes[0]);
}
foreach($classes as $class) {
$available = Config::inst()->get($class, 'only_available_in');
if (!empty($available) && is_array($available)) {
if(in_array($this->Name, $available)) {
$widgets->push(singleton($class));
}
}else {
$widgets->push(singleton($class));
}
}
}
return $widgets;
}
/**
* @return HasManyList
*/
public function UsedWidgets()
{
// Call class_exists() to load Widget.php earlier and avoid a segfault
class_exists('Widget');
$relationName = $this->name;
$widgets = $this->form->getRecord()->getComponent($relationName)->Items();
/**
* @return HasManyList
*/
public function UsedWidgets() {
// Call class_exists() to load Widget.php earlier and avoid a segfault
class_exists('Widget');
$relationName = $this->name;
$widgets = $this->form->getRecord()->getComponent($relationName)->Items();
return $widgets;
}
return $widgets;
}
/**
* @return string
*/
public function IdxField()
{
return $this->id() . 'ID';
}
/**
* @return string
*/
public function IdxField() {
return $this->id() . 'ID';
}
/**
*
* @return int
*/
public function Value()
{
$relationName = $this->name;
/**
*
* @return int
*/
public function Value() {
$relationName = $this->name;
return $this->form->getRecord()->getComponent($relationName)->ID;
}
return $this->form->getRecord()->getComponent($relationName)->ID;
}
/**
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record)
{
$name = $this->name;
$idName = $name . "ID";
/**
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record) {
$name = $this->name;
$idName = $name . "ID";
$widgetarea = $record->getComponent($name);
$widgetarea->write();
$record->$idName = $widgetarea->ID;
$widgets = $widgetarea->Items();
// store the field IDs and delete the missing fields
// alternatively, we could delete all the fields and re add them
$missingWidgets = array();
if ($widgets) {
foreach ($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget;
}
}
$widgetarea = $record->getComponent($name);
$widgetarea->write();
$record->$idName = $widgetarea->ID;
$widgets = $widgetarea->Items();
// store the field IDs and delete the missing fields
// alternatively, we could delete all the fields and re add them
$missingWidgets = array();
if($widgets) {
foreach($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget;
}
}
if (!$this->getForm()) {
throw new Exception("no form");
}
if(!$this->getForm()) throw new Exception("no form");
$widgetData = $this->getForm()->getRequest()->requestVar('Widget');
if ($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
$widgetData = $this->getForm()->getRequest()->requestVar('Widget');
if($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
foreach ($widgetAreaData as $newWidgetID => $newWidgetData) {
foreach($widgetAreaData as $newWidgetID => $newWidgetData) {
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if (!is_numeric($newWidgetID)) {
$newWidgetID = 0;
}
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) {
$newWidgetID = 0;
}
$widget = null;
if ($newWidgetID) {
// \"ParentID\" = '0' is for the new page
$widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID);
$widget = null;
if($newWidgetID) {
// \"ParentID\" = '0' is for the new page
$widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID);
// check if we are updating an existing widget
if ($widget && isset($missingWidgets[$widget->ID])) {
unset($missingWidgets[$widget->ID]);
}
}
// check if we are updating an existing widget
if($widget && isset($missingWidgets[$widget->ID])) {
unset($missingWidgets[$widget->ID]);
}
}
// create a new object
if (!$widget
&& !empty($newWidgetData['Type'])
&& class_exists($newWidgetData['Type'])
&& is_subclass_of($newWidgetData['Type'], 'Widget')
) {
$widget = Injector::inst()->create($newWidgetData['Type']);
$widget->ID = 0;
$widget->ParentID = $record->$name()->ID;
}
// create a new object
if(!$widget
&& !empty($newWidgetData['Type'])
&& class_exists($newWidgetData['Type'])
&& is_subclass_of($newWidgetData['Type'], 'Widget')
) {
$widget = Injector::inst()->create($newWidgetData['Type']);
$widget->ID = 0;
$widget->ParentID = $record->$name()->ID;
}
if ($widget) {
if ($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID;
}
if($widget) {
if($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID;
}
$widget->populateFromPostData($newWidgetData);
}
}
}
// remove the fields not saved
if($missingWidgets) {
foreach($missingWidgets as $removedWidget) {
if(isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}
}
}
$widget->populateFromPostData($newWidgetData);
}
}
}
// remove the fields not saved
if ($missingWidgets) {
foreach ($missingWidgets as $removedWidget) {
if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}
}
}
}

View File

@ -10,287 +10,303 @@
*
* @package widgets
*/
class Widget extends DataObject {
class Widget extends DataObject
{
/**
* @var array
*/
private static $db = array(
"Title" => "Varchar(255)",
"Sort" => "Int",
"Enabled" => "Boolean",
);
/**
* @var array
*/
private static $db = array(
"Title" => "Varchar(255)",
"Sort" => "Int",
"Enabled" => "Boolean",
);
/**
* @var array
*/
private static $defaults = array(
'Enabled' => true,
);
/**
* @var array
*/
private static $defaults = array(
'Enabled' => true,
);
/**
* @var array
*/
private static $casting = array(
'CMSTitle' => 'Text',
'Description' => 'Text',
);
private static $only_available_in = array();
/**
* @var array
*/
private static $casting = array(
'CMSTitle' => 'Text',
'Description' => 'Text',
);
private static $only_available_in = array();
/**
* @var array
*/
private static $has_one = array(
"Parent" => "WidgetArea",
);
/**
* @var array
*/
private static $has_one = array(
"Parent" => "WidgetArea",
);
/**
* @var string
*/
private static $default_sort = "\"Sort\"";
/**
* @var string
*/
private static $default_sort = "\"Sort\"";
/**
* @var string
*/
private static $title = "Widget Title";
/**
* @var string
*/
private static $title = "Widget Title";
/**
* @var string
*/
private static $cmsTitle = "Name of this widget";
/**
* @var string
*/
private static $cmsTitle = "Name of this widget";
/**
* @var string
*/
private static $description = "Description of what this widget does.";
/**
* @var string
*/
private static $description = "Description of what this widget does.";
/**
* @var array
*/
private static $summary_fields = array(
'CMSTitle' => 'Title'
);
/**
* @var array
*/
private static $summary_fields = array(
'CMSTitle' => 'Title'
);
/**
* @var WidgetController
*/
protected $controller;
/**
* @var WidgetController
*/
protected $controller;
public function populateDefaults()
{
parent::populateDefaults();
$this->setField('Title', $this->getTitle());
}
/**
* Note: Overloaded in {@link WidgetController}.
*
* @return string HTML
*/
public function WidgetHolder()
{
return $this->renderWith("WidgetHolder");
}
public function populateDefaults() {
parent::populateDefaults();
$this->setField('Title', $this->getTitle());
}
/**
* Note: Overloaded in {@link WidgetController}.
*
* @return string HTML
*/
public function WidgetHolder() {
return $this->renderWith("WidgetHolder");
}
/**
* Default way to render widget in templates.
* @return string HTML
*/
public function forTemplate($holder = true)
{
if ($holder) {
return $this->WidgetHolder();
}
return $this->Content();
}
/**
* Renders the widget content in a custom template with the same name as the
* current class. This should be the main point of output customization.
*
* Invoked from within WidgetHolder.ss, which contains the "framing" around
* the custom content, like a title.
*
* Note: Overloaded in {@link WidgetController}.
*
* @return string HTML
*/
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
}
/**
* Default way to render widget in templates.
* @return string HTML
*/
public function forTemplate($holder = true){
if($holder){
return $this->WidgetHolder();
}
return $this->Content();
}
/**
* Renders the widget content in a custom template with the same name as the
* current class. This should be the main point of output customization.
*
* Invoked from within WidgetHolder.ss, which contains the "framing" around
* the custom content, like a title.
*
* Note: Overloaded in {@link WidgetController}.
*
* @return string HTML
*/
public function Content() {
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
}
/**
* @return string
* @deprecated
*/
public function Title()
{
return $this->getTitle();
}
/**
* @return string
* @deprecated
*/
public function Title() {
return $this->getTitle();
}
/**
* Get the frontend title for this widget
*
* @return string
*/
public function getTitle()
{
return $this->getField('Title')
?: _t($this->class.'.TITLE', $this->config()->title);
}
/**
* Get the frontend title for this widget
*
* @return string
*/
public function getTitle() {
return $this->getField('Title')
?: _t($this->class.'.TITLE', $this->config()->title);
}
/**
* @return string
* @deprecated
*/
public function CMSTitle()
{
return $this->getCMSTitle();
}
/**
* @return string
* @deprecated
*/
public function CMSTitle() {
return $this->getCMSTitle();
}
/**
* @return string
*/
public function getCMSTitle()
{
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
}
/**
* @return string
*/
public function getCMSTitle() {
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
}
/**
* @return string
* @deprecated
*/
public function Description()
{
return $this->getDescription();
}
/**
* @return string
* @deprecated
*/
public function Description() {
return $this->getDescription();
}
/**
* @return string
*/
public function getDescription()
{
return _t($this->class.'.DESCRIPTION', $this->config()->description);
}
/**
* @return string
*/
public function getDescription() {
return _t($this->class.'.DESCRIPTION', $this->config()->description);
}
/**
* @return string - HTML
*/
public function DescriptionSegment()
{
return $this->renderWith('WidgetDescription');
}
/**
* @see WidgetController::editablesegment()
*
* @return string - HTML
*/
public function EditableSegment()
{
return $this->renderWith('WidgetEditor');
}
/**
* @return string - HTML
*/
public function DescriptionSegment() {
return $this->renderWith('WidgetDescription');
}
/**
* @see WidgetController::editablesegment()
*
* @return string - HTML
*/
public function EditableSegment() {
return $this->renderWith('WidgetEditor');
}
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = new FieldList(
new TextField('Title', $this->fieldLabel('Title'), null, 255),
new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* @return FieldList
*/
public function CMSEditor()
{
$fields = $this->getCMSFields();
$outputFields = new FieldList();
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = new FieldList(
new TextField('Title', $this->fieldLabel('Title'), null, 255),
new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* @return FieldList
*/
public function CMSEditor() {
$fields = $this->getCMSFields();
$outputFields = new FieldList();
foreach ($fields as $field) {
$name = $field->getName();
$value = $this->getField($name);
if ($value) {
$field->setValue($value);
}
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
$field->setName($name);
$outputFields->push($field);
}
foreach($fields as $field) {
$name = $field->getName();
$value = $this->getField($name);
if ($value) {
$field->setValue($value);
}
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
$field->setName($name);
$outputFields->push($field);
}
return $outputFields;
}
return $outputFields;
}
/**
* @return string
*/
public function ClassName()
{
return $this->class;
}
/**
* @return string
*/
public function ClassName() {
return $this->class;
}
/**
* @return string
*/
public function Name()
{
return "Widget[".$this->ID."]";
}
/**
* @return string
*/
public function Name() {
return "Widget[".$this->ID."]";
}
/**
* @throws Exception
*
* @return WidgetController
*/
public function getController()
{
if ($this->controller) {
return $this->controller;
}
/**
* @throws Exception
*
* @return WidgetController
*/
public function getController() {
if($this->controller) {
return $this->controller;
}
foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller";
if (class_exists($controllerClass)) {
break;
}
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller";
if(class_exists($controllerClass)) {
break;
}
$controllerClass = "{$widgetClass}Controller";
if (class_exists($controllerClass)) {
break;
}
}
$controllerClass = "{$widgetClass}Controller";
if(class_exists($controllerClass)) {
break;
}
}
if (!class_exists($controllerClass)) {
throw new Exception("Could not find controller class for $this->classname");
}
if(!class_exists($controllerClass)) {
throw new Exception("Could not find controller class for $this->classname");
}
$this->controller = Injector::inst()->create($controllerClass, $this);
$this->controller = Injector::inst()->create($controllerClass, $this);
return $this->controller;
}
/**
* @param array $data
*/
public function populateFromPostData($data) {
$fields = $this->getCMSFields();
foreach($data as $name => $value) {
if($name != "Type") {
if ($field = $fields->dataFieldByName($name)) {
$field->setValue($value);
$field->saveInto($this);
}
else {
$this->setField($name, $value);
}
}
}
//Look for checkbox fields not present in the data
foreach($fields as $field) {
if($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
$field->setValue(false);
$field->saveInto($this);
}
}
$this->write();
// The field must be written to ensure a unique ID.
$this->Name = $this->class.$this->ID;
$this->write();
}
return $this->controller;
}
/**
* @param array $data
*/
public function populateFromPostData($data)
{
$fields = $this->getCMSFields();
foreach ($data as $name => $value) {
if ($name != "Type") {
if ($field = $fields->dataFieldByName($name)) {
$field->setValue($value);
$field->saveInto($this);
} else {
$this->setField($name, $value);
}
}
}
//Look for checkbox fields not present in the data
foreach ($fields as $field) {
if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
$field->setValue(false);
$field->saveInto($this);
}
}
$this->write();
// The field must be written to ensure a unique ID.
$this->Name = $this->class.$this->ID;
$this->write();
}
}

View File

@ -5,79 +5,84 @@
*
* @package widgets
*/
class WidgetArea extends DataObject {
class WidgetArea extends DataObject
{
/**
* @var array
*/
private static $has_many = array(
"Widgets" => "Widget"
);
/**
* @var array
*/
private static $has_many = array(
"Widgets" => "Widget"
);
/**
*
* @var string
*/
public $template = __CLASS__;
/**
* Used in template instead of {@link Widgets()} to wrap each widget in its
* controller, making it easier to access and process form logic and
* actions stored in {@link WidgetController}.
*
* @return SS_List - Collection of {@link WidgetController} instances.
*/
public function WidgetControllers()
{
$controllers = new ArrayList();
/**
*
* @var string
*/
public $template = __CLASS__;
/**
* Used in template instead of {@link Widgets()} to wrap each widget in its
* controller, making it easier to access and process form logic and
* actions stored in {@link WidgetController}.
*
* @return SS_List - Collection of {@link WidgetController} instances.
*/
public function WidgetControllers() {
$controllers = new ArrayList();
foreach ($this->ItemsToRender() as $widget) {
$controller = $widget->getController();
foreach($this->ItemsToRender() as $widget) {
$controller = $widget->getController();
$controller->init();
$controllers->push($controller);
}
$controller->init();
$controllers->push($controller);
}
return $controllers;
}
return $controllers;
}
/**
* @return HasManyList
*/
public function Items()
{
return $this->getComponents('Widgets');
}
/**
* @return HasManyList
*/
public function Items() {
return $this->getComponents('Widgets');
}
/**
* @return HasManyList
*/
public function ItemsToRender()
{
return $this->getComponents('Widgets')
->filter("Enabled", 1);
}
/**
* @return HasManyList
*/
public function ItemsToRender() {
return $this->getComponents('Widgets')
->filter("Enabled", 1);
}
/**
* @return string - HTML
*/
public function forTemplate()
{
return $this->renderWith($this->template);
}
/**
* @return string - HTML
*/
public function forTemplate() {
return $this->renderWith($this->template);
}
/**
*
* @param string $template
*/
public function setTemplate($template)
{
$this->template = $template;
}
/**
*
* @param string $template
*/
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();
}
}
/**
* Delete all connected Widgets when this WidgetArea gets deleted
*/
public function onBeforeDelete()
{
parent::onBeforeDelete();
foreach ($this->Widgets() as $widget) {
$widget->delete();
}
}
}

View File

@ -3,445 +3,455 @@
* @package cms
* @subpackage tests
*/
class WidgetAreaEditorTest extends SapphireTest {
/**
* This is the widget you want to use for your unit tests.
*/
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget';
class WidgetAreaEditorTest extends SapphireTest
{
/**
* This is the widget you want to use for your unit tests.
*/
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget';
protected $extraDataObjects = array(
'WidgetAreaEditorTest_FakePage',
'WidgetAreaEditorTest_TestWidget',
);
protected $usesDatabase = true;
protected $extraDataObjects = array(
'WidgetAreaEditorTest_FakePage',
'WidgetAreaEditorTest_TestWidget',
);
protected $usesDatabase = true;
protected $requiredExtensions = array(
"SiteTree" => array(
"WidgetPageExtension"
)
);
function testFillingOneArea() {
$data = array(
'Widget' => array(
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidget',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
protected $requiredExtensions = array(
"SiteTree" => array(
"WidgetPageExtension"
)
);
public function testFillingOneArea()
{
$data = array(
'Widget' => array(
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidget',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testFillingTwoAreas() {
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
public function testFillingTwoAreas()
{
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
// Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
}
function testDeletingOneWidgetFromOneArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
// Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
}
public function testDeletingOneWidgetFromOneArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testDeletingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
public function testDeletingAWidgetFromEachArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testEditingOneWidget() {
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
public function testEditingOneWidget()
{
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
public function testEditingAWidgetFromEachArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page);
$editorBott->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$editorSide->saveInto($page);
$editorBott->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
}
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly {
private static $has_one = array(
"BottomBar" => "WidgetArea",
);
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly
{
private static $has_one = array(
"BottomBar" => "WidgetArea",
);
}
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly {
private static $cmsTitle = "Test widget";
private static $title = "Test widget";
private static $description = "Test widget";
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly
{
private static $cmsTitle = "Test widget";
private static $title = "Test widget";
private static $description = "Test widget";
}

View File

@ -3,92 +3,97 @@
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest extends FunctionalTest {
class WidgetControllerTest extends FunctionalTest
{
protected static $fixture_file = 'WidgetControllerTest.yml';
protected static $fixture_file = 'WidgetControllerTest.yml';
protected $extraDataObjects = array(
'WidgetControllerTestPage',
'WidgetControllerTest_Widget',
);
public function testWidgetFormRendering()
{
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment);
$formAction = sprintf('%s/widget/%d/Form', $page->URLSegment, $widget->ID);
$this->assertContains(
$formAction,
$response->getBody(),
"Widget forms are rendered through WidgetArea templates"
);
}
public function testWidgetFormSubmission()
{
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment);
$response = $this->submitForm('Form_Form', null, array('TestValue'=>'Updated'));
protected $extraDataObjects = array(
'WidgetControllerTestPage',
'WidgetControllerTest_Widget',
);
function testWidgetFormRendering() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment);
$formAction = sprintf('%s/widget/%d/Form', $page->URLSegment, $widget->ID);
$this->assertContains(
$formAction,
$response->getBody(),
"Widget forms are rendered through WidgetArea templates"
);
}
function testWidgetFormSubmission() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment);
$response = $this->submitForm('Form_Form', null, array('TestValue'=>'Updated'));
$this->assertContains(
'TestValue: Updated',
$response->getBody(),
"Form values are submitted to correct widget form"
);
$this->assertContains(
sprintf('Widget ID: %d', $widget->ID),
$response->getBody(),
"Widget form acts on correct widget, as identified in the URL"
);
}
$this->assertContains(
'TestValue: Updated',
$response->getBody(),
"Form values are submitted to correct widget form"
);
$this->assertContains(
sprintf('Widget ID: %d', $widget->ID),
$response->getBody(),
"Widget form acts on correct widget, as identified in the URL"
);
}
}
/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest_Widget extends Widget implements TestOnly {
private static $db = array(
'TestValue' => 'Text'
);
class WidgetControllerTest_Widget extends Widget implements TestOnly
{
private static $db = array(
'TestValue' => 'Text'
);
}
/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly {
private static $allowed_actions = array(
'Form'
);
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly
{
private static $allowed_actions = array(
'Form'
);
function Form() {
$widgetform = new Form(
$this,
'Form',
new FieldList(
new TextField('TestValue')
),
new FieldList(
new FormAction('doAction')
)
);
public function Form()
{
$widgetform = new Form(
$this,
'Form',
new FieldList(
new TextField('TestValue')
),
new FieldList(
new FormAction('doAction')
)
);
return $widgetform;
}
function doAction($data, $form) {
return sprintf('TestValue: %s\nWidget ID: %d',
$data['TestValue'],
$this->widget->ID
);
}
return $widgetform;
}
public function doAction($data, $form)
{
return sprintf('TestValue: %s\nWidget ID: %d',
$data['TestValue'],
$this->widget->ID
);
}
}

View File

@ -3,25 +3,27 @@
* @package cms
* @subpackage tests
*/
class WidgetControllerTestPage extends Page implements TestOnly {
private static $has_one = array(
'WidgetControllerTestSidebar' => 'WidgetArea'
);
class WidgetControllerTestPage extends Page implements TestOnly
{
private static $has_one = array(
'WidgetControllerTestSidebar' => 'WidgetArea'
);
}
/**
* @package cms
* @subpackage tests
*/
class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly {
/**
* Template selection doesnt work in test folders,
* so we enforce a template name.
*/
function getViewer($action) {
$templates = array('WidgetControllerTestPage');
return new SSViewer($templates);
}
class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly
{
/**
* Template selection doesnt work in test folders,
* so we enforce a template name.
*/
public function getViewer($action)
{
$templates = array('WidgetControllerTestPage');
return new SSViewer($templates);
}
}