<?php

/* Log */

//modify a changelog
if(isset($_POST['action']))
{
	$iChangelogId = isset($_POST['changelogId'])?$_POST['changelogId']:'';
	$sAction = $sDesc = $sNewValue = '';
	switch($_POST['action'])
	{
		case 'addChangelog':
			if($_POST['0description']!='')
			{
				$iChangelogId = insertRow(CHANGELOG_TABLE, array($_POST['0description'], $_POST['0progress']?NEXT_VERSION:'0', $_POST['0type'], $_POST['0progress']));
				$sAction = 'CREATE';
				$sDesc = $_POST['0description'];
			}
			break;
		case 'complete':
			$sAction = 'ACTIVATE';
			$sDesc = getTextFromId(CHANGELOG_TABLE, $iChangelogId);
			updateRow(CHANGELOG_TABLE, $iChangelogId, array('progress'=>1, 'version'=>NEXT_VERSION));
			break;
		case 'rollback':
			$sAction = 'DESACTIVATE';
			$sDesc = getTextFromId(CHANGELOG_TABLE, $iChangelogId);
			updateRow(CHANGELOG_TABLE, $iChangelogId, array('progress'=>0, 'version'=>'0'));
			break;
		case 'deleteChangelog':
			$sAction = 'DELETE';
			$sDesc = getTextFromId(CHANGELOG_TABLE, $iChangelogId);
			deleteRow(CHANGELOG_TABLE, $iChangelogId);
			break;
		case 'modifyChangelog':
			$sAction = 'MODIFY';
			$sDesc = getTextFromId(CHANGELOG_TABLE, $iChangelogId);
			$sNewValue = $_POST[$iChangelogId.'description'];
			updateRow(CHANGELOG_TABLE, $iChangelogId, array(CHANGELOG_TABLE=>$sNewValue, 'type'=>$_POST[$iChangelogId.'type']));
			break;
		case 'publishChangelog':
			if(publishChangeLog($iChangelogId))
			{
				addMessage(getSuccess('Changelog Publi&eacute;'));
			}
			break;
	}
	if($sAction!='')
	{
		addFeed($sAction, $iChangelogId, CHANGELOG_TABLE, $sDesc, $sNewValue);
	}
}

//displays versions log, future features and bug fixes
$sUserOS = getOS();
$sPack = 'qcm_v'.STABLE_VERSION.'.'.(($sUserOS==OS_MAC || $sUserOS==OS_WINDOWS)?'zip':'tar.gz');
$sCurrentVersion = CURRENT_VERSION.(IS_BETA?' (b&eacute;ta)':'');

//add a changelog
$sAddChangelogType = getChangelogTypeSelect();
$sAddChangelogProgress = '<select name="0progress"><option value="0">Non</option><option value="1">Oui</option></select>';
$sAddChangelogButton = '<input type="button" onclick="setFormAction(0, \'addChangelog\', 1)" value="Ok" />';
$asNextVersionDisplay = array($sAddChangelogType.'&nbsp;<input type="text" name="0description" onclick="setFormAction(\'0\', \'addChangelog\');" style="width:500px;" /> Effectu&eacute; : '.$sAddChangelogProgress.'&nbsp;'.$sAddChangelogButton);

$oLogs = selectRows(array('from'=>CHANGELOG_TABLE, 'orderBy'=>array('version'=>'DESC', 'progress'=>'DESC', 'type'=>'ASC', 'led'=>'DESC')));
foreach($oLogs as $asLogs)
{
	$sDescription = ucfirst($asLogs[CHANGELOG_TABLE]);
	$sType = $asLogs['type'];
	$sVersion = $asLogs['version'];
	
	//dates
	$iTimeStamp = strtotime($asLogs['led']);
	$sDate = ($asLogs['led']>0)?'&nbsp;('.date('d/m/Y', $iTimeStamp).')':'';
	if($sDate!='' && (!isset($asMaxTimeStamp[$sVersion]) || $iTimeStamp > $asMaxTimeStamp[$sVersion]))
	{
		$asMaxTimeStamp[$sVersion] = $iTimeStamp;
		$asMaxDates[$sVersion] = $sDate;
	}
	
	$sSeparator = ' : ';
	
	//current version
	if($sVersion==0 || $sVersion==NEXT_VERSION)
	{
		$iChangeLogId = $asLogs['id_'.CHANGELOG_TABLE];
		
		//on progress
		if($asLogs['progress']==0)
		{
			$sAction = 'complete';
			$sActionTitle = 'En cours';
			$sType = getChangelogTypeSelect($iChangeLogId, $sType);
			$sDescription = '<input type="text" onclick="setFormAction('.$iChangeLogId.', \'modifyChangelog\');" class="nude" style="width:500px;font-size:small;font-style:italic;font-family:Verdana,Geneva,sans-serif;" name="'.$iChangeLogId.'description" value="'.$sDescription.'" />';
			$sDeleteButton = '&nbsp;<a href="#" class="XButton rounded" onclick="setFormAction('.$iChangeLogId.', \'deleteChangelog\', 1);return false;">X</a>';
			$sSubmitButton = '&nbsp;<a href="#" class="XButton rounded" onclick="setFormAction('.$iChangeLogId.', \'modifyChangelog\', 1);return false;">Ok</a>';
			$sSeparator = substr($sSeparator, 0, -1);
			$sPublishButton = '';
			$sDate = '';
		}
		//completed
		else
		{
			$sDeleteButton = $sSubmitButton = '&nbsp;';
			$sAction = 'rollback';
			$sActionTitle = 'Fini';
			$sPublishButton = '&nbsp;<a href="#" class="XButton rounded" onclick="setFormAction('.$iChangeLogId.', \'publishChangelog\', 1);return false;">Publier</a>';
		}
		$sProgressLink = '<a style="font-weight:bold;" href="#" onclick="setFormAction('.$iChangeLogId.', \''.$sAction.'\',  \'1\');return false;">['.$sActionTitle.']</a>';
		$asNextVersionDisplay[] = $sProgressLink.$sPublishButton.$sDeleteButton.$sSubmitButton.$sType.$sSeparator.$sDescription.$sDate;
	}
	//old versions
	else
	{
		$asLogDisplay[$sVersion][] = $sType.$sSeparator.$sDescription.$sDate;
	}
}

$sVersionDisplay = '';
if(isset($asLogDisplay))
{
	foreach($asLogDisplay as $sVersion=>$asChanges)
	{
		$sHtmlVersion = 'v'.$sVersion.(array_key_exists($sVersion, $asMaxDates)?$asMaxDates[$sVersion]:'');
		$sVersionDisplay .= getHtml($sHtmlVersion.getHtml(getHtml($asChanges, 'li'), 'ul'), 'li');
	}
}
$sNextVersionDisplay = 'v'.$sCurrentVersion.getHtml(getHtml($asNextVersionDisplay, 'li'), 'ul');
?>
<script type="text/javascript">
function setFormAction(changeLogId, action, submit)
{
	document.forms['changelog'].elements['changelogId'].value = changeLogId;
	document.forms['changelog'].elements['action'].value = action;
	if(submit)
	{
		document.forms['changelog'].submit();
	}
}
</script>
<form name="changelog" method="post">
	<fieldset class="rounded">
		<legend class="rounded">T&eacute;l&eacute;chargements <strong>v<?php echo STABLE_VERSION; ?></strong></legend>
		<p style="text-align:center;"><a class="download rounded" href="?page=download&file=<?php echo encodeUrl($sPack); ?>" title="T&eacute;l&eacute;charger la derni&egrave;re version">T&eacute;l&eacute;charger</a></p>
	</fieldset>
	<fieldset class="rounded"><legend class="rounded">En d&eacute;veloppement <strong>v<?php echo NEXT_VERSION; ?></strong></legend>
		<ul>
			<li><?php echo $sNextVersionDisplay; ?></li>
		</ul>
	</fieldset>
	<fieldset class="rounded"><legend class="rounded">Historique</legend>
		<ul>
			<?php echo $sVersionDisplay; ?>
		</ul>
	</fieldset>
	<input type="hidden" name="action" value="addChangeLog" />
	<input type="hidden" name="changelogId" value="" />
</form>