Version: current

     1 <?php
     2 /**
     3  * Represents a set of widgets shown on a page.
     4  * @package sapphire
     5  * @subpackage widgets
     6  */
     7 class WidgetArea extends DataObject {
     8
     9     static $db = array();
    10
    11     static $has_one = array();
    12
    13     static $has_many = array(
    14         "Widgets" => "Widget"
    15     );
    16
    17     static $many_many = array();
    18
    19     static $belongs_many_many = array();
    20
    21     public $template = __CLASS__;
    22
    23     /**
    24      * Used in template instead of {@link Widgets()}
    25      * to wrap each widget in its controller, making
    26      * it easier to access and process form logic
    27      * and actions stored in {@link Widget_Controller}.
    28      *
    29      * @return DataObjectSet Collection of {@link Widget_Controller}
    30      */
    31     function WidgetControllers() {
    32         $controllers = new DataObjectSet();
    33
    34         foreach($this->ItemsToRender() as $widget) {
    35             // find controller
    36             $controllerClass = '';
    37             foreach(array_reverse(ClassInfo::ancestry($widget->class)) as $widgetClass) {
    38                 $controllerClass = "{$widgetClass}_Controller";
    39                 if(class_exists($controllerClass)) break;
    40             }
    41             $controller = new $controllerClass($widget);
    42             $controller->init();
    43             $controllers->push($controller);
    44         }
    45
    46         return $controllers;
    47     }
    48
    49     function Items() {
    50         return $this->getComponents('Widgets');
    51     }
    52
    53     function ItemsToRender() {
    54         return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1");
    55     }
    56
    57     function forTemplate() {
    58         return $this->renderWith($this->template);
    59     }
    60
    61     function setTemplate($template) {
    62         $this->template = $template;
    63     }
    64
    65     function onBeforeDelete() {
    66         parent::onBeforeDelete();
    67         foreach($this->Widgets() as $widget) {
    68             $widget->delete();
    69         }
    70     }
    71 }
    72
    73 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -