diff --git a/_config/contentcontroller-url-handler.yml b/_config/contentcontroller-url-handler.yml new file mode 100644 index 0000000..602824c --- /dev/null +++ b/_config/contentcontroller-url-handler.yml @@ -0,0 +1,8 @@ +--- +Name: contentcontroller-url-handler +--- +ContentController: + extensions: + - WidgetContentControllerExtension + url_handlers: + 'widget/$ID!': 'handleWidget' \ No newline at end of file diff --git a/code/controller/WigetContentControllerExtension.php b/code/controller/WigetContentControllerExtension.php new file mode 100644 index 0000000..78d729f --- /dev/null +++ b/code/controller/WigetContentControllerExtension.php @@ -0,0 +1,57 @@ +/widget/. + * + * @return RequestHandler + */ + function handleWidget() { + $SQL_id = $this->owner->getRequest()->param('ID'); + if(!$SQL_id) return false; + + // find WidgetArea relations + $widgetAreaRelations = array(); + $hasOnes = $this->owner->data()->has_one(); + if(!$hasOnes) return false; + foreach($hasOnes as $hasOneName => $hasOneClass) { + if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) { + $widgetAreaRelations[] = $hasOneName; + } + } + + // find widget + $widget = null; + foreach($widgetAreaRelations as $widgetAreaRelation) { + if($widget) break; + $widget = $this->owner->data()->$widgetAreaRelation()->Widgets( + sprintf('"Widget"."ID" = %d', $SQL_id) + )->First(); + } + if(!$widget) user_error('No widget found', E_USER_ERROR); + + // find controller + $controllerClass = ''; + foreach(array_reverse(ClassInfo::ancestry($widget->class)) as $widgetClass) { + $controllerClass = "{$widgetClass}_Controller"; + if(class_exists($controllerClass)) break; + } + if(!$controllerClass) user_error( + sprintf('No controller available for %s', $widget->class), + E_USER_ERROR + ); + + return new $controllerClass($widget); + } + +} \ No newline at end of file