vendor\project-biz\menu-bundle\src\Menu\MenuItem.php line 29

Open in your IDE?
  1. <?php
  2. namespace ProjectBiz\MenuBundle\Menu;
  3. use ProjectBiz\MenuBundle\Service\MenuContext;
  4. use ProjectBiz\PortalBundle\Exceptions\PortalException;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. use ProjectBiz\DatabaseBundle\Database\DatabaseSchemaCache;
  7. class MenuItem extends MenuItemContainer
  8. {
  9.     private $context;
  10.     private $label;
  11.     private $position;
  12.     private $type;
  13.     private $namedRoute;
  14.     private $paramsJson;
  15.     private $tooltip;
  16.     private $id;
  17.     /*
  18.      * Can be set in favor of $namedRoute.
  19.      * Especially to set a route like '?sort_by=MPM_Projectname&sort_dir=DESC'.
  20.      */
  21.     private $route;
  22.     private $validator;
  23.     private $condition;
  24.     public function __construct(
  25.         MenuContext $context null,
  26.         $label,
  27.         $position,
  28.         $type,
  29.         $namedRoute,
  30.         $paramsJson null,
  31.         $tooltip,
  32.         $validator null,
  33.         $condition null,
  34.         $route null,
  35.         $id null
  36.     ) {
  37.         $this->context      $context;
  38.         $this->label        $label;
  39.         $this->position     $position;
  40.         $this->type         $type;
  41.         $this->namedRoute   $namedRoute;
  42.         $this->paramsJson   $paramsJson;
  43.         $this->tooltip      $tooltip;
  44.         $this->validator    $validator;
  45.         $this->condition    $condition;
  46.         $this->route        $route;
  47.         $this->id           $id;
  48.     }
  49.     public function getLabel()
  50.     {
  51.       /*  if ($this->translator instanceof TranslatorInterface) {
  52.             return $this->translator->trans($this->label);
  53.         }*/
  54.         return $this->label;
  55.     }
  56.     public function getPosition()
  57.     {
  58.         return $this->position;
  59.     }
  60.     public function getType()
  61.     {
  62.         return $this->type;
  63.     }
  64.     public function getNamedRoute()
  65.     {
  66.         return $this->namedRoute;
  67.     }
  68.     public function getRoute()
  69.     {
  70.         return $this->route;
  71.     }
  72.     public function getCondition()
  73.     {
  74.         return $this->condition;
  75.     }
  76.     public function getId()
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function getParams()
  81.     {
  82.         if (!isset($this->cacheParams)) {
  83.             if (null != $this->paramsJson) {
  84.                 $this->cacheParams json_decode($this->paramsJsontrue);
  85.                 if ($this->cacheParams === null) {
  86.                     throw new PortalException(
  87.                     'json_decode für Navigations Eintrag "' $this->label '" in Spalte MenuEntry_Params fehlgeschlagen: '
  88.                     PHP_EOL PHP_EOL .
  89.                     $this->paramsJson
  90.                     PHP_EOL PHP_EOL .
  91.                     'Fehler: ' json_last_error_msg()
  92.                     );
  93.                 }
  94.             } else {
  95.                 $this->cacheParams null;
  96.             }
  97.         }
  98.         return $this->cacheParams;
  99.     }
  100.     public function isValid($context)
  101.     {
  102.         if (is_a($this->validator'ProjectBiz\MenuBundle\Menu\MenuItemValidator')) {
  103.             return $this->validator->validate($context);
  104.         }
  105.         return true;
  106.     }
  107.     public function getTooltip()
  108.     {
  109.       /*  if ($this->translator instanceof TranslatorInterface) {
  110.             return $this->translator->trans($this->tooltip);
  111.         }*/
  112.         return $this->tooltip;
  113.     }
  114.     public function getInjectedParams($data$columnName null)
  115.     {
  116.         $params $this->getParams(); //json_decode(json_encode($menuEntry['MenuEntry_Params']), true);
  117.         if ((null !== $params) && isset($params['route_params'])) {
  118.             $route_params $params['route_params'];
  119.         } else {
  120.             $route_params = array();
  121.         }
  122.         if ((null !== $this->context) &&
  123.             (null !== $this->context->getGlobalContext()) &&
  124.             (null !== $params) &&
  125.             isset($params['global_inject'])
  126.         ) {
  127.             $globalInject $params['global_inject'];
  128.             $route_params $this->inject($route_params$this->context->getGlobalContext(), $globalInject);
  129.         }
  130.         $routeInject null;
  131.         if ((null !== $params) && isset($params['context_inject'])) {
  132.             $routeInject $params['context_inject'];
  133.         } else {
  134.             if ((null !== $this->context) && (null !== $this->context->getPrimaryKey())) {
  135.                 $routeInject = array();
  136.                 $routeInject[$this->context->getPrimaryKey()] = 'id';
  137.             }
  138.         }
  139.         /*
  140.          * Automatically inject the columnname and columnid for column-context-menus to provide it in the target route.
  141.          * The prefixed underscore is supposed to indicate that the variables are injected automatically.
  142.          */
  143.         if ($columnName) {
  144.             $route_params['_columnname'] = $columnName;
  145.             /*
  146.              * Referenced columns are separated by colons.
  147.              * For example: MPM_initialized_LINK_User_ID:User_ID
  148.              * To get the column-definition id of the column, only the column name before the colon is helpful.
  149.              * If $columnName does not contain a colon, this command has no effect.
  150.              */
  151.             $columnName explode(':'$columnName)[0];
  152.             $columnDefinitions $this->getDatabaseSchemaCache()->getColumnDefinitionsByColumns([$columnName]);
  153.             $route_params['_columnid'] = $columnDefinitions[$columnName]->getId();
  154.         }
  155.         if (null !== $routeInject) {
  156.             $route_params $this->inject($route_params$data$routeInject);
  157.         }
  158.         return $route_params;
  159.     }
  160.     public function getWrapperAttr()
  161.     {
  162.         $params $this->getParams();
  163.         if ((null !== $params) && isset($params['wrapper_attr'])) {
  164.             return $params['wrapper_attr'];
  165.         }
  166.         return null;
  167.     }
  168.     public function getItemAttr()
  169.     {
  170.         $params $this->getParams();
  171.         if ((null !== $params) && isset($params['item_attr'])) {
  172.             return $params['item_attr'];
  173.         }
  174.         return null;
  175.     }
  176.     public function getChildrenAttr()
  177.     {
  178.         $params $this->getParams();
  179.         if ((null !== $params) && isset($params['children_attr'])) {
  180.             return $params['children_attr'];
  181.         }
  182.         return null;
  183.     }
  184.     public function getTagOr($defaultTag null)
  185.     {
  186.         $params $this->getParams();
  187.         if ((null !== $params) && isset($params['tag'])) {
  188.             return $params['tag'];
  189.         }
  190.         return $defaultTag;
  191.     }
  192.     /**
  193.      * Copy keys from $source to $target using the mapping in $rules.
  194.      *
  195.      * @param $target
  196.      * @param $source
  197.      * @param $rules
  198.      *
  199.      * @return mixed
  200.      */
  201.     protected function inject($target$source$rules)
  202.     {
  203.         foreach ($rules as $srcKey => $targetKey) {
  204.             if (isset($source[$srcKey])) {
  205.                 $target[$targetKey] = $source[$srcKey];
  206.             }
  207.         }
  208.         return $target;
  209.     }
  210.     public function getTranslator()
  211.     {
  212.         return $this->translator;
  213.     }
  214.     public function getDatabaseSchemaCache()
  215.     {
  216.         return $this->databaseSchemaCache;
  217.     }
  218.     public function setTranslator(TranslatorInterface $translator)
  219.     {
  220.         $this->translator $translator;
  221.         return $this;
  222.     }
  223.     public function setDatabaseSchemaCache(DatabaseSchemaCache $databaseSchemaCache)
  224.     {
  225.         $this->databaseSchemaCache $databaseSchemaCache;
  226.         return $this;
  227.     }
  228. }