Merge branch '1.1' into 1.2

This commit is contained in:
Daniel Hensby 2017-01-25 13:29:00 +00:00
commit 1bbaba52e0
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 132 additions and 114 deletions

View File

@ -6,39 +6,42 @@
*/ */
class WidgetContentControllerExtension extends Extension class WidgetContentControllerExtension extends Extension
{ {
/** /**
* *
* @var array * @var array
*/ */
private static $allowed_actions = array( private static $allowed_actions = array(
'handleWidget' 'handleWidget'
); );
/** /**
* Handles widgets attached to a page through one or more {@link WidgetArea} * Handles widgets attached to a page through one or more {@link WidgetArea}
* elements. * elements.
* *
* Iterated through each $has_one relation with a {@link WidgetArea} and * Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier. * looks for connected widgets by their database identifier.
* *
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>. * Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
* *
* @return RequestHandler * @return RequestHandler
*/ */
public function handleWidget() public function handleWidget()
{ {$SQL_id = $this->owner->getRequest()->param('ID');
$SQL_id = $this->owner->getRequest()->param('ID'); if(!$SQL_id) {return false;
if (!$SQL_id) { }
return false; /** @var SiteTree $widgetOwner */
} $widgetOwner = $this->owner->data();
while($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()){
$widgetOwner = $widgetOwner->Parent();
}
// find WidgetArea relations // find WidgetArea relations
$widgetAreaRelations = array(); $widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne(); $hasOnes = $widgetOwner->hasOne();
if (!$hasOnes) { if(!$hasOnes) {
return false; return false;
} }
foreach ($hasOnes as $hasOneName => $hasOneClass) { foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) { if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
@ -54,10 +57,10 @@ class WidgetContentControllerExtension extends Extension
break; break;
} }
$widget = $this->owner->data()->$widgetAreaRelation()->Widgets() $widget = $widgetOwner->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id) ->filter('ID', $SQL_id)
->First(); ->First();
} }
if (!$widget) { if (!$widget) {
user_error('No widget found', E_USER_ERROR); user_error('No widget found', E_USER_ERROR);

View File

@ -45,80 +45,95 @@ class WidgetController extends Controller
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; $id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action); $segment = Controller::join_links('widget', $id, $action);
if ($page = Director::get_current_page()) { $page = Director::get_current_page();
return $page->Link($segment); if($page && !($page instanceof WidgetController)) {return $page->Link($segment);
} }
return Controller::curr()->Link($segment); if ($controller = $this->getParentController()) {return $controller->Link($segment);
} }
/** return $segment;
* @return Widget }
*/
public function getWidget() /**
* Cycles up the controller stack until it finds a non-widget controller
* This is needed becauseController::currreturns the widget controller,
* which means anyLinkfunction turns into endless loop.
*
* @return Controller
*/
public function getParentController()
{ {
return $this->widget; foreach(Controller::$controller_stack as $controller) {
} if (!($controller instanceof WidgetController)) {
return $controller;
}
}
return false;
}
/** /**
* Overloaded from {@link Widget->Content()} to allow for controller / form * @return Widget
* linking. */
* public function getWidget()
* @return string HTML {return $this->widget;
*/ }
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
}
/** /**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/ * Overloaded from {@link Widget->Content()} to allow for controller / form
* form linking. * linking.
* *
* @return string HTML * @return string HTML
*/ */
public function WidgetHolder() public function Content()
{ {return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
return $this->renderWith("WidgetHolder"); }
}
/** /**
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()} * Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* to render a administrator-view of the widget. It is assumed that this * form linking.
* view contains form elements which are submitted and saved through *
* {@link WidgetAreaEditor} within the CMS interface. * @return string HTML
* */
* @return string HTML public function WidgetHolder()
*/ {return $this->renderWith("WidgetHolder");
public function editablesegment() }
{
/**
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* 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.
*
* @return string HTML
*/
public function editablesegment() {
// use left and main to set the html config // use left and main to set the html config
$leftandmain = LeftAndMain::create(); $leftandmain = LeftAndMain::create();
$leftandmain->init(); $leftandmain->init();
$className = $this->urlParams['ID'];
$className = $this->urlParams['ID']; if (class_exists('Translatable') && Member::currentUserID()) {
if (class_exists('Translatable') && Member::currentUserID()) { // set current locale based on logged in user's locale
// set current locale based on logged in user's locale $locale = Member::currentUser()->Locale;
$locale = Member::currentUser()->Locale; i18n::set_locale($locale);
i18n::set_locale($locale); }
} if(class_exists($className) && is_subclass_of($className, 'Widget')) {
if (class_exists($className) && is_subclass_of($className, 'Widget')) { $obj = new $className();
$obj = new $className(); return $obj->EditableSegment();
return $obj->EditableSegment(); } else {
} else { user_error("Bad widget class: $className", E_USER_WARNING);
user_error("Bad widget class: $className", E_USER_WARNING); return "Bad widget class name given";
return "Bad widget class name given"; }
} }
}
} }
/** /**