<?php
/**
 * Wiki farm animal manager class
 *
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
 * @desc     Process and render animal management related requests
 */

// must be run under farm plugin context within Dokuwiki or SOAP context
if(!defined('DOKU_FARMPLUGINLOADED') && !defined('DOKU_FARMPLUGINSOAPACTION')) die();
if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', defined('DOKU_FARMPLUGINLOADED') ? DOKU_INC.'lib/plugins/farm/' : './');

require_once(DOKU_FARM_PLUGIN.'animal.class.php');

class dokuwiki_farm_animalmanager {
	var $manager = null;
	private $animal = null;
	
	/**
	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
	 */
	function __construct($manager) {
		$this->manager = & $manager;
	}
	
	/**
	 * Builds and return a link inside animal manager
	 * @param $opts array of farm options
	 * @return url string
	 */
	function wl($opts = array()) {
		$a = array_merge(array('aid' => $this->animal->getName()), $opts);
		return $this->manager->wl('animal', $a);
	}
	
	/**
	 * Process requests
	 */
	public function process() {
		// Animal creation request
		if(isset($this->manager->opt['new'])) {
			if(isset($this->manager->opt['save'])) {
				if(!checkSecurityToken()) {
					$this->manager->error('system_errors', 'system_badtoken_failure');
					return; // any changes done by post
				}
				if(isset($_POST['animal_name']) && !empty($_POST['animal_name'])) {
					$tpl = (isset($_POST['animal_template']) && !empty($_POST['animal_template'])) ? $_POST['animal_template'] : null;
					$host = (isset($_POST['animal_host']) && !empty($_POST['animal_host'])) ? $_POST['animal_host'] : null;
					if($n = dokuwiki_farm_animal::createNew($_POST['animal_name'], $tpl, $host, $this)) {
						$this->manager->success('animal_new_create_success');
						unset($this->manager->opt['save']);
						unset($this->manager->opt['new']);
						$this->manager->opt['aid'] = $n;
					}
				}else{
					$this->manager->error('animal_new_errors', 'animal_new_noname_failure');
					return;
				}
			}else return;
		}
		
		// Animal selected check
		if(!isset($this->manager->opt['aid'])) {
			$this->manager->error('animal_errors', 'animal_noid_failure');
			return;
		}
		if(!dokuwiki_farm_animal::exists($this->manager->opt['aid'])) {
			$this->manager->error('animal_errors', array('code' => 'animal_unknownanimal_failure', 'data' => array($this->manager->opt['aid'])));
			return;
		}
		$this->animal = new dokuwiki_farm_animal($this->manager->opt['aid'], $this->manager);
		
		// Options check
		if(
			!isset($this->manager->opt['status']) &&
			!isset($this->manager->opt['lockstate']) &&
			!(isset($this->manager->opt['users']) && (isset($this->manager->opt['save']) || isset($this->manager->opt['deleteuser']))) &&
			!(isset($this->manager->opt['editfile']) && isset($this->manager->opt['save']) && isset($this->manager->opt['file'])  && !empty($this->manager->opt['file'])) &&
			!(isset($this->manager->opt['editfile']) && isset($this->manager->opt['create']) && isset($this->manager->opt['newfile'])  && !empty($this->manager->opt['file'])) &&
			!(isset($this->manager->opt['delete']) && (isset($this->manager->opt['confirm']) || isset($this->manager->opt['cancel'])))
		) return;
		
		// Security check
		if(!checkSecurityToken()) {
			$this->manager->error('system_errors', 'system_badtoken_failure');
			return; // any changes done by post
		}
		
		// Status change request
		if(isset($this->manager->opt['status'])) {
			$s = $this->manager->opt['status'];
			if(in_array($s, array('open', 'maintenance', 'closed', 'abuse'))) {
				if(!$this->animal->setMetadata('status', $s)) {
					$this->manager->error('animal_status_errors', array('code' => 'animal_status_update_failure', 'data' => array($s)));
				}else $this->manager->success('animal_status_update_success');
			}
		}
		if(isset($this->manager->opt['lockstate'])) {
			$s = $this->manager->opt['lockstate'];
			if(in_array($s, array('', 'edits', 'admin', 'all'))) {
				if(!$this->animal->setMetadata('lockstate', $s)) {
					$this->manager->error('animal_status_errors', array('code' => 'animal_status_update_failure', 'data' => array($s)));
				}else $this->manager->success('animal_status_update_success');
			}
		}
		
		// User create request
		if(isset($this->manager->opt['users']) && isset($this->manager->opt['save'])) {
			if(!isset($_POST['new_animal_user_login']) || !isset($_POST['new_animal_user_pass']) || !isset($_POST['new_animal_user_name']) || !isset($_POST['new_animal_user_mail']) || !isset($_POST['new_animal_user_grps'])) {
				$this->manager->error('animal_users_errors', 'postparametermissing_failure');
			}elseif(empty($_POST['new_animal_user_login'])) {
				$this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_login'))));
			}elseif(empty($_POST['new_animal_user_pass'])) {
				$this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_pass'))));
			}elseif(empty($_POST['new_animal_user_mail'])) {
				$this->manager->error('animal_users_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('animal_users_mail'))));
			}else{
				if(empty($_POST['new_animal_user_name'])) $_POST['new_animal_user_name'] = $_POST['new_animal_user_login'];
				
				$_POST['new_animal_user_login'] = preg_replace('/.*:/','',$_POST['new_animal_user_login']);
				$_POST['new_animal_user_login'] = cleanID($_POST['new_animal_user_login']);
				$_POST['new_animal_user_name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['new_animal_user_name']));
				$_POST['new_animal_user_mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['new_animal_user_mail']));
				
				if(empty($_POST['new_animal_user_login'])) {
					$this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_login'))));
				}elseif(empty($_POST['new_animal_user_pass'])) {
					$this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_pass'))));
				}elseif(empty($_POST['new_animal_user_name'])) {
					$this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_name'))));
				}elseif(empty($_POST['new_animal_user_mail']) || !mail_isvalid($_POST['new_animal_user_mail'])) {
					$this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_mail'))));
				}elseif(!preg_match('`^\s*[a-z0-9_-]+(\s*,\s*[a-z0-9_-]+)*\s*$`i', $_POST['new_animal_user_grps'])) {
					$this->manager->error('animal_users_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('animal_users_grps'))));
				}else{
					if($this->animal->addUser($_POST['new_animal_user_login'], $_POST['new_animal_user_pass'], $_POST['new_animal_user_name'], $_POST['new_animal_user_mail'], array_map('trim', explode(',', $_POST['new_animal_user_grps'])))) {
						$this->manager->success('animal_users_add_success');
					}else $this->manager->error('animal_users_errors', 'animal_users_add_create_failure');
				}

			}
		}
		
		// User delete request
		if(isset($this->manager->opt['users']) && isset($this->manager->opt['deleteuser'])) {
			if($this->animal->deleteUser(base64_decode($this->manager->opt['deleteuser']))) {
				$this->manager->success('animal_users_delete_success');
			}else $this->manager->error('animal_users_errors', 'animal_users_delete_failure');
		}
		
		// File edition request
		if(isset($this->manager->opt['editfile']) && isset($this->manager->opt['save']) && isset($this->manager->opt['file'])  && !empty($this->manager->opt['file'])) {
			if(isset($this->manager->opt['editfile_delete'])) {
				if($this->animal->setFileContent(base64_decode($this->manager->opt['file']), '', true)) {
					$this->manager->success('animal_editfile_delete_success');
					$this->manager->opt['file'] = null;
				}else $this->manager->error('animal_editfile_errors', 'animal_editfile_delete_failure');
			}elseif(isset($_POST['animal_file']) && (isset($this->manager->opt['editfile_saveempty']) || !empty($_POST['animal_file']))) {
				if($this->animal->setFileContent(base64_decode($this->manager->opt['file']), isset($this->manager->opt['editfile_saveempty']) ? '' : $_POST['animal_file'])) {
					$this->manager->success('animal_editfile_save_success');
				}else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure');
			}else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure');
		}
		if(isset($this->manager->opt['editfile']) && isset($this->manager->opt['create']) && isset($this->manager->opt['newfile'])  && !empty($this->manager->opt['file'])) {
			if($this->animal->setFileContent($this->manager->opt['file'], '')) {
				$this->manager->success('animal_editfile_save_success');
				$this->manager->opt['file'] = base64_encode($this->manager->opt['file']);
			}else $this->manager->error('animal_editfile_errors', 'animal_editfile_save_failure');
		}
		
		// Animal delete request
		if(isset($this->manager->opt['delete']) && isset($this->manager->opt['cancel'])) {
			if(isset($this->manager->opt['confirm'])) unset($this->manager->opt['confirm']);
			unset($this->manager->opt['delete']);
		}
		if(isset($this->manager->opt['delete']) && isset($this->manager->opt['confirm'])) {
			if($this->animal->getName() == $this->manager->conf['animaltemplate']) {
				$this->manager->error('animal_delete_errors', array('code' => 'animal_delete_tpl_failure', 'data' => $this->manager->conf['animaltemplate']));
			}elseif($this->animal->delete()) {
				$this->manager->success('animal_delete_success');
				$this->animal = null;
				unset($this->manager->opt['delete']);
				unset($this->manager->opt['confirm']);
				unset($this->manager->opt['aid']);
			}else $this->manager->error('animal_delete_errors', array('code' => 'animal_delete_failure', 'data' => $this->animal->getName()));
		}
	}
	
	/**
	 * Renders header outputed before error / success section
	 */
	public function htmlheader() {
		$displaydefault = !isset($this->manager->opt['config']) && !isset($this->manager->opt['users']) && !isset($this->manager->opt['editfile']) && !isset($this->manager->opt['delete']);
		
		// Pannel menu
		if($this->animal) {
			ptln('<div class="animal_hud">');
			ptln('	<a '.($displaydefault ? 'class="current_action"' : '').' href="'.$this->wl().'" title="'.$this->manager->getLang('animal_infos').'"><img src="'.DOKU_FARM_WWW.'images/chart.png" alt="infos" /></a>');
			ptln('	<a '.(isset($this->manager->opt['users']) ? 'class="current_action"' : '').' href="'.$this->wl(array('users' => true)).'" title="'.$this->manager->getLang('animal_users').'"><img src="'.DOKU_FARM_WWW.'images/users.png" alt="users" /></a>');
			ptln('	<a '.(isset($this->manager->opt['editfile']) ? 'class="current_action"' : '').' href="'.$this->wl(array('editfile' => true)).'" title="'.$this->manager->getLang('animal_editfile').'"><img src="'.DOKU_FARM_WWW.'images/edit.png" alt="edit file" /></a>');
			ptln('	<a '.(isset($this->manager->opt['delete']) ? 'class="current_action"' : '').' href="'.$this->wl(array('delete' => true)).'" title="'.$this->manager->getLang('animal_delete').'"><img src="'.DOKU_FARM_WWW.'images/delete.png" alt="delete animal" /></a>');
			ptln('	<a href="'.$this->animal->getUrl().'" target="_blank" title="'.$this->manager->getLang('visitanimal').'"><img src="'.DOKU_FARM_WWW.'images/www.png" alt="visit" /></a>');
			ptln('</div>');
		}
	}
	
	/**
	 * Renders
	 */
	public function html() {
		$displaydefault = !isset($this->manager->opt['config']) && !isset($this->manager->opt['users']) && !isset($this->manager->opt['editfile']) && !isset($this->manager->opt['delete']);
		
		// Animal infos
		if($displaydefault && $this->animal) {
			ptln('		<div class="animal_info">');
			if($this->manager->conf['animalmaxsize'] > 0) {
				$s = $this->animal->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($this->manager->conf['animalmaxsize'] <= 0) ptln('			'.$this->manager->getLang('animal_info_size').' : '.$this->animal->getInfos()->getSize('all', true).'<br />');
			
			$status = $this->animal->getInfos()->getStatus();
			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->wl().'&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><br />');
			
			$lockstate = $this->animal->getInfos()->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->wl().'&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><br />');
			
			ptln('			'.$this->manager->getLang('animal_info_creation').' : '.$this->animal->getInfos()->getCreation(true).'<br />');
			ptln('			<br />');
			foreach($this->animal->getInfos()->pagesInfo(true) as $k => $i) {
				ptln('			'.$this->manager->getLang('animal_info_pages'.$k).' : '.$i.'<br />');
			}
			ptln('			<br />');
			foreach($this->animal->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').' : '.$this->animal->getInfos()->getSize('cache', true).'<br />');
			ptln('			'.$this->manager->getLang('animal_info_indexsize').' : '.$this->animal->getInfos()->getSize('index', true).'<br />');
			ptln('		</div>');
		}
		
		// Animal users
		if(isset($this->manager->opt['users']) && $this->animal) {
			ptln('<div class="farm_cmd_title">');
			ptln('	'.$this->manager->getLang('animal_users'));
			ptln('</div>');
			
			ptln('<div class="farm_cmd_info">');
			ptln('	'.$this->manager->getLang('animal_users_warning'));
			ptln('</div>');
			
			$sort = isset($this->manager->opt['sort']) ? $this->manager->opt['sort'] : 'name';
			$grp = isset($this->manager->opt['grp']) ? $this->manager->opt['grp'] : null;
			$users = $this->animal->getUsers();
			switch($sort) {
				case 'name' : uasort($users, create_function('$a,$b', 'return strcmp($a[\'name\'], $b[\'name\']);')); break;
				case 'namedesc' : uasort($users, create_function('$a,$b', 'return -1 * strcmp($a[\'name\'], $b[\'name\']);')); break;
			}
			
			if($grp) ptln($this->manager->getLang('animal_users_onlygrpactive').' : '.$grp.' [<a href="'.$this->wl(array('users' => true, 'grp' => '', 'sort' => $sort)).'">X</a>]');
			ptln('<table>');
			ptln('	<tr>');
			ptln('		<th>'.$this->manager->getLang('animal_users_login').' <a href="'.$this->wl(array('users' => true, 'grp' => $grp, 'sort' => $sort == 'name' ? 'namedesc' : 'name')).'" title="'.$this->manager->getLang('animal_users_sortname').'"><img src="'.DOKU_FARM_WWW.'images/arrow_'.($sort == 'name' ? 'up' : 'down').'.png" alt="sort" /></a></th>');
			ptln('		<th>'.$this->manager->getLang('animal_users_name').'</th>');
			ptln('		<th>'.$this->manager->getLang('animal_users_mail').'</th>');
			ptln('		<th>'.$this->manager->getLang('animal_users_grps').'</th>');
			ptln('		<th>&nbsp;</th>');
			ptln('	</tr>');
			foreach($this->animal->getUsers() as $l => $u) {
				$grps = array();
				if($grp && !in_array($grp, $u['grps'])) continue;
				foreach($u['grps'] as $g) $grps[] = '<a href="'.$this->wl(array('users' => true, 'grp' => $g)).'" title="'.$this->manager->getLang('animal_users_onlygrp').'">'.$g.'</a>';
				ptln('	<tr>');
				ptln('		<td>'.$l.'</td>');
				ptln('		<td>'.$u['name'].'</td>');
				ptln('		<td><a href="mailto:'.$u['mail'].'">'.$u['mail'].'</a></td>');
				ptln('		<td>'.implode(', ', $grps).'</td>');
				ptln('		<td><input type="image" src="'.DOKU_FARM_WWW.'images/delete.png" onclick="if(confirm(\''.$this->manager->getLang('animal_users_reallydeleteuser', $l, 'js').'\')) document.location=\''.$this->wl(array('users' => true, 'deleteuser' => base64_encode($l))).'&amp;sectok='.getSecurityToken().'\';" title="'.$this->manager->getLang('animal_users_deleteuser').'" /></td>');
				ptln('	</tr>');
			}
			ptln('</table>');
			
			$this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[users]' => true));
			ptln('	<br /><b>'.$this->manager->getLang('animal_users_add').'</b><br />');
			ptln('	'.$this->manager->getLang('animal_users_login').' : <input type="text" name="new_animal_user_login" value="'.(isset($_POST['new_animal_user_login']) ? $_POST['new_animal_user_login'] : '').'" /><br />');
			ptln('	'.$this->manager->getLang('animal_users_pass').' : <input type="text" name="new_animal_user_pass" value="'.(isset($_POST['new_animal_user_pass']) ? $_POST['new_animal_user_pass'] : '').'" /><br />');
			ptln('	'.$this->manager->getLang('animal_users_name').' : <input type="text" name="new_animal_user_name" value="'.(isset($_POST['new_animal_user_name']) ? $_POST['new_animal_user_name'] : '').'" /><br />');
			ptln('	'.$this->manager->getLang('animal_users_mail').' : <input type="text" name="new_animal_user_mail" value="'.(isset($_POST['new_animal_user_mail']) ? $_POST['new_animal_user_mail'] : '').'" /><br />');
			ptln('	'.$this->manager->getLang('animal_users_grps').' : <input type="text" name="new_animal_user_grps" value="'.(isset($_POST['new_animal_user_grps']) ? $_POST['new_animal_user_grps'] : '').'" /><br />');
			ptln('	<input type="submit" name="farm_opt[save]" value="'.$this->manager->getLang('btn_create').'" />');
			ptln('</form>');
		}
		
		// Animal edit file
		if(isset($this->manager->opt['editfile']) && $this->animal) {
			ptln('<div class="farm_cmd_title">');
			ptln('	'.$this->manager->getLang('animal_editfile'));
			ptln('</div>');
			
			ptln('<div class="farm_cmd_info">');
			ptln('	'.$this->manager->getLang('animal_change_warning'));
			ptln('</div>');
			
			$file = isset($this->manager->opt['file']) ? base64_decode($this->manager->opt['file']) : null;
			$file = @file_exists($this->animal->getPath().'/'.$file) ? $file : null;
			$f64 = $file ? base64_encode($file) : null;
			
			ptln('<div id="animal_fs" openned="'.$f64.'">');
			ptln('	'.$this->manager->getLang('animal_editfile_fs', $this->animal->getName()));
			foreach($this->animal->getFs() as $r64 => $i) {
				ptln('	<div class="animal_'.$i['type'].' '.($f64 == $r64 && $i['type'] == 'file' ? 'animal_file_current' : '').'" ref="'.$r64.'" pref="'.$i['pref'].'" lvl="'.$i['lvl'].'" style="margin-left:'.($i['lvl'] * 16).'px">');
				if($i['type'] == 'file') ptln('<a href="'.$this->wl(array('editfile' => true, 'file' => $r64)).'#animal_editfile">');
				ptln($i['name']);
				if($i['type'] == 'file') ptln('</a>');
				if($i['type'] == 'file') {
					ptln('<div class="file_infos">('.$this->animal->getInfos()->formatedprint($this->manager->getLang('file_info'), array('size' => $i['size'], 'date' => $i['mtime'], 'dformat' => $conf['dformat'])).')</div>');
				}
				ptln('</div>');
			}
			ptln('</div>');
			$this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[editfile]' => true, 'farm_opt[newfile]' => true));
			ptln($this->manager->getLang('animal_editfile_create').' : <input type="text" id="editfile_path" name="farm_opt[file]" value="" />');
			ptln(' <input type="submit" name="farm_opt[create]" value="'.$this->manager->getLang('btn_create').'" />');
			ptln('</form>');
			
			if($file) {
				$this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[editfile]' => true, 'farm_opt[file]' => base64_encode($file)));
				ptln('	<a name="animal_editfile"></a>'.$this->manager->getLang('animal_editfile_file', $file).'<br />');
				ptln('	<textarea name="animal_file" rows="15" cols="80">'.$this->animal->getFileContent($file).'</textarea><br />');
				ptln('	<input type="checkbox" name="farm_opt[editfile_saveempty]" /> '.$this->manager->getLang('animal_editfile_saveempty').'<br />');
				ptln('	<input type="checkbox" name="farm_opt[editfile_delete]" /> '.$this->manager->getLang('animal_editfile_delete').'<br />');
				ptln('	<input type="submit" name="farm_opt[save]" value="'.$this->manager->getLang('btn_save').'" />');
				ptln('</form>');
			}
		}
		
		// New animal
		if(isset($this->manager->opt['new'])) {
			ptln('<div class="farm_cmd_title">');
			ptln('	'.$this->manager->getLang('animal_new_title'));
			ptln('</div>');
			
			$this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[new]' => 1));
			ptln('	'.$this->manager->getLang('animal_new_name').' : <i>'.$this->manager->conf['farmwebroot'].'</i><input name="animal_name" value="" /><br />');
			ptln('	'.$this->manager->getLang('animal_new_template').' : <select name="animal_template">');
			foreach(dokuwiki_farm_animal::listAnimals() as $a) ptln('		<option '.($a->getName() == $this->manager->conf['animaltemplate'] ? 'selected="selected"' : '').' value="'.$a->getName().'">'.$a->getName().'</option>');
			ptln('	</select><br />');
			if($this->manager->conf['virtual']) ptln('	'.$this->manager->getLang('animal_new_host').' : <input name="animal_host" value="" /><br />');
			ptln('	<input type="submit" name="farm_opt[save]" value="'.$this->manager->getLang('btn_create').'" />');
			ptln('</form>');
		}
		
		// Delete animal
		if(isset($this->manager->opt['delete'])) {
			ptln('<div class="farm_cmd_title">');
			ptln('	'.$this->manager->getLang('animal_delete'));
			ptln('</div>');
			
			$this->manager->formHead(array('farm_cmd' => 'animal', 'farm_opt[aid]' => $this->animal->getName(), 'farm_opt[delete]' => 1));
			ptln('	'.$this->manager->getLang('animal_delete_sure', $this->animal->getName()).' <input type="submit" name="farm_opt[confirm]" value="'.$this->manager->getLang('btn_confirm').'" /> <input type="submit" name="farm_opt[cancel]" value="'.$this->manager->getLang('btn_cancel').'" />');
			ptln('</form>');
		}
		
		ptln('<div class="farm_cmd_footer"></div>');
	}
}

?>
