<?php
if (isset($order) && $order == 'datetime') $order = 'id';

print "
<div class='container'>
	<form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
		<div class='form'>
			<div class='form-head toggle ";
	if (isset($_SESSION['backend']['toggle_filter']) && $_SESSION['backend']['toggle_filter']) print'toggled';
	print "'>
				<i class='material-icons md-24'>filter_alt</i>".LABEL_FILTER."<i class='ico-toggle material-icons md-24'>expand_less</i>
			</div>
			<div class='form-content ";
	if (isset($_SESSION['backend']['toggle_filter'])) print $_SESSION['backend']['toggle_filter'];
	print "' data-module='".$module."' data-backend-module='".$backend_module."'>
				<input type='hidden' name='csrf_token' value='".htmlspecialchars(csrf_token())."'>
				<input type='hidden' name='filter' value='1' />
				<input type='hidden' name='export' value='0' />";
				
	include('includes/function/modules_manager.php');
	print "
				<div class='form-action'>
					<button type='submit' class='btn search' title='".LABEL_DO_FILTER."'><i class='material-icons md-24'>filter_alt</i>".LABEL_DO_FILTER."</button>
				</div><!-- /.form-action -->
				
			</div><!-- /.form-content -->
		</div><!-- /.form -->";

	
$response = remote_call('https://update.comator.org/update.php?domain='.$_SERVER['SERVER_NAME']);
$update_all_count = 0;
if (isset($response->module) && $response->module){
	foreach ($response->module AS $tmp_key => $tmp_value){
		if ($tmp_key == 'setup') continue;
		$tmp_version = 'N/A';
		if ($tmp_key == 'core' && is_file('version.txt')) $tmp_version = trim((string)file_get_contents('version.txt'));
		else if (is_file('modules/'.$tmp_key.'/version.txt')) $tmp_version = trim((string)file_get_contents('modules/'.$tmp_key.'/version.txt'));
		if ($tmp_version != 'N/A' && version_compare(convertToVersion($tmp_value->version), convertToVersion($tmp_version), '>')) $update_all_count++;
	}
}
	
if ($update_all_count){
	print "
		<div class='button'><a href='#' class='btn update_all'><i class='material-icons md-24'>update</i>Update All</a></div>";
}

print "	
		<div class='table'>
			<div class='table-head'>
				<div class='table-row'>
					<div class='table-cell'><a href='#' class='order'>".LABEL_TITLE;
if ($order == 'title') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-medium'><a href='#' class='order' data-order='text'>".LABEL_VERSION;
if ($order == 'text') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-medium'><a href='#' class='order' data-order='level_module'>".LABEL_UPDATE;
if ($order == 'level_module') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell toolbar size-1'></div><!-- /.table-cell -->
				</div><!-- /.table-row -->
			</div><!-- /.table-head -->";
			
if (isset($response->module) && $response->module){
	$core_update = false;
	foreach ($response->module AS $key => $value){
		if ($key == 'core' && is_file('version.txt')){
			$fh = fopen('version.txt', 'r');
			while ($line = fgets($fh)) {
        $current_version = convertToVersion(trim($line));
      	$module_version = convertToVersion($value->version);
				if (version_compare($module_version, $current_version, '>')) $core_update = true;
			}
			fclose($fh);
		}
	}
	print "
			<div class='table-body no-context'>";
	foreach ($response->module AS $key => $value){
		if ($key != 'setup'){
			$version = 'N/A';
			if ($key == 'core' && is_file('version.txt')){
				$fh = fopen('version.txt', 'r');
				while ($line = fgets($fh)) {
					if ($line) $version = trim($line);
				}
				fclose($fh);
			} else if (is_file('modules/'.$key.'/version.txt')) {
				$fh = fopen('modules/'.$key.'/version.txt', 'r');
				while ($line = fgets($fh)) {
					if ($line) $version = trim($line);
				}
				fclose($fh);				
			}
			
      $current_version = convertToVersion($version);
      $module_version = convertToVersion($value->version);
      
			print "
				<div class='table-row ";
			if ($version != 'N/A' && version_compare($module_version, $current_version, '>')) print "important";
			print "'>
					<div class='table-cell'>".$key."</div><!-- /.table-cell -->
					<div class='table-cell'>".$version."</div><!-- /.table-cell -->
					<div class='table-cell'>".$value->version."</div><!-- /.table-cell -->
					<div class='table-cell toolbar'>";
			if (!$core_update OR $key == 'core'){
				 if ($version == 'N/A'){
					print "<a href='#' class='install_module' data-module='".$key."' title='".LABEL_INSTALL."'><i class='material-icons md-22'>download</i></a>";
				} else if (version_compare($module_version, $current_version, '>')) {
					print "<a href='#' class='update_module' data-module='".$key."' data-current='".$version."' data-target='".$value->version."' title='".LABEL_UPDATE."'><i class='material-icons md-22'>update</i></a>";
				}
			}
			print "
					</div><!-- /.table-cell -->
				</div><!-- /.table-row -->";
		}
	}
	print "
			</div><!-- /.table-body -->";
}
		
print "
	</div><!-- /.table -->
</div><!-- /.container -->";