silverstripe-widgets/code/controller/WidgetContentControllerExtension.php

69 lines
1.7 KiB
PHP
Raw Normal View History

<?php
/**
* Add this to ContentController to enable widgets
*
* @package widgets
*/
2015-11-18 05:08:21 +01:00
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;
}
2015-11-18 05:08:21 +01:00
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName;
}
}
2015-11-18 05:08:21 +01:00
// find widget
$widget = null;
2015-11-18 05:08:21 +01:00
foreach ($widgetAreaRelations as $widgetAreaRelation) {
if ($widget) {
break;
}
2015-11-18 05:08:21 +01:00
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id)
->First();
}
2015-11-18 05:08:21 +01:00
if (!$widget) {
user_error('No widget found', E_USER_ERROR);
}
return $widget->getController();
}
2013-05-09 18:14:28 +02:00
}