1<?php
2  /**
3   * @license    http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
4   * @author     Francois Merciol <dokuplugin@merciol.fr>
5   *
6   * Plugin Glossary: manage forms for glossary
7   */
8
9if (!defined ('DOKU_INC'))
10    define ('DOKU_INC', realpath (dirname (__FILE__).'/../../../').'/');
11if (!defined ('DOKU_PLUGIN'))
12    define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15require_once (dirname (__FILE__).'/../glossary.class.php');
16
17// ============================================================
18class syntax_plugin_glossary_div extends DokuWiki_Syntax_Plugin {
19
20  // ============================================================
21  function getType () { return 'substition'; }
22  function getPType () { return 'block'; }
23  function getAllowedTypes () { return array ('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
24  function getSort () { return 309; }
25  function connectTo ($mode) {
26    $this->Lexer->addSpecialPattern ('\{\{glossary[^}]*\}\}', $mode, 'plugin_glossary_div');
27  }
28
29  // ============================================================
30  function handle ($match, $state, $pos, Doku_Handler $handler) {
31    switch ($state) {
32    case DOKU_LEXER_SPECIAL :
33      return array ($state, trim (substr ($match, 10, -2))); // "{{glossary" => 10 "}}" => 2
34    }
35    return false;
36  }
37
38  // ============================================================
39  function render ($mode, Doku_Renderer $renderer, $indata) {
40    if ($mode != 'xhtml')
41      return false;
42    list ($state, $data) = $indata;
43    $data = " ".$data." ";
44    $renderer->info ['cache'] = FALSE;
45    $cmd = "";
46    $arg = "";
47
48    if (preg_match_all ("#(\"[^\"]*\")* help (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0) {
49      $this->help ($renderer);
50      return true;
51    }
52    // namespace
53    global $ID;
54    $ns = getNS ($ID);
55    if (preg_match_all ("#>([^ ]*) .*#", strtolower ($data), $dumy) > 0) {
56      $ns = $dumy[1][0];
57      if (($ns == '*') || ($ns == ':'))
58	$ns = '';
59      elseif ($ns == '.')
60	$ns = getNS ($ID);
61      else
62	$ns = cleanID ($ns);
63    }
64    if (preg_match_all ("#(\"[^\"]*\")* sample (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
65      $cmd = "printSample";
66    elseif (preg_match_all ("#(\"[^\"]*\")* list (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
67      $cmd = "printList";
68    elseif (preg_match_all ("#(\"[^\"]*\")* poll=\"(?<word>[^\"]*)\" (\"[^\"]*\")*#", $data, $dumy) > 0) {
69      $cmd = "printPoll";
70      $arg = $dumy['word'][0];
71    } elseif (preg_match_all ("#(\"[^\"]*\")* proposal (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
72      $cmd = "printProposal";
73    elseif (preg_match_all ("#(\"[^\"]*\")* admin-proposal (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
74      $cmd = "adminProposal";
75    elseif (preg_match_all ("#(\"[^\"]*\")* admin-definition (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
76      $cmd = "adminDefinition";
77    elseif (preg_match_all ("#(\"[^\"]*\")* admin-glossaries (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0)
78      $cmd = "adminGlossaries";
79    else {
80      $this->help ($renderer);
81      return true;
82    }
83
84    $glossary = new glossary ($this, $ns);
85    ob_start ();
86    $glossary->$cmd ($arg);
87    $text = ob_get_contents();
88    ob_end_clean ();
89    foreach ($glossary->message as $type => $msg)
90      $text = '<div class="'.$type.'">'.$msg.'</div>'.$text;
91    $renderer->doc .= '<div class="glossary">'.$text.'</div>';
92    return true;
93  }
94
95  // ============================================================
96  function isAdmin () {
97    global $INFO;
98    return
99      isset ($INFO ['userinfo']) &&
100      isset ($INFO ['userinfo']['grps']) &&
101      in_array (trim ($this->getConf ('adminGroup')), $INFO ['userinfo']['grps']);
102  }
103
104  // ============================================================
105  function help (Doku_Renderer $renderer) {
106    $url = "http://admin.parlenet.org/plugins/glossary/";
107    $renderer->doc .=
108      '      <h1>Help Glossary</h1>'.NL.
109      '      <ul>'.NL.
110      '        <li><b>{{glossary&gt;</b>namespace [help] (sample|list|proposal|admin-proposal|admin-definition|admin-glossaries)<b>}}</b></li>'.NL.
111      '        <li><b>&lt;glossary</b> (help|clock|face-smile|face-sad|stop|one-way|search) <b>/&gt;</b></li>'.NL.
112      '        <li><b>&lt;glossary</b> (word|translate) <b>&gt;</b> ... <b>&lt;/glossary&gt;</b></li>'.NL.
113      '      </ul>'.NL.
114      '      <p><a class="urlextern" rel="nofollow" title="'.$url.'" href="'.$url.'">'.$url.'</a></p>'.NL;
115  }
116
117  // ============================================================
118} // syntax_plugin_GLOSSARY
119?>
120