2012-06-22 04:03:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Add this to ContentController to enable widgets
|
2013-05-07 10:56:38 +02:00
|
|
|
*
|
|
|
|
* @package widgets
|
2012-06-22 04:03:37 +02:00
|
|
|
*/
|
|
|
|
class WidgetContentControllerExtension extends Extension {
|
2012-10-05 23:39:49 +02:00
|
|
|
|
2012-10-05 23:47:26 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $allowed_actions = array(
|
2012-06-22 04:03:37 +02:00
|
|
|
'handleWidget'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2012-06-22 04:03:37 +02:00
|
|
|
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
|
|
|
|
*
|
|
|
|
* @return RequestHandler
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function handleWidget() {
|
2012-06-22 04:03:37 +02:00
|
|
|
$SQL_id = $this->owner->getRequest()->param('ID');
|
|
|
|
if(!$SQL_id) return false;
|
|
|
|
|
|
|
|
// find WidgetArea relations
|
|
|
|
$widgetAreaRelations = array();
|
|
|
|
$hasOnes = $this->owner->data()->has_one();
|
2013-05-07 10:56:38 +02:00
|
|
|
|
|
|
|
if(!$hasOnes) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-22 04:03:37 +02:00
|
|
|
foreach($hasOnes as $hasOneName => $hasOneClass) {
|
|
|
|
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
|
|
|
|
$widgetAreaRelations[] = $hasOneName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find widget
|
|
|
|
$widget = null;
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-06-22 04:03:37 +02:00
|
|
|
foreach($widgetAreaRelations as $widgetAreaRelation) {
|
2013-05-07 10:56:38 +02:00
|
|
|
if($widget) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-22 04:03:37 +02:00
|
|
|
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets(
|
|
|
|
sprintf('"Widget"."ID" = %d', $SQL_id)
|
|
|
|
)->First();
|
|
|
|
}
|
|
|
|
|
2013-05-07 10:56:38 +02:00
|
|
|
if(!$widget) {
|
|
|
|
user_error('No widget found', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $widget->getController();
|
2012-06-22 04:03:37 +02:00
|
|
|
}
|
2013-05-09 18:14:28 +02:00
|
|
|
}
|