1<?php
2
3/**
4 * All DokuWiki plugins to extend the parser/rendering mechanism
5 * need to inherit from this class
6 */
7
8// must be run within DokuWiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_showwhen extends DokuWiki_Syntax_Plugin {
19
20/**
21 * return some info
22 */
23function getInfo(){
24    return array(
25        'author' => 'In Mean',
26        'email'  => 'inmean[at]inmean[.]com',
27        'date'   => '2025-03-22',
28        'name'   => 'showwhen Plugin',
29        'desc'   =>
30'Shows text only if all of some conditions are true.
31Fork from showif plugin by Harald Ronge
32Lazy hiding based on plugin nodisp from Myron Turner.
33
34Syntax is <showif [condition1], [condition2], ...>[text]</showif>
35
36Supported conditions are:
37
381. isloggedin
392. isnotloggedin
403. mayonlyread
414. mayatleastread
425. mayedit
436. isadmin
44
45Administrators will always see everything except mayonlyread.
46Not all combinations are useful ;-)
47
48',
49        'url'    => 'https://www.dokuwiki.org/plugin:showwhen',
50    );
51}
52
53function getType(){ return 'container'; }
54function getPType(){ return 'stack'; }
55function getAllowedTypes() { return array(
56            'container',
57            'formatting',
58            'substition',
59            'protected',
60            'disabled',
61            'paragraphs',
62            'baseonly' //new
63            ); }
64function getSort(){ return 168; } //196? I have no clue ...
65function connectTo($mode) { $this->Lexer->addEntryPattern('<showif.*?>(?=.*?</showif>)',$mode,'plugin_showwhen'); }
66function postConnect() { $this->Lexer->addExitPattern('</showif>','plugin_showwhen'); }
67
68
69/**
70 * Handle the match
71     */
72    function handle($match, $state, $pos, Doku_Handler $handler){
73
74
75        switch ($state) {
76          case DOKU_LEXER_ENTER :
77		  // remove <showif and >
78          $args  = trim(substr($match, 8, -1)); // $arg will be loggedin or mayedit
79          return array($state, explode(",",$args));
80
81          case DOKU_LEXER_UNMATCHED : return array($state, $match);
82          case DOKU_LEXER_EXIT : return array($state, '');
83        }
84
85        return array();
86    }
87
88    /**
89     * Create output
90     */
91    function render($mode, Doku_Renderer $renderer, $data) {
92        global $INFO, $INPUT;
93
94        if($mode == 'xhtml'){
95            $renderer->nocache(); // disable caching
96            list($state, $match) = $data;
97
98            switch ($state) {
99              case DOKU_LEXER_ENTER :
100				$show = 0;
101				//$i = 0;
102				$conditions = $match;
103				// Loop through conditions
104				foreach($conditions as $val) {
105					// All conditions have to be true
106					if
107					(
108						(($val == "mayedit") && (auth_quickaclcheck($INFO['id'])) >= AUTH_EDIT)
109						||
110						//mayonlyread will be hidden for an administrator!
111						(($val == "mayonlyread") && (auth_quickaclcheck($INFO['id'])) == AUTH_READ)
112						||
113						(($val == "mayatleastread") && (auth_quickaclcheck($INFO['id'])) >= AUTH_READ)
114						||
115						($val == "isloggedin" && $INPUT->server->str('REMOTE_USER'))
116						||
117						($val == "isnotloggedin" && !$INPUT->server->str('REMOTE_USER'))
118						||
119						(($val == "isadmin") && ($INFO['isadmin'] || $INFO['ismanager'] ))
120					) $show = 1;
121					else {$show = 0; break;}
122				}
123                //always open a div so DOKU_LEXER_EXIT can close it without checking state
124                // perhaps display:inline?
125				if ($show == 1) $renderer->doc .= "<div>";
126				elseif ($show == 0) $renderer->doc .= "<div style='display:none'>";
127
128                break;
129
130                case DOKU_LEXER_UNMATCHED : $renderer->doc .= $renderer->_xmlEntities($match); break;
131                case DOKU_LEXER_EXIT : $renderer->doc .= "</div>"; break;
132            }
133            return true;
134        }
135        return false;
136    }
137
138
139
140}
141
142?>
143