<? 
$this->title->create($this->Language('Manage model'));

echo $this->getError('No such model');
echo $this->getError('Cannot install');
echo $this->getMessage('Successfully installed');
?>

<table class="content">
<tr>
	<th><?=$this->Language('Model');?></th>
	<th><?=$this->Language('Available version');?></th>
	<th><?=$this->Language('Current version');?></th>
	<th></th>
	<th></th>
</tr>


<?

$exp = explode("\n", str_replace('.zip', '', $this->file_list));
$model_list = "";

foreach($exp as $v){

	if($v){

		$version = 0;

		$check = explode('_v_', $v);
		$model = $check[0];
		$model_list .= $model_list ? ",'".$model."'" : "'".$model."'";
		$version = str_replace('_', '.', $check[1]);

		$this->templates->columns("(version+0.1) AS new_version")->append();
		$this->templates->templates("WHERE type IN('MODEL', 'CORE') and name = '".$model."' GROUP BY name ORDER BY name ");
		$row = $this->templates->Assoc();

		if(!$row['version']){

			$row['version'] = '1.0';
		}

		$text = $this->templates->Numrows == 0 ? 'Install' : 'Update';

		$download_link = '';
		if($_SESSION['create_install'][$row['name']])
			$download_link = '<a href="'.$_SESSION['create_install'][$row['name']].'" class="_blank">'.$this->Language('Download file').'</a>';

		echo '<tr>
			<td>'.$model.'</td>
			<td>'.$version.'</td>
			<td>'.$row['version'].'</td>
			<td>'.((float)$version >= (float)$row['version'] ? '<a href="'.$this->Link('?action=install&file='.$v).'">'.$this->Language($text).'</a>' : '').'</td>
			<td>'.($this->templates->Numrows > 0 ? '<input type="text" name="version" value="'.$row['new_version'].'" style="width:25px;"> <a href="'.$this->Link('?action=create_install&model_name='.$model).'" class="create_link">'.$this->Language('Create install').'</a> '.$download_link : '').'</td>
		</tr>';
	}
}

$this->templates->columns("(version+0.1) AS new_version")->append();
$this->templates->templates("WHERE type IN('MODEL', 'CORE') ".($model_list ? " and name NOT IN(".$model_list.") " : "")." GROUP BY name ORDER BY name ");
while($row = $this->templates->Assoc()){

	$download_link = '';
	if($_SESSION['create_install'][$row['name']])
		$download_link = '<a href="'.$_SESSION['create_install'][$row['name']].'" class="_blank">'.$this->Language('Download file').'</a>';

	echo '<tr>
		<td>'.$row['name'].'</td>
		<td></td>
		<td>'.$row['version'].'</td>
		<td></td>
		<td><input type="text" name="version" value="'.$row['new_version'].'" style="width:25px;"> <a href="'.$this->Link('?action=create_install&model_name='.$row['name']).'" class="create_link">'.$this->Language('Create install').'</a> '.$download_link.'</td>
	</tr>';
}

?>
</table>

<form method="post" enctype="multipart/form-data" style="margin-top:20px;">
<?=$this->Language('IF model is not listed:');?> <input type="file" name="model_file"> <input type="submit" name="add_model_from_file" value="<?=$this->Language('Add model');?>">
</form>