2012-04-18 23:15:45 +02:00
|
|
|
<?php
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2017-01-17 23:43:11 +01:00
|
|
|
namespace SilverStripe\Widgets\Model;
|
|
|
|
|
2017-12-19 02:20:20 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-01-17 23:43:11 +01:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-12-19 02:20:20 +01:00
|
|
|
use SilverStripe\ORM\HasManyList;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-01-17 23:43:11 +01:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
/**
|
|
|
|
* Represents a set of widgets shown on a page.
|
|
|
|
*/
|
2015-11-18 05:08:21 +01:00
|
|
|
class WidgetArea extends DataObject
|
|
|
|
{
|
2017-12-19 02:20:20 +01:00
|
|
|
private static $has_many = [
|
2017-01-17 23:43:11 +01:00
|
|
|
"Widgets" => Widget::class
|
2017-12-19 02:20:20 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $owns = [
|
|
|
|
'Widgets',
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $cascade_deletes = [
|
|
|
|
'Widgets',
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $extensions = [
|
2017-12-20 22:45:55 +01:00
|
|
|
Versioned::class,
|
2017-12-19 02:20:20 +01:00
|
|
|
];
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2017-01-17 23:43:11 +01:00
|
|
|
private static $table_name = 'WidgetArea';
|
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
public $template = __CLASS__;
|
2017-01-17 23:43:11 +01:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
2017-01-17 23:43:11 +01: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
|
2015-11-18 05:08:21 +01:00
|
|
|
* actions stored in {@link WidgetController}.
|
2017-01-17 23:43:11 +01:00
|
|
|
*
|
2015-11-18 05:08:21 +01:00
|
|
|
* @return SS_List - Collection of {@link WidgetController} instances.
|
|
|
|
*/
|
|
|
|
public function WidgetControllers()
|
|
|
|
{
|
|
|
|
$controllers = new ArrayList();
|
2015-12-06 01:04:35 +01:00
|
|
|
$items = $this->ItemsToRender();
|
2017-01-17 23:43:11 +01:00
|
|
|
if (!is_null($items)) {
|
2015-12-06 01:04:35 +01:00
|
|
|
foreach ($items as $widget) {
|
2017-12-19 02:20:20 +01:00
|
|
|
/** @var Widget $widget */
|
|
|
|
|
|
|
|
/** @var Controller $controller */
|
2015-12-06 01:04:35 +01:00
|
|
|
$controller = $widget->getController();
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2017-01-17 23:43:11 +01:00
|
|
|
$controller->doInit();
|
2015-12-06 01:04:35 +01:00
|
|
|
$controllers->push($controller);
|
|
|
|
}
|
2015-11-18 05:08:21 +01:00
|
|
|
}
|
|
|
|
return $controllers;
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
|
|
|
* @return HasManyList
|
|
|
|
*/
|
|
|
|
public function Items()
|
|
|
|
{
|
2017-12-19 02:20:20 +01:00
|
|
|
return $this->Widgets();
|
2015-11-18 05:08:21 +01:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
|
|
|
* @return HasManyList
|
|
|
|
*/
|
|
|
|
public function ItemsToRender()
|
|
|
|
{
|
2017-12-19 02:20:20 +01:00
|
|
|
return $this->Items()->filter('Enabled', 1);
|
2015-11-18 05:08:21 +01:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
|
|
|
* @return string - HTML
|
|
|
|
*/
|
|
|
|
public function forTemplate()
|
|
|
|
{
|
|
|
|
return $this->renderWith($this->template);
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
*/
|
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|