controller.php 7.18 KB
<?php
namespace Concrete\Package\ThemeMitic;

use Concrete\Core\Application\Application as app;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\Asset\Asset;
use Concrete\Core\Asset\AssetList;
use Concrete\Core\Package\Package;
use Concrete\Core\Block\BlockType\BlockType;
use Concrete\Core\Block\BlockType\Set as BlockTypeSet;
use Concrete\Core\Support\Facade\Config;
use Concrete\Package\ThemeMitic\Src\Bootstrap4\CustomGridFramework;
use Concrete\Package\ThemeMitic\Src\Search\Pagination\View\Manager;
use Core;

defined('C5_EXECUTE') or die("Access Denied.");

class Controller extends Package
{
    protected $pkgHandle = 'theme_mitic';
    protected $pkgVersion = '4.0.0';
    protected $pkgPrefix = 'mitic';
    protected $pkgAlias = 'MITIC';

    protected $pkgAllowsFullContentSwap = true;
    protected $pkgAutoloaderMapCoreExtensions = true;
    protected $appVersionRequired = '5.7.5.9';
    protected $pkgAutoloaderRegistries = array(
        'src' => '\Concrete\Package\ThemeMitic\Src'
    );

    protected $pkg;

    public static $_jsPath = 'js'; // base folder: js path
    public static $_cssPath = 'css'; // base folder: css path

    /*
    protected $presetBlocks = array(
        'banner',
        'menu',
        'what_i_do',
        'about_me',
        'contacts',
        'social_media',
        'footer'
    );*/

    public static function create()
    {
        return new self(new app);
    }

    public function getPackageName()
    {
        return t('MITIC');
    }

    public function getPackageDescription()
    {
        return t('Plantilla institucional basada en la Guía Estándar para sitios web del Gobierno Nacional del Paraguay', self::getPackageNameID());
    }

    public static function getPackageNameID()
    {
        return 'mitic';
    }
    
    public function getPackageBlockTypeSet()
    {
        return strtolower(self::getPackageNameID());
    }

    public function getPackagePrefix()
    {
        return $this->pkgPrefix;
    }

    public function getPackageVersion()
    {
        return $this->pkgVersion;
    }

    public function getPackageDetails()
    {
        return t('%1$s %2$s', $this->pkgAlias, $this->getPackageVersion());
    }

    public function getPresetHandle($themeID)
    {
        $app = Application::getFacadeApplication();

        $db = $app->make('database')->connection();
        $pn = $db->fetchColumn('SELECT preset FROM PageThemeCustomStyles WHERE pThemeID = ?', array($themeID));

        $o = ($pn == true ? $pn : 'initial');

        return $o;
    }

    /** * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Assets paths
    */
    private static function _getThemePath()
    {
        return 'themes/' . strtolower(self::getPackageNameID()) . '/';
    }
    
    private static function _getJsPath()
    {
        return self::_getThemePath() . self::$_jsPath . '/';
    }

    private static function _getCssPath()
    {
        return self::_getThemePath() . self::$_cssPath . '/';
    }

    /** * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Assets register
    */
    public function on_start()
    {
        Core::bind('manager/view/pagination', function ($app) {
            return new Manager($app);
        });

        $al = AssetList::getInstance();

        $ph = array(
            'position' => Asset::ASSET_POSITION_HEADER,
            'minify' => true,
            'combine' => true
        );

        $pf = array(
            'position' => Asset::ASSET_POSITION_FOOTER,
            'minify' => true,
            'combine' => true
        );

        /**
        * Required JS + CSS this Theme
        */

        $al->registerGroup(
            'jst.theme.assets',
            array()
        );

        /**
        * Required JS + CSS Animate for this Theme
        */
        $al->register('javascript', 'jt.jquery.waypoints', self::_getJsPath() . 'min/jquery.waypoints.min.js', $pf, $this);

        // Register Assets Animate
        $al->register('javascript', 'animate-lib', self::_getJsPath() . 'min/jquery.lazy.animate.min.js', $pf, $this);

        $al->register('css', 'style.animate', self::_getCssPath() . 'build/animate/animate.min.css', $ph, $this);
        $al->register('css', 'style.animate.delay', self::_getCssPath() . 'build/animate/animate.delay.min.css', $ph, $this);

        $al->registerGroup(
            'jst.animate.assets',
            array()
        );

        $manager = Core::make('manager/grid_framework');
        $manager->extend('bootstrap4', function($app) {
            return new CustomGridFramework();
        });
    }

    /** * * * * * * * * * * * * * * * * * * * * * * * * * *
    * custom Blocks methods Utils
    */
    protected function getBlocksAndTablesNames()
    {
        $blocksAndTables = array();

        foreach (array_keys($this->getBlocksAvailable()) as $key) {
            $blocksAndTables[$this->pkgPrefix . '_' . $key] = 'bt' . self::getPackageNameID() . ucfirst(str_replace('_', '', mb_convert_case(mb_strtolower($key, "UTF-8"), MB_CASE_TITLE, "UTF-8")));
        }

        return $blocksAndTables;
    }

    public function getAreasNames()
    {
        $areas = array();

        foreach (array_keys($this->getBlocksAvailable()) as $key) {

            // list areas to display
            $areas[] = ucwords(str_replace('_', ' ', $key));
        }

        return $areas;
    }

    protected function getBlocksAvailable()
    {
        $o = array();

        // custom sort areas (in app config)
        $appConfigBlocks = Config::get('app.config.blocks');

        $blocks = (is_array($appConfigBlocks) == false ? $this->presetBlocks : $appConfigBlocks);

        if (is_array($blocks)) {
            foreach ($blocks as $key => $value) {
                if (is_array($value)) {
                    $o[$key] = $value;
                } else {
                    $o[$value] = array();
                }
            }
        }

        return $o;
    }

    /** * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Configure / Install / Upgrade / Uninstall
    */
    public function install()
    {
        $this->pkg = parent::install();

        if (BlockTypeSet::getByHandle($this->getPackageBlockTypeSet()) == false) {
            BlockTypeSet::add($this->getPackageBlockTypeSet(), ucfirst($this->getPackageBlockTypeSet()), $this->pkg);
            Theme::add($this->getPackageBlockTypeSet(), $this->pkg);
        }

        $this->configureBlocks();
    }
    
    public function upgrade()
    {
        parent::upgrade();

        $this->pkg = Package::getByHandle($this->pkgHandle);

        $this->configureBlocks();
    }
    
    public function uninstall()
    {
        parent::uninstall();

        $app = Application::getFacadeApplication();

        $db = $app->make('database')->connection();

        foreach ($this->getBlocksAndTablesNames() as $table) {
            $db->executeQuery('DROP TABLE IF EXISTS ' . $table);
        }
    }
    
    protected function configureBlocks()
    {
        foreach (array_keys($this->getBlocksAndTablesNames()) as $block) {
            $$block = BlockType::getByHandle($block);
            
            if (!is_object($$block)) {
                BlockType::installBlockType($block, $this->pkg);
            }
        }
    }
}