1<?php
2/**
3 * E-shop Plugin: Displays forms for ordering things from e-shop
4 *
5 * @license    MIT (http://www.opensource.org/licenses/mit-license.php)
6 * @author     Pavol Rusnak <stick@gk2.sk>
7 */
8
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) die();
11if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_PLUGIN.'eshop/images/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class syntax_plugin_eshop extends DokuWiki_Syntax_Plugin {
15
16    function getType() { return 'substition'; }
17
18    function getPType() { return 'block'; }
19
20    function getSort() { return 201; }
21
22    function connectTo($mode) {
23        $this->Lexer->addSpecialPattern('\{\{eshop>[^}]*\}\}', $mode, 'plugin_eshop');
24    }
25
26    function handle($match, $state, $pos, &$handler) {
27        $match = substr($match, 8, -2);
28        $pairs = explode('&', $match);
29        foreach ($pairs as $pair) {
30            list($key, $value) = explode('=', $pair, 2);
31            $data[trim($key)] = trim($value);
32        }
33        $data = array_change_key_case($data, CASE_LOWER);
34
35        $btcusd = $this->getConf('btcusd');
36        $eurusd = $this->getConf('eurusd');
37
38        if ($data['usd'] && $btcusd && !$data['btc']) {
39            $data['btc'] = $data['usd'] / $btcusd;
40        } else
41        if ($data['btc'] && $btcusd && !$data['usd']) {
42            $data['usd'] = $data['btc'] * $btcusd;
43        }
44        if ($data['usd'] && $eurusd) {
45            $data['eur'] = $data['usd'] / $eurusd;
46        }
47        if (!$data['btc']) $data['btc'] = 0.0;
48        if (!$data['eur']) $data['eur'] = 0.0;
49        if (!$data['usd']) $data['usd'] = 0.0;
50        return $data;
51    }
52
53    function render($mode, &$renderer, $data) {
54        if ($data === false || $mode != 'xhtml') return false;
55
56        global $INFO;
57        $out  = '<form method="post" action="">';
58        $out .= '<table class="eshop_plugin">';
59        $out .= sprintf('<tr><th>Price in USD:</th><td class="price" id="eshop_price_usd" data-unitprice="%f">%0.2f</td></tr>', $data['usd'], $data['usd']);
60        $out .= sprintf('<tr><th>Price in EUR:</th><td class="price" id="eshop_price_eur" data-unitprice="%f">%0.2f</td></tr>', $data['eur'], $data['eur']);
61        $out .= sprintf('<tr><th>Price in BTC:</th><td class="price" id="eshop_price_btc" data-unitprice="%f">%0.3f</td></tr>', $data['btc'], $data['btc']);
62        $out .= '<tr><th>Quantity:</th><td class="count"><select name="count" id="eshop_count">';
63        for ($i = 1; $i <= 10; $i++) {
64            $out .= sprintf('<option value="%d">%d</option>', $i, $i);
65        }
66        $out .= '</select></td></tr>';
67        $out .= '<tr><td colspan="2" class="button"><input class="submit" type="submit" value="Buy"/></td></tr>';
68        $out .= '</table>';
69        $out .= sprintf('<input type="hidden" name="id" value="%s" />', end(explode(':', $INFO['id'])));
70        $out .= sprintf('<input type="hidden" name="name" value="%s" />', $INFO['meta']['title']);
71        $out .= sprintf('<input type="hidden" name="btcunit" value="%f" />', $data['btc']);
72        $out .= sprintf('<input type="hidden" name="btctotal" id="eshop_total" value="%f" />', $data['btc']);
73        $out .= '</form>';
74
75        $renderer->doc .= $out;
76        return true;
77    }
78
79}
80
81?>
82