<?php

if (isset($_POST['pluginId']) && isnum($_POST['pluginId']))
{
	if ($_sql->query("UPDATE plugins SET pluginStatus = ".(isset($_POST['pluginEnable']) ? 1 : 0)." WHERE pluginId = $_POST[pluginId]"))
		redirect(get_current_link());
	else
		array_push($error, $_locale['plugin_not_updated']);
}

echo "<h1>$_locale[plugins]</h1>";

$pluginsQuery = $_sql->query("SELECT * FROM plugins ORDER BY pluginStatus DESC");
$plugins = array();
if ($pluginsQuery->num_rows)
{
	echo "<table class='designed plugins'>";
	echo "<tr><td>{locale:plugin_name}</td><td>{locale:description}</td><td>{locale:scope}</td><td>{locale:status}</td></tr>";
	while ($data = $pluginsQuery->fetch_assoc())
	{
		$pinfo = './plugins/'.$data['pluginLib'].'/info.json';
		if (file_exists($pinfo))
		{
			$pinfo = (array)json_decode(file_get_contents($pinfo));

			if ($data['pluginStatus'])
				$button = "<button name='pluginDisable' class='orange'>{locale:disable}</button>";
			else $button = "<button name='pluginEnable'>{locale:enable}</button>";

			echo "<tr><td>$pinfo[name]</td><td>$pinfo[description]<td>$pinfo[paths]</td><td><form action='".get_current_link()."' method='post'><input type='hidden' name='pluginId' value='$data[pluginId]'/>$button</form></td></tr>";
		}
		array_push($plugins, $data['pluginLib']);
	}
	echo "</table>";
} else
	echo "<p>$_locale[plugins_empty]</p>";


if ($handle = opendir('./plugins')) {

	while (false !== ($entry = readdir($handle)))
	{
		if (!in_array($entry, $plugins) && file_exists('./plugins/'.$entry.'/info.json'))
			if ($_sql->query("INSERT INTO plugins (pluginLib, pluginStatus) VALUES ('$entry', 0)"))
				array_push($info, $_locale['plugin_added'].$entry);
			else
				array_push($error, $_locale['plugin_not_added'].$entry);
	}

	closedir($handle);
}