<?php
require_once dirname( __FILE__ ) . "/../../lib/bootstrap.php";

$session = session::getInstance();

$request = httpRequest::getInstance();
$parm = $request->get_parms("srt", "ord",
						 "act",
						 "p0",	// base_id
						 "bit",
						 "nam",		// name
						 "searchable",		// name
						 "printable",		// name
						 "dic0",	// ccoch delete icon 0
						 "dic1",	// ccoch delete icon 1
						 "labelon",	// ccoch delete icon 1
						 "labeloff"	// ccoch delete icon 1
					 );

$lng = isset($session->locale)?$session->locale:GV_default_lng;

if(isset($session->usr_id) && isset($session->ses_id))
{
	$ses_id = $session->ses_id;
	$usr_id = $session->usr_id;
}	
					 
if(!($ph_session = phrasea_open_session($ses_id,$usr_id)))
{
	phrasea::headers(403);
}
$conn = connection::getInstance();
if(!$conn)
{
	phrasea::headers(500);
}


if(is_null($parm['p0']))
	phrasea::headers(400);

$user = user::getInstance($usr_id);
if(!isset($user->_rights_sbas[$parm['p0']]) || !$user->_rights_sbas[$parm['p0']]['bas_modify_struct'])
{
	phrasea::headers(403);
}
	
phrasea::headers();
?>
<html lang="<?php echo $session->usr_i18n;?>">
	<head>
		<title></title>
		<link type="text/css" rel="stylesheet" href="/include/minify/f=skins/common/main.css" />
		<link type="text/css" rel="stylesheet" href="/include/minify/f=skins/admin/admincolor.css" />
		<style type="text/css">
		BODY
		{
		    
		}
		H4
		{
		    MARGIN-TOP: 0px;
		    FONT-WEIGHT: normal;
		    FONT-SIZE: 18px;
		    MARGIN-BOTTOM: 0px;
		    MARGIN-LEFT: 5px
		}
		img.editer, img.adder, img.deleter{
			cursor:pointer;
		}
		</style>
	</head>
	<body>

<?php
$loadit = true;

if($parm["act"]=="DELETE")
{
	status::deleteStatus($parm['p0'], $parm["bit"]);
}

if($parm["act"]=="APPLY")
{
	$searchable = ($parm["searchable"]=='on')?'1':'0';
	$printable = ($parm["printable"]=='on')?'1':'0';
	
	$properties = array('searchable'	=> $searchable,
				'printable'	=>	$printable,
				'name'		=> $parm["nam"],
				'labelon'	=> $parm["labelon"],
				'labeloff'	=> $parm["labeloff"]
				);
	
	status::updateStatus($parm['p0'], $parm["bit"], $properties);
	
	if($parm["dic0"])
	{
		status::deleteIcon($parm['p0'],$parm['bit'],'off');
	}
	if(isset($_FILES['ic0']))
	{
		status::updateIcon($parm['p0'],$parm['bit'],'off',$_FILES['ic0']);
	}
	
	
	if($parm["dic1"])
	{
		status::deleteIcon($parm['p0'],$parm['bit'],'on');
	}
	if(isset($_FILES['ic1']))
	{
		status::updateIcon($parm['p0'],$parm['bit'],'on',$_FILES['ic1']);
	}
}



if($parm["act"]=="ADD" || $parm["act"]=="EDIT")
{
	$status = status::getStatus($parm['p0']);
	
	$status = isset($status[$parm['bit']]) ? $status[$parm['bit']] : array('name'=>'','labelon'=>'','labeloff'=>'','img_on'=>'','img_off'=>'','searchable'=>'0','printable'=>'0')
	?>

	<form enctype="multipart/form-data" method="post" name="chgStatbits" action="./statbits.php" target="_self">
		<table style="width:700px;">
			<tr style="text-align:center;">
				<td colspan="2"><h4><?php echo _('phraseanet::status bit');?></h4></td>
			</tr>
			<tr>
				<td colspan="2"></td>
			</tr>
			<tr>
				<td colspan="2"><?php echo _('admin::status: nom du status : ');?> <input type="text" name="nam" value="<?php echo str_replace('"','&quot;',$status['name']);?>" /></td>
			</tr>
			<tr style="text-align:center;">
				<td><h4><?php echo _('admin::status: case A')?></h4></td>
				<td><h4><?php echo _('admin::status: case B')?></h4></td>
			</tr>
			<tr>
				<td><?php echo _('admin::status: parametres si decoche')?></td>
				<td><?php echo _('admin::status: parametres si coche')?></td>
			</tr>
			<tr>
				<td><?php echo _('admin::status: texte a afficher')?><input type="text" name="labeloff" value="<?php echo $status['labeloff']?>" /></td>
				<td><?php echo _('admin::status: texte a afficher')?><input type="text" name="labelon" value="<?php echo $status['labelon']?>" /></td>
			</tr>
			<tr>
				<td><?php echo _('admin::status: symboliser par')?> <?php echo ($status['img_off'] ? '<img src="'.$status['img_off'].'" />':_('admin::status: aucun symlboler'));?></td>
				<td><?php echo _('admin::status: symboliser par')?> <?php echo ($status['img_on'] ? '<img src="'.$status['img_on'].'" />':_('admin::status: aucun symlboler'));?></td>
			</tr>
			<tr>
				<td><input type="file" name="ic0" /></td>
				<td><input type="file" name="ic1" /></td>
			</tr>
			<tr>
				<td><input type="checkbox" name="dic0" /><?php echo _('admin::status:: aucun symbole');?></td>
				<td><input type="checkbox" name="dic1" /><?php echo _('admin::status:: aucun symbole');?></td>
			</tr>
			<tr>
				<td colspan="2"><input type="checkbox" name="printable" <?php echo ($status['printable']=='1'?"checked":"")?> />  <?php echo _('status:: Afficher le status dans les feuilles de reponses pour tous les utilisateurs') ?></td>
			</tr>
			<tr>
				<td colspan="2"><input type="checkbox" name="searchable" <?php echo ($status['searchable']=='1'?"checked":"")?> /> <?php echo _('status:: retrouver sous forme de filtre dans la recherche') ?></td>
			</tr>
			<tr>
				<td colspan="2" style="text-align:center;"><input type="button" onclick="document.location.replace('./statbits.php?p0=<?php echo $parm["p0"]; ?>')" value="<?php echo _('boutton::annuler'); ?>"/> <input type="submit" value="<?php echo _('boutton::valider'); ?>"/></td>
			</tr>
		
		</table>
		<input type="hidden" name="act" value="APPLY" />
		<input type="hidden" name="p0" value="<?php echo $parm["p0"];?>" />
		<input type="hidden" name="bit" value="<?php echo $parm["bit"];?>" />
	</form>
	</body>
</html>
	
	<?php
	exit;
}



	$status = status::getStatus($parm['p0']);
	?>
	
	<h4><?php echo _('phraseanet::status bit');?></h4>
	
	<form enctype="multipart/form-data" method="post" name="chgStatbits" action="./statbits.php" onsubmit="return(false);" target="_self">
		<input type="hidden" name="p0" value="<?php echo $parm["p0"];?>" />
		<input type="hidden" name="bit" value="???" />
		<input type="hidden" name="act" value="???" />
	</form>

	<table class="ulist" style="margin-left:10px">
		<col width="50px" />
		<col width="20px" />
		<col width="20px" />
		<col width="150px" />
		<col width="100px" />
		<col width="100px" />
		<col width="100px" />
		<col width="100px" />
		<thead>
			<th><?php echo _('status:: numero de bit'); ?></th>
			<th colspan="2"/>
			<th><?php echo _('status:: nom'); ?></th>
			<th><?php echo _('status:: icone A'); ?></th>
			<th><?php echo _('status:: icone B'); ?></th>
			<th><?php echo _('status:: cherchable par tous'); ?></th>
			<th><?php echo _('status:: Affichable pour tous'); ?></th>
		</thead>
	
	<?php
	for($bit=4; $bit<64; $bit++)
	{
		?>
		<tr>
			<td style="text-align:center"><?php echo $bit;?></td>
		
		<?php		
		if(isset($status[$bit]))
		{
			?>
			<td style="text-align:center">
				<form id="editer_<?php echo $bit;?>">
					<input type="hidden" name="p0" value="<?php echo $parm['p0'];?>" />
					<input type="hidden" name="bit" value="<?php echo $bit;?>" />
					<input type="hidden" name="act" value="EDIT" />
					<img class="editer" src="/skins/icons/edit_0.gif" onclick="document.getElementById('editer_<?php echo $bit;?>').submit();">
				</form>
			</td>
			<td style="text-align:center">
				<form id="deleter_<?php echo $bit;?>">
					<input type="hidden" name="p0" value="<?php echo $parm['p0'];?>" />
					<input type="hidden" name="bit" value="<?php echo $bit;?>" />
					<input type="hidden" name="act" value="DELETE" />
					<img class="deleter" src="/skins/icons/delete_0.gif" onclick="if(confirm('<?php echo str_replace("'","\'",_('admin::status: confirmer la suppression du status ?'))?>'))document.getElementById('deleter_<?php echo $bit;?>').submit();">
				</form>
			</td>
			<?php
		}
		else
		{
			?>
			<td colspan="2" style="text-align:center">
				<form id="adder_<?php echo $bit;?>">
					<input type="hidden" name="p0" value="<?php echo $parm['p0'];?>" />
					<input type="hidden" name="bit" value="<?php echo $bit;?>" />
					<input type="hidden" name="act" value="ADD" />
					<img class="adder" src="/skins/icons/light_new.gif" onclick="document.getElementById('adder_<?php echo $bit;?>').submit();">
				</form>
			<?php
		}
			
		if(isset($status[$bit]))
		{
			?>
			<td style="text-align:center"><?php echo $status[$bit]["name"]?></td>

			<td style="text-align:center">
				<?php echo $status[$bit]["img_off"] ? "<img title='".$status[$bit]["labeloff"]."' src='".$status[$bit]["img_off"] ."'/>":"";?>
			</td>
			
			<td style="text-align:center">
				<?php echo $status[$bit]["img_on"] ? "<img title='".$status[$bit]["labelon"]."' src='".$status[$bit]["img_on"] ."'/>":"";?>
			</td>
			
			<td><?php echo (isset($status[$bit]['searchable']) && $status[$bit]['searchable']=='1')?'oui':'non';?></td>
			<td><?php echo (isset($status[$bit]['printable']) && $status[$bit]['printable']=='1')?'oui':'non';?></td>

			<?php

		}
		else
		{
			?>
			<td/><td/><td/><td/><td/>
			<?php
		}
		?>
		</tr>
		<?php
	}
	?>
	</table>
</body>
</html>
