<?php
include_once('RCCWP_CustomWriteModule.php');

class RCCWP_CustomWriteModulePage
{
	function Content($customWriteModule = null)
	{
		$customWriteModuleName = "";
		
		if ($customWriteModule != null)
		{
			$customWriteModuleName = $customWriteModule->name;
		?>
		<input type="hidden" name="custom-write-module-id" value="<?php echo $_REQUEST['custom-write-module-id']?>" />
		<?php
		}
		
  		?>

		<?php
		if (isset($_GET['err_msg'])) :
			switch ($_GET['err_msg']){
				case -1:
		?>
			<div class="updated fade"><p> A module with the same name already exists. Please choose a different name.</p></div>

		<?php
			}
		endif;
		?>

		<table class="form-table" width="100%" border="0" cellspacing="0" cellpadding="6">
		<tbody>

		<tr valign="top">
			<th scope="row"  align="right">Name:</th>
			<td><input name="custom-write-module-name" id="custom-write-module-name" size="40" type="text" value="<?php echo $customWriteModuleName?>" /></td>
		</tr>

		<tr valign="top">
			<th scope="row"  align="right">Description:</th>
			<td><textarea name="custom-write-module-description" id="custom-write-module-description" rows="3" cols="40"><?php echo $customWriteModule->description?></textarea></td>
		</tr>

		</tbody>
		</table>
		
		<?php
	}
	
	function Edit()
	{
		$customWriteModule = RCCWP_CustomWriteModule::Get((int)$_REQUEST['custom-write-module-id']);
		?>



		<div class="wrap">
		
		<h2>Edit Custom Write Module</h2>
		
		<form action="<?php echo RCCWP_ManagementPage::GetCustomWriteModuleEditUrl($_REQUEST['custom-write-module-id']) ?>" method="post" id="edit-custom-write-module-form">
		
		<?php
		RCCWP_CustomWriteModulePage::Content($customWriteModule);
		?>
		
		<p class="submit" >
			<input name="cancel-edit-custom-write-module" type="submit" id="cancel-edit-custom-write-module" value="<?php _e('Cancel'); ?>" /> 
			<input name="submit-edit-custom-write-module" type="submit" id="submit-edit-custom-write-module" value="<?php _e('Update'); ?>" />
		</p>
		</form>
		
		</div>
		
		<?php
	}
	
	
	function View($param = 23)
	{
		if(isset($_GET['custom-write-module-id']) && !empty($_GET['custom-write-module-id']) )
			$customWriteModuleId = (int)$_GET['custom-write-module-id'];
		if(isset($_POST['custom-write-module-id']) && !empty($_POST['custom-write-module-id']) )
			$customWriteModuleId = (int)$_POST['custom-write-module-id'];

		$customWriteModule = RCCWP_CustomWriteModule::Get($customWriteModuleId);
		?>

		<div class="wrap">

		<h2>Custom Write Module Info</h2>
		<h4><a href="?page=FlutterManageModules&view-modules=1"> « Back to Custom Modules List</a></h4>
		<form action="" method="post" id="view-write-module-form">
		
		<input type="hidden" name="custom-write-module-id" value="<?php echo $customWriteModuleId?>" />
			

  		<table class="form-table" width="100%" border="0" cellspacing="0" cellpadding="6">
  		<tbody>
  		<tr>
			<th scope="row" align="right">Name:</th>
			<td><?php echo $customWriteModule->name?></td>
		</tr>
		<tr>
			<th scope="row" align="right">Description:</th>
			<td><?php echo $customWriteModule->description?></td>
		</tr>
  		</tbody>
  		</table>
		  
		<script type="text/javascript" language="javascript">
			function confirmBeforeDelete()
			{
				return confirm("Are you sure you want to delete this module? Please notice that all the template files of this module will be deleted too.");							
			}
		</script>
		<p class="submit" >
			<input name="edit-custom-write-module" type="submit" id="edit-custom-write-module" value="Edit Module" />
			<input onclick="return confirmBeforeDelete();" name="delete-custom-write-module" type="submit" id="delete-custom-write-module" value="Delete Module" />
		</p>
		
		</form>
		
		<form action="" method="post" id="view-module-duplicates">
			<br /><br />
			<h3>Module Duplicates for Layout</h3>
			<div class="tablenav"><div class="alignright">
				<input name="create-module-duplicate" type="submit" id="create-module-duplicate" value="Create Duplicate" class="button-secondary"  />
			</div></div>
			<br class="clear"/>

			<table cellpadding="3" cellspacing="3" width="100%" class="widefat">
			<thead>
			<tr>
				<th scope="col">Name</th>
				<th scope="col" colspan="2">Action</th>
			</tr>
			</thead>
			<tbody>
			<?php
			$customWriteModuleDuplicates = RCCWP_ModuleDuplicate::GetCustomModulesDuplicates($customWriteModuleId);
			foreach ($customWriteModuleDuplicates as $customWriteModuleDuplicate) :
				$class = $class == '' ? 'alternate' : '';
			?>
				<tr class="<?php echo $class?>">
					<td><?php echo $customWriteModuleDuplicate->duplicate_name?></td>
					<td><a href="<?php echo RCCWP_ManagementPage::GetModuleDuplicateEditUrl($customWriteModuleId, $customWriteModuleDuplicate->duplicate_id)?>" class="edit">Rename</a></td>
					<td><a href="<?php echo RCCWP_ManagementPage::GetModuleDuplicateDeleteUrl($customWriteModuleId, $customWriteModuleDuplicate->duplicate_id)?>" class="delete">Delete</a></td>
				</tr>
			<?php
			endforeach;
			?>
			</tbody>
			</table>
		  
		</form>

		</div>
		
		<?php
	}

	function Import()
	{
		include_once('RCCWP_CustomWriteModule.php');
		
		if(isset($_FILES['import-module-file']) && !empty($_FILES['import-module-file']['tmp_name']) ) {
			$zipFilePath = $_FILES['import-module-file']['tmp_name'];
		}
		else {
			die("Error uploading file!");
		}

		$moduleName = basename($_FILES['import-module-file']['name'], ".zip");
		$moduleID = RCCWP_CustomWriteModule::Import($zipFilePath, $moduleName);
		unlink($zipFilePath);
		
		echo "<h3>The module was imported successfuly.</h3>";
		echo '<p><a href="' . RCCWP_ManagementPage::GetCustomWriteModuleEditUrl($moduleID).'"> Click here </a> to edit the module. </p>';
		
	}
	
}
?>