1<?php
2/**
3 * DokuWiki Plugin Listusergroup (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Richard Gfrerer <richard.gfrerer@gmx.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class helper_plugin_listusergroup extends DokuWiki_Plugin {
15
16  function getMethods() {
17    $result = array();
18    $result[] = array(
19      'name'   => 'getXHTML',
20      'desc'   => 'returns the XHTML to display a user list',
21      'params' => array(
22		'group (optional)'	=> 'array',
23        'show (optional)'	=> 'array',
24        'link (optional)'	=> 'array',
25        'class (optional)'	=> 'array'),
26      'return' => array('xhtml' => 'string')
27    );
28    return $result;
29  }
30
31
32    /**
33     * Constructor loads default config settings once
34     */
35    function helper_plugin_listusergroup() {
36        $this->defaults['namespace']  = $this->getConf('namespace');
37        $this->defaults['homeicon']   = $this->getConf('homeicon');
38	}
39
40	/**
41	 * Returns the XHTML of a user list
42	 */
43	function getXHTML($data) {
44		return $src = $this->_getListAsString($data);
45	}
46
47
48	function _getListAsString($data) {
49        global $auth;
50        global $lang;
51
52		if (is_null($xhtml_renderer)) {
53			require_once DOKU_INC . 'inc/parser/xhtml.php';
54			$xhtml_renderer = new Doku_Renderer_xhtml();
55		}
56
57
58        if (!method_exists($auth,"retrieveUsers")) return '';
59
60		$confNS = $this->getConf('namespace');
61		$confHomeicon = $this->getConf('homeicon');
62
63        $users = array();
64
65		/* handle user groups */
66        foreach ($data['group'] as $grp) {
67            $getuser = $auth->retrieveUsers(0,-1,array('grps'=>'^'.preg_quote($grp,'/').'$'));
68            $users = $users + $getuser;
69        }
70
71		$tableclass = $data['class']?$data['class'][0]: 'inline';
72
73		/* possible table classes: inline, pagelist, htCore, ul, diff */
74        $xhtml_renderer->doc .= '<table class="'.$tableclass.'">';
75
76		/* handle table header*/
77		if (in_array('header', $data['show'])) {
78		    $xhtml_renderer->doc .= '<tr>';
79			foreach ($data['show'] as $show) {
80				if (substr( $show, 0, strlen('existing') ) === "existing") $show = substr($show, strlen('existing'));
81				if ($show==='home' || $show==='header') continue;
82				$xhtml_renderer->doc .= '<th>'.$lang[$show].'</th>';
83			}
84		    $xhtml_renderer->doc .= '</tr>';
85		}
86
87		/* handle table body*/
88		$posHome = array_search('home', $data['show']);
89		$posUser = array_search('user', $data['show']);
90		$iconPos = -1;
91		if ($posHome>$posUser) $iconPos = 1;
92
93		$isShowExistingHome = in_array('existinghome', $data['show']);
94		$isShowHome = in_array('home', $data['show']);
95                $isShowGroups = in_array('groups', $data['show']);
96		$isLinkUser = $data['link']?in_array('user', $data['link']):false;
97		$isLinkEmail = $data['link']?in_array('email', $data['link']):false;
98
99
100        foreach ($users as $user => $info) {
101			$exists = true;
102			/* skip non-existing hompage entries, if defined */
103			if ($isShowExistingHome) {
104				$exists = null;
105				$usertmp = $user; // user obj will be manipulated by 'resolve_pageid'
106				resolve_pageid($confNS,$usertmp,$exists);
107				if (!$exists) continue;
108			}
109
110            $xhtml_renderer->doc .= '<tr>';
111
112			foreach ($data['show'] as $show) {
113				/* skip home and header option (do not create a column) */
114				if ($show==='home' || $show==='existinghome' || $show==='header') continue;
115
116            	$xhtml_renderer->doc .= '<td>';
117
118				/* handle user option */
119				if ($show==='user') {
120					if ( ($isShowHome||$isShowExistingHome) && $iconPos<0) $xhtml_renderer->doc .= ' <span '.$confHomeicon.'></span> ';
121					if ($isLinkUser) {
122						$xhtml_renderer->doc .= $xhtml_renderer->internallink($confNS.':'.$user);
123					} else {
124						$xhtml_renderer->doc .= hsc($user);
125					}
126					if ( ($isShowHome||$isShowExistingHome) && $iconPos>0) $xhtml_renderer->doc .= ' <span '.$confHomeicon.'></span> ';
127				}
128
129				/* handle groups option */
130				if ($show==='groups') {
131					if ($isShowGroups) {
132						$xhtml_renderer->doc .= hsc(implode( ", ", $info['grps']));
133					}
134				}
135
136				/* handle fullname option */
137				if ($show==='fullname') {
138					$xhtml_renderer->doc .= hsc($info['name']);
139				}
140
141				/* handle email option */
142				if ($show==='email') {
143					if ($isLinkEmail) {
144						$xhtml_renderer->doc .= $xhtml_renderer->emaillink($info['mail']);
145					} else {
146						$xhtml_renderer->doc .= hsc($info['mail']);
147					}
148				}
149				$xhtml_renderer->doc .= '</td>';
150			}
151            $xhtml_renderer->doc .= '</tr>';
152        }
153        $xhtml_renderer->doc .= '</table>';
154        return $xhtml_renderer->doc;
155    }
156}
157
158