<?php

//////////////////////////////////////////////////////////////
//===========================================================
// customscripts_theme.php
//===========================================================
// SOFTACULOUS 
// Version : 1.1
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       10th Jan 2009
// Time:       21:00 hrs
// Site:       http://www.softaculous.com/ (SOFTACULOUS)
// ----------------------------------------------------------
// Please Read the Terms of use at http://www.softaculous.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Inc.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('SOFTACULOUS')){

	die('Hacking Attempt');

}

function customscripts_theme(){

global $theme, $globals, $user, $l, $langs, $skins, $error, $saved, $iscripts, $scripts, $allcatwise, $custom_catwise, $cscripts, $removed;

softheader(__('$0 - Custom Scripts', array(APP)));

echo '<div class="container-fluid  mt-4 mb-3">
		<div class="sai_form">';

error_handle($error);

if(!empty($removed)){
	echo '<div class="alert alert-success text-center"><i class="fas fa-check sai_icons mr-2"></i>
	'.__('The script was removed successfully.').'</div>';	
}

//The necessary JavaScripts
echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
	ids = new Array('.implode(', ', array_keys($cscripts)).');
	function confirm_remove(sid){	
		return confirm("'.__js('Are you sure you want to remove this custom script ?').'");
	};
// ]]></script>';

echo '<form accept-charset="'.$globals['charset'].'" name="updatesoft" method="post" action="">
	<div class="sai-card p-4 mt-2">
		<h2 class="sai-heading">'.__('Custom Scripts').'</h2>
		<hr class = "sai_main_head_hr">
		<div class="table-responsive">
			<table class="table mb-0">
				<thead class="tables-cols">
					<th width="10%">'.__('Name').'</th>
					<th width="35%" class="text-center">'.__('Description').'</th>
					<th width="15%" class="text-center">'.__('Version').'</th>
					<th width="4%" class="text-center">'.__('Action').'</th>
				</thead>
			</table>';
			foreach($custom_catwise as $cattype => $catscripts){

				if(empty($catscripts)){
					continue;
				}
				
				echo '<table class="table mb-0">
					<tr>
						<td colspan="7" class="sai-script-type text-center">
							<h4>'.$l['type_'.$cattype].'</h4>
						</td>
					</tr>
				</table>';
				
				foreach($catscripts as $cat => $cv){
				
				echo '<table class="soft-table-list table">
						<thead class="sai-sub-type mt-4">
							<td colspan="6">
								'.$l['cat_'.$cattype.'_'.$cat].'
							</td>
						</thead>';
						$i=0;
						foreach($cv as $k => $v){
						
							echo '<tr class="'.($i%2 == 0 ? "altrow" : "naltrow").'">
								<td class="sai_bboxtxt align-middle" width="15%">'.$v['name'].'</td>
								<td class="sai_bboxtxt align-middle" width="55%">'.$v['desc'].'</td>
								<td class="sai_bboxtxt text-center align-middle" width="10%">'.$v['ver'].'</td>
								<td class="sai_bboxtxt text-center align-middle" width="10%">
									<a class="text-decoration-none mr-2" href="'.$globals['ind'].'act=customscripts&sact=edit&sid='.$k.'" title="'.__('Edit').'">
										<i class="fas fa-pencil-alt px-1 sai_icons" style="color:#FF5D00;"></i>
									</a>
									<a href="'.$globals['ind'].'act=customscripts&remid='.$k.'" title="'.__('Remove').'" class="text-decoration-none mr-2">
										<i class="fas fa-trash-alt sai_icons" style="color:red;" onclick="return confirm_remove('.$k.');"></i>
									</a>
								</td>
							</tr>';
							$i++;		
						}
					echo '</table>';
				}
				
			}
			echo '</div>
			<div class="text-center py-2">
				<input type="button" name="add" onclick="window.location=\''.$globals['ind'].'act=customscripts&sact=add\'" value="'.__('Add Custom Scripts').'" class="flat-butt" />
				<a href="http://www.softaculous.com/docs/Making_Custom_Package" target="_blank" class="mt-3 d-block">
					<span class="sai_bboxtxt">'.__('Documentation to make a Script Package').'</span>
				</a>
			</div>
		</div>
	</div>
	</div>
	'.csrf_display().'
	</form><br />';

softfooter();

}

function edit_cs_theme(){

global $user, $globals, $scripts, $iscripts, $l, $theme, $error, $saved, $report, $softid, $cs, $categories, $edited, $cscripts, $sid;

softheader(__('$0 - Custom Scripts', array(APP)));

echo '<form accept-charset="'.$globals['charset'].'" name="editcs" method="post" action="" class="form-horizontal">

<div class="container-fluid">
	<div class="container">
		<div class="row my-5">
			<div class="col-12 col-lg-10 mx-auto">
				<div class="sai-card p-4">
					<div class="sai_main_head">
						<i class="fas sai-apps sai-icon d-inline-block mr-1"></i>
						<h2 class="sai-heading d-inline-block mb-2 position-relative" style="top:-4px;">'.__('Edit Custom Script').'</h2>
					</div>
					<hr class="sai_main_head_hr"/>
					<div class="sai_form">';
		
					error_handle($error);
					
					if(!empty($edited)){
						echo '<div class="alert alert-success text-center"><a href="#close" class="close" data-dismiss="alert">&times;</a>
						<i class="fas fa-check sai_icons mr-2"></i>
						'.__('The Custom Script Details were saved successfully.').'</div>';
					}
					
					echo '
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="csname" class="sai-label d-block">'.__('Script Name').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The name of the custom script').'</span>
							<input type="text" name="csname" class="form-control" id="csname" size="30" value="'.aPOSTval('csname', $cs['name']).'" />
						</div>
						<div class="col-12 mb-3 col-lg-6">
							<label for="softname" class="sai-label d-block">'.__('$0 Name', array(APP)).'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The folder name of the script package. Should not exceed 7 characters.').'</span>
							<input type="text" name="softname" class="form-control" id="softname" size="30" value="'.aPOSTval('softname', $cs['softname']).'" />
						</div>
					</div>
					
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="desc" class="sai-label d-block">'.__('Script Description').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('A short description of the script.').'</span>
							<input type="text" name="desc" class="form-control" id="desc" size="30" value="'.aPOSTval('desc', $cs['desc']).'" />
						</div>
						<div class="col-12 mb-3 col-lg-6">
							<label for="ver" class="sai-label d-block">'.__('Script Version').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The version of this custom script').'</span>
							<input type="text" name="ver" class="form-control" id="ver" size="30" value="'.aPOSTval('ver', $cs['ver']).'" />
						</div>
					</div>
					
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="cat" class="sai-label d-block">'.__('Category').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('Please select the category').'</span>
							<select name="cat" id="cat" class="form-control">';
							foreach($categories as $k => $v){
								echo '<option value="'.$k.'" '.($cs['cat'] == $k ? 'selected="selected"' : '').'>'.$l['cat_php_'.$k].'</option>';
							}
						echo '</select>
						</div>';
		
						if(!empty($cscripts)){
							echo '
							<div class="col-12 mb-3 col-lg-6">
								<label for="parent" class="sai-label d-block">'.__('Parent').'</label>
								<span class="sai_exp2 d-block mb-1">'.__('If you would like to add this script as a child please choose a parent').'</span>
								<select name="parent" id="parent" class="form-control">
									<option value="">'.__('&mdash;  No Parent (Default)  &mdash;').'</option>';
									foreach($cscripts as $ck => $cv){
										
										// Cannot be a parent of self
										if($sid == $ck){
											continue;
										}
										
										// If it is already a child it cannot be a parent
										if(!empty($cv['parent'])){
											continue;
										}
										
										echo '<option value="'.$ck.'" '.(POSTval('parent', @$cscripts[$sid]['parent']) == $ck ? 'selected="selected"' : '').'>'.$cv['name'].'</option>';
									}
								echo '</select>
							</div>';
						}
				echo '</div>
					<p class="mt-3 text-center">
						'.csrf_display().'
						<input type="submit" name="edit_submit" class="flat-butt" value="'.__('Edit Script').'"/>
					</p>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</form>';

softfooter();

}


function add_cs_theme(){

global $user, $globals, $scripts, $iscripts, $l, $theme, $error, $saved, $report, $softid, $cs, $categories, $added, $cscripts;

softheader(__('$0 - Custom Scripts', array(APP)));

echo '<form accept-charset="'.$globals['charset'].'" name="addcs" method="post" action="" class="form-horizontal">

<div class="container-fluid">
	<div class="container">
		<div class="row my-5">
			<div class="col-12 col-lg-10 mx-auto">
				<div class="sai-card p-4">
					<div class="sai_main_head">
						<i class="fas sai-apps sai-icon d-inline-block mr-1"></i>
						<h2 class="sai-heading d-inline-block mb-2 position-relative" style="top:-4px;">'.__('Add a Custom Script').'</h2>
					</div>
					<hr class="sai_main_head_hr"/>
					<div class="sai_form">';
		
					error_handle($error);
					
					if(!empty($added)){
						echo '<div class="alert alert-success text-center"><a href="#close" class="close" data-dismiss="alert">&times;</a>
						<i class="fas fa-check sai_icons mr-2"></i>
						'.__('The Custom Script was added successfully.').'</div>';
					}
					
					if(!empty($edited)){
						echo '<div class="alert alert-success text-center"><a href="#close" class="close" data-dismiss="alert">&times;</a>
						<i class="fas fa-check sai_icons mr-2"></i>
						'.__('The Custom Script Details were saved successfully.').'</div>';
					}
					
					echo '
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="csname" class="sai-label d-block">'.__('Script Name').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The name of the custom script').'</span>
							<input type="text" name="csname" class="form-control" id="csname" size="30" value="'.aPOSTval('csname', '').'" />
						</div>
						<div class="col-12 mb-3 col-lg-6">
							<label for="softname" class="sai-label d-block">'.__('$0 Name', array(APP)).'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The folder name of the script package. Should not exceed 7 characters.').'</span>
							<input type="text" name="softname" class="form-control" id="softname" size="30" value="'.aPOSTval('softname', '').'" />
						</div>
					</div>
					
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="desc" class="sai-label d-block">'.__('Script Description').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('A short description of the script.').'</span>
							<input type="text" name="desc" class="form-control" id="desc" size="30" value="'.aPOSTval('desc', '').'" />
						</div>
						<div class="col-12 mb-3 col-lg-6">
							<label for="ver" class="sai-label d-block">'.__('Script Version').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('The version of this custom script').'</span>
							<input type="text" name="ver" class="form-control" id="ver" size="30" value="'.aPOSTval('ver', '').'" />
						</div>
					</div>
					
					<div class="row mb-3">
						<div class="col-12 mb-3 col-lg-6">
							<label for="cat" class="sai-label d-block">'.__('Category').'</label>
							<span class="sai_exp2 d-block mb-1">'.__('Please select the category').'</span>
							<select name="cat" id="cat" class="form-control">';
							foreach($categories as $k => $v){
								echo '<option value="'.$k.'" '.(optPOST('cat') == $k ? 'selected="selected"' : '').'>'.$l['cat_php_'.$k].'</option>';
							}
						echo '</select>
						</div>';
		
						if(!empty($cscripts)){
							echo '
							<div class="col-12 mb-3 col-lg-6">
								<label for="parent" class="sai-label d-block">'.__('Parent').'</label>
								<span class="sai_exp2 d-block mb-1">'.__('If you would like to add this script as a child please choose a parent').'</span>
								<select name="parent" id="parent" class="form-control">
									<option value="" '.(empty($_POST['parent']) ? 'selected="selected"' : '').'>'.__('&mdash;  No Parent (Default)  &mdash;').'</option>';
									foreach($cscripts as $ck => $cv){
										
										// If it is already a child it cannot be a parent
										if(!empty($cv['parent'])){
											continue;
										}
										
										echo '<option value="'.$ck.'" '.(optPOST('parent') == $ck ? 'selected="selected"' : '').'>'.$cv['name'].'</option>';
									}
								echo '</select>
							</div>';
						}
				echo '</div>
					<p class="mt-3 text-center">
						'.csrf_display().'
						<input type="submit" name="add_submit" class="flat-butt" value="'.__('Add Script').'"/>
					</p>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</form>';

softfooter();

}

?>