Source of file Widget.php

Size: 7,992 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
<?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\Exceptions\CoreException;
use NextDom\Helpers\DBHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\CmdManager;
use NextDom\Managers\WidgetManager;
use NextDom\Model\Entity\Parents\BaseEntity;
use NextDom\Model\Entity\Parents\DisplayEntity;
use NextDom\Model\Entity\Parents\NameEntity;
use NextDom\Model\Entity\Parents\SubTypeEntity;
use NextDom\Model\Entity\Parents\TypeEntity;
use ReflectionException;

/**
 * Widget
 *
 * @ORM\Table(name="Widget")
 * @ORM\Entity
 */
class Widget extends BaseEntity {

    const TABLE_NAME = NextDomObj::WIDGET;

    use NameEntity,
        TypeEntity,
        SubTypeEntity,
        DisplayEntity;

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

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

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

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

    /**
     * @param $_template
     * @return $this
     */
    public function setTemplate($_template) {
        $this->updateChangeState($this->template, $_template);
        $this->template = $_template;
        return $this;
    }

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

    /**
     * @param $_replace
     * @return $this
     */
    public function setReplace($_replace) {
        $this->updateChangeState($this->replace, $_replace);
        $this->replace = $_replace;
        return $this;
    }

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

    /**
     * @param $_test
     * @return $this
     */
    public function setTest($_test) {
        $this->updateChangeState($this->test, $_test);
        $this->test = $_test;
        return $this;
    }

    public function preInsert() {
        
    }

    public function preSave() {
        if ($this->getType() == '') {
            $this->setType('action');
        }
    }

    public function preUpdate() {
        $widget = WidgetManager::byId($this->getId());
        if ($widget->getName() != $this->getName()) {
            $usedBy = $widget->getUsedBy();
            if (is_array($usedBy) && count($usedBy) > 0) {
                foreach ($usedBy as $cmd) {
                    if ($cmd->getTemplate('dashboard') == 'custom::' . $widget->getName()) {
                        $cmd->setTemplate('dashboard', 'custom::' . $this->getName());
                    }
                    if ($cmd->getTemplate('mobile') == 'custom::' . $widget->getName()) {
                        $cmd->setTemplate('mobile', 'custom::' . $this->getName());
                    }
                    $cmd->save(true);
                }
            }
        }
        if ($widget->getType() != $this->getType() || $widget->getSubType() != $this->getSubType()) {
            $usedBy = $widget->getUsedBy();
            if (is_array($usedBy) && count($usedBy) > 0) {
                foreach ($usedBy as $cmd) {
                    if ($cmd->getTemplate('dashboard') == 'custom::' . $widget->getName()) {
                        $cmd->setTemplate('dashboard', 'default');
                    }
                    if ($cmd->getTemplate('mobile') == 'custom::' . $widget->getName()) {
                        $cmd->setTemplate('mobile', 'default');
                    }
                    $cmd->save(true);
                }
            }
        }
    }

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

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

    /**
     * @return bool
     * @throws CoreException
     * @throws ReflectionException
     */
    public function save() {
        DBHelper::save($this);
        return true;
    }

    public function postSave() {
        $usedBy = $this->getUsedBy();
        if (is_array($usedBy) && count($usedBy) > 0) {
            foreach ($usedBy as $cmd) {
                $eqLogic = $cmd->getEqLogic();
                if (is_object($eqLogic)) {
                    $eqLogic->emptyCacheWidget();
                }
            }
        }
    }

    public function preRemove() {
        
    }

    public function remove() {
        $usedBy = $this->getUsedBy();
        if (is_array($usedBy) && count($usedBy) > 0) {
            foreach ($usedBy as $cmd) {
                if ($cmd->getTemplate('dashboard') == 'custom::' . $this->getName()) {
                    $cmd->setTemplate('dashboard', 'default');
                }
                if ($cmd->getTemplate('mobile') == 'custom::' . $this->getName()) {
                    $cmd->setTemplate('mobile', 'default');
                }
                $cmd->save(true);
            }
        }
        DBHelper::remove($this);
    }

    /**
     * 
     * @return Cmd[]
     */
    public function getUsedBy() {
        return array_merge(
            CmdManager::searchTemplate('dashboard":"custom::' . $this->getName() . '"'),
            CmdManager::searchTemplate('mobile":"custom::' . $this->getName() . '"')
        );
    }

    public function postRemove() {
        //WidgetManager::cleanWidget();
    }

    public function emptyTest() {
        $this->test = null;
    }

    /**
     * @return string
     */
    public function getLinkToConfiguration() {
        return 'index.php?v=d&p=widget&id=' . $this->getId();
    }

    /**
     * @param array $_data
     * @param int $_level
     * @param int $_drill
     * @return array|null
     */
    public function getLinkData(&$_data = ['node' => [], 'link' => []], $_level = 0, $_drill = 3) {
        if (isset($_data['node']['widget' . $this->getId()])) {
            return null;
        }
        $_level++;
        if ($_level > $_drill) {
            return $_data;
        }
        $icon = Utils::findCodeIcon('fa-comments-o');
        $_data['node']['widget' . $this->getId()] = [
            'id' => 'widget' . $this->getId(),
            'name' => substr($this->getHumanName(), 0, 20),
            'icon' => $icon['icon'],
            'fontfamily' => $icon['fontfamily'],
            'fontsize' => '1.5em',
            'fontweight' => ($_level == 1) ? 'bold' : 'normal',
            'texty' => -14,
            'textx' => 0,
            'title' => $this->getHumanName(),
            'url' => 'index.php?v=d&p=widget&id=' . $this->getId(),
        ];
        return null;
    }

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

}