<?php
/**
 * Wiki farm manager overview class
 *
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
 * @desc     Renders an overview of the farm's animals
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');

if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);

define('DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE', 10);

include_once DOKU_FARM_PLUGIN.'animal.class.php';

class dokuwiki_farm_overview {
	var $manager = null;
	
	/**
	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
	 */
	function __construct($manager) {
		$this->manager = & $manager;
	}
	
	/**
	 * Process requests
	 */
	function process() {
	}
	
	/**
	 * Renders
	 */
	function html() {
		global $conf;
		
		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('overview_title').'</div>');
		
		// Gather animals / sort
		$animals = dokuwiki_farm_animal::listAnimals($this->manager);
		$sort = '';
		if(isset($this->manager->opt['sort'])) {
			$sort = $this->manager->opt['sort'];
			if(preg_match('/^size(desc)?$/', $sort)) {
				$l  =array();
				for($i=0; $i<count($animals); $i++) $l[sprintf('%020d', $animals[$i]->getInfos()->getSize()).uniqid()] = $animals[$i];
				if($sort == 'size') {
					ksort($l);
				}else krsort($l);
				$animals = array();
				foreach($l as $s => $i) $animals[] = $i;
			}
			if(preg_match('/^name(desc)?$/', $sort)) {
				$l  =array();
				for($i=0; $i<count($animals); $i++) $l[$animals[$i]->getName().uniqid()] = $animals[$i];
				if($sort == 'name') {
					ksort($l);
				}else krsort($l);
				$animals = array();
				foreach($l as $s => $i) $animals[] = $i;
			}
		}
		
		$details = isset($this->manager->opt['details']) ? $this->manager->opt['details'] : null;
		
		// Outputs usages progressbars if needed
		if($this->manager->conf['farmmaxsize'] > 0) {
			$s = dokuwiki_farm_animal::farmSize();
			$p = $s / $this->manager->conf['farmmaxsize'];
			if($p > 1) $p == 1;
			$g = min(255, 511 - round($p * 511));
			$r = min(255, round(511 * $p));
			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 />');
		}
		
		if($this->manager->conf['farmmaxanimals'] > 0) {
			$s = count(dokuwiki_farm_animal::listAnimals());
			$p = $s / $this->manager->conf['farmmaxanimals'];
			if($p > 1) $p == 1;
			$g = min(255, 511 - round($p * 511));
			$r = min(255, round(511 * $p));
			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 />');
		}
		
		// Sorting options
		ptln('	<div class="animal_overview_sort">');
		ptln('		'.$this->manager->getLang('overview_sort').' : ');
		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'size')).'">'.$this->manager->getLang('overview_sort_size_asc').'</a> / ');
		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'sizedesc')).'">'.$this->manager->getLang('overview_sort_size_desc').'</a> / ');
		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'name')).'">'.$this->manager->getLang('overview_sort_name_asc').'</a> / ');
		ptln('		<a href="'.$this->manager->wl('overview', array('sort' => 'namedesc')).'">'.$this->manager->getLang('overview_sort_name_desc').'</a>');
		ptln('	</div>');
		
		// Animal pages
		$cnt = 0;
		$page = isset($this->manager->opt['page']) ? (int)$this->manager->opt['page'] : 1;
		if($page < 1) $page = 1;
		$pages = ceil(count($animals) / DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE);
		if($page > $pages) $page = $pages;
		
		echo '	<div class="animal_overview_pages">'."\n";
		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>');
		echo '	</div>'."\n";
		
		$start = ($page - 1) * DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE;
		foreach(array_slice($animals, $start, DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE) as $a) {
			ptln('	<div class="animal_overview"><a name="animal_'.$a->getName().'"></a>');
			
			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>');
			
			ptln('		<div class="animal_info">');
			if($this->manager->conf['animalmaxsize'] > 0) {
				$s = $a->getInfos()->getSize('all', false);
				$p = $s / $this->manager->conf['animalmaxsize'];
				if($p > 1) $p == 1;
				$g = min(255, 511 - round($p * 511));
				$r = min(255, round(511 * $p));
				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 />');
			}
			if($details != $a->getName()) {
				if($this->manager->conf['animalmaxsize'] <= 0) ptln('			'.$this->manager->getLang('animal_info_size').' : '.$a->getInfos()->getSize('all', true).', ');
				ptln('			'.$this->manager->getLang('animal_info_creation').' : '.strftime($conf['dformat'], $a->getMetadata('creation_date')));
				ptln('			<a href="'.$this->manager->wl('overview', array('sort' => $sort, 'details' => $a->getName())).'#animal_'.$a->getName().'">'.$this->manager->getLang('animal_info_more').'</a>');
			}else{
				if($this->manager->conf['animalmaxsize'] <= 0) ptln('			'.$this->manager->getLang('animal_info_size').' : '.$a->getInfos()->getSize('all', true).'<br />');
				ptln('			'.$this->manager->getLang('animal_info_status').' : '.$this->manager->getLang('animal_status_'.$a->getInfos()->getStatus()).'<br />');
				ptln('			'.$this->manager->getLang('animal_info_creation').' : '.$a->getInfos()->getCreation(true).'<br />');
				ptln('			<br />');
				
				foreach($a->getInfos()->pagesInfo(true) as $k => $i) {
					ptln('			'.$this->manager->getLang('animal_info_pages'.$k).' : '.$i.'<br />');
				}
				ptln('			<br />');
				
				foreach($a->getInfos()->mediasInfo(true) as $k => $i) {
					ptln('			'.$this->manager->getLang('animal_info_medias'.$k).' : '.$i.'<br />');
				}
				ptln('			<br />');
				
				ptln('			'.$this->manager->getLang('animal_info_cachesize').' : '.$a->getInfos()->getSize('cache', true).'<br />');
				ptln('			'.$this->manager->getLang('animal_info_indexsize').' : '.$a->getInfos()->getSize('index', true).'<br />');
			}
			ptln('		</div>');
			
			ptln('		<div class="cmd_buttons">');
			$status = $a->getMetadata('status');
			if($status == '') $status = 'open';
			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;">');
			ptln('				<option '.($status == 'open' ? 'selected="selected"' : '').' value="open">'.$this->manager->getLang('animal_status_open').'</option>');
			ptln('				<option '.($status == 'maintenance' ? 'selected="selected"' : '').' value="maintenance">'.$this->manager->getLang('animal_status_maintenance').'</option>');
			ptln('				<option '.($status == 'closed' ? 'selected="selected"' : '').' value="closed">'.$this->manager->getLang('animal_status_closed').'</option>');
			ptln('				<option '.($status == 'abuse' ? 'selected="selected"' : '').' value="abuse">'.$this->manager->getLang('animal_status_abuse').'</option>');
			ptln('			</select> | ');
			$lockstate = $a->getLockState();
			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;">');
			ptln('				<option '.($lockstate == '' ? 'selected="selected"' : '').' value="">'.$this->manager->getLang('animal_lockstate_none').'</option>');
			ptln('				<option '.($lockstate == 'edits' ? 'selected="selected"' : '').' value="edits">'.$this->manager->getLang('animal_lockstate_edits').'</option>');
			ptln('				<option '.($lockstate == 'admin' ? 'selected="selected"' : '').' value="admin">'.$this->manager->getLang('animal_lockstate_admin').'</option>');
			ptln('				<option '.($lockstate == 'all' ? 'selected="selected"' : '').' value="all">'.$this->manager->getLang('animal_lockstate_all').'</option>');
			ptln('			</select> | ');
			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> | ');
			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> | ');
			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>');
			ptln('		</div>');
			
			ptln('	</div>');
			
			$cnt++;
			
			if($cnt >= DOKU_FARM_PLUGIN_ANIMALOVERVIEW_PERPAGE) break;
		}
		
		echo '	<div class="animal_overview_pages">'."\n";
		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>');
		echo '	</div>'."\n";
	}
}
?>
