<?php

//////////////////////////////////////////////////////////////
//===========================================================
// addserver_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 addstorage_theme(){

global $theme, $globals, $kernel, $user, $l, $cluster, $error, $servers, $servergroups, $done;

softheader($l['<title>']);

$GLOBALS['breadcrumbs'] = array('dashboard', 'lm_storage', 'lms_addstorage');
					
echo page_header(__('Add Storage'), 'fas fa-hdd',$globals['docs'].'add-new-storage/', 0, 0, '');

if(!empty($done)){
	show_alert(__('Storage details has been added. Return to <a href="index.php?act=storage">Storage Overview</a>'),"0","storage");
}

error_handle($error);

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

addonload("storageSet();");

function thin_block(){
	
	var thin_is = $_("type").value;
	var params = $("#addstorage").serialize();
	
	if(thin_is =="thin block"){
		showalert("'.__J('Thin LVM requires knowledge of Linux Management. If your VPS(s) cease to function/work then Virtualizor is not responsible for it. Do you agree ?').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				exec(params);
			}
		});
	}else{
		exec(params);
	}
	return false;
};

function exec(params){
	$(\'[name = "addstorage"]\').prop("disabled", true)
	$("#processing_symb").show();
	$.ajax({
		url: "'.$globals['index'].'&act=addstorage&api=json",
		data:params+"&addstorage=1",
		dataType : "json",
		method : "POST",
		success:function(data){
			$(\'[name = "addstorage"]\').prop("disabled", false)
			$("#processing_symb").hide();
			if("error" in data) {
				
				setTimeout(function(){var error = "";
				error = "<ul>";
				for (var k in data["error"]) {
					error += "<li>"+data["error"][k]+"</li>"; 
				}
				error += "</ul>";
				showalert(error ,"1");
				return;} ,200);
			}else{
				setTimeout(function(){
					$("#addstorage")[0].reset();
					showalert(\''.__J('Storage details has been added. Return to <a href="index.php?act=storage">Storage Overview</a>').'\');
				return;} ,200);
			}
		}
	});	
}

function storageSet(storage){
	
	$(".block, .file, .openvz, .thin-block, .zfs-block, .zfs-thin-block, .zfs-block-compressed, .ceph-block, .lightbit-block, .linbit-drbd-block").hide();
	
	if(typeof(storage) == "undefined" || storage === null){
		storage = "block";
	}
	
	// Replace spaces with - as class name conflicts 
	storage = storage.replace(/\s/g, "-")
	
	$("." + storage).show();
};

// ]]></script>
<div id="form-container" class="card p-2 mx-3">
	<form accept-charset="'.$globals['charset'].'" id="addstorage" method="post" action="" class="form-horizontal">
		<div class="row mx-auto w-100 my-2">
			<div class="col-md-6 mb-2">
				<div class="row mx-auto w-100 gap-2">
					<div class="col-md-12 p-0">
						<label class="control-label">'.__('Name').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Name of the storage which will be shown while creating VPS').'"><i class="fas text-primary fa-info-circle"></i></a></label>
						<input type="text" class="form-control" name="name" id="st_name" size="30" value="'.POSTval('name', '').'" />
					</div>

					<div class="col-md-12 p-0">
						<label class="control-label">'.__('Storage Type').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('LVM is for regular block device storage. Thin LVM is for thin provisioning and allows overselling of disk space').'"><i class="fas text-primary fa-info-circle"></i></a></label>
						<select class="custom-select" name="type" id="type" onchange="storageSet(this.value)">
							<option value="block" '.POSTselect('type', 'block', 1).'>'.storage_type_text('block').'</option>
							<option value="file" '.POSTselect('type', 'file').'>'.storage_type_text('file').'</option>
							<option value="openvz" '.POSTselect('type', 'openvz').'>'.storage_type_text('openvz').'</option>
							<option value="thin block" '.POSTselect('type', 'thin block').'>'.storage_type_text('thin block').'</option>
							<option value="zfs block" '.POSTselect('type', 'zfs block').'>'.storage_type_text('zfs block').'</option>
							<option value="zfs thin block" '.POSTselect('type', 'zfs thin block').'>'.storage_type_text('zfs thin block').'</option>
							<option value="zfs block compressed" '.POSTselect('type', 'zfs block compressed').'>'.storage_type_text('zfs block compressed').'</option>
							<option value="zfs thin block compressed" '.POSTselect('type', 'zfs thin block compressed').'>'.storage_type_text('zfs thin block compressed').'</option>
							<option value="ceph block" '.POSTselect('type', 'ceph block').'>'.storage_type_text('ceph block').'</option>
							<option value="lightbit block" '.POSTselect('type', 'lightbit block').'>'.storage_type_text('lightbit block').'</option>
							<option value="linbit drbd block" '.POSTselect('type', 'linbit drbd block').'>'.storage_type_text('linbit drbd block').'</option>
						</select>
					</div>

					<div class="col-md-12 p-0">
						<label class="control-label block">'.__('Storage Path').'<br>'.__('Volume Group path <b>/dev/VG_NAME</b>').'</label>
						<label class="control-label openvz">'.__('Storage Path').'<br>'.__('Absolute path <b>/vz</b>').'</label>
						<label class="control-label file">'.__('Storage Path').'<br>'.__('Absolute path <b>/FileStorage</b>').'</label>
						<label class="control-label thin-block">'.__('Storage Path').'<br>'.__('Thin Volume Group path <b>/dev/VGName/THINPOOLNAME</b>').'</label>
						<label class="control-label zfs-block zfs-thin-block zfs-block-compressed zfs-thin-block-compressed">'.__('Storage Path').'<br>'.__('ZFS Volume Group path <b>/dev/zvol/YOUR_POOLNAME</b>').'</label>
						<label class="control-label ceph-block">'.__('Storage Path').'<br>'.__('Ceph Block Device path <b>/dev/rbd/CEPH_POOLNAME</b>').'</label>
						<label class="control-label lightbit-block">'.__('Storage Path').'<br>'.__('Lightbit disk storage path <b>/dev/disk/by-id</b>').'</label>
						<label class="control-label linbit-drbd-block">'.__('Storage Path').'<br>'.__('Linbit Storage Path name should be same as Storage name').'</label>
						<input type="text" class="form-control" name="path" size="30" value="'.POSTval('path', '').'" />
					</div>
				</div>
			</div>

			<div class="col-md-6 mb-2">
				<!-- Server Selection -->
				<div class="row mx-auto w-100">
					<label class="control-label">'.__('Server').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The Servers which will be able to use this Storage').'"><i class="fas text-primary fa-info-circle"></i></a></label>';
				
					$POSTserid = @$_POST['serid'];
					$POSTserid = empty($POSTserid) ? array() : $POSTserid;
					
					echo '<select class="form-control" name="serid[]" id="serid" size="8" multiple="multiple">
						<option value="-1" '.(in_array(-1, $POSTserid) ? 'selected="selected"' : '').'>'.__('All Servers').'</option>';
						
						foreach ($servergroups as $k => $v){
							echo '<option class="fhead" value="'.$k.'_group" '.(in_array($k.'_group', $POSTserid) ? 'selected="selected"' : '').'>[Group]&nbsp;'.$v['sg_name'].'</option>';
							
							foreach ($servers as $m => $n){
								if($n['sgid'] == $k){
									echo '<option value="'.$n['serid'].'" '.(in_array((string)$n['serid'], $POSTserid) ? 'selected="selected"' : '').'>&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;'.$n['server_name'].'</option>';
								}
							}
						}
						
						echo '
					</select>
				</div>
			</div>

			<!-- LightBit Project -->
			<div class="col-md-6 mb-2 lightbit-block">
				<label class="control-label">'.__('Lightbit project name').'</label>
				<input type="text" class="form-control" name="project_name" value="'.POSTval('project_name', '').'" />
			</div>

			<!-- File Format -->
			<div class="col-md-6 mb-2 block file thin-block zfs-block zfs-thin-block zfs-block-compressed">
				<label class="control-label">'.__('File Format').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Format of the VPS Disks that will be created in this storage').'"><i class="fas text-primary fa-info-circle"></i></a></label>
				<select class="custom-select" name="format">
					<option value="raw" '.POSTselect('format', 'raw', 1).'>RAW</option>
					<option value="qcow2" '.POSTselect('format', 'qcow2').'>QCOW2</option>
					<option value="vhd" '.POSTselect('format', 'vhd').'>VHD</option>';
					//<option value="vmdk" '.POSTselect('format', 'vmdk').'>VMDK</option>
					//<option value="vpc" '.POSTselect('format', 'vpc').'>VPC (VHD Format)</option>
				echo '</select>
			</div>

			<!-- Oversell -->
			<div class="col-md-6 mb-2">
				<label class="control-label">'.__('Overcommit').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The max disk space you want to Overcommit for this storage. In <b>GB\'s</b>').'"><i class="fas text-primary fa-info-circle"></i></a></label>
				<input type="number" class="form-control" name="oversell" size="30" value="'.POSTval('oversell', '').'" />
			</div>

			<!-- Alert Threshold -->
			<div class="col-md-6 my-2">
				<label class="control-label">'.__('Alert Threshold').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('If the used size crosses this percentage, an email will be sent to the Admin').'"><i class="fas text-primary fa-info-circle"></i></a></label>
				<div>
					<input type="number" class="form-control w-25 float-left" name="alert_threshold" size="10" value="'.POSTval('alert_threshold', '90').'" />
					<label class="py-1 px-2">%</label>
				</div>
			</div>
						
			<!-- Primary Storage -->
			<div class="col-md-6 my-2">
				<div class="row mx-auto w-100">
					<label class="control-label">'.__('Primary Storage').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Will this be the primary storage for the selected nodes. One Node can have only one primary storage').'"><i class="fas text-primary fa-info-circle"></i></a></label>
					<div class="px-4">
						'.get_switch('primary_storage', '', '', array('chk_it' => 1, 'val' => 'primary_storage')).'
					</div>
				</div>
			</div>
		</div>
		
		<div class="notebox" align="left"><b>'.__('Note').' : </b> &nbsp; '.__('<b>ZFS</b> - If you are adding a ZFS storage, please ensure that you know how to handle and manage a ZFS storage. Otherwise you might end up losing all your data and Virtualizor will not be responsible for it !').'</div>
		<br>

		<center>
			<input type="button" name="addstorage" class="go_btn" value="'.__('Add Storage').'" onclick="thin_block();"; />
		</center>
		<br/>
	</form>
</div>';

softfooter();
echo '<div id="processing_symb" style="display: none;"><div class="loader-lg"></div></div>';
}

