<commit_before><?php

namespace App\Event\Plugin\Menu\View;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;
use Menu\Event\EventName as MenuEventName;
use Menu\MenuBuilder\MenuInterface;
use Menu\MenuBuilder\MenuItemContainerInterface;
use Menu\MenuBuilder\MenuItemFactory;
use Search\Model\Table\DashboardsTable;

class DashboardMenuListener implements EventListenerInterface
{
    /**
     * @inheritdoc
     *
     * @return array associative array or event key names pointing to the function
     * that should be called in the object when the respective event is fired
     */
    public function implementedEvents()
    {
        return [
            (string)MenuEventName::GET_MENU_ITEMS() => 'getMenuItems',
        ];
    }

    /**
     * Method that updates the provided Menu to include Dashboard links
     *
     * @param Event $event Event object
     * @param string $name Menu name
     * @param array $user Current user
     * @param bool $fullBaseUrl Flag for fullbase url on menu links
     * @param array $modules Modules to fetch menu items for
     * @param MenuInterface|null $menu Menu object to be updated
     * @return void
     */
    public function getMenuItems(Event $event, $name, array $user, $fullBaseUrl = false, array $modules = [], MenuInterface $menu = null)
    {
        if ($name === MENU_MAIN && empty($modules)) {
            $this->addDashboardItems($menu, $user);
            $event->setResult($menu);
        }
    }

    /**
     * Creates the necessary menu items for the Dashboard menu.
     * All newly created items are added to the specified container
     *
     * @param MenuInterface $menu The menu to add the created dashboard menu items.
     * @return void
     */
    private function addDashboardItems(MenuInterface $menu, array $user)
    {
        $link = MenuItemFactory::createMenuItem([
            'label' => 'Dashboard',
            'url' => '#',
            'icon' => 'tachometer',
        ]);
        $menu->addMenuItem($link);

        $createLink = MenuItemFactory::createMenuItem([
            'label' => 'Create',
            'url' => '/search/dashboards/add',
            'icon' => 'plus',
            'order' => 999999999
        ]);
        $link->addMenuItem($createLink);

        $this->addDashboardItemsFromTable($link, $user, 10);
    }

    /**
     * Iterates through Dashboard table query and creates a new menu item for each record found
     * The newly created items will be added under the specified Menu container
     *
     * @param MenuItemContainerInterface $container Menu Container
     * @param int $startAt Starting order position
     * @return void
     */
    private function addDashboardItemsFromTable(MenuItemContainerInterface $container, array $user, $startAt)
    {
        /** @var DashboardsTable $table */
        $table = TableRegistry::get('Search.Dashboards');
        $query = $table->getUserDashboards($user);

        /**
         * @var int $i
         * @var EntityInterface $entity
         */
        foreach ($query as $i => $entity) {
            $entityItem = MenuItemFactory::createMenuItem([
                'label' => $entity->get('name'),
                'url' => '/search/dashboards/view/' . $entity->get('id'),
                'icon' => 'tachometer',
                'order' => $startAt + $i,
            ]);
            $container->addMenuItem($entityItem);
        }
    }
}
<commit_msg>Remove use statement for DashboardsTable (task #6867)<commit_after><?php

namespace App\Event\Plugin\Menu\View;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;
use Menu\Event\EventName as MenuEventName;
use Menu\MenuBuilder\MenuInterface;
use Menu\MenuBuilder\MenuItemContainerInterface;
use Menu\MenuBuilder\MenuItemFactory;

class DashboardMenuListener implements EventListenerInterface
{
    /**
     * @inheritdoc
     *
     * @return array associative array or event key names pointing to the function
     * that should be called in the object when the respective event is fired
     */
    public function implementedEvents()
    {
        return [
            (string)MenuEventName::GET_MENU_ITEMS() => 'getMenuItems',
        ];
    }

    /**
     * Method that updates the provided Menu to include Dashboard links
     *
     * @param Event $event Event object
     * @param string $name Menu name
     * @param array $user Current user
     * @param bool $fullBaseUrl Flag for fullbase url on menu links
     * @param array $modules Modules to fetch menu items for
     * @param MenuInterface|null $menu Menu object to be updated
     * @return void
     */
    public function getMenuItems(Event $event, $name, array $user, $fullBaseUrl = false, array $modules = [], MenuInterface $menu = null)
    {
        if ($name === MENU_MAIN && empty($modules)) {
            $this->addDashboardItems($menu, $user);
            $event->setResult($menu);
        }
    }

    /**
     * Creates the necessary menu items for the Dashboard menu.
     * All newly created items are added to the specified container
     *
     * @param MenuInterface $menu The menu to add the created dashboard menu items.
     * @return void
     */
    private function addDashboardItems(MenuInterface $menu, array $user)
    {
        $link = MenuItemFactory::createMenuItem([
            'label' => 'Dashboard',
            'url' => '#',
            'icon' => 'tachometer',
        ]);
        $menu->addMenuItem($link);

        $createLink = MenuItemFactory::createMenuItem([
            'label' => 'Create',
            'url' => '/search/dashboards/add',
            'icon' => 'plus',
            'order' => 999999999
        ]);
        $link->addMenuItem($createLink);

        $this->addDashboardItemsFromTable($link, $user, 10);
    }

    /**
     * Iterates through Dashboard table query and creates a new menu item for each record found
     * The newly created items will be added under the specified Menu container
     *
     * @param MenuItemContainerInterface $container Menu Container
     * @param int $startAt Starting order position
     * @return void
     */
    private function addDashboardItemsFromTable(MenuItemContainerInterface $container, array $user, $startAt)
    {
        $table = TableRegistry::get('Search.Dashboards');
        $query = $table->getUserDashboards($user);

        /**
         * @var int $i
         * @var EntityInterface $entity
         */
        foreach ($query as $i => $entity) {
            $entityItem = MenuItemFactory::createMenuItem([
                'label' => $entity->get('name'),
                'url' => '/search/dashboards/view/' . $entity->get('id'),
                'icon' => 'tachometer',
                'order' => $startAt + $i,
            ]);
            $container->addMenuItem($entityItem);
        }
    }
}

{'fraction_non_alphanumeric': 0.11195855518779681, 'fraction_numerical': 0.003741545546121744, 'mean_word_length': 2.469795307039441, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6434783', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1837, 'n_words': 547}