<?php

require_once('include/admin.inc.php');

auth_redirect(); // Require logged in user to access this page.

if( isset($_GET['delete']) && (int)$_GET['delete'] ) {
	check_nonce('delete-metatype-'.(int)$_GET['delete']);
	if(! $mtdb->query( 'DELETE FROM meta_t WHERE id=' . (int)$_GET['delete'] ) )
	{
		adminlog("Error on deleting metatype ".(int)$_GET['delete'], MTS_TYPE_META, MTA_DELETE, E_WARNING);
		mtdie("Error on update: ". htmlentities(mysqli_error()));
	}
	$info.='<p>Deleted metatype successfully.<p>';
	adminlog("Metatype ".(int)$_GET['delete']." deleted.", MTS_TYPE_META, MTA_DELETE);
}

if( isset($_POST['action']) && $_POST['action'] == 'new_meta' ) {
	check_nonce('new-metatype');

	$name = trim($_POST['name']);

	if( check_type_name( $name ) ) {
		if(! $mtdb->query( 'INSERT INTO meta_t(name) VALUES("'. mysqli_real_escape_string($mtdb->link, $name) . '")' ) )
		{
			adminlog("Error on inserting metatype ".(int)$_GET['delete'], MTS_TYPE_META, MTA_INSERT, E_WARNING);
			mtdie("Error on insertion: ". htmlentities(mysqli_error()));
		}
	}
	$info.='<p>New metatype created successfully.<p>';
	adminlog("Metatype ".$name." added.", MTS_TYPE_META, MTA_ADD);
}

if( isset($_POST['action']) && $_POST['action'] == 'edit_meta' ) {
	check_nonce('save-metatype-'.(int)$_POST['type_id']);

	$name = trim($_POST['name']);

	if( check_type_name( $name ) ) {
		if(! $mtdb->query( 'UPDATE meta_t SET name = "' . mysqli_real_escape_string($mtdb->link, $name) . '" WHERE id=' . (int)$_POST['type_id']) )
		{
			adminlog("Error updating metatype ".(int)$_GET['delete'], MTS_TYPE_META, MTA_UPDATE, E_WARNING);
			mtdie("Error on update: ". htmlentities(mysqli_error()));
		}
	}
	$info.='<p>Changes to metatype saved successfully.<p>';
	adminlog("Metatype ".$name." updated.", MTS_TYPE_META, MTA_UPDATE);
}

//get all metatypes
$metas = $mtdb->getAll("SELECT id, name FROM meta_t");

adminhead('Metatypes');
adminmenu();
?>
<h2>Metatype Management</h2>
<p>Make changes to the metatypes which organize the types.</p>

<table class="widefat">
	<thead>
	<tr>
	<th scope="col" style="text-align: center;">Metatype #</th>
	<th scope="col">Name</th>
	<th scope="col"></th>
	<th scope="col"></th>
	</tr>
	</thead>

	<tbody id="the-list">
	<?php
	$alternate=false;
	foreach( $metas as $s ) {
		$alternate=!$alternate;
		?>
		<tr id="type-<?php echo $s->id; ?>" <?php if($alternate) echo 'class="alternate"'; ?>>
		 <th scope="row" style="text-align: center;"><?php echo $s->id; ?></th>
		 <td><?php echo htmlentities($s->name); ?></td>
		 <td style="text-align: center;"><a href="edit-metatype.php?edit=<?php echo (int)$s->id; ?>">Edit</a></td>
		 <td style="text-align: center;"><a class="delete" href="?delete=<?php echo (int)$s->id; ?>">Delete</a></td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>

<form enctype="multipart/form-data" name="create-user" id="create-user" action="manage-metatypes.php" method="post">
<?php nonce_field('new-metatype'); ?>
<input type="hidden" name="action" value="new_meta" />

<h2>Create New Metatype</h2>
<div class="narrow">
	<table class="editform" width="100%" cellspacing="2" cellpadding="5">
	<tr>
	<th scope="row" width="33%">Name</th>
	<td width="66%"><input name="name" type="text" id="name" value="" /></td>
	</tr>
	</table>

	<p class="submit"><input type="submit" value="Create &raquo;" name="submit" /></p>
</div>
</form>

<?php
adminfooter();
?>
