silverstripe-widgets/code/model/WidgetArea.php

84 lines
1.5 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
2012-04-18 23:15:45 +02:00
/**
* Represents a set of widgets shown on a page.
*
* @package widgets
2012-04-18 23:15:45 +02:00
*/
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 WidgetController}.
2012-04-18 23:15:45 +02:00
*
* @return SS_List - Collection of {@link WidgetController} instances.
2012-04-18 23:15:45 +02:00
*/
public function WidgetControllers() {
2012-04-18 23:15:45 +02:00
$controllers = new ArrayList();
foreach($this->ItemsToRender() as $widget) {
$controller = $widget->getController();
2012-04-18 23:15:45 +02:00
$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() {
2015-04-30 12:49:17 +02:00
return $this->getComponents('Widgets')
->filter("Enabled", 1);
2012-04-18 23:15:45 +02:00
}
/**
* @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();
}
}
}