<?php

//////////////////////////////////////////////////////////////
//===========================================================
// ostemplates_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function getSortHeader($header_name, $sort_column, $sort_column_by, $column ,$size = 90){

	global $theme, $globals;

	$str = '<div class="d-inline-flex align-items-center" style="width:'.$size.'px;">
		<div class="mr-2">
			<span class="" >'.$header_name.'</span>
		</div>
		<span class="sorter">
			<a class="text-always-white" href="'.$globals['index'].'act=ostemplates&sortcolumn='.$column.'&sortby=asc"><i class="fas fa-sort-up"></i></a><br>
			<a class="text-always-white" href="'.$globals['index'].'act=ostemplates&sortcolumn='.$column.'&sortby=desc"><i class="fas fa-sort-down"></i></a>
		</span>
	</div>';
		
	return $str;
}

function ostemplates_theme(){

global $theme, $globals, $kernel, $user, $l, $oslist, $error, $installed, $done, $mgs, $servers, $sort_column, $sort_column_by, $ostemplates;

softheader($l['<title>']);
					
$buttons = array( 
	0 => array(
		'name' => __('Add Template'), 
		'act' => 'addtemplate'),
	1 => array(
		'name' => __('Template Browser'), 
		'act' => 'os')
);

$GLOBALS['breadcrumbs'] = array('dashboard', 'lm_ostemplates', 'lms_ostemplates');

echo page_header(__('OS Templates'), 'fas fa-compact-disc',$globals['docs'].'os-templates/', 0, 0, $buttons);

error_handle($error);

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[

function go_click(ele) {
	
	if($(ele).parent().prev().find("select").val() == 0){
		showalert("'.__J('No action selected !').'","1");
		return false;
	}
	
	var os_list = new Array();
	
	$(".ios:checked").each(function() {
		os_list.push($(this).val());
	});
	
	if(os_list.length < 1){
		showalert("'.__J('Nothing selected !').'", "1");
		return false;
	}
	
	if($(ele).parent().prev().find("select").val() == 1){
		Delos(os_list);
	} else if($(ele).parent().prev().find("select").val() == 2){
		show_server_select(os_list);
	}
	
}

function Delos(os_list, installed_list){
	
	var alerttext;

	if(installed_list == undefined || installed_list == null || installed_list == ""){
		alerttext = "'.__J('Are you sure, you want to delete the selected item(s) ?').'";
	}else{
		alerttext = "'.__J('Are you sure, you want to delete the selected item, this OS Template is used in some VPS. Total VPS = ').'" +installed_list;
	}

	showalert(alerttext ,"3" ,function(confirm){
		if(confirm == false){
			return false;
		}else{
			var finalData = new Object();
			finalData["delete"] = os_list.join(",");

			//alert(finalData);
			//return false;
			
			$("#progress_bar").show();
			
			$.ajax({
				type: "POST",
				url: "'.$globals['index'].'act=ostemplates&api=json",
				data : finalData,
				dataType : "json",
				success: function(data){
					$("#progress_bar").hide();
					if("done" in data){
						setTimeout(function(){showalert("'.__J('The action was completed sucessfully').'");
						$("#alert-modal").on("hidden.bs.modal", function (e) {
							location.reload(true);
						});} ,200);
					}
					if("error" in data){
						setTimeout(function(){showalert(data["error"],"1");} ,200);
					}
					if("fatal_error_text" in data){
						setTimeout(function(){showalert(data["fatal_error_text"] ,"1");} ,200);
					}
					
				},
				error: function(data) {
					$("#progress_bar").hide();
					//alert(data.description);
					return false;
				}
			});
		}
	});
	return false;
};

function serids_select_change() {
	var serids = $("#serids_select").val().join(",");
	$("#serids").val(serids);
}

function show_server_select(os_list) {';
	
	if(is_slave()) {
		echo 'showalert("'.__J('Slave servers are not allowed to sync OS templates').'","1");
		return false;';
	} else {
		
		echo 'var $modal_form = $(\'<form id="sync_os_form" action="" method="post"> \
			<input type="hidden" id="sync_os" name="sync_os" /> \
			<input type="hidden" id="serids" name="serids" /> \
			<br> \
			<div class="row"> \
				<div class="col-sm-6"> \
				<label class="control-label">'.__J('Select Servers').'&nbsp;<a class="info" data-placement="right" tooltip="'.__J('Select all the servers to which you want to sync the OS template(s)').'"><i class="fas text-primary fa-info-circle"></i></a></label> \
				</div> \
				<div class="col-sm-6"> \
					<select class="form-control" id="serids_select" onchange="serids_select_change();" multiple> \ ';
						
						foreach($servers as $serid => $server) {
							if(empty($serid)) continue;
							echo '<option value="'.$serid.'">'.$server['server_name'].' - '.$server['ip'].'</option> \ ';
						}
						
					echo '</select> \
				</div> \
			</div> \
			<br> \
			<div class="row"> \
				<div class="col-sm-6"> \
					<label class="control-label">'.__J('Force Sync').'&nbsp;<a class="info" data-placement="right" tooltip="'.__J('If checked, existing template will be deleted and ostemplate will be re-downloaded').'"><i class="fas text-primary fa-info-circle"></i></a></label> \
				</div> \
				<div class="col-sm-6"> \
					<div class="custom-control custom-checkbox"> \
						<input type="checkbox" class="ios custom-control-input" id="force_sync" name="force_sync"> \
						<label class="custom-control-label" for="force_sync"></label> \
					</div> \
				</div> \
			</div> \
			<br> \
			<div class="row"> \
				<div class="col-sm-12 text-center"> \
					<input class="go_btn" value="'.__J('Sync OS Template(s)').'" type="submit"> \
				</div> \
			</div>\
		</form>\');
		
		// Show dialog for server select
		$modal_form.find("#sync_os").val(os_list.join(","));
		$("#common_modal .fhead").text("'.__J('Sync OS Template(s)').'");
		$("#common_modal_body").html($modal_form);
		$("#common_modal").modal("show");
		show_tooltip();';
		
	}
	
echo '}

// ]]></script>

';

if($done == 'sync_os'){
	echo '<div class="notice"><b>NOTE : </b>&nbsp; '.__('The OS template sync process has been started in the background.').'</div>';
} elseif(!empty($done)) {
	show_alert(__('The OS Template was deleted successfully'));
}

if(empty($oslist)){
	show_alert(__('There are no OS Templates !'), "2");
	echo '<div class="notice"><b>NOTE : </b>&nbsp; '.__('There are no OS Templates !').'</div>';
}else{
echo'
<div class="card p-2 mx-3">
	<div class="row mx-auto w-100 mb-3">
		<div class="col-sm-4 col-lg-4 col-md-4 text-right d-flex align-items-center">
			<div class="bottom-menu float-right w-100">
				<div class="row my-auto">
					<div class="p-0">
						<label>'.__('With Selected').'</label>
					</div>
					<div class="col p-0">
						<select class="custom-select" name="os_task_select" id="os_task_select">
							<option value="0">---</option>
							<option value="1">'.__('Delete').'</option>
							<option value="2">'.__('Sync OS Template(s)').'</option>
						</select>
					</div>
					<div class="pl-2">
						<input type="submit" id ="ostemplate_submit" class="go_btn" name="ostemplate_submit" value="'.__('Go').'" onclick="go_click(this); return false;">
					</div>
				</div>
			</div>
		</div>
		<div class="col-sm-8 col-lg-8 col-md-8 p-0 d-flex align-items-center justify-content-end">
			'.get_num_records(count($ostemplates)).'
		</div>
	</div>
	<form accept-charset="'.$globals['charset'].'" name="multi_os" id="multi_os" method="post" action="" class="form-horizontal">
		<div class="w-100 mx-auto sticky">
			<table class="table table-hover tablesorter common-table">
				<tr class="table_heading">
					<th align="center" class="z-index-1 min_width">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th class="min_width" align="center">'.getSortHeader(__('OSID'), $sort_column, $sort_column_by, 'osid' ,30).'</th>
					<th class="text-center">'.__('OS').'</th>
					<th align="center">'.__('Type').'</th>
					<th align="center">'.getSortHeader(__('Name'), $sort_column, $sort_column_by, 'name').'</th>
					<th align="center">'.__('Filename').'</th>
					<th align="center">'.__('Media Groups').'</th>
					<th align="left">'.getSortHeader(__('Size'), $sort_column, $sort_column_by, 'size').'</th>
					<th align="left">'.__('Size on disk').'</th>
					<th align="left">'.__('Used By').'</th>
					<th colspan="3" class="min_width text-right pr-3">'.__('Manage').'</th>
				</tr>';

			$i = 1;
			foreach ($ostemplates as $k => $v) {
				echo'<tr>
					<td width="20" valign="middle" align="center">
						'.get_checkbox('ostemplate_list[]', '', '', array('chk_it' => 0), $k).'
					</td>
					<td align="left">'.$k.'</td>
					<td align="center"><img src="'.distro_logo($v['distro']).'" width="35px"/></td>
					<td align="left"><script>document.write(show_virtname("'.$v['type'].(empty($v['hvm']) ? '' : 'hvm').'"));</script></td>
					<td align="left">'.$v['name'].'</td>
					<td '.(empty($v['filename']) ? 'align="center">-</td>' : "align='left'>{$v['filename']}</td>").'
					<td align="left">';

					$tmp = array();
								
					if(empty($v['mg'])){
						echo '<em>'.__('None').'</em>';
					}else{
						foreach($v['mg'] as $mk){
							$tmp[] = $mgs[$mk]['mg_name'];
						}
						echo implode(', ', $tmp);				
					}
								
					echo '</td>
					<td align="left">'.round($v['size']/1024/1024).' MB</td>
					<td align="left">'.round(vfilesize($globals[$v['type'].'os'].'/'.$v['filename'])/1024/1024).' MB</td>
					<td align="left">'.(empty($installed[$k]) ? 0 : $installed[$k]).'</td>
					<td align="right" width="20">
						<div class="badge_container">
							<a class="badge badge-yellow" href="javascript:void(0);" title="'.__('Sync OS Template(s)').'" onclick="show_server_select(['.$k.']); return false"><i class="fas fa-sync-alt h6 m-0"></i></a>
							<a class="badge badge-manage" href="'.$globals['ind'].'act=edittemplate&osid='.$k.'"  title="'.__('Edit OS').'"><i class="fas fa-edit"></i></a>
							<a class="badge badge-trash" href="javascript:void(0);" onclick="return Delos(['.$k.'],['.$installed[$k].']);"  title="'.__('Delete OS').'"><i class="fas fa-trash"></i></a>
						</div>
					</td>
					
				</tr>';
				$i++;
			}
		echo '</table>
		</div>
		<div class="row mx-auto w-100 mt-5">
			<div class="col-sm-4 col-lg-4 col-md-4 text-right mb-3">
				<div class="bottom-menu float-right w-100">
					<div class="row my-auto">
						<div class="p-0">
							<label>'.__('With Selected').'</label>
						</div>
						<div class="col p-0">
							<select class="custom-select" name="os_task_select" id="os_task_select">
								<option value="0">---</option>
								<option value="1">'.__('Delete').'</option>
								<option value="2">'.__('Sync OS Template(s)').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="ostemplate_submit" class="go_btn" name="ostemplate_submit" value="'.__('Go').'" onclick="go_click(this); return false;">
						</div>
					</div>
				</div>
			</div>
			<div class="col-sm-8 col-lg-8 col-md-8 text-right mb-3"></div>
		</div>
	</form>
	<div id="progress_bar" style="display:none;">
		<br />
		<center>
			<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
		</center>
		<div class="my-2" align="center">
			<div class="progress w-50">
				<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100">
				</div>
			</div>
		</div>
		<center>
			'.__('You will be notified once the Action is Completed.').'
		</center>
	</div>';

}

echo '
	<div class="text-center my-4">
		<div class="col-sm-11">
			<a href="'.$globals['ind'].'act=addtemplate" title="'.__('Add Template').'" class="go_btn">'.__('Add Template').'</a> &nbsp;
			<a href="'.$globals['ind'].'act=os" title="'.__('Template Browser').'" class="go_btn">'.__('Template Browser').'</a>
		</div>
	</div>
<br/>
</div>';
softfooter();

}

