winter/modules/backend/classes/WidgetBase.php

281 lines
7.1 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Classes;
use Str;
use File;
use stdClass;
use Session;
/**
* Widget base class.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
abstract class WidgetBase
{
use \System\Traits\ViewMaker;
2014-05-14 23:24:20 +10:00
use \System\Traits\AssetMaker;
use \System\Traits\ConfigMaker;
use \Backend\Traits\WidgetMaker;
use \October\Rain\Support\Traits\Emitter;
/**
* @var object Supplied configuration.
2014-05-14 23:24:20 +10:00
*/
public $config;
/**
* @var Backend\Classes\Controller Backend controller object.
*/
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
* @return void
*/
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.
*/
2014-10-10 23:12:50 +02:00
if (!$configuration) {
2014-05-14 23:24:20 +10:00
$configuration = [];
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$this->config = $this->makeConfig($configuration);
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)) {
2014-05-14 23:24:20 +10:00
$this->alias = (isset($this->config->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();
/*
* 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 widgets primary contents.
* @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) {
2014-05-14 23:24:20 +10:00
$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
2014-09-06 21:57:23 +10:00
return Str::evalHtmlId($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 = Str::evalHtmlArray($name);
/*
* 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;
}
2014-05-14 23:24:20 +10:00
//
// Session management
//
/**
* Saves a widget related key/value pair in to session data.
* @param string $key Unique key for the data store.
* @param string $value The value to store.
* @return void
*/
protected function putSession($key, $value)
{
$sessionId = $this->makeSessionId();
$currentStore = $this->getSession();
$currentStore[$key] = $value;
Session::put($sessionId, serialize($currentStore));
}
/**
2014-05-17 18:08:01 +02:00
* Retrieves a widget related key/value pair from session data.
2014-05-14 23:24:20 +10:00
* @param string $key Unique key for the data store.
* @param string $default A default value to use when value is not found.
* @return string
*/
protected function getSession($key = null, $default = null)
{
$sessionId = $this->makeSessionId();
$currentStore = [];
2014-10-10 23:12:50 +02:00
if (Session::has($sessionId)) {
2014-05-14 23:24:20 +10:00
$currentStore = unserialize(Session::get($sessionId));
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-10 23:12:50 +02:00
if ($key === null) {
2014-05-14 23:24:20 +10:00
return $currentStore;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
return isset($currentStore[$key]) ? $currentStore[$key] : $default;
}
/**
* Returns a unique session identifier for this widget and controller action.
* @return string
*/
protected function makeSessionId()
{
// Removes Class name and "Controllers" directory
$rootNamespace = Str::getClassId(Str::getClassNamespace(Str::getClassNamespace($this->controller)));
// The controller action is intentionally omitted, session should be shared for all actions
return 'widget.' . $rootNamespace . '-' . class_basename($this->controller) . '-' . $this->getId();
2014-05-14 23:24:20 +10:00
}
/**
* Resets all session data related to this widget.
* @return void
*/
public function resetSession()
{
$sessionId = $this->makeSessionId();
Session::forget($sessionId);
}
}