1<?php
2
3/**
4 * Mikio Syntax Plugin: Carousel
5 *
6 * @link        http://github.com/nomadjimbob/mikioplugin
7 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author      James Collins <james.collins@outlook.com.au>
9 */
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12require_once(dirname(__FILE__) . '/core.php');
13
14class syntax_plugin_mikioplugin_carousel extends syntax_plugin_mikioplugin_core
15{
16    public $tag                 = 'carousel';
17    public $hasEndTag           = true;
18    public $options             = array(
19        'transition'    => array(
20            'type'     => 'choice',
21            'data'     => array('slide', 'fade'),
22            'default'  => ''
23        ),
24        'indicators'    => array(
25            'type'     => 'choice',
26            'data'     => array('true', 'false', 'circle'),
27            'default'  => 'true'
28        ),
29        'controls'      => array('type'     => 'boolean',   'default'   => 'true'),
30        'start'         => array('type'     => 'boolean',   'default'   => 'false'),
31        'cover'         => array('type'     => 'boolean',   'default'   => 'false'),
32        'control-color' => array('type'     => 'color',     'default'   => '#fff'),
33        'control-outline-color' => array('type'     => 'color',     'default'   => ''),
34        'control-outline-width' => array('type'     => 'multisize',     'default'   => ''),
35        'dynamic' => array('type' => 'text', 'default' => ''),
36        'dynamic-prefix' => array('type' => 'text', 'default' => ''),
37        'dynamic-start' => array('type' => 'number', 'default' => '-1'),
38        'dynamic-count' => array('type' => 'number', 'default' => '-1'),
39    );
40
41    public function __construct()
42    {
43        $this->addCommonOptions('height');
44    }
45
46
47    public function render_lexer_enter(Doku_Renderer $renderer, $data)
48    {
49        $classes = $this->buildClass($data, array('transition'));
50        $styles = $this->buildStyle(array('height' => $data['height']), TRUE);
51
52        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'carousel' . ($data['cover'] ? ' ' . $this->classPrefix . 'image-cover' : '') . $classes . '" data-auto-start="' . ($data['start'] ? 'true' : 'false') . '"' . $styles . '>';
53        $renderer->doc .= '<div class="' . $this->elemClass . ' ' . $this->classPrefix . 'carousel-inner">';
54
55        if (strlen($data['dynamic']) > 0) {
56            global $conf;
57
58            $namespace = $data['dynamic'];
59            if (substr($namespace, -1) === ':') {
60                $namespace = substr($namespace, 0, -1);
61            }
62
63            $path = str_replace(':', '/', $namespace);
64
65            $pages = array();
66            $count = count($pages);
67
68            search($pages, $conf['datadir'] . '/' . $path, 'search_allpages', array('depth' => 1, 'skipacl' => true));
69
70            for ($i = 0; $i < $count; $i++) {
71                $page = $pages[$i];
72                if ($data['dynamic-start'] == -1 || $data['dynamic-start'] <= ($i + 1)) {
73                    if ($data['dynamic-start'] != -1 && $data['dynamic-count'] != -1 && $data['dynamic-start'] + $data['dynamic-count'] >= $i) {
74                        break;
75                    }
76
77                    $item_data = array();
78
79                    $page_id = $namespace . ':' . $page['id'];
80                    preg_match('/{{([^>|}]+(\.jpg|\.gif|\.png))\|?.*}}/', rawWiki($page_id), $image_matches);
81                    if (count($image_matches) > 1) {
82                        $item_data['image'] = $image_matches[1];
83                    }
84
85                    $item_data['title'] = (strlen($data['dynamic-prefix']) > 0 ? $data['dynamic-prefix'] . ' ' : '') . p_get_first_heading($page_id);
86                    $item_data['url'] = $page_id;
87
88                    $this->syntaxRender($renderer, 'carouselitem', '', $item_data, MIKIO_LEXER_SPECIAL);
89                }
90            }
91        }
92    }
93
94
95    public function render_lexer_exit(Doku_Renderer $renderer, $data)
96    {
97        $renderer->doc .= '</div>';
98
99        if ($data['controls'] === TRUE) {
100            $svg_styles = $this->buildStyle(array('fill' => $data['control-color'], 'stroke' => $data['control-outline-color'], 'stroke-width' => $data['control-outline-width']), TRUE);
101
102            $renderer->doc .= '<a href="#" class="' . $this->elemClass . ' ' . $this->classPrefix . 'carousel-control ' . $this->classPrefix . 'carousel-control-prev" role="button"><svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 8 8"' . $svg_styles . '><path d="M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z"/></svg></a>';
103            $renderer->doc .= '<a href="#" class="' . $this->elemClass . ' ' . $this->classPrefix . 'carousel-control ' . $this->classPrefix . 'carousel-control-next" role="button"><svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 8 8"' . $svg_styles . '><path d="M2.75 0l-1.5 1.5L3.75 4l-2.5 2.5L2.75 8l4-4-4-4z"/></svg></a>';
104        }
105
106        if (strcasecmp($data['indicators'], 'false') != 0) {
107            $renderer->doc .= '<ul class="' . $this->elemClass . ' ' . $this->classPrefix . 'carousel-indicators' . (strcasecmp($data['indicators'], 'circle') == 0 ? ' ' . $this->classPrefix . 'carousel-indicators-circle' : '') . '"></ul>';
108        }
109
110        $renderer->doc .= '</div>';
111    }
112}
113