silverstripe-widgets/code/model/WidgetArea.php

92 lines
1.7 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
/**
* Represents a set of widgets shown on a page.
* @package cms
* @subpackage widgets
*/
class WidgetArea extends DataObject {
/**
*
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $has_many = array(
2012-04-18 23:15:45 +02:00
"Widgets" => "Widget"
);
/**
*
* @var string
*/
2012-04-18 23:15:45 +02:00
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 Widget_Controller}.
*
* @return SS_List Collection of {@link Widget_Controller}
*/
public function WidgetControllers() {
2012-04-18 23:15:45 +02:00
$controllers = new ArrayList();
foreach($this->ItemsToRender() as $widget) {
// find controller
$controllerClass = '';
foreach(array_reverse(ClassInfo::ancestry($widget->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller";
if(class_exists($controllerClass)) break;
}
$controller = new $controllerClass($widget);
$controller->init();
$controllers->push($controller);
}
return $controllers;
}
/**
*
* @return HasManyList
*/
public function Items() {
2012-04-18 23:15:45 +02:00
return $this->getComponents('Widgets');
}
/**
*
* @return HasManyList
*/
public function ItemsToRender() {
2012-04-18 23:15:45 +02:00
return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1");
}
/**
*
* @return string - HTML
*/
public function forTemplate() {
2012-04-18 23:15:45 +02:00
return $this->renderWith($this->template);
}
/**
*
* @param string $template
*/
public function setTemplate($template) {
2012-04-18 23:15:45 +02:00
$this->template = $template;
}
/**
* Delete all connected Widgets when this WidgetArea gets deleted
*/
public function onBeforeDelete() {
2012-04-18 23:15:45 +02:00
parent::onBeforeDelete();
foreach($this->Widgets() as $widget) {
$widget->delete();
}
}
}