brick-camp.php 2.91 KB
Newer Older
1 2 3 4 5 6
<?php
namespace Grav\Plugin;

use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Uri;
7 8
use Grav\Plugin\BrickCamp\Techs;
use Grav\Plugin\BrickCamp\TermsTwigExtension;
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
use RocketTheme\Toolbox\Event\Event;

/**
 * Class BrickCampPlugin
 * @package Grav\Plugin
 */
class BrickCampPlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => [
                ['onPluginsInitialized', 0],
            ],
        ];
    }

    /**
     * Initialize the plugin
     */
    public function onPluginsInitialized()
    {
        // Increase php execution time
        ini_set('max_execution_time', 300);

        // Don't proceed if we are in the admin view
        if ($this->isAdmin()) {
            return;
        }

        // autoload subclasses when needed
        include_once __DIR__ . '/classes/autoload.php';

        // subscribe to needed events
        $this->enable([
            'onTwigExtensions' => ['onTwigExtensions', 0],
            'onFormInitialized' => ['onFormInitialized', 0],
            // 'onFormProcessed' => ['onFormProcessed', 0],
            'onPageProcessed' => ['onPageProcessed', 0],
            'onCollectionProcessed' => ['onCollectionProcessed', 0],
        ]);
    }

    /**
     * Initialize the Twig Exensions for querying information in template files
     */
    public function onTwigExtensions()
    {
        $this->grav['twig']->twig->addExtension(new TermsTwigExtension);
    }

    /**
     * Initialize Filter Forms
     *
     * @param Event $event
     * @return void
     */
    public function onFormInitialized(Event $event)
    {
        $form = $event['form'];

        // autofill form with URL filters
        if (substr($form->name(), 0, 6) == 'filter') {
            $this->grav['assets']->addJs('plugin://brick-camp/assets/items-filter.js', 110);
        }
    }

    public function onPageProcessed(Event $event)
    {
        $page = $event['page'];
        if ($page->template() == 'tech') {
            Techs::onPageProcessed($page);
        }
    }

    /**
     * Edit collections based on URI
     *
     * @param Event $event
     * @return void
     */
    public function onCollectionProcessed(Event $event)
    {
        $collection = $event['collection'];
        $params = $collection->params();

95 96 97 98 99 100
        if (isset($params['url_taxonomy_filters'])) {
            $process_taxonomy = $params['url_taxonomy_filters'];
        } else {
            $process_taxonomy = $this->grav['config']->get('system.pages.url_taxonomy_filters');
        }
        
101 102 103 104 105 106 107 108 109 110 111 112
        if ($process_taxonomy) {
            $orderby = $this->grav['uri']->param('orderby');
            $orderdir = $this->grav['uri']->param('orderdir');
        
            if ($orderby) {
                $collection->order($orderby, $orderdir ?: 'asc');
            } elseif ($orderdir) {
                $collection->order('default', $orderdir);
            }
        }
    }
}