2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Classes;
|
|
|
|
|
|
|
|
use Lang;
|
2015-01-28 18:03:35 +11:00
|
|
|
use ApplicationException;
|
2014-05-14 23:24:20 +10:00
|
|
|
use October\Rain\Extension\ExtensionBase;
|
2014-09-29 12:19:19 +10:00
|
|
|
use System\Traits\ViewMaker;
|
2015-03-07 12:48:39 +11:00
|
|
|
use October\Rain\Html\Helper as HtmlHelper;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller Behavior base class
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class ControllerBehavior extends ExtensionBase
|
|
|
|
{
|
2014-09-29 12:32:07 +10:00
|
|
|
use \Backend\Traits\WidgetMaker;
|
2017-06-10 04:58:29 +10:00
|
|
|
use \Backend\Traits\SessionMaker;
|
2014-05-14 23:24:20 +10:00
|
|
|
use \System\Traits\AssetMaker;
|
|
|
|
use \System\Traits\ConfigMaker;
|
2014-09-29 12:32:07 +10:00
|
|
|
use \System\Traits\ViewMaker {
|
2014-05-14 23:24:20 +10:00
|
|
|
ViewMaker::makeFileContents as localMakeFileContents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Supplied configuration.
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
2016-01-23 15:37:15 -05:00
|
|
|
* @var \Backend\Classes\Controller Reference to the back end controller.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Properties that must exist in the controller using this behavior.
|
|
|
|
*/
|
|
|
|
protected $requiredProperties = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct($controller)
|
|
|
|
{
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->viewPath = $this->configPath = $this->guessViewPath('/partials');
|
|
|
|
$this->assetPath = $this->guessViewPath('/assets', true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate controller properties
|
|
|
|
*/
|
|
|
|
foreach ($this->requiredProperties as $property) {
|
|
|
|
if (!isset($controller->{$property})) {
|
|
|
|
throw new ApplicationException(Lang::get('system::lang.behavior.missing_property', [
|
|
|
|
'class' => get_class($controller),
|
|
|
|
'property' => $property,
|
|
|
|
'behavior' => get_called_class()
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the configuration values
|
|
|
|
* @param mixed $config Config object or array
|
|
|
|
* @param array $required Required config items
|
|
|
|
*/
|
2015-02-09 20:45:53 +10:00
|
|
|
public function setConfig($config, $required = [])
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
$this->config = $this->makeConfig($config, $required);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safe accessor for configuration values.
|
2016-01-23 15:37:15 -05:00
|
|
|
* @param string $name Config name, supports array names like "field[key]"
|
|
|
|
* @param mixed $default Default value if nothing is found
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-02-07 16:30:55 +10:00
|
|
|
public function getConfig($name = null, $default = null)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2015-02-07 16:30:55 +10:00
|
|
|
/*
|
|
|
|
* Return all config
|
|
|
|
*/
|
2018-08-15 18:54:46 +02:00
|
|
|
if ($name === null) {
|
2015-02-07 16:30:55 +10:00
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/*
|
|
|
|
* Array field name, eg: field[key][key2][key3]
|
|
|
|
*/
|
2015-03-07 12:48:39 +11:00
|
|
|
$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) {
|
2015-07-18 08:50:31 +10:00
|
|
|
if (!is_array($result) || !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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protects a public method from being available as an controller action.
|
|
|
|
* These methods could be defined in a controller to override a behavior default action.
|
|
|
|
* Such methods should be defined as public, to allow the behavior object to access it.
|
|
|
|
* By default public methods of a controller are considered as actions.
|
2014-05-17 18:08:01 +02:00
|
|
|
* To prevent this occurrence, methods should be hidden by using this method.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @param mixed $methodName Specifies a method name.
|
|
|
|
*/
|
|
|
|
protected function hideAction($methodName)
|
|
|
|
{
|
2014-10-10 23:12:50 +02:00
|
|
|
if (!is_array($methodName)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$methodName = [$methodName];
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$this->controller->hiddenActions = array_merge($this->controller->hiddenActions, $methodName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes all views in context of the controller, not the behavior.
|
|
|
|
* @param string $filePath Absolute path to the view file.
|
|
|
|
* @param array $extraParams Parameters that should be available to the view.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function makeFileContents($filePath, $extraParams = [])
|
|
|
|
{
|
|
|
|
$this->controller->vars = array_merge($this->controller->vars, $this->vars);
|
|
|
|
return $this->controller->makeFileContents($filePath, $extraParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true in case if a specified method exists in the extended controller.
|
|
|
|
* @param string $methodName Specifies the method name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function controllerMethodExists($methodName)
|
|
|
|
{
|
|
|
|
return method_exists($this->controller, $methodName);
|
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|