<?php
namespace ProjectBiz\MenuBundle\Menu;
use ProjectBiz\MenuBundle\Service\MenuContext;
use ProjectBiz\PortalBundle\Exceptions\PortalException;
use Symfony\Contracts\Translation\TranslatorInterface;
use ProjectBiz\DatabaseBundle\Database\DatabaseSchemaCache;
class MenuItem extends MenuItemContainer
{
private $context;
private $label;
private $position;
private $type;
private $namedRoute;
private $paramsJson;
private $tooltip;
private $id;
/*
* Can be set in favor of $namedRoute.
* Especially to set a route like '?sort_by=MPM_Projectname&sort_dir=DESC'.
*/
private $route;
private $validator;
private $condition;
public function __construct(
MenuContext $context = null,
$label,
$position,
$type,
$namedRoute,
$paramsJson = null,
$tooltip,
$validator = null,
$condition = null,
$route = null,
$id = null
) {
$this->context = $context;
$this->label = $label;
$this->position = $position;
$this->type = $type;
$this->namedRoute = $namedRoute;
$this->paramsJson = $paramsJson;
$this->tooltip = $tooltip;
$this->validator = $validator;
$this->condition = $condition;
$this->route = $route;
$this->id = $id;
}
public function getLabel()
{
/* if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($this->label);
}*/
return $this->label;
}
public function getPosition()
{
return $this->position;
}
public function getType()
{
return $this->type;
}
public function getNamedRoute()
{
return $this->namedRoute;
}
public function getRoute()
{
return $this->route;
}
public function getCondition()
{
return $this->condition;
}
public function getId()
{
return $this->id;
}
public function getParams()
{
if (!isset($this->cacheParams)) {
if (null != $this->paramsJson) {
$this->cacheParams = json_decode($this->paramsJson, true);
if ($this->cacheParams === null) {
throw new PortalException(
'json_decode für Navigations Eintrag "' . $this->label . '" in Spalte MenuEntry_Params fehlgeschlagen: '
. PHP_EOL . PHP_EOL .
$this->paramsJson
. PHP_EOL . PHP_EOL .
'Fehler: ' . json_last_error_msg()
);
}
} else {
$this->cacheParams = null;
}
}
return $this->cacheParams;
}
public function isValid($context)
{
if (is_a($this->validator, 'ProjectBiz\MenuBundle\Menu\MenuItemValidator')) {
return $this->validator->validate($context);
}
return true;
}
public function getTooltip()
{
/* if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($this->tooltip);
}*/
return $this->tooltip;
}
public function getInjectedParams($data, $columnName = null)
{
$params = $this->getParams(); //json_decode(json_encode($menuEntry['MenuEntry_Params']), true);
if ((null !== $params) && isset($params['route_params'])) {
$route_params = $params['route_params'];
} else {
$route_params = array();
}
if ((null !== $this->context) &&
(null !== $this->context->getGlobalContext()) &&
(null !== $params) &&
isset($params['global_inject'])
) {
$globalInject = $params['global_inject'];
$route_params = $this->inject($route_params, $this->context->getGlobalContext(), $globalInject);
}
$routeInject = null;
if ((null !== $params) && isset($params['context_inject'])) {
$routeInject = $params['context_inject'];
} else {
if ((null !== $this->context) && (null !== $this->context->getPrimaryKey())) {
$routeInject = array();
$routeInject[$this->context->getPrimaryKey()] = 'id';
}
}
/*
* Automatically inject the columnname and columnid for column-context-menus to provide it in the target route.
* The prefixed underscore is supposed to indicate that the variables are injected automatically.
*/
if ($columnName) {
$route_params['_columnname'] = $columnName;
/*
* Referenced columns are separated by colons.
* For example: MPM_initialized_LINK_User_ID:User_ID
* To get the column-definition id of the column, only the column name before the colon is helpful.
* If $columnName does not contain a colon, this command has no effect.
*/
$columnName = explode(':', $columnName)[0];
$columnDefinitions = $this->getDatabaseSchemaCache()->getColumnDefinitionsByColumns([$columnName]);
$route_params['_columnid'] = $columnDefinitions[$columnName]->getId();
}
if (null !== $routeInject) {
$route_params = $this->inject($route_params, $data, $routeInject);
}
return $route_params;
}
public function getWrapperAttr()
{
$params = $this->getParams();
if ((null !== $params) && isset($params['wrapper_attr'])) {
return $params['wrapper_attr'];
}
return null;
}
public function getItemAttr()
{
$params = $this->getParams();
if ((null !== $params) && isset($params['item_attr'])) {
return $params['item_attr'];
}
return null;
}
public function getChildrenAttr()
{
$params = $this->getParams();
if ((null !== $params) && isset($params['children_attr'])) {
return $params['children_attr'];
}
return null;
}
public function getTagOr($defaultTag = null)
{
$params = $this->getParams();
if ((null !== $params) && isset($params['tag'])) {
return $params['tag'];
}
return $defaultTag;
}
/**
* Copy keys from $source to $target using the mapping in $rules.
*
* @param $target
* @param $source
* @param $rules
*
* @return mixed
*/
protected function inject($target, $source, $rules)
{
foreach ($rules as $srcKey => $targetKey) {
if (isset($source[$srcKey])) {
$target[$targetKey] = $source[$srcKey];
}
}
return $target;
}
public function getTranslator()
{
return $this->translator;
}
public function getDatabaseSchemaCache()
{
return $this->databaseSchemaCache;
}
public function setTranslator(TranslatorInterface $translator)
{
$this->translator = $translator;
return $this;
}
public function setDatabaseSchemaCache(DatabaseSchemaCache $databaseSchemaCache)
{
$this->databaseSchemaCache = $databaseSchemaCache;
return $this;
}
}