winter/modules/backend/classes/WidgetBase.php

217 lines
5.4 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Classes;
use October\Rain\Html\Helper as HtmlHelper;
use October\Rain\Extension\Extendable;
use stdClass;
2014-05-14 23:24:20 +10:00
/**
* Widget base class.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
abstract class WidgetBase extends Extendable
2014-05-14 23:24:20 +10:00
{
use \System\Traits\ViewMaker;
2014-05-14 23:24:20 +10:00
use \System\Traits\AssetMaker;
use \System\Traits\ConfigMaker;
use \System\Traits\EventEmitter;
use \Backend\Traits\ErrorMaker;
2014-05-14 23:24:20 +10:00
use \Backend\Traits\WidgetMaker;
use \Backend\Traits\SessionMaker;
2014-05-14 23:24:20 +10:00
/**
* @var object Supplied configuration.
2014-05-14 23:24:20 +10:00
*/
public $config;
/**
* @var \Backend\Classes\Controller Backend controller object.
2014-05-14 23:24:20 +10:00
*/
2014-05-18 15:53:47 +10:00
protected $controller;
2014-05-14 23:24:20 +10:00
/**
2015-02-28 12:43:53 +11:00
* @var string Defined alias used for this widget.
2014-05-14 23:24:20 +10:00
*/
2015-02-28 12:43:53 +11:00
public $alias;
2014-05-14 23:24:20 +10:00
/**
2015-02-28 12:43:53 +11:00
* @var string A unique alias to identify this widget.
2014-05-14 23:24:20 +10:00
*/
2015-02-28 12:43:53 +11:00
protected $defaultAlias = 'widget';
2014-05-14 23:24:20 +10:00
/**
* Constructor
* @param \Backend\Classes\Controller $controller
2014-05-17 18:08:01 +02:00
* @param array $configuration Proactive configuration definition.
2014-05-14 23:24:20 +10:00
*/
public function __construct($controller, $configuration = [])
{
$this->controller = $controller;
$this->viewPath = $this->configPath = $this->guessViewPath('/partials');
$this->assetPath = $this->guessViewPath('/assets', true);
/*
* Apply configuration values to a new config object, if a parent
* constructor hasn't done it already.
2014-05-14 23:24:20 +10:00
*/
if ($this->config === null) {
$this->config = $this->makeConfig($configuration);
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
/*
* If no alias is set by the configuration.
*/
2014-10-10 23:12:50 +02:00
if (!isset($this->alias)) {
$this->alias = $this->config->alias ?? $this->defaultAlias;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
/*
* Prepare assets used by this widget.
*/
$this->loadAssets();
parent::__construct();
2014-05-14 23:24:20 +10:00
/*
* Initialize the widget.
*/
2014-10-10 23:12:50 +02:00
if (!$this->getConfig('noInit', false)) {
2014-05-14 23:24:20 +10:00
$this->init();
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
}
/**
* Initialize the widget, called by the constructor and free from its parameters.
* @return void
*/
2014-10-10 23:12:50 +02:00
public function init()
{
}
2014-05-14 23:24:20 +10:00
/**
* Renders the widget's primary contents.
2014-05-14 23:24:20 +10:00
* @return string HTML markup supplied by this widget.
*/
2014-10-10 23:12:50 +02:00
public function render()
{
}
2014-05-14 23:24:20 +10:00
/**
* Adds widget specific asset files. Use $this->addJs() and $this->addCss()
* to register new assets to include on the page.
* @return void
*/
2014-10-10 23:12:50 +02:00
protected function loadAssets()
{
}
2014-05-14 23:24:20 +10:00
/**
* Binds a widget to the controller for safe use.
* @return void
*/
public function bindToController()
{
2014-10-10 23:12:50 +02:00
if ($this->controller->widget === null) {
$this->controller->widget = new stdClass;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$this->controller->widget->{$this->alias} = $this;
}
2015-02-28 12:43:53 +11:00
/**
* Transfers config values stored inside the $config property directly
* on to the root object properties. If no properties are defined
* all config will be transferred if it finds a matching property.
* @param array $properties
* @return void
*/
protected function fillFromConfig($properties = null)
{
if ($properties === null) {
$properties = array_keys((array) $this->config);
}
foreach ($properties as $property) {
if (property_exists($this, $property)) {
$this->{$property} = $this->getConfig($property, $this->{$property});
}
}
}
2014-05-14 23:24:20 +10:00
/**
* Returns a unique ID for this widget. Useful in creating HTML markup.
* @param string $suffix An extra string to append to the ID.
* @return string A unique identifier.
*/
public function getId($suffix = null)
{
2014-09-29 13:12:34 +10:00
$id = class_basename(get_called_class());
2014-10-10 23:12:50 +02:00
if ($this->alias != $this->defaultAlias) {
$id .= '-' . $this->alias;
2014-10-10 23:12:50 +02:00
}
2014-10-10 23:12:50 +02:00
if ($suffix !== null) {
2014-05-14 23:24:20 +10:00
$id .= '-' . $suffix;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
return HtmlHelper::nameToId($id);
2014-05-14 23:24:20 +10:00
}
/**
* Returns a fully qualified event handler name for this widget.
* @param string $name The ajax event handler name.
* @return string
*/
public function getEventHandler($name)
{
return $this->alias . '::' . $name;
}
/**
* Safe accessor for configuration values.
* @param string $name Config name, supports array names like "field[key]"
* @param string $default Default value if nothing is found
* @return string
*/
public function getConfig($name, $default = null)
{
/*
* Array field name, eg: field[key][key2][key3]
*/
$keyParts = HtmlHelper::nameToArray($name);
2014-05-14 23:24:20 +10:00
/*
* First part will be the field name, pop it off
*/
$fieldName = array_shift($keyParts);
2014-10-10 23:12:50 +02:00
if (!isset($this->config->{$fieldName})) {
2014-05-14 23:24:20 +10:00
return $default;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$result = $this->config->{$fieldName};
/*
* Loop the remaining key parts and build a result
*/
foreach ($keyParts as $key) {
2014-10-10 23:12:50 +02:00
if (!array_key_exists($key, $result)) {
2014-05-14 23:24:20 +10:00
return $default;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$result = $result[$key];
}
return $result;
}
2014-05-18 15:53:47 +10:00
/**
* Returns the controller using this widget.
*/
public function getController()
{
return $this->controller;
}
}