Source of file InteractAjax.php

Size: 4,609 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
<?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\AjaxParams;
use NextDom\Enums\Common;
use NextDom\Enums\SQLField;
use NextDom\Enums\UserRight;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\CmdManager;
use NextDom\Managers\InteractDefManager;
use NextDom\Managers\InteractQueryManager;
use NextDom\Model\Entity\InteractDef;

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

    public function all()
    {
        $results = Utils::o2a(InteractDefManager::all());
        foreach ($results as &$result) {
            // @TODO TOus sélectionnés dans tous les cas
            $result['nbInteractQuery'] = count(InteractQueryManager::byInteractDefId($result[AjaxParams::ID]));
            $result['nbEnableInteractQuery'] = count(InteractQueryManager::byInteractDefId($result[AjaxParams::ID]));
            if ($result[SQLField::LINK_TYPE] == 'cmd' && $result[SQLField::LINK_ID] != '') {
                $linkId = '';
                foreach (explode('&&', $result[SQLField::LINK_ID]) as $cmd_id) {
                    $cmd = CmdManager::byId($cmd_id);
                    if (is_object($cmd)) {
                        $linkId .= CmdManager::cmdToHumanReadable('#' . $cmd->getId() . '# && ');
                    }

                }
                $result[SQLField::LINK_ID] = trim(trim($linkId), '&&');
            }
        }
        $this->ajax->success($results);
    }

    public function byId()
    {
        $result = Utils::o2a(InteractDefManager::byId(Utils::init(AjaxParams::ID)));
        $result['nbInteractQuery'] = count(InteractQueryManager::byInteractDefId($result[AjaxParams::ID]));
        $result['nbEnableInteractQuery'] = count(InteractQueryManager::byInteractDefId($result[AjaxParams::ID]));
        $this->ajax->success(NextDomHelper::toHumanReadable($result));
    }

    public function save()
    {
        $interact_json = NextDomHelper::fromHumanReadable(json_decode(Utils::init('interact'), true));
        if (isset($interact_json[AjaxParams::ID])) {
            $interact = InteractDefManager::byId($interact_json[AjaxParams::ID]);
        }
        if (!isset($interact) || !is_object($interact)) {
            $interact = new InteractDef();
        }
        Utils::a2o($interact, $interact_json);
        $interact->save();
        $this->ajax->success(Utils::o2a($interact));
    }

    public function regenerateInteract()
    {
        InteractDefManager::regenerateInteract();
        $this->ajax->success();
    }

    public function remove()
    {
        $interact = InteractDefManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($interact)) {
            throw new CoreException(__('Interaction inconnue. Vérifiez l\'ID'));
        }
        $interact->remove();
        $this->ajax->success();
    }

    public function changeState()
    {
        $interactQuery = InteractQueryManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($interactQuery)) {
            throw new CoreException(__('InteractQuery ID inconnu'));
        }
        $interactQuery->setEnable(Utils::init(AjaxParams::ENABLE));
        $interactQuery->save();
        $this->ajax->success();
    }

    public function changeAllState()
    {
        $interactQueries = InteractQueryManager::byInteractDefId(Utils::init(AjaxParams::ID));
        if (is_array($interactQueries)) {
            foreach ($interactQueries as $interactQuery) {
                $interactQuery->setEnable(Utils::init(AjaxParams::ENABLE));
                $interactQuery->save();
            }
        }
        $this->ajax->success();
    }

    public function execute()
    {
        $this->ajax->success(InteractQueryManager::tryToReply(Utils::init('query')));
    }
}