Merge pull request #107 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-19 17:34:49 +00:00
commit dfe1236eef
9 changed files with 1233 additions and 1177 deletions

View File

@ -4,62 +4,65 @@
* *
* @package widgets * @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}
* @var array * elements.
*/ *
private static $allowed_actions = array( * Iterated through each $has_one relation with a {@link WidgetArea} and
'handleWidget' * 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
* Handles widgets attached to a page through one or more {@link WidgetArea} $widgetAreaRelations = array();
* elements. $hasOnes = $this->owner->data()->hasOne();
*
* 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 if (!$hasOnes) {
$widgetAreaRelations = array(); return false;
$hasOnes = $this->owner->data()->hasOne(); }
if(!$hasOnes) { foreach ($hasOnes as $hasOneName => $hasOneClass) {
return false; if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
} $widgetAreaRelations[] = $hasOneName;
}
}
foreach($hasOnes as $hasOneName => $hasOneClass) { // find widget
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) { $widget = null;
$widgetAreaRelations[] = $hasOneName;
}
}
// find widget foreach ($widgetAreaRelations as $widgetAreaRelation) {
$widget = null; if ($widget) {
break;
}
foreach($widgetAreaRelations as $widgetAreaRelation) { $widget = $this->owner->data()->$widgetAreaRelation()->Widgets()
if($widget) { ->filter('ID', $SQL_id)
break; ->First();
} }
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets() if (!$widget) {
->filter('ID', $SQL_id) user_error('No widget found', E_USER_ERROR);
->First(); }
}
if(!$widget) { return $widget->getController();
user_error('No widget found', E_USER_ERROR); }
}
return $widget->getController();
}
} }

View File

@ -18,103 +18,109 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetController extends Controller { class WidgetController extends Controller
{
/**
* @var Widget
*/
protected $widget;
/** /**
* @var Widget * @var array
*/ */
protected $widget; private static $allowed_actions = array(
'editablesegment'
);
/** /**
* @var array * @param Widget $widget
*/ */
private static $allowed_actions = array( public function __construct($widget = null)
'editablesegment' {
); 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()) {
* @param string $action return $page->Link($segment);
* @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 Controller::curr()->Link($segment);
return $page->Link($segment); }
}
return Controller::curr()->Link($segment); /**
} * @return Widget
*/
public function getWidget()
{
return $this->widget;
}
/** /**
* @return Widget * Overloaded from {@link Widget->Content()} to allow for controller / form
*/ * linking.
public function getWidget() { *
return $this->widget; * @return string HTML
} */
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
}
/** /**
* Overloaded from {@link Widget->Content()} to allow for controller / form * Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* linking. * form linking.
* *
* @return string HTML * @return string HTML
*/ */
public function Content() { public function WidgetHolder()
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class))); {
} return $this->renderWith("WidgetHolder");
}
/** /**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/ * Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* form linking. * to render a administrator-view of the widget. It is assumed that this
* * view contains form elements which are submitted and saved through
* @return string HTML * {@link WidgetAreaEditor} within the CMS interface.
*/ *
public function WidgetHolder() { * @return string HTML
return $this->renderWith("WidgetHolder"); */
} public function editablesegment()
{
/** $className = $this->urlParams['ID'];
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()} if (class_exists('Translatable') && Member::currentUserID()) {
* to render a administrator-view of the widget. It is assumed that this // set current locale based on logged in user's locale
* view contains form elements which are submitted and saved through $locale = Member::currentUser()->Locale;
* {@link WidgetAreaEditor} within the CMS interface. i18n::set_locale($locale);
* }
* @return string HTML if (class_exists($className) && is_subclass_of($className, 'Widget')) {
*/ $obj = new $className();
public function editablesegment() { return $obj->EditableSegment();
$className = $this->urlParams['ID']; } else {
if (class_exists('Translatable') && Member::currentUserID()) { user_error("Bad widget class: $className", E_USER_WARNING);
// set current locale based on logged in user's locale return "Bad widget class name given";
$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 * @deprecated Use WidgetController
* @package widgets * @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. * feel free to create your own relationships, naming conventions, etc.
* without using this class. * without using this class.
*/ */
class WidgetPageExtension extends DataExtension { class WidgetPageExtension extends DataExtension
{
private static $db = array(
'InheritSideBar' => 'Boolean',
);
private static $db = array( private static $defaults = array(
'InheritSideBar' => 'Boolean', 'InheritSideBar' => true
); );
private static $defaults = array( private static $has_one = array(
'InheritSideBar' => true 'SideBar' => 'WidgetArea'
); );
private static $has_one = array( public function updateCMSFields(FieldList $fields)
'SideBar' => 'WidgetArea' {
); $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( * @return WidgetArea
"Root.Widgets", */
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent') public function SideBarView()
); {
$fields->addFieldToTab( if (
"Root.Widgets", $this->owner->InheritSideBar
new WidgetAreaEditor("SideBar") && ($parent = $this->owner->getParent())
); && $parent->hasMethod('SideBarView')
} ) {
return $parent->SideBarView();
} elseif ($this->owner->SideBar()->exists()) {
return $this->owner->SideBar();
}
}
/** public function onBeforeDuplicate($duplicatePage)
* @return WidgetArea {
*/ if ($this->owner->hasField('SideBarID')) {
public function SideBarView() { $sideBar = $this->owner->getComponent('SideBar');
if( $duplicateWidgetArea = $sideBar->duplicate();
$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) { foreach ($sideBar->Items() as $originalWidget) {
if($this->owner->hasField('SideBarID')) { $widget = $originalWidget->duplicate(false);
$sideBar = $this->owner->getComponent('SideBar'); $widget->ParentID = $duplicateWidgetArea->ID;
$duplicateWidgetArea = $sideBar->duplicate(); $widget->write();
}
foreach($sideBar->Items() as $originalWidget) { $duplicatePage->SideBarID = $duplicateWidgetArea->ID;
$widget = $originalWidget->duplicate(false); }
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID; return $duplicatePage;
}
}
return $duplicatePage;
}
/**
* Support Translatable so that we don't link WidgetAreas across translations
*/
public function onTranslatableCreate() {
//reset the sidebar ID
$this->owner->SideBarID = 0;
}
/**
* 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 * @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');
/** return $this->renderWith("WidgetAreaEditor");
* @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 ArrayList
*/
public function AvailableWidgets()
{
$widgets= new ArrayList();
/** foreach ($this->widgetClasses as $widgetClass) {
* $classes = ClassInfo::subclassesFor($widgetClass);
* @return ArrayList
*/
public function AvailableWidgets() {
$widgets= new ArrayList();
foreach($this->widgetClasses as $widgetClass) { if (isset($classes['Widget'])) {
$classes = ClassInfo::subclassesFor($widgetClass); unset($classes['Widget']);
} elseif (isset($classes[0]) && $classes[0] == 'Widget') {
unset($classes[0]);
}
if (isset($classes['Widget'])) { foreach ($classes as $class) {
unset($classes['Widget']); $available = Config::inst()->get($class, 'only_available_in');
}
else if (isset($classes[0]) && $classes[0] == 'Widget') {
unset($classes[0]);
}
foreach($classes as $class) { if (!empty($available) && is_array($available)) {
if (in_array($this->Name, $available)) {
$widgets->push(singleton($class));
}
} else {
$widgets->push(singleton($class));
}
}
}
$available = Config::inst()->get($class, 'only_available_in'); return $widgets;
}
if (!empty($available) && is_array($available)) { /**
if(in_array($this->Name, $available)) { * @return HasManyList
$widgets->push(singleton($class)); */
} public function UsedWidgets()
}else { {
$widgets->push(singleton($class)); // Call class_exists() to load Widget.php earlier and avoid a segfault
} class_exists('Widget');
}
}
return $widgets; $relationName = $this->name;
} $widgets = $this->form->getRecord()->getComponent($relationName)->Items();
/** 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 string
*/
public function IdxField()
{
return $this->id() . 'ID';
}
return $widgets; /**
} *
* @return int
*/
public function Value()
{
$relationName = $this->name;
/** return $this->form->getRecord()->getComponent($relationName)->ID;
* @return string }
*/
public function IdxField() {
return $this->id() . 'ID';
}
/** /**
* * @param DataObjectInterface $record
* @return int */
*/ public function saveInto(DataObjectInterface $record)
public function Value() { {
$relationName = $this->name; $name = $this->name;
$idName = $name . "ID";
return $this->form->getRecord()->getComponent($relationName)->ID; $widgetarea = $record->getComponent($name);
} $widgetarea->write();
/** $record->$idName = $widgetarea->ID;
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record) {
$name = $this->name;
$idName = $name . "ID";
$widgetarea = $record->getComponent($name); $widgets = $widgetarea->Items();
$widgetarea->write();
$record->$idName = $widgetarea->ID; // store the field IDs and delete the missing fields
// alternatively, we could delete all the fields and re add them
$missingWidgets = array();
$widgets = $widgetarea->Items(); if ($widgets) {
foreach ($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget;
}
}
// store the field IDs and delete the missing fields if (!$this->getForm()) {
// alternatively, we could delete all the fields and re add them throw new Exception("no form");
$missingWidgets = array(); }
if($widgets) { $widgetData = $this->getForm()->getRequest()->requestVar('Widget');
foreach($widgets as $existingWidget) { if ($widgetData && isset($widgetData[$this->getName()])) {
$missingWidgets[$existingWidget->ID] = $existingWidget; $widgetAreaData = $widgetData[$this->getName()];
}
}
if(!$this->getForm()) throw new Exception("no form"); foreach ($widgetAreaData as $newWidgetID => $newWidgetData) {
$widgetData = $this->getForm()->getRequest()->requestVar('Widget'); // Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if($widgetData && isset($widgetData[$this->getName()])) { if (!is_numeric($newWidgetID)) {
$widgetAreaData = $widgetData[$this->getName()]; $newWidgetID = 0;
}
foreach($widgetAreaData as $newWidgetID => $newWidgetData) { $widget = null;
if ($newWidgetID) {
// \"ParentID\" = '0' is for the new page
$widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID);
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query // check if we are updating an existing widget
if(!is_numeric($newWidgetID)) { if ($widget && isset($missingWidgets[$widget->ID])) {
$newWidgetID = 0; unset($missingWidgets[$widget->ID]);
} }
}
$widget = null; // create a new object
if($newWidgetID) { if (!$widget
// \"ParentID\" = '0' is for the new page && !empty($newWidgetData['Type'])
$widget = Widget::get() && class_exists($newWidgetData['Type'])
->filter('ParentID', array(0, $record->$name()->ID)) && is_subclass_of($newWidgetData['Type'], 'Widget')
->byID($newWidgetID); ) {
$widget = Injector::inst()->create($newWidgetData['Type']);
$widget->ID = 0;
$widget->ParentID = $record->$name()->ID;
}
// check if we are updating an existing widget if ($widget) {
if($widget && isset($missingWidgets[$widget->ID])) { if ($widget->ParentID == 0) {
unset($missingWidgets[$widget->ID]); $widget->ParentID = $record->$name()->ID;
} }
}
// create a new object $widget->populateFromPostData($newWidgetData);
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) { // remove the fields not saved
if($widget->ParentID == 0) { if ($missingWidgets) {
$widget->ParentID = $record->$name()->ID; 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 * @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 * @var array
*/ */
private static $db = array( private static $defaults = array(
"Title" => "Varchar(255)", 'Enabled' => true,
"Sort" => "Int", );
"Enabled" => "Boolean",
);
/** /**
* @var array * @var array
*/ */
private static $defaults = array( private static $casting = array(
'Enabled' => true, '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 * @var string
*/ */
private static $has_one = array( private static $default_sort = "\"Sort\"";
"Parent" => "WidgetArea",
);
/** /**
* @var string * @var string
*/ */
private static $default_sort = "\"Sort\""; private static $title = "Widget Title";
/** /**
* @var string * @var string
*/ */
private static $title = "Widget Title"; private static $cmsTitle = "Name of this widget";
/** /**
* @var string * @var string
*/ */
private static $cmsTitle = "Name of this widget"; private static $description = "Description of what this widget does.";
/** /**
* @var string * @var array
*/ */
private static $description = "Description of what this widget does."; private static $summary_fields = array(
'CMSTitle' => 'Title'
);
/** /**
* @var array * @var WidgetController
*/ */
private static $summary_fields = array( protected $controller;
'CMSTitle' => 'Title'
);
/** public function populateDefaults()
* @var WidgetController {
*/ parent::populateDefaults();
protected $controller; $this->setField('Title', $this->getTitle());
}
public function populateDefaults() { /**
parent::populateDefaults(); * Note: Overloaded in {@link WidgetController}.
$this->setField('Title', $this->getTitle()); *
} * @return string HTML
*/
public function WidgetHolder()
{
return $this->renderWith("WidgetHolder");
}
/** /**
* Note: Overloaded in {@link WidgetController}. * Default way to render widget in templates.
* * @return string HTML
* @return string HTML */
*/ public function forTemplate($holder = true)
public function WidgetHolder() { {
return $this->renderWith("WidgetHolder"); if ($holder) {
} return $this->WidgetHolder();
}
return $this->Content();
}
/** /**
* Default way to render widget in templates. * Renders the widget content in a custom template with the same name as the
* @return string HTML * current class. This should be the main point of output customization.
*/ *
public function forTemplate($holder = true){ * Invoked from within WidgetHolder.ss, which contains the "framing" around
if($holder){ * the custom content, like a title.
return $this->WidgetHolder(); *
} * Note: Overloaded in {@link WidgetController}.
return $this->Content(); *
} * @return string HTML
*/
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
}
/** /**
* Renders the widget content in a custom template with the same name as the * @return string
* current class. This should be the main point of output customization. * @deprecated
* */
* Invoked from within WidgetHolder.ss, which contains the "framing" around public function Title()
* the custom content, like a title. {
* return $this->getTitle();
* Note: Overloaded in {@link WidgetController}. }
*
* @return string HTML
*/
public function Content() {
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
}
/** /**
* @return string * Get the frontend title for this widget
* @deprecated *
*/ * @return string
public function Title() { */
return $this->getTitle(); public function getTitle()
} {
return $this->getField('Title')
?: _t($this->class.'.TITLE', $this->config()->title);
}
/** /**
* Get the frontend title for this widget * @return string
* * @deprecated
* @return string */
*/ public function CMSTitle()
public function getTitle() { {
return $this->getField('Title') return $this->getCMSTitle();
?: _t($this->class.'.TITLE', $this->config()->title); }
}
/** /**
* @return string * @return string
* @deprecated */
*/ public function getCMSTitle()
public function CMSTitle() { {
return $this->getCMSTitle(); return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
} }
/** /**
* @return string * @return string
*/ * @deprecated
public function getCMSTitle() { */
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle); public function Description()
} {
return $this->getDescription();
}
/** /**
* @return string * @return string
* @deprecated */
*/ public function getDescription()
public function Description() { {
return $this->getDescription(); return _t($this->class.'.DESCRIPTION', $this->config()->description);
} }
/** /**
* @return string * @return string - HTML
*/ */
public function getDescription() { public function DescriptionSegment()
return _t($this->class.'.DESCRIPTION', $this->config()->description); {
} return $this->renderWith('WidgetDescription');
}
/** /**
* @return string - HTML * @see WidgetController::editablesegment()
*/ *
public function DescriptionSegment() { * @return string - HTML
return $this->renderWith('WidgetDescription'); */
} public function EditableSegment()
{
return $this->renderWith('WidgetEditor');
}
/** /**
* @see WidgetController::editablesegment() * @return FieldList
* */
* @return string - HTML public function getCMSFields()
*/ {
public function EditableSegment() { $fields = new FieldList(
return $this->renderWith('WidgetEditor'); new TextField('Title', $this->fieldLabel('Title'), null, 255),
} new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
/** /**
* @return FieldList * @return FieldList
*/ */
public function getCMSFields() { public function CMSEditor()
$fields = new FieldList( {
new TextField('Title', $this->fieldLabel('Title'), null, 255), $fields = $this->getCMSFields();
new CheckboxField('Enabled', $this->fieldLabel('Enabled')) $outputFields = new FieldList();
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
/** foreach ($fields as $field) {
* @return FieldList $name = $field->getName();
*/ $value = $this->getField($name);
public function CMSEditor() { if ($value) {
$fields = $this->getCMSFields(); $field->setValue($value);
$outputFields = new FieldList(); }
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
$field->setName($name);
$outputFields->push($field);
}
foreach($fields as $field) { return $outputFields;
$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 string
*/
public function ClassName()
{
return $this->class;
}
/** /**
* @return string * @return string
*/ */
public function ClassName() { public function Name()
return $this->class; {
} return "Widget[".$this->ID."]";
}
/** /**
* @return string * @throws Exception
*/ *
public function Name() { * @return WidgetController
return "Widget[".$this->ID."]"; */
} public function getController()
{
if ($this->controller) {
return $this->controller;
}
/** foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
* @throws Exception $controllerClass = "{$widgetClass}_Controller";
*
* @return WidgetController
*/
public function getController() {
if($this->controller) {
return $this->controller;
}
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) { if (class_exists($controllerClass)) {
$controllerClass = "{$widgetClass}_Controller"; break;
}
if(class_exists($controllerClass)) { $controllerClass = "{$widgetClass}Controller";
break;
}
$controllerClass = "{$widgetClass}Controller"; if (class_exists($controllerClass)) {
break;
}
}
if(class_exists($controllerClass)) { if (!class_exists($controllerClass)) {
break; throw new Exception("Could not find controller class for $this->classname");
} }
}
if(!class_exists($controllerClass)) { $this->controller = Injector::inst()->create($controllerClass, $this);
throw new Exception("Could not find controller class for $this->classname");
}
$this->controller = Injector::inst()->create($controllerClass, $this); return $this->controller;
}
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
* @param array $data foreach ($fields as $field) {
*/ if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
public function populateFromPostData($data) { $field->setValue(false);
$fields = $this->getCMSFields(); $field->saveInto($this);
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 $this->write();
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;
// The field must be written to ensure a unique ID. $this->write();
$this->Name = $this->class.$this->ID; }
$this->write();
}
} }

View File

@ -5,79 +5,84 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetArea extends DataObject { class WidgetArea extends DataObject
{
/**
* @var array
*/
private static $has_many = array(
"Widgets" => "Widget"
);
/** /**
* @var array *
*/ * @var string
private static $has_many = array( */
"Widgets" => "Widget" public $template = __CLASS__;
);
/** /**
* * Used in template instead of {@link Widgets()} to wrap each widget in its
* @var string * controller, making it easier to access and process form logic and
*/ * actions stored in {@link WidgetController}.
public $template = __CLASS__; *
* @return SS_List - Collection of {@link WidgetController} instances.
*/
public function WidgetControllers()
{
$controllers = new ArrayList();
/** foreach ($this->ItemsToRender() as $widget) {
* Used in template instead of {@link Widgets()} to wrap each widget in its $controller = $widget->getController();
* 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->init();
$controller = $widget->getController(); $controllers->push($controller);
}
$controller->init(); return $controllers;
$controllers->push($controller); }
}
return $controllers; /**
} * @return HasManyList
*/
public function Items()
{
return $this->getComponents('Widgets');
}
/** /**
* @return HasManyList * @return HasManyList
*/ */
public function Items() { public function ItemsToRender()
return $this->getComponents('Widgets'); {
} return $this->getComponents('Widgets')
->filter("Enabled", 1);
}
/** /**
* @return HasManyList * @return string - HTML
*/ */
public function ItemsToRender() { public function forTemplate()
return $this->getComponents('Widgets') {
->filter("Enabled", 1); return $this->renderWith($this->template);
} }
/** /**
* @return string - HTML *
*/ * @param string $template
public function forTemplate() { */
return $this->renderWith($this->template); public function setTemplate($template)
} {
$this->template = $template;
}
/** /**
* * Delete all connected Widgets when this WidgetArea gets deleted
* @param string $template */
*/ public function onBeforeDelete()
public function setTemplate($template) { {
$this->template = $template; 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 * @package cms
* @subpackage tests * @subpackage tests
*/ */
class WidgetAreaEditorTest extends SapphireTest { class WidgetAreaEditorTest extends SapphireTest
/** {
* This is the widget you want to use for your unit tests. /**
*/ * This is the widget you want to use for your unit tests.
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget'; */
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget';
protected $extraDataObjects = array( protected $extraDataObjects = array(
'WidgetAreaEditorTest_FakePage', 'WidgetAreaEditorTest_FakePage',
'WidgetAreaEditorTest_TestWidget', 'WidgetAreaEditorTest_TestWidget',
); );
protected $usesDatabase = true; protected $usesDatabase = true;
protected $requiredExtensions = array( protected $requiredExtensions = array(
"SiteTree" => array( "SiteTree" => array(
"WidgetPageExtension" "WidgetPageExtension"
) )
); );
function testFillingOneArea() { public function testFillingOneArea()
$data = array( {
'Widget' => array( $data = array(
'BottomBar' => array( 'Widget' => array(
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidget', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidget',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
} }
function testFillingTwoAreas() { public function testFillingTwoAreas()
$data = array( {
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
// Make sure they both got saved // Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
} }
function testDeletingOneWidgetFromOneArea() { public function testDeletingOneWidgetFromOneArea()
// First get some widgets in there {
$data = array( // First get some widgets in there
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
), ),
'BottomBar' => array( 'BottomBar' => array(
$bottWidgets[0]->ID => array( $bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom', 'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
} }
function testDeletingAWidgetFromEachArea() { public function testDeletingAWidgetFromEachArea()
// First get some widgets in there {
$data = array( // First get some widgets in there
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
), ),
'BottomBar' => array( 'BottomBar' => array(
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
} }
function testEditingOneWidget() { public function testEditingOneWidget()
// First get some widgets in there {
$data = array( // First get some widgets in there
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
), ),
'BottomBar' => array( 'BottomBar' => array(
$bottWidgets[0]->ID => array( $bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom', 'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
} }
function testEditingAWidgetFromEachArea() { public function testEditingAWidgetFromEachArea()
// First get some widgets in there {
$data = array( // First get some widgets in there
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
), ),
'BottomBar' => array( 'BottomBar' => array(
$bottWidgets[0]->ID => array( $bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom-edited', 'Title' => 'MyTestWidgetBottom-edited',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
} }
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() { public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
// First get some widgets in there {
$data = array( // First get some widgets in there
'Widget' => array( $data = array(
'SideBar' => array( 'Widget' => array(
'new-1' => array( 'SideBar' => array(
'Title' => 'MyTestWidgetSide', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetSide',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
), )
'BottomBar' => array( ),
'new-1' => array( 'BottomBar' => array(
'Title' => 'MyTestWidgetBottom', 'new-1' => array(
'Type' => $this->widgetToTest, 'Title' => 'MyTestWidgetBottom',
'Sort' => 0 'Type' => $this->widgetToTest,
) 'Sort' => 0
) )
) )
); )
$request = new SS_HTTPRequest('get', 'post', array(), $data); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $editorSide->saveInto($page);
$editorBott->saveInto($page); $editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest, 'Type' => $this->widgetToTest,
'Sort' => 0 'Sort' => 0
) )
), ),
'BottomBar' => array( 'BottomBar' => array(
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
} }
} }
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly { class WidgetAreaEditorTest_FakePage extends Page implements TestOnly
private static $has_one = array( {
"BottomBar" => "WidgetArea", private static $has_one = array(
); "BottomBar" => "WidgetArea",
);
} }
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly { class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly
private static $cmsTitle = "Test widget"; {
private static $title = "Test widget"; private static $cmsTitle = "Test widget";
private static $description = "Test widget"; private static $title = "Test widget";
private static $description = "Test widget";
} }

View File

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

View File

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