2012-04-18 23:15:45 +02:00
|
|
|
<?php
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
/**
|
|
|
|
* Represents a set of widgets shown on a page.
|
2013-05-07 10:56:38 +02:00
|
|
|
*
|
|
|
|
* @package widgets
|
2012-04-18 23:15:45 +02:00
|
|
|
*/
|
|
|
|
class WidgetArea extends DataObject {
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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"
|
|
|
|
);
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-04-18 23:15:45 +02:00
|
|
|
public $template = __CLASS__;
|
|
|
|
|
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* 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
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* @return SS_List - Collection of {@link WidgetController} instances.
|
2012-04-18 23:15:45 +02:00
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function WidgetControllers() {
|
2012-04-18 23:15:45 +02:00
|
|
|
$controllers = new ArrayList();
|
|
|
|
|
|
|
|
foreach($this->ItemsToRender() as $widget) {
|
2013-05-07 10:56:38 +02:00
|
|
|
$controller = $widget->getController();
|
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
$controller->init();
|
|
|
|
$controllers->push($controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $controllers;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasManyList
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Items() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->getComponents('Widgets');
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasManyList
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function ItemsToRender() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1");
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string - HTML
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function forTemplate() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith($this->template);
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function setTemplate($template) {
|
2012-04-18 23:15:45 +02:00
|
|
|
$this->template = $template;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all connected Widgets when this WidgetArea gets deleted
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function onBeforeDelete() {
|
2012-04-18 23:15:45 +02:00
|
|
|
parent::onBeforeDelete();
|
|
|
|
foreach($this->Widgets() as $widget) {
|
|
|
|
$widget->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|