Source of file Message.php

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

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
<?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\NextDomObj;
use NextDom\Helpers\DBHelper;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\EventManager;
use NextDom\Managers\ScenarioExpressionManager;
use NextDom\Model\Entity\Parents\BaseEntity;
use NextDom\Model\Entity\Parents\LogicalIdEntity;

/**
 * Message
 *
 * @ORM\Table(name="message", indexes={@ORM\Index(name="plugin_logicalID", columns={"plugin", "logicalId"})})
 * @ORM\Entity
 */
class Message extends BaseEntity
{
    const CLASS_NAME = Message::class;
    const DB_CLASS_NAME = '`message`';
    const TABLE_NAME = NextDomObj::MESSAGE;

    use LogicalIdEntity;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime", nullable=false)
     */
    protected $date;

    /**
     * @var string
     *
     * @ORM\Column(name="plugin", type="string", length=127, nullable=false)
     */
    protected $plugin;

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

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

    /**
     * @param bool $_writeMessage
     *
     * @return bool|Message|null
     *
     * @throws \ReflectionException
     * @throws \Exception
     */
    public function save($_writeMessage = true)
    {
        if ($this->getMessage() == '') {
            return null;
        }

        $values = ['plugin' => $this->getPlugin()];
        if ($this->getLogicalId() == '') {
            $this->setLogicalId($this->getPlugin() . '::' . ConfigManager::genKey());
            $values['message'] = $this->getMessage();
            $sql = 'SELECT count(*)
                    FROM message
                    WHERE plugin = :plugin
                    AND message = :message';
            $result = DBHelper::getOne($sql, $values);
            if ($result['count(*)'] != 0) {
                $values['date'] = $this->getDate();
                $sql = 'UPDATE message
                        SET date = :date
                        WHERE plugin = :plugin
                        AND message = :message
                        LIMIT 1';
                DBHelper::exec($sql, $values);
                return $this;
            }
        } else {
            $values['logicalId'] = $this->getLogicalId();
            $sql = 'SELECT count(*)
                    FROM message
                    WHERE plugin = :plugin
                    AND logicalId = :logicalId';
            $result = DBHelper::getOne($sql, $values);
            if ($result['count(*)'] != 0) {
                $values['date'] = $this->getDate();
                $sql = 'UPDATE message
                        SET date = :date
                        WHERE plugin = :plugin
                        AND logicalId = :logicalId
                        LIMIT 1';
                DBHelper::exec($sql, $values);
                return $this;
            }
        }

        if ($_writeMessage) {
            DBHelper::save($this);
            $params = [
                '#plugin#' => $this->getPlugin(),
                '#subject#' => $this->getMessage(),
                '#message#' => $this->getMessage(),
            ];
            $actions = ConfigManager::byKey('actionOnMessage');
            if (is_array($actions) && count($actions) > 0) {
                foreach ($actions as $action) {
                    $options = array();
                    if (isset($action['options'])) {
                        $options = $action['options'];
                    }
                    foreach ($options as &$value) {
                        $value = str_replace(array_keys($params), $params, $value);
                    }
                    ScenarioExpressionManager::createAndExec('action', $action['cmd'], $options);
                }
            }
            EventManager::add('notify', ['title' => __('Message de ') . $this->getPlugin(), 'message' => $this->getMessage(), 'category' => 'message']);
            EventManager::add('message::refreshMessageNumber');
        }


        return true;
    }

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

    /*     * **********************Getteur Setteur*************************** */

    /**
     * @param $_message
     * @return $this
     */
    public function setMessage($_message)
    {
        $this->updateChangeState($this->message, $_message);
        $this->message = $_message;
        return $this;
    }

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

    /**
     * @param $_plugin
     * @return $this
     */
    public function setPlugin($_plugin)
    {
        $this->updateChangeState($this->plugin, $_plugin);
        $this->plugin = $_plugin;
        return $this;
    }

    public function remove()
    {
        EventManager::add('message::refreshMessageNumber');
        return parent::remove();
    }

    /**
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * @param $_date
     * @return $this
     */
    public function setDate($_date)
    {
        $this->updateChangeState($this->date, $_date);
        $this->date = $_date;
        return $this;
    }

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

    /**
     * @param $_action
     * @return $this
     */
    public function setAction($_action)
    {
        $this->updateChangeState($this->action, $_action);
        $this->action = $_action;
        return $this;
    }
}