Source of file InteractQuery.php

Size: 11,816 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Model/Entity/InteractQuery.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
<?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/>.
 */

namespace NextDom\Model\Entity;

use NextDom\Enums\DateFormat;
use NextDom\Enums\NextDomObj;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\DBHelper;
use NextDom\Helpers\LogHelper;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\CmdManager;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\CronManager;
use NextDom\Managers\InteractDefManager;
use NextDom\Managers\ScenarioExpressionManager;
use NextDom\Model\Entity\Parents\BaseEntity;

/**
 * Interactquery
 *
 * @ORM\Table(name="interactQuery", indexes={@ORM\Index(name="fk_sarahQuery_sarahDef1_idx", columns={"interactDef_id"}), @ORM\Index(name="query", columns={"query"})})
 * @ORM\Entity
 */
class InteractQuery extends BaseEntity
{
    const TABLE_NAME = NextDomObj::INTERACT_QUERY;

    /**
     * @var integer
     *
     * @ORM\Column(name="interactDef_id", type="integer", nullable=false)
     */
    protected $interactDef_id;

    /**
     * @var string
     *
     * @ORM\Column(name="query", type="text", length=65535, nullable=true)
     */
    protected $query;

    /**
     * @var string
     *
     * @ORM\Column(name="actions", type="text", length=65535, nullable=true)
     */
    protected $actions;

    /**
     * @return $this
     * @throws \NextDom\Exceptions\CoreException
     * @throws \ReflectionException
     */
    public function save()
    {
        if ($this->getQuery() == '') {
            throw new CoreException(__('La commande vocale ne peut pas être vide'));
        }
        if ($this->getInteractDef_id() == '') {
            throw new CoreException(__('InteractDef_id ne peut pas être vide'));
        }
        DBHelper::save($this);
        return $this;
    }

    /**
     * @return string
     */
    public function getQuery()
    {
        return $this->query;
    }

    /**
     * @param $_query
     * @return $this
     */
    public function setQuery($_query)
    {
        $this->updateChangeState($this->query, $_query);
        $this->query = $_query;
        return $this;
    }

    /**
     * @return int
     */
    public function getInteractDef_id()
    {
        return $this->interactDef_id;
    }

    /**
     * @param $_interactDef_id
     * @return $this
     */
    public function setInteractDef_id($_interactDef_id)
    {
        $this->updateChangeState($this->interactDef_id, $_interactDef_id);
        $this->interactDef_id = $_interactDef_id;
        return $this;
    }

    /**
     * @param $_parameters
     * @return mixed
     * @throws \NextDom\Exceptions\CoreException
     * @throws \ReflectionException
     */
    public function executeAndReply($_parameters)
    {
        if (isset($_parameters['reply_cmd'])) {
            unset($_parameters['reply_cmd']);
        }
        $interactDef = InteractDefManager::byId($this->getInteractDef_id());
        if (!is_object($interactDef)) {
            return __('Inconsistance de la base de données');
        }
        if (isset($_parameters['profile']) && trim($interactDef->getPerson()) != '') {
            $person = strtolower($interactDef->getPerson());
            $person = explode('|', $person);
            if (!in_array($_parameters['profile'], $person)) {
                return __('Vous n\'êtes pas autorisé à exécuter cette action');
            }
        }
        $reply = $interactDef->selectReply();
        $replace = ['#query#' => $this->getQuery()];
        foreach ($_parameters as $key => $value) {
            $replace['#' . $key . '#'] = $value;
        }
        $tags = null;
        if (isset($_parameters['dictation'])) {
            $tags = InteractDefManager::getTagFromQuery($this->getQuery(), $_parameters['dictation']);
            $replace['#dictation#'] = $_parameters['dictation'];
        }
        if (is_array($tags)) {
            $replace = array_merge($replace, $tags);
        }
        $executeDate = null;

        if (isset($replace['#duration#'])) {
            $dateConvert = [
                'heure' => 'hour',
                'mois' => 'month',
                'semaine' => 'week',
                'année' => 'year',
            ];
            $replace['#duration#'] = str_replace(array_keys($dateConvert), $dateConvert, $replace['#duration#']);
            $executeDate = strtotime('+' . $replace['#duration#']);
        }
        if (isset($replace['#time#'])) {
            $time = str_replace(['h'], [':'], $replace['#time#']);
            if (strlen($time) == 1) {
                $time .= ':00';
            } else if (strlen($time) == 2) {
                $time .= ':00';
            } else if (strlen($time) == 3) {
                $time .= '00';
            }
            $time = str_replace('::', ':', $time);
            $executeDate = strtotime($time);
            if ($executeDate < strtotime('now')) {
                $executeDate += 3600;
            }
        }
        if ($executeDate !== null && !isset($_parameters['execNow'])) {
            if (date('Y', $executeDate) < 2000) {
                return __('Erreur : impossible de calculer la date de programmation');
            }
            if ($executeDate < (strtotime('now') + 60)) {
                $executeDate = strtotime('now') + 60;
            }
            $crons = CronManager::searchClassAndFunction('interactQuery', 'doIn', '"interactQuery_id":' . $this->getId());
            if (is_array($crons)) {
                foreach ($crons as $cron) {
                    if ($cron->getState() != 'run') {
                        $cron->remove();
                    }
                }
            }
            $cron = new Cron();
            $cron->setClass('interactQuery');
            $cron->setFunction('doIn');
            $cron->setOption(array_merge(['interactQuery_id' => intval($this->getId())], $_parameters));
            $cron->setLastRun(date(DateFormat::FULL));
            $cron->setOnce(1);
            $cron->setSchedule(CronManager::convertDateToCron($executeDate));
            $cron->save();
            $replace['#valeur#'] = date(DateFormat::FULL, $executeDate);
            $result = ScenarioExpressionManager::setTags(str_replace(array_keys($replace), $replace, $reply));
            return $result;
        }
        $replace['#valeur#'] = '';
        $colors = array_change_key_case(ConfigManager::byKey('convertColor'));
        if (is_array($this->getActions('cmd'))) {
            foreach ($this->getActions('cmd') as $action) {
                try {
                    $options = [];
                    if (isset($action['options'])) {
                        $options = $action['options'];
                    }
                    if ($tags !== null) {
                        foreach ($options as &$option) {
                            $option = str_replace(array_keys($replace), $replace, $option);
                        }
                        if (isset($options['color']) && isset($colors[strtolower($options['color'])])) {
                            $options['color'] = $colors[strtolower($options['color'])];
                        }
                    }
                    $cmd = CmdManager::byId(str_replace('#', '', $action['cmd']));
                    if (is_object($cmd)) {
                        $replace['#unite#'] = $cmd->getUnite();
                        $replace['#commande#'] = $cmd->getName();
                        $replace['#objet#'] = '';
                        $replace['#equipement#'] = '';
                        $eqLogic = $cmd->getEqLogicId();
                        if (is_object($eqLogic)) {
                            $replace['#equipement#'] = $eqLogic->getName();
                            $linkedObject = $eqLogic->getObject();
                            if (is_object($linkedObject)) {
                                $replace['#objet#'] = $linkedObject->getName();
                            }
                        }
                    }
                    $tags = [];
                    if (isset($options['tags'])) {
                        $options['tags'] = Utils::arg2array($options['tags']);
                        foreach ($options['tags'] as $key => $value) {
                            $tags['#' . trim(trim($key), '#') . '#'] = ScenarioExpressionManager::setTags(trim($value));
                        }
                    }
                    $options['tags'] = array_merge($replace, $tags);
                    $return = ScenarioExpressionManager::createAndExec('action', $action['cmd'], $options);
                    if (trim($return) !== '' && trim($return) !== null) {
                        $replace['#valeur#'] .= ' ' . $return;
                    }
                } catch (\Exception $e) {
                    LogHelper::addError('interact', __('Erreur lors de l\'exécution de ') . $action['cmd'] . __('. Détails : ') . $e->getMessage());
                }
            }
        }
        if ($interactDef->getOptions('waitBeforeReply') != '' && $interactDef->getOptions('waitBeforeReply') != 0 && is_numeric($interactDef->getOptions('waitBeforeReply'))) {
            sleep($interactDef->getOptions('waitBeforeReply'));
        }
        $reply = NextDomHelper::evaluateExpression($reply);
        $replace['#valeur#'] = trim($replace['#valeur#']);
        $replace['#profile#'] = isset($_parameters['profile']) ? $_parameters['profile'] : '';
        if ($interactDef->getOptions('convertBinary') != '') {
            $convertBinary = explode('|', $interactDef->getOptions('convertBinary'));
            if (is_array($convertBinary) && count($convertBinary) == 2) {
                $replace['1'] = $convertBinary[1];
                $replace['0'] = $convertBinary[0];
            }
        }
        foreach ($replace as $key => $value) {
            if (is_array($value)) {
                unset($replace[$key]);
            }
        }
        if ($replace['#valeur#'] == '') {
            $replace['#valeur#'] = __('aucune valeur');
        }
        $replace['"'] = '';
        return str_replace(array_keys($replace), $replace, $reply);
    }

    /**
     * @param string $_key
     * @param string $_default
     * @return array|bool|mixed|null|string
     */
    public function getActions($_key = '', $_default = '')
    {
        return Utils::getJsonAttr($this->actions, $_key, $_default);
    }

    /**
     * @param $_key
     * @param $_value
     * @return $this
     */
    public function setActions($_key, $_value)
    {
        $actions = Utils::setJsonAttr($this->actions, $_key, $_value);
        $this->updateChangeState($this->actions, $actions);
        $this->actions = $actions;
        return $this;
    }

    /**
     * @return InteractDef
     * @throws \NextDom\Exceptions\CoreException
     * @throws \ReflectionException
     */
    public function getInteractDef()
    {
        return InteractDefManager::byId($this->interactDef_id);
    }

    /**
     * @param $_replace
     * @param $_by
     * @param $_in
     */
    public function replaceForContextual($_replace, $_by, $_in)
    {
        Interactquery::replaceForContextual($_replace, $_by, $_in);
    }
}