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}
* 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')) {
* @var array $widgetAreaRelations[] = $hasOneName;
*/ }
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) { // 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(); }
}
return $widget->getController();
if(!$widget) { }
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 * @var Widget
*/ */
protected $widget; protected $widget;
/** /**
* @var array * @var array
*/ */
private static $allowed_actions = array( private static $allowed_actions = array(
'editablesegment' 'editablesegment'
); );
/** /**
* @param Widget $widget * @param Widget $widget
*/ */
public function __construct($widget = null) { public function __construct($widget = null)
if($widget) { {
$this->widget = $widget; if ($widget) {
$this->failover = $widget; $this->widget = $widget;
} $this->failover = $widget;
}
parent::__construct();
} parent::__construct();
}
/** /**
* @param string $action * @param string $action
* @return string * @return string
*/ */
public function Link($action = null) { public function Link($action = null)
$id = ($this->widget) ? $this->widget->ID : null; {
$segment = Controller::join_links('widget', $id, $action); $id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action);
if($page = Director::get_current_page()) {
return $page->Link($segment); if ($page = Director::get_current_page()) {
} return $page->Link($segment);
}
return Controller::curr()->Link($segment);
} return Controller::curr()->Link($segment);
}
/**
* @return Widget /**
*/ * @return Widget
public function getWidget() { */
return $this->widget; public function getWidget()
} {
return $this->widget;
/** }
* Overloaded from {@link Widget->Content()} to allow for controller / form
* linking. /**
* * Overloaded from {@link Widget->Content()} to allow for controller / form
* @return string HTML * linking.
*/ *
public function Content() { * @return string HTML
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class))); */
} 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 * Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
*/ * form linking.
public function WidgetHolder() { *
return $this->renderWith("WidgetHolder"); * @return string HTML
} */
public function WidgetHolder()
/** {
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()} return $this->renderWith("WidgetHolder");
* 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. /**
* * Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* @return string HTML * to render a administrator-view of the widget. It is assumed that this
*/ * view contains form elements which are submitted and saved through
public function editablesegment() { * {@link WidgetAreaEditor} within the CMS interface.
$className = $this->urlParams['ID']; *
if (class_exists('Translatable') && Member::currentUserID()) { * @return string HTML
// set current locale based on logged in user's locale */
$locale = Member::currentUser()->Locale; public function editablesegment()
i18n::set_locale($locale); {
} $className = $this->urlParams['ID'];
if(class_exists($className) && is_subclass_of($className, 'Widget')) { if (class_exists('Translatable') && Member::currentUserID()) {
$obj = new $className(); // set current locale based on logged in user's locale
return $obj->EditableSegment(); $locale = Member::currentUser()->Locale;
} else { i18n::set_locale($locale);
user_error("Bad widget class: $className", E_USER_WARNING); }
return "Bad widget class name given"; 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)
{
if ($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
/** foreach ($sideBar->Items() as $originalWidget) {
* @return WidgetArea $widget = $originalWidget->duplicate(false);
*/ $widget->ParentID = $duplicateWidgetArea->ID;
public function SideBarView() { $widget->write();
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) { $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 $properties
* @param array $widgetClasses *
* @param int $maxWidgets * @return string - HTML
*/ */
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) { public function FieldHolder($properties = array())
$this->MaxWidgets = $maxWidgets; {
$this->widgetClasses = $widgetClasses; Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
parent::__construct($name);
}
/** 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]);
}
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']); * @return HasManyList
} */
else if (isset($classes[0]) && $classes[0] == 'Widget') { public function UsedWidgets()
unset($classes[0]); {
} // Call class_exists() to load Widget.php earlier and avoid a segfault
class_exists('Widget');
foreach($classes as $class) {
$relationName = $this->name;
$available = Config::inst()->get($class, 'only_available_in'); $widgets = $this->form->getRecord()->getComponent($relationName)->Items();
if (!empty($available) && is_array($available)) {
if(in_array($this->Name, $available)) {
$widgets->push(singleton($class));
}
}else {
$widgets->push(singleton($class));
}
}
}
return $widgets;
}
/** 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 $widgets; /**
} * @return string
*/
public function IdxField()
{
return $this->id() . 'ID';
}
/** /**
* @return string *
*/ * @return int
public function IdxField() { */
return $this->id() . 'ID'; public function Value()
} {
$relationName = $this->name;
/** return $this->form->getRecord()->getComponent($relationName)->ID;
* }
* @return int
*/
public function Value() {
$relationName = $this->name;
return $this->form->getRecord()->getComponent($relationName)->ID; /**
} * @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record)
{
$name = $this->name;
$idName = $name . "ID";
/** $widgetarea = $record->getComponent($name);
* @param DataObjectInterface $record $widgetarea->write();
*/
public function saveInto(DataObjectInterface $record) { $record->$idName = $widgetarea->ID;
$name = $this->name;
$idName = $name . "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); if (!$this->getForm()) {
$widgetarea->write(); throw new Exception("no form");
}
$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"); $widgetData = $this->getForm()->getRequest()->requestVar('Widget');
if ($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
$widgetData = $this->getForm()->getRequest()->requestVar('Widget'); foreach ($widgetAreaData as $newWidgetID => $newWidgetData) {
if($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
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 $widget = null;
if(!is_numeric($newWidgetID)) { if ($newWidgetID) {
$newWidgetID = 0; // \"ParentID\" = '0' is for the new page
} $widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID);
$widget = null; // check if we are updating an existing widget
if($newWidgetID) { if ($widget && isset($missingWidgets[$widget->ID])) {
// \"ParentID\" = '0' is for the new page unset($missingWidgets[$widget->ID]);
$widget = Widget::get() }
->filter('ParentID', array(0, $record->$name()->ID)) }
->byID($newWidgetID);
// check if we are updating an existing widget // create a new object
if($widget && isset($missingWidgets[$widget->ID])) { if (!$widget
unset($missingWidgets[$widget->ID]); && !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) {
if(!$widget if ($widget->ParentID == 0) {
&& !empty($newWidgetData['Type']) $widget->ParentID = $record->$name()->ID;
&& 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) { $widget->populateFromPostData($newWidgetData);
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();
// 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 * @var array
*/ */
private static $casting = array( private static $has_one = array(
'CMSTitle' => 'Text', "Parent" => "WidgetArea",
'Description' => 'Text', );
);
private static $only_available_in = array();
/** /**
* @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());
}
/**
* Note: Overloaded in {@link WidgetController}.
*
* @return string HTML
*/
public function WidgetHolder()
{
return $this->renderWith("WidgetHolder");
}
public function populateDefaults() { /**
parent::populateDefaults(); * Default way to render widget in templates.
$this->setField('Title', $this->getTitle()); * @return string HTML
} */
public function forTemplate($holder = true)
/** {
* Note: Overloaded in {@link WidgetController}. if ($holder) {
* return $this->WidgetHolder();
* @return string HTML }
*/ return $this->Content();
public function WidgetHolder() { }
return $this->renderWith("WidgetHolder");
} /**
* 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
* @return string HTML * @deprecated
*/ */
public function forTemplate($holder = true){ public function Title()
if($holder){ {
return $this->WidgetHolder(); return $this->getTitle();
} }
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 * 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');
}
/**
* @see WidgetController::editablesegment()
*
* @return string - HTML
*/
public function EditableSegment()
{
return $this->renderWith('WidgetEditor');
}
/** /**
* @return string - HTML * @return FieldList
*/ */
public function DescriptionSegment() { public function getCMSFields()
return $this->renderWith('WidgetDescription'); {
} $fields = new FieldList(
new TextField('Title', $this->fieldLabel('Title'), null, 255),
/** new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
* @see WidgetController::editablesegment() );
* $this->extend('updateCMSFields', $fields);
* @return string - HTML return $fields;
*/ }
public function EditableSegment() {
return $this->renderWith('WidgetEditor'); /**
} * @return FieldList
*/
public function CMSEditor()
{
$fields = $this->getCMSFields();
$outputFields = new FieldList();
/** foreach ($fields as $field) {
* @return FieldList $name = $field->getName();
*/ $value = $this->getField($name);
public function getCMSFields() { if ($value) {
$fields = new FieldList( $field->setValue($value);
new TextField('Title', $this->fieldLabel('Title'), null, 255), }
new CheckboxField('Enabled', $this->fieldLabel('Enabled')) $name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
); $field->setName($name);
$this->extend('updateCMSFields', $fields); $outputFields->push($field);
return $fields; }
}
/**
* @return FieldList
*/
public function CMSEditor() {
$fields = $this->getCMSFields();
$outputFields = new FieldList();
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 if (class_exists($controllerClass)) {
*/ break;
public function getController() { }
if($this->controller) {
return $this->controller;
}
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) { $controllerClass = "{$widgetClass}Controller";
$controllerClass = "{$widgetClass}_Controller";
if (class_exists($controllerClass)) {
if(class_exists($controllerClass)) { break;
break; }
} }
$controllerClass = "{$widgetClass}Controller"; if (!class_exists($controllerClass)) {
throw new Exception("Could not find controller class for $this->classname");
if(class_exists($controllerClass)) { }
break;
}
}
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
/** */
* @param array $data public function populateFromPostData($data)
*/ {
public function populateFromPostData($data) { $fields = $this->getCMSFields();
$fields = $this->getCMSFields(); foreach ($data as $name => $value) {
foreach($data as $name => $value) { if ($name != "Type") {
if($name != "Type") { if ($field = $fields->dataFieldByName($name)) {
if ($field = $fields->dataFieldByName($name)) { $field->setValue($value);
$field->setValue($value); $field->saveInto($this);
$field->saveInto($this); } else {
} $this->setField($name, $value);
else { }
$this->setField($name, $value); }
} }
}
} //Look for checkbox fields not present in the data
foreach ($fields as $field) {
//Look for checkbox fields not present in the data if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
foreach($fields as $field) { $field->setValue(false);
if($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) { $field->saveInto($this);
$field->setValue(false); }
$field->saveInto($this); }
}
} $this->write();
$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
* 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();
* @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->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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorSide = new WidgetAreaEditor('SideBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList()); $editorBott = new WidgetAreaEditor('BottomBar');
$form->setRequest($request); $form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$page = new WidgetAreaEditorTest_FakePage(); $form->setRequest($request);
$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',
);
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( $this->assertContains(
'WidgetControllerTestPage', 'TestValue: Updated',
'WidgetControllerTest_Widget', $response->getBody(),
); "Form values are submitted to correct widget form"
);
function testWidgetFormRendering() { $this->assertContains(
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); sprintf('Widget ID: %d', $widget->ID),
$page->publish('Stage', 'Live'); $response->getBody(),
"Widget form acts on correct widget, as identified in the URL"
$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"
);
}
} }
/** /**
* @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( private static $allowed_actions = array(
'Form' 'Form'
); );
function Form() { public function Form()
$widgetform = new Form( {
$this, $widgetform = new Form(
'Form', $this,
new FieldList( 'Form',
new TextField('TestValue') new FieldList(
), new TextField('TestValue')
new FieldList( ),
new FormAction('doAction') new FieldList(
) new FormAction('doAction')
); )
);
return $widgetform; return $widgetform;
} }
function doAction($data, $form) { public function doAction($data, $form)
return sprintf('TestValue: %s\nWidget ID: %d', {
$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, * Template selection doesnt work in test folders,
* so we enforce a template name. * so we enforce a template name.
*/ */
function getViewer($action) { public function getViewer($action)
$templates = array('WidgetControllerTestPage'); {
$templates = array('WidgetControllerTestPage');
return new SSViewer($templates);
} return new SSViewer($templates);
}
} }