1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 
<?php
/* --------------------------------------------------------------
  AbstractModuleCenterModule.inc.php 2017-09-28
  Gambio GmbH
  http://www.gambio.de
  Copyright (c) 2017 Gambio GmbH
  Released under the GNU General Public License (Version 2)
  [http://www.gnu.org/licenses/gpl-2.0.html]
  --------------------------------------------------------------
*/

/**
 * Class AbstractModuleCenterModule
 * @implements  ModuleCenterModuleInterface
 * @category    System
 * @package     Modules
 */
abstract class AbstractModuleCenterModule implements ModuleCenterModuleInterface
{
    /**
     * @var LanguageTextManager $languageTextManager
     */
    protected $languageTextManager;

    /**
     * @var CI_DB_query_builder
     */
    protected $db;

    /**
     * @var null|bool $isInstalled
     */
    protected $isInstalled = null;

    /**
     * @var string $name
     */
    protected $name = '';

    /**
     * @var string $title
     */
    protected $title = '';

    /**
     * @var string $description
     */
    protected $description = '';

    /**
     * @var int $sortOrder
     */
    protected $sortOrder = 100000;

    /**
     * @var CacheControl $cacheControl
     */
    protected $cacheControl;


    /**
     * @param LanguageTextManager $languageTextManager
     * @param CI_DB_query_builder $db
     * @param CacheControl        $cacheControl
     */
    public function __construct(LanguageTextManager $languageTextManager,
                                CI_DB_query_builder $db,
                                CacheControl $cacheControl)
    {
        $this->languageTextManager = $languageTextManager;
        $this->db                  = $db;
        $this->cacheControl        = $cacheControl;

        $this->_init();
    }


    /**
     * Initialize the module e.g. set title, description, sort order etc.
     *
     * Function will be called in the constructor
     */
    abstract protected function _init();


    /**
     * Set module name
     */
    protected function _setModuleName()
    {
        $moduleName = get_called_class();
        $moduleName = substr($moduleName, 0, strpos($moduleName, 'ModuleCenterModule'));

        $this->name = $moduleName;
    }


    /**
     * Set isInstalled flag
     */
    protected function _setIsInstalled()
    {
        $configurationKey  = 'MODULE_CENTER_' . strtoupper($this->getName()) . '_INSTALLED';
        $isInstalledResult = $this->db->select('gm_value')
                                      ->from('gm_configuration')
                                      ->where('gm_key', $configurationKey)
                                      ->get();
        $isInstalled       = $isInstalledResult->row();
        $this->isInstalled = $isInstalled ? (boolean)$isInstalled->gm_value : null;
    }


    /**
     * Installs the module
     */
    public function install()
    {
        $this->isInstalled = true;
        $this->_store(true);
    }


    /**
     * Uninstalls the module
     */
    public function uninstall()
    {
        $this->isInstalled = false;
        $this->_store(false);
    }


    /**
     * Returns true, if the module is installed. Otherwise false is returned.
     *
     * @return bool
     */
    final public function isInstalled()
    {
        if(is_null($this->isInstalled))
        {
            $this->_setIsInstalled();
        }

        return $this->isInstalled;
    }
    
    
    /**
     * Returns true, if the module should be displayed in module center.
     *
     * @return bool
     */
    public function isVisible()
    {
        return true;
    }


    /**
     * Returns the name of the module
     *
     * The name is filtered by a regular expression.
     * Only alphanumeric characters are allowed.
     *
     * @return string
     */
    final public function getName()
    {
        if(empty($this->name))
        {
            $this->_setModuleName();
        }

        return preg_replace('[\W]', '', $this->name);
    }


    /**
     * Returns the title of the module
     *
     * @return string
     */
    final public function getTitle()
    {
        return substr(strip_tags($this->title), 0, 50);
    }


    /**
     * Returns the description of the module
     *
     * @return string
     */
    final public function getDescription()
    {
        return substr(strip_tags($this->description, '<br><i><strong><u>'), 0, 500);
    }


    /**
     * Returns the sort order of the module
     *
     * @return double
     */
    final public function getSortOrder()
    {
        return (double)$this->sortOrder;
    }


    final protected function _store($installed)
    {
        $installed        = (boolean)$installed;
        $configurationKey = 'MODULE_CENTER_' . strtoupper($this->getName()) . '_INSTALLED';

        $this->db->set('gm_key', $configurationKey);
        $this->db->set('gm_value', (int)$installed);
        $this->db->where('gm_key', $configurationKey);
        $this->db->replace('gm_configuration');

        $this->_clearCache();
    }


    /**
     * Empty modules cache after module installation
     */
    final protected function _clearCache()
    {
        $this->cacheControl->clear_data_cache();
    }
}