Source of file CmdAjax.php

Size: 25,125 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Ajax/CmdAjax.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
<?php
/* This file is part of Jeedom.
 *
 * Jeedom 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.
 *
 * Jeedom 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 Jeedom. If not, see <http://www.gnu.org/licenses/>.
 */

namespace NextDom\Ajax;

use NextDom\Enums\ActionRight;
use NextDom\Enums\AjaxParams;
use NextDom\Enums\CmdConfigKey;
use NextDom\Enums\CmdType;
use NextDom\Enums\CmdViewType;
use NextDom\Enums\Common;
use NextDom\Enums\DateFormat;
use NextDom\Enums\NextDomObj;
use NextDom\Enums\UserRight;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\AuthentificationHelper;
use NextDom\Helpers\FileSystemHelper;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\CmdManager;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\EqLogicManager;
use NextDom\Managers\HistoryManager;
use NextDom\Managers\InteractDefManager;
use NextDom\Managers\JeeObjectManager;
use NextDom\Managers\PluginManager;
use NextDom\Managers\ScenarioManager;
use NextDom\Managers\UserManager;
use NextDom\Model\Entity\Cmd;
use NextDom\Model\Entity\EqLogic;
use NextDom\Model\Entity\Scenario;

/**
 * Class CmdAjax
 * @package NextDom\Ajax
 */
class CmdAjax extends BaseAjax
{
    protected $NEEDED_RIGHTS = UserRight::USER;
    protected $MUST_BE_CONNECTED = true;
    protected $CHECK_AJAX_TOKEN = true;

    /**
     * Get one or more command(s) HTML render
     *
     * @throws CoreException
     */
    public function toHtml()
    {
        // Render list of id
        if (Utils::init(AjaxParams::IDS) != '') {
            $result = [];
            foreach (json_decode(Utils::init(AjaxParams::IDS), true) as $id => $value) {
                $cmd = CmdManager::byId($id);
                if (!is_object($cmd)) {
                    continue;
                }
                $result[$cmd->getId()] = [
                    'html' => $cmd->toHtml($value[Common::VERSION]),
                    AjaxParams::ID => $cmd->getId(),
                ];
            }
            $this->ajax->success($result);
        } else {
            // Render one command
            $cmd = CmdManager::byId(Utils::init(AjaxParams::ID));
            if (!is_object($cmd)) {
                throw new CoreException(__('Commande inconnue - Vérifiez l\'ID'));
            }
            $result = [];
            $result[AjaxParams::ID] = $cmd->getId();
            $result['html'] = $cmd->toHtml(Utils::init(AjaxParams::VERSION), Utils::init(AjaxParams::OPTION), Utils::init('cmdColor', null));
            $this->ajax->success($result);
        }
    }

    /**
     * Execute a command
     *
     * @throws CoreException
     */
    public function execCmd()
    {
        $cmdId = Utils::init(AjaxParams::ID);
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande ID inconnu : ') . $cmdId);
        }
        $eqLogic = $cmd->getEqLogicId();
        if ($cmd->isType(CmdType::ACTION) && !$eqLogic->hasRight('x')) {
            throw new CoreException(__('Vous n\'êtes pas autorisé à faire cette action'));
        }
        if (!$cmd->checkAccessCode(Utils::init('codeAccess'))) {
            throw new CoreException(__('Cette action nécessite un code d\'accès'), -32005);
        }
        if ($cmd->isType(CmdType::ACTION) && $cmd->getConfiguration('actionConfirm') == 1 && Utils::init('confirmAction') != 1) {
            throw new CoreException(__('Cette action nécessite une confirmation'), -32006);
        }
        $options = json_decode(Utils::init('value', '{}'), true);
        if (Utils::init('utid') != '') {
            $options['utid'] = Utils::init('utid');
        }
        $this->ajax->success($cmd->execCmd($options));
    }

    /**
     * Get an object from his name, eqLogic name and command name
     *
     * @throws CoreException
     */
    public function getByObjectNameEqNameCmdName()
    {
        $objectName = Utils::init(AjaxParams::OBJECT_NAME);
        $eqLogicName = Utils::init(AjaxParams::EQLOGIC_NAME);
        $cmdName = Utils::init(AjaxParams::CMD_NAME);
        $cmd = CmdManager::byObjectNameEqLogicNameCmdName($objectName, $eqLogicName, $cmdName);
        if (!is_object($cmd)) {
            throw new CoreException(__('Cmd inconnu : ') . $objectName . '/' . $eqLogicName . '/' . $cmdName);
        }
        $this->ajax->success($cmd->getId());
    }

    /**
     * Get an object from his name and command name
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function getByObjectNameCmdName()
    {
        $objectName = Utils::init(AjaxParams::OBJECT_NAME);
        $cmdName = Utils::init(AjaxParams::CMD_NAME);
        $cmd = CmdManager::byObjectNameCmdName($objectName, $cmdName);
        if (!is_object($cmd)) {
            throw new CoreException(__('Cmd inconnu : ') . $objectName . '/' . $cmdName, 9999);
        }
        $this->ajax->success(Utils::o2a($cmd));
    }

    /**
     * Get command object by his id
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function byId()
    {
        $cmdId = Utils::init(AjaxParams::ID);
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande inconnue : ') . $cmdId, 9999);
        }
        $this->ajax->success(NextDomHelper::toHumanReadable(Utils::o2a($cmd)));
    }

    /**
     * Copy history from cmd to another one
     *
     * @throws CoreException
     */
    public function copyHistoryToCmd()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        HistoryManager::copyHistoryToCmd(Utils::init('source_id'), Utils::init('target_id'));
        $this->ajax->success();
    }

    /**
     * Replace command with another one
     *
     * @throws CoreException
     */
    public function replaceCmd()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        NextDomHelper::replaceTag(['#' . str_replace('#', '', Utils::init('source_id')) . '#' => '#' . str_replace('#', '', Utils::init('target_id')) . '#']);
        $this->ajax->success();
    }

    /**
     * Get command by human name
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function byHumanName()
    {
        $humanName = Utils::init(AjaxParams::HUMAN_NAME);
        $cmd_id = CmdManager::humanReadableToCmd($humanName);
        $cmd = CmdManager::byId(str_replace('#', '', $cmd_id));
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande inconnue : ') . $humanName, 9999);
        }
        $this->ajax->success(Utils::o2a($cmd));
    }

    /**
     * Get list of command, eqLogic and scenario that use a command
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function usedBy()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $cmdId = Utils::init(AjaxParams::ID);
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande inconnue : ') . $cmdId, 9999);
        }
        $cmdUserBy = $cmd->getUsedBy();
        $result = [NextDomObj::CMD => [], NextDomObj::EQLOGIC => [], NextDomObj::SCENARIO => []];
        /**
         * @var Cmd $cmd
         */
        foreach ($cmdUserBy[NextDomObj::CMD] as $cmd) {
            $info = Utils::o2a($cmd);
            $info[Common::HUMAN_NAME] = $cmd->getHumanName();
            $info['link'] = $cmd->getEqLogic()->getLinkToConfiguration();
            $result[NextDomObj::CMD][] = $info;
        }
        /**
         * @var EqLogic $eqLogic
         */
        foreach ($cmdUserBy[NextDomObj::EQLOGIC] as $eqLogic) {
            $info = Utils::o2a($eqLogic);
            $info[Common::HUMAN_NAME] = $eqLogic->getHumanName();
            $info['link'] = $eqLogic->getLinkToConfiguration();
            $result[NextDomObj::EQLOGIC][] = $info;
        }
        /**
         * @var Scenario $scenario
         */
        foreach ($cmdUserBy[NextDomObj::SCENARIO] as $scenario) {
            $info = Utils::o2a($cmd);
            $info[Common::HUMAN_NAME] = $scenario->getHumanName();
            $info['link'] = $scenario->getLinkToConfiguration();
            $result[NextDomObj::SCENARIO][] = $info;
        }
        $this->ajax->success($result);
    }

    /**
     * Get command human name
     *
     * @throws \ReflectionException
     */
    public function getHumanCmdName()
    {
        $this->ajax->success(CmdManager::cmdToHumanReadable('#' . Utils::init(AjaxParams::ID) . '#'));
    }

    /**
     * Get list of command by eqLogic id
     * @throws \ReflectionException
     */
    public function byEqLogic()
    {
        $this->ajax->success(Utils::o2a(CmdManager::byEqLogicId(Utils::init('eqLogic_id'))));
    }

    /**
     * Get a command by his id with eqLogic name and Object name
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function getCmd()
    {
        $cmdId = Utils::init(AjaxParams::ID);
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande inconnue : ') . $cmdId);
        }
        $result = NextDomHelper::toHumanReadable(Utils::o2a($cmd));
        $eqLogic = $cmd->getEqLogicId();
        $result['eqLogic_name'] = $eqLogic->getName();
        $result['plugin'] = $eqLogic->getEqType_Name();
        if ($eqLogic->getObject_id() > 0) {
            $result['object_name'] = $eqLogic->getObject()->getName();
        }
        $this->ajax->success($result);
    }

    /**
     * Save change on command
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function save()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $cmdAjaxData = NextDomHelper::fromHumanReadable(json_decode(Utils::init(NextDomObj::CMD), true));
        $cmd = CmdManager::byId($cmdAjaxData[AjaxParams::ID]);
        if (!is_object($cmd)) {
            $cmd = new Cmd();
        }
        Utils::a2o($cmd, $cmdAjaxData);
        $cmd->save();
        $this->ajax->success(Utils::o2a($cmd));
    }

    /**
     * Save a list of command
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function multiSave()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $cmds = json_decode(Utils::init(NextDomObj::CMD), true);
        foreach ($cmds as $cmdAjaxData) {
            $cmd = CmdManager::byId($cmdAjaxData[AjaxParams::ID]);
            if (!is_object($cmd)) {
                continue;
            }
            Utils::a2o($cmd, $cmdAjaxData);
            $cmd->save();
        }
        $this->ajax->success();
    }

    /**
     * Change history item from datetime to another
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function changeHistoryPoint()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $cmdId = Utils::init('cmd_id');
        $targetDatetime = Utils::init('datetime');
        $srcDatetime = Utils::init('oldValue');
        if ($cmdId === '') {
            throw new CoreException(__('Historique impossible'));
        }
        $history = HistoryManager::byCmdIdDatetime($cmdId, $targetDatetime);
        foreach (['+1 hour', '+1 day', '+1 week', '+1 month'] as $timeStep) {
            if (is_object($history)) {
                break;
            }
            $history = HistoryManager::byCmdIdDatetime($cmdId, $targetDatetime, date(DateFormat::FULL, strtotime($targetDatetime . $timeStep)), $srcDatetime);
        }
        if (!is_object($history)) {
            throw new CoreException(__('Aucun point ne correspond pour l\'historique : ') . $cmdId . ' - ' . $targetDatetime . ' - ' . $srcDatetime);
        }
        $value = Utils::init('value', null);
        if ($value === '') {
            $history->remove();
        } else {
            $history->setValue($value);
            $history->save(null, true);
        }
        $this->ajax->success();
    }

    /**
     * Get command history
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function getHistory()
    {
        global $NEXTDOM_INTERNAL_CONFIG;
        $result = [];
        $data = [];
        $userDateStart = Utils::init(AjaxParams::DATE_START, '');
        $userDateEnd = Utils::init(AjaxParams::DATE_END, '');
        $dateStart = null;
        $dateEnd = null;
        $dateRange = Utils::init(AjaxParams::DATE_RANGE);
        if ($dateRange != '' && $dateRange != Common::ALL) {
            if (Utils::isJson($dateRange)) {
                $dateRange = json_decode($dateRange, true);
                if (isset($dateRange[Common::START])) {
                    $dateStart = $dateRange[Common::START];
                }
                if (isset($dateRange[Common::END])) {
                    $dateEnd = $dateRange[Common::END];
                }
            } else {
                if (Utils::init(AjaxParams::DATE_RANGE) == '1 day') {
                    $dateStart = date(DateFormat::FULL_MIDNIGHT, strtotime('- 2 days ' . $dateEnd));
                } else {
                    $dateStart = date(DateFormat::FULL_MIDNIGHT, strtotime('- ' . Utils::init(AjaxParams::DATE_RANGE) . ' ' . $dateEnd));
                }
                $dateEnd = date(DateFormat::FULL);
            }
        }
        if ($userDateStart != '') {
            $dateStart = $userDateStart;
        }
        if ($userDateEnd != '') {
            $dateEnd = $userDateEnd;
            if ($dateEnd == date(DateFormat::FULL_DAY)) {
                $dateEnd = date(DateFormat::FULL);
            }
        }
        if (strtotime($dateEnd) > strtotime('now')) {
            $dateEnd = date(DateFormat::FULL);
        }
        if ($dateStart !== '') {
            $dateStart = Utils::init(AjaxParams::DATE_START, date(DateFormat::FULL_DAY, strtotime(ConfigManager::byKey('history::defautShowPeriod') . ' ' . date(DateFormat::FULL_DAY))));
        }
        $result[CmdConfigKey::MAX_VALUE] = '';
        $result[CmdConfigKey::MIN_VALUE] = '';
        if ($dateStart === null) {
            $result['dateStart'] = '';
        } else {
            $result['dateStart'] = $dateStart;
        }
        if ($dateEnd === null) {
            $result['dateEnd'] = '';
        } else {
            $result['dateEnd'] = $dateEnd;
        }

        $cmdId = Utils::init(AjaxParams::ID);
        if (is_numeric($cmdId)) {
            $cmd = CmdManager::byId($cmdId);
            if (!is_object($cmd)) {
                throw new CoreException(__('Commande ID inconnu : ') . $cmdId);
            }
            $eqLogic = $cmd->getEqLogicId();
            if (!$eqLogic->hasRight(ActionRight::READ)) {
                throw new CoreException(__('Vous n\'êtes pas autorisé à faire cette action'));
            }
            $groupingType = Utils::init(AjaxParams::GROUPING_TYPE);
            if ($groupingType == '') {
                $groupingType = $cmd->getDisplay(AjaxParams::GROUPING_TYPE);
            }
            $histories = $cmd->getHistory($dateStart, $dateEnd, $groupingType);
            $result['cmd_name'] = $cmd->getName();
            $result['history_name'] = $cmd->getHumanName();
            $result[Common::UNITE] = $cmd->getUnite();
            $result[NextDomObj::CMD] = Utils::o2a($cmd);
            $result[NextDomObj::EQLOGIC] = Utils::o2a($cmd->getEqLogicId());
            $result[Common::TIMELINE_ONLY] = $NEXTDOM_INTERNAL_CONFIG[NextDomObj::CMD][Common::TYPE][Common::INFO]['subtype'][$cmd->getSubType()][Common::IS_HISTORIZED][Common::TIMELINE_ONLY];
            $previousValue = null;
            // Todo Optimisation possible
            $derive = intval(Utils::init('derive', $cmd->getDisplay('graphDerive')));
            if (trim($derive) == '') {
                $derive = intval($cmd->getDisplay('graphDerive'));
            }
            $result['derive'] = $derive;
            foreach ($histories as $history) {
                $infoHistory = [];
                if ($cmd->getDisplay(AjaxParams::GROUPING_TYPE) != '') {
                    $infoHistory[] = floatval(strtotime($history->getDatetime() . " UTC")) * 1000 - 1;
                } else {
                    $infoHistory[] = floatval(strtotime($history->getDatetime() . " UTC")) * 1000;
                }
                if ($NEXTDOM_INTERNAL_CONFIG[NextDomObj::CMD][Common::TYPE][Common::INFO][Common::SUBTYPE][$cmd->getSubType()][Common::IS_HISTORIZED][Common::TIMELINE_ONLY]) {
                    $value = $history->getValue();
                } else {
                    $value = ($history->getValue() === null) ? null : floatval($history->getValue());
                    if ($derive === 1) {
                        if ($value !== null && $previousValue !== null) {
                            $value = $value - $previousValue;
                        } else {
                            $value = null;
                        }
                        $previousValue = ($history->getValue() === null) ? null : floatval($history->getValue());
                    }
                }
                $infoHistory[] = $value;
                if (!$NEXTDOM_INTERNAL_CONFIG[NextDomObj::CMD][Common::TYPE][Common::INFO][Common::SUBTYPE][$cmd->getSubType()][Common::IS_HISTORIZED][Common::TIMELINE_ONLY]) {
                    if (($value !== null && $value > $result[CmdConfigKey::MAX_VALUE]) || $result[CmdConfigKey::MAX_VALUE] == '') {
                        $result[CmdConfigKey::MAX_VALUE] = round($value, 1);
                    }
                    if (($value !== null && $value < $result[CmdConfigKey::MIN_VALUE]) || $result[CmdConfigKey::MIN_VALUE] == '') {
                        $result[CmdConfigKey::MIN_VALUE] = round($value, 1);
                    }
                }
                $data[] = $infoHistory;
            }
        } else {
            $histories = HistoryManager::getHistoryFromCalcul(NextDomHelper::fromHumanReadable($cmdId), $dateStart, $dateEnd, Utils::init('allowZero', false), Utils::init(AjaxParams::GROUPING_TYPE));
            if (is_array($histories)) {
                foreach ($histories as $datetime => $value) {
                    $infoHistory = [];
                    $infoHistory[] = floatval($datetime) * 1000;
                    $infoHistory[] = ($value === null) ? null : floatval($value);
                    if ($value > $result[CmdConfigKey::MAX_VALUE] || $result[CmdConfigKey::MAX_VALUE] == '') {
                        $result[CmdConfigKey::MAX_VALUE] = round($value, 1);
                    }
                    if ($value < $result[CmdConfigKey::MIN_VALUE] || $result[CmdConfigKey::MIN_VALUE] == '') {
                        $result[CmdConfigKey::MIN_VALUE] = round($value, 1);
                    }
                    $data[] = $infoHistory;
                }
            }
            $result['cmd_name'] = $cmdId;
            $result['history_name'] = $cmdId;
            $result[Common::UNITE] = Utils::init(Common::UNITE);
        }
        $result['data'] = $data;
        $this->ajax->success($result);
    }

    /**
     * Clear history
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function emptyHistory()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $cmdId = Utils::init(AjaxParams::ID);
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            throw new CoreException(__('Commande ID inconnu : ') . $cmdId);
        }
        $cmd->emptyHistory(Utils::init('date'));
        $this->ajax->success();
    }

    /**
     * Set command order
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function setOrder()
    {
        $cmds = json_decode(Utils::init('cmds'), true);
        $eqLogics = [];
        foreach ($cmds as $cmdJsonData) {
            if (!isset($cmdJsonData[AjaxParams::ID]) || trim($cmdJsonData[AjaxParams::ID]) == '') {
                continue;
            }
            $cmd = CmdManager::byId($cmdJsonData[AjaxParams::ID]);
            if (!is_object($cmd)) {
                continue;
            }
            if ($cmd->getOrder() != $cmdJsonData['order']) {
                $cmd->setOrder($cmdJsonData['order']);
                $cmd->save();
            }
            if (isset($cmdJsonData['line']) && isset($cmdJsonData[Common::COLUMN])) {
                $renderVersion = Utils::init(AjaxParams::VERSION, CmdViewType::DASHBOARD);
                if (!isset($eqLogics[$cmd->getEqLogic_id()])) {
                    $eqLogics[$cmd->getEqLogic_id()] = [NextDomObj::EQLOGIC => $cmd->getEqLogic(), Common::CHANGED => false];
                }
                $layoutDisplay = 'layout::' . $renderVersion . '::table::cmd::' . $cmd->getId();
                if ($eqLogics[$cmd->getEqLogic_id()][NextDomObj::EQLOGIC]->getDisplay($layoutDisplay . '::line') != $cmdJsonData['line']
                    || $eqLogics[$cmd->getEqLogic_id()][NextDomObj::EQLOGIC]->getDisplay($layoutDisplay . '::column') != $cmdJsonData[Common::COLUMN]) {
                    $eqLogics[$cmd->getEqLogic_id()][NextDomObj::EQLOGIC]->setDisplay($layoutDisplay . '::line', $cmdJsonData['line']);
                    $eqLogics[$cmd->getEqLogic_id()][NextDomObj::EQLOGIC]->setDisplay($layoutDisplay . '::column', $cmdJsonData[Common::COLUMN]);
                    $eqLogics[$cmd->getEqLogic_id()][Common::CHANGED] = true;
                }
            }
        }
        /**
         * @var EqLogic[] $eqLogic
         */
        foreach ($eqLogics as $eqLogic) {
            if (!$eqLogic[Common::CHANGED]) {
                continue;
            }
            $eqLogic[NextDomObj::EQLOGIC]->save(true);
        }
        $this->ajax->success();
    }

    /**
     * Upload file from dashboard
     *
     * When file is uploaded, the method execute of the cmd is called with the filename in option.
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function fileUpload()
    {
        AuthentificationHelper::isConnectedOrFail();

        $destDirectory = NEXTDOM_TMP . '/uploads';
        FileSystemHelper::mkdirIfNotExists($destDirectory);

        $filename = Utils::readUploadedFile($_FILES, "upload", $destDirectory, 8, []);
        if (!$filename) {
            $this->ajax->error(__('File error'));
        }

        $cmdId = Utils::init('cmdId');
        $cmd = CmdManager::byId($cmdId);
        if (!is_object($cmd)) {
            $this->ajax->error(__('Command not found : ') . $cmdId);
        }

        $cmd->execute(['filename' => $destDirectory . '/' . $filename]);
        $this->ajax->success();
    }

    /**
     * Change command visibility of multiple commands
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function setIsVisibles()
    {
        $cmdIds = json_decode(Utils::init('cmds'), true);
        foreach ($cmdIds as $cmdId) {
            $cmd = CmdManager::byId($cmdId);
            if (!is_object($cmd)) {
                throw new CoreException(__('Cmd inconnu. Vérifiez l\'ID') . ' ' . $cmdId);
            }
            $cmd->setIsVisible(Utils::init('isVisible'));
            $cmd->save(true);
        }
        $this->ajax->success();
    }

    public function getDeadCmd()
    {
        $result = [
            Common::CORE => [NextDomObj::CMD => NextDomHelper::getDeadCmd(), 'name' => __('Jeedom')],
            NextDomObj::CMD => [NextDomObj::CMD => CmdManager::deadCmd(), 'name' => __('Commande')],
            NextDomObj::JEE_OBJECT => [NextDomObj::CMD => JeeObjectManager::deadCmd(), 'name' => __('Objet')],
            NextDomObj::SCENARIO => [NextDomObj::CMD => ScenarioManager::consystencyCheck(true), 'name' => __('Scénario')],
            NextDomObj::INTERACT_DEF => [NextDomObj::CMD => InteractDefManager::deadCmd(), 'name' => __('Intéraction')],
            NextDomObj::USER => [NextDomObj::CMD => UserManager::deadCmd(), 'name' => __('Utilisateur')],
        ];
        foreach (PluginManager::listPlugin(true) as $plugin) {
            $plugin_id = $plugin->getId();
            $result[$plugin_id] = [NextDomObj::CMD => [], 'name' => 'Plugin ' . $plugin->getName()];
            if (method_exists($plugin_id, 'deadCmd')) {
                $result[$plugin_id][NextDomObj::CMD] = $plugin_id::deadCmd();
            } else {
                $result[$plugin_id][NextDomObj::CMD] = EqLogicManager::deadCmdGeneric($plugin_id);
            }
        }
        $this->ajax->success($result);
    }
}