Source of file DashBoardController.php

Size: 6,995 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Controller/Pages/DashBoardController.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
<?php

/* This file is part of NextDom Software.
 *
 * NextDom is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NextDom Software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NextDom Software. If not, see <http://www.gnu.org/licenses/>.
 *
 * @Support <https://www.nextdom.org>
 * @Email   <admin@nextdom.org>
 * @Authors/Contributors: Sylvaner, Byackee, cyrilphoenix71, ColonelMoutarde, edgd1er, slobberbone, Astral0, DanoneKiD
 */

namespace NextDom\Controller\Pages;

use NextDom\Controller\BaseController;
use NextDom\Enums\AjaxParams;
use NextDom\Enums\Common;
use NextDom\Enums\ControllerData;
use NextDom\Enums\NextDomObj;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\Render;
use NextDom\Helpers\Utils;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\JeeObjectManager;
use NextDom\Managers\UserManager;

/**
 * Class DashboardController
 * @package NextDom\Controller
 */
class DashBoardController extends BaseController
{
    /**
     * Render dashboard
     *
     * @param array $pageData Page data
     *
     * @return string Content of Dashboard V2 page
     *
     * @throws \Exception
     */
    public static function get(&$pageData): string
    {
        $defaultDashboardObjectId = '';
        $objectIdFromUrl = Utils::init(AjaxParams::OBJECT_ID, '');
        $pageData[ControllerData::JS_VARS]['nextdom_Welcome'] = ConfigManager::byKey('nextdom::Welcome');
        $pageData[ControllerData::JS_VARS]['SEL_CATEGORY'] = Utils::init(AjaxParams::CATEGORY, 'all');
        $pageData[ControllerData::JS_VARS]['SEL_TAG'] = Utils::init(AjaxParams::TAG, 'all');
        $pageData[ControllerData::JS_VARS]['SEL_SUMMARY'] = Utils::init(AjaxParams::SUMMARY);

        if(Utils::init('summary') != ''){
            $defaultDashboardObject = JeeObjectManager::getRootObjects();
        }else{
            $defaultDashboardObject = JeeObjectManager::byId($_SESSION['user']->getOptions('defaultDashboardObject'));
        }
        if (!empty($defaultDashboardObject)) {
            $defaultDashboardObjectId = $defaultDashboardObject->getId();
        }

        if ($objectIdFromUrl != '') {
            $currentJeeObject = JeeObjectManager::byId($objectIdFromUrl);
        } else {
            if ($defaultDashboardObjectId != "") {
                $currentJeeObject = JeeObjectManager::byId($defaultDashboardObjectId);
            } else {
                $currentJeeObject = JeeObjectManager::getRootObjects();
            }
        }
        if (!empty($currentJeeObject)) {
            $currentJeeObjectId = $currentJeeObject->getId();
        } else {
            throw new CoreException(__('Aucun objet racine trouvé. Pour en créer un, allez dans dashboard -> <a href="/index.php?' . http_build_query(['v' => 'd', 'p' => 'object']). '">Liste objets et résumés</a>'));
        }

        $pageData[ControllerData::JS_VARS]['SEL_OBJECT_ID'] = $currentJeeObjectId;
        $pageData[ControllerData::JS_VARS]['rootObjectId'] = $currentJeeObjectId;
        $pageData[ControllerData::JS_VARS]['serverTZoffsetMin'] = Utils::getTZoffsetMin();
        $pageData[ControllerData::JS_VARS]['nextdom_Welcome'] = ConfigManager::byKey('nextdom::Welcome', 'core', 1);

        $pageData['dashboardCategory'] = Utils::init(AjaxParams::CATEGORY, Common::ALL);
        $pageData['dashboardSummary'] = Utils::init(AjaxParams::SUMMARY, Common::ALL);
        $pageData['dashboardCategories'] = NextDomHelper::getConfiguration('eqLogic:category', true);
        $pageData['dashboardDefaultObjectId'] = $defaultDashboardObjectId;
        $pageData['dashboardObjectId'] = $currentJeeObjectId;
        $pageData['dashboardObject'] = $currentJeeObject;
        $pageData['dashboardObjectParentNumber'] = $currentJeeObject->parentNumber();
        $pageData['dashboardObjectListMenu'] = self::getObjectsListMenu($currentJeeObjectId);
        $pageData['dashboardChildrenObjects'] = JeeObjectManager::buildTree($currentJeeObject);

        $pageData[ControllerData::JS_POOL][] = self::PATH_TO_JS . '/libs/widget.js';
        $pageData[ControllerData::JS_POOL][] = self::PATH_TO_JS . '/desktop/pages/dashboard.js';
        $pageData[ControllerData::JS_END_POOL][] = self::PATH_TO_JS . '/desktop/pages/dashboard_events.js';
        // A remettre une fois mise sous forme de thème
        $pageData[ControllerData::JS_POOL][] = '/vendor/node_modules/isotope-layout/dist/isotope.pkgd.min.js';
        $pageData[ControllerData::JS_POOL][] = '/assets/3rdparty/jquery.multi-column-select/multi-column-select.js';

        return Render::getInstance()->get('/desktop/pages/dashboard.html.twig', $pageData);
    }

    /**
     * Get layers
     * @param int $currentObjectId
     * @return array
     * @throws \Exception
     */
    private static function getObjectsListMenu($currentObjectId)
    {
        if ($currentObjectId === '') {
            return [];
        }
        $currentObject = JeeObjectManager::byId($currentObjectId);

        // Get parents
        $parentObjects = [];
        $parentObjects[] = $currentObject;
        $father = $currentObject->getFather();
        while ($father !== null) {
            $parentObjects[] = $father;
            $father = $father->getFather();
        }
        $parentObjects = array_reverse($parentObjects);

        // Get all layers before current object
        $result = [];
        foreach ($parentObjects as $selectedLayerObject) {
            $layerResult = [];
            if ($selectedLayerObject->getFather() === null) {
                $layer = JeeObjectManager::getRootObjects(true);
            } else {
                $layer = $selectedLayerObject->getFather()->getChild();
            }
            foreach ($layer as $item) {
                $itemData = [];
                $itemData[NextDomObj::JEE_OBJECT] = $item;
                $itemData[Common::ACTIVE] = false;
                if ($item->getId() == $selectedLayerObject->getId()) {
                    $itemData[Common::ACTIVE] = true;
                }
                $layerResult[] = $itemData;
            }
            $result[] = $layerResult;
        }
        // Add children layer
        $children = $currentObject->getChild(true);
        $childrenLayer = [];
        foreach ($children as $item) {
            $itemData = [];
            $itemData[NextDomObj::JEE_OBJECT] = $item;
            $itemData[Common::ACTIVE] = false;
            $childrenLayer[] = $itemData;
        }
        $result[] = $childrenLayer;
        return $result;
    }
}