ORIENT/modules/backend/traits/InspectableContainer.php

74 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace Backend\Traits;
2019-07-12 15:42:22 +00:00
use Lang;
2014-10-10 23:37:48 +00:00
use Request;
2015-01-28 07:03:35 +00:00
use ApplicationException;
2014-05-14 13:24:20 +00:00
/**
* Inspectable Container Trait
* Extension for controllers that can host inspectable widgets (Components, etc.)
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
trait InspectableContainer
{
public function onInspectableGetOptions()
{
2016-04-12 08:04:15 +00:00
// Disable asset broadcasting
$this->flushAssets();
2014-05-14 13:24:20 +00:00
$property = trim(Request::input('inspectorProperty'));
2014-10-10 22:07:30 +00:00
if (!$property) {
2014-05-14 13:24:20 +00:00
throw new ApplicationException('The property name is not specified.');
2014-10-10 22:07:30 +00:00
}
2014-05-14 13:24:20 +00:00
$className = trim(Request::input('inspectorClassName'));
2014-10-10 22:07:30 +00:00
if (!$className) {
2014-05-14 13:24:20 +00:00
throw new ApplicationException('The inspectable class name is not specified.');
2014-10-10 22:07:30 +00:00
}
2014-05-14 13:24:20 +00:00
$traitFound = in_array('System\Traits\PropertyContainer', class_uses_recursive($className));
2014-10-10 22:07:30 +00:00
if (!$traitFound) {
2014-05-14 13:24:20 +00:00
throw new ApplicationException('The options cannot be loaded for the specified class.');
2014-10-10 22:07:30 +00:00
}
2014-05-14 13:24:20 +00:00
$obj = new $className(null);
2014-05-14 13:24:20 +00:00
2016-01-30 06:16:33 +00:00
// Nested properties have names like object.property.
// Convert them to Object.Property.
$propertyNameParts = explode('.', $property);
$propertyMethodName = '';
foreach ($propertyNameParts as $part) {
$part = trim($part);
if (!strlen($part)) {
continue;
}
$propertyMethodName .= ucfirst($part);
}
$methodName = 'get'.$propertyMethodName.'Options';
2014-10-10 22:07:30 +00:00
if (method_exists($obj, $methodName)) {
2014-05-14 13:24:20 +00:00
$options = $obj->$methodName();
2014-11-04 06:41:48 +00:00
}
else {
2014-05-14 13:24:20 +00:00
$options = $obj->getPropertyOptions($property);
2014-10-10 22:07:30 +00:00
}
2014-05-14 13:24:20 +00:00
/*
* Convert to array to retain the sort order in JavaScript
*/
$optionsArray = [];
foreach ((array) $options as $value => $title) {
2019-07-12 15:42:22 +00:00
$optionsArray[] = ['value' => $value, 'title' => Lang::get($title)];
}
2014-05-14 13:24:20 +00:00
return [
'options' => $optionsArray
2014-05-14 13:24:20 +00:00
];
}
}