1<?php
2/**
3 * Wiki farm manager overview class
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
7 * @desc     Renders an overview of the farm's animals
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
14if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');
15
16if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);
17
18define('DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE', 10);
19
20include_once DOKU_FARM_PLUGIN.'animal.class.php';
21
22class dokuwiki_farm_overview {
23	var $manager = null;
24
25	/**
26	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
27	 */
28	function __construct($manager) {
29		$this->manager = & $manager;
30	}
31
32	/**
33	 * Process requests
34	 */
35	function process() {
36	}
37
38	/**
39	 * Renders
40	 */
41	function html() {
42		global $conf;
43
44		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('overview_title').'</div>');
45
46		// Gather animals / sort
47		$animals = dokuwiki_farm_animal::listAnimals($this->manager);
48		$sort = '';
49		if(isset($this->manager->opt['sort'])) {
50			$sort = $this->manager->opt['sort'];
51			if(preg_match('/^size(desc)?$/', $sort)) {
52				$l  =array();
53				for($i=0; $i<count($animals); $i++) $l[sprintf('%020d', $animals[$i]->getInfos()->getSize()).uniqid()] = $animals[$i];
54				if($sort == 'size') {
55					ksort($l);
56				}else krsort($l);
57				$animals = array();
58				foreach($l as $s => $i) $animals[] = $i;
59			}
60			if(preg_match('/^name(desc)?$/', $sort)) {
61				$l  =array();
62				for($i=0; $i<count($animals); $i++) $l[$animals[$i]->getName().uniqid()] = $animals[$i];
63				if($sort == 'name') {
64					ksort($l);
65				}else krsort($l);
66				$animals = array();
67				foreach($l as $s => $i) $animals[] = $i;
68			}
69		}
70
71		$details = isset($this->manager->opt['details']) ? $this->manager->opt['details'] : null;
72
73		// Outputs usages progressbars if needed
74		if($this->manager->conf['farmmaxsize'] > 0) {
75			$s = dokuwiki_farm_animal::farmSize();
76			$p = $s / $this->manager->conf['farmmaxsize'];
77			if($p > 1) $p == 1;
78			$g = min(255, 511 - round($p * 511));
79			$r = min(255, round(511 * $p));
80			ptln('			'.$this->manager->getLang('overview_diskusage').' : <span class="size_disp" style="background-color:rgb('.$r.','.$g.',0)">'.round(100 * $p).'% ('.$this->manager->nicesize($s).' / '.$this->manager->nicesize($this->manager->conf['farmmaxsize']).')</span><br />');
81		}
82
83		if($this->manager->conf['farmmaxanimals'] > 0) {
84			$s = count(dokuwiki_farm_animal::listAnimals());
85			$p = $s / $this->manager->conf['farmmaxanimals'];
86			if($p > 1) $p == 1;
87			$g = min(255, 511 - round($p * 511));
88			$r = min(255, round(511 * $p));
89			ptln('			'.$this->manager->getLang('overview_animalsamount').' : <span class="size_disp" style="background-color:rgb('.$r.','.$g.',0)">'.round(100 * $p).'% ('.$s.' / '.$this->manager->conf['farmmaxanimals'].')</span><br />');
90		}
91
92		// Sorting options
93		ptln('	<div class="animal_overview_sort">');
94		ptln('		'.$this->manager->getLang('overview_sort').' : ');
95		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'size')).'">'.$this->manager->getLang('overview_sort_size_asc').'</a> / ');
96		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'sizedesc')).'">'.$this->manager->getLang('overview_sort_size_desc').'</a> / ');
97		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'name')).'">'.$this->manager->getLang('overview_sort_name_asc').'</a> / ');
98		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'namedesc')).'">'.$this->manager->getLang('overview_sort_name_desc').'</a>');
99		ptln('	</div>');
100
101		// Animal pages
102		$cnt = 0;
103		$page = isset($this->manager->opt['page']) ? (int)$this->manager->opt['page'] : 1;
104		if($page < 1) $page = 1;
105		$pages = ceil(count($animals) / DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE);
106		if($page > $pages) $page = $pages;
107
108		echo '	<div class="animal_overview_pages">'."\n";
109		for($p = 1; $p <= $pages; $p++) ptln('		<a class="animal_overview_'.($p == $page ? 'current' : '').'page" href="'.$this->manager->wl('overview', array('sort' => $sort, 'page' => $p)).'">'.$p.'</a>');
110		echo '	</div>'."\n";
111
112		$start = ($page - 1) * DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE;
113		foreach(array_slice($animals, $start, DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE) as $a) {
114			ptln('	<div class="animal_overview"><a name="animal_'.$a->getName().'"></a>');
115
116			ptln('		<div class="animal_name"><a href="'.$this->manager->wl('animal', array('aid' => $a->getName())).'" title="'.$this->manager->getLang('manageanimal').'">'.$a->getName().'</a> <a href="'.$a->getUrl().'" target="_blank" title="'.$this->manager->getLang('visitanimal').'"><img src="'.DOKU_FARM_WWW.'images/www.png" alt="visit" /></a></div>');
117
118			ptln('		<div class="animal_info">');
119			if($this->manager->conf['animalmaxsize'] > 0) {
120				$s = $a->getInfos()->getSize('all', false);
121				$p = $s / $this->manager->conf['animalmaxsize'];
122				if($p > 1) $p == 1;
123				$g = min(255, 511 - round($p * 511));
124				$r = min(255, round(511 * $p));
125				ptln('			'.$this->manager->getLang('animal_info_size').' : <span class="size_disp" style="background-color:rgb('.$r.','.$g.',0)">'.round(100 * $p).'% ('.$this->manager->nicesize($s).' / '.$this->manager->nicesize($this->manager->conf['animalmaxsize']).')</span><br />');
126			}
127			if($details != $a->getName()) {
128				if($this->manager->conf['animalmaxsize'] <= 0) ptln('			'.$this->manager->getLang('animal_info_size').' : '.$a->getInfos()->getSize('all', true).', ');
129				ptln('			'.$this->manager->getLang('animal_info_creation').' : '.strftime($conf['dformat'], $a->getMetadata('creation_date')));
130				ptln('			<a href="'.$this->manager->wl('overview', array('sort' => $sort, 'details' => $a->getName())).'#animal_'.$a->getName().'">'.$this->manager->getLang('animal_info_more').'</a>');
131			}else{
132				if($this->manager->conf['animalmaxsize'] <= 0) ptln('			'.$this->manager->getLang('animal_info_size').' : '.$a->getInfos()->getSize('all', true).'<br />');
133				ptln('			'.$this->manager->getLang('animal_info_status').' : '.$this->manager->getLang('animal_status_'.$a->getInfos()->getStatus()).'<br />');
134				ptln('			'.$this->manager->getLang('animal_info_creation').' : '.$a->getInfos()->getCreation(true).'<br />');
135				ptln('			<br />');
136
137				foreach($a->getInfos()->pagesInfo(true) as $k => $i) {
138					ptln('			'.$this->manager->getLang('animal_info_pages'.$k).' : '.$i.'<br />');
139				}
140				ptln('			<br />');
141
142				foreach($a->getInfos()->mediasInfo(true) as $k => $i) {
143					ptln('			'.$this->manager->getLang('animal_info_medias'.$k).' : '.$i.'<br />');
144				}
145				ptln('			<br />');
146
147				ptln('			'.$this->manager->getLang('animal_info_cachesize').' : '.$a->getInfos()->getSize('cache', true).'<br />');
148				ptln('			'.$this->manager->getLang('animal_info_indexsize').' : '.$a->getInfos()->getSize('index', true).'<br />');
149			}
150			ptln('		</div>');
151
152			ptln('		<div class="cmd_buttons">');
153			$status = $a->getMetadata('status');
154			if($status == '') $status = 'open';
155			ptln('			'.$this->manager->getLang('animal_info_status').' : <select onmouseover="if(!this.i)this.i=this.selectedIndex+1;" onchange="if(confirm(\''.$this->manager->getLang('animal_info_setstatus_confirm', null, 'js').'\')) document.location=\''.$this->manager->wl('animal', array('aid' => $a->getName())).'&amp;farm_opt__status=\'+this.value+\'&amp;sectok='.getSecurityToken().'\'; else if(this.i)this.selectedIndex=this.i-1;">');
156			ptln('				<option '.($status == 'open' ? 'selected="selected"' : '').' value="open">'.$this->manager->getLang('animal_status_open').'</option>');
157			ptln('				<option '.($status == 'maintenance' ? 'selected="selected"' : '').' value="maintenance">'.$this->manager->getLang('animal_status_maintenance').'</option>');
158			ptln('				<option '.($status == 'closed' ? 'selected="selected"' : '').' value="closed">'.$this->manager->getLang('animal_status_closed').'</option>');
159			ptln('				<option '.($status == 'abuse' ? 'selected="selected"' : '').' value="abuse">'.$this->manager->getLang('animal_status_abuse').'</option>');
160			ptln('			</select> | ');
161			$lockstate = $a->getLockState();
162			ptln('			'.$this->manager->getLang('animal_info_lockstate').' : <select onmouseover="if(!this.i)this.i=this.selectedIndex+1;" onchange="if(confirm(\''.$this->manager->getLang('animal_info_setlockstate_confirm', null, 'js').'\')) document.location=\''.$this->manager->wl('animal', array('aid' => $a->getName())).'&amp;farm_opt__lockstate=\'+this.value+\'&amp;sectok='.getSecurityToken().'\'; else if(this.i)this.selectedIndex=this.i-1;">');
163			ptln('				<option '.($lockstate == '' ? 'selected="selected"' : '').' value="">'.$this->manager->getLang('animal_lockstate_none').'</option>');
164			ptln('				<option '.($lockstate == 'edits' ? 'selected="selected"' : '').' value="edits">'.$this->manager->getLang('animal_lockstate_edits').'</option>');
165			ptln('				<option '.($lockstate == 'admin' ? 'selected="selected"' : '').' value="admin">'.$this->manager->getLang('animal_lockstate_admin').'</option>');
166			ptln('				<option '.($lockstate == 'all' ? 'selected="selected"' : '').' value="all">'.$this->manager->getLang('animal_lockstate_all').'</option>');
167			ptln('			</select> | ');
168			ptln('			<a href="'.$this->manager->wl('animal', array('aid' => $a->getName(), 'config' => 1)).'" title="'.$this->manager->getLang('animal_config').'"><img src="'.DOKU_FARM_WWW.'images/configure.png" alt="edit configuration" /></a> | ');
169			ptln('			<a href="'.$this->manager->wl('animal', array('aid' => $a->getName(), 'users' => 1)).'" title="'.$this->manager->getLang('animal_users').'"><img src="'.DOKU_FARM_WWW.'images/users.png" alt="delete" /></a> | ');
170			ptln('			<a href="'.$this->manager->wl('animal', array('aid' => $a->getName(), 'delete' => 1)).'" title="'.$this->manager->getLang('animal_delete').'"><img src="'.DOKU_FARM_WWW.'images/delete.png" alt="delete" /></a>');
171			ptln('		</div>');
172
173			ptln('	</div>');
174
175			$cnt++;
176
177			if($cnt >= DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE) break;
178		}
179
180		echo '	<div class="animal_overview_pages">'."\n";
181		for($p = 1; $p <= $pages; $p++) ptln('		<a class="animal_overview_'.($p == $page ? 'current' : '').'page" href="'.$this->manager->wl('overview', array('sort' => $sort, 'page' => $p)).'">'.$p.'</a>');
182		echo '	</div>'."\n";
183	}
184}
185?>
186