<?php
/*=====================================================================*\
|| ###################################################################
|| # Bugdar
|| # Copyright ©2002-2007 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

// TODO - add API for product.php

require_once('./global.php');
require_once('./includes/functions_datastore.php');

NavLinks::productsAdd();
$navigator->set_focus('tab', 'products', null);

if (!can_perform('canadminversions'))
{
	admin_login();
}

function construct_option_list($type_display, $type, $id, $edit, $addcomponent, $addversion, $delete, $displayorder)
{
	global $bugsys;

	$type = ($type == 'component' ? 'product' : $type);
	$type_action = (($type == 'version') ? 'versionid' : 'productid');

	if ($edit)
	{
		$opt .= '<option value="product.php?do=edit' . $type . '&' . $type_action . '=' . $id . '">' . sprintf(T('Edit %1$s'), $type_display) . '</option>';
	}

	if ($delete)
	{
		$opt .= '<option value="product.php?do=delete' . $type . '&' . $type_action . '=' . $id . '">' . sprintf(T('Delete %1$s'), $type_display) . '</option>';
	}

	if ($addcomponent)
	{
		$opt .= '<option value="product.php?do=addproduct&productid=' . $id . '">' . T('Add Component') . '</option>';
	}

	if ($addversion)
	{
		$opt .= '<option value="product.php?do=addversion&productid=' . $id . '">' . T('Add Version') . '</option>';
	}

	$type = strtolower($type);

	$name = $type . '_' . $id;

	$displayorder = (($displayorder != -1) ? '<input type="text" name="displayorder[' . $name . ']" value="' . $displayorder . '" size="4" class="input" /> ' : '');

	return $displayorder . '<select id="' . $name . '" name="' . $name . '" onchange="exec_action(\'' . $name . '\')" class="input">' . $opt . '</select> <input type="button" class="button" name="gobutton" value="  ' . T('Go') . '  " onclick="exec_action(\'' . $name . '\')" />';
}

// ###################################################################

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

// ###################################################################

if ($_REQUEST['do'] == 'killversion')
{
	$bugsys->input_clean('versionid', TYPE_UINT);
	$db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE versionid = " . $bugsys->in['versionid']);
	$db->query("DELETE FROM " . TABLE_PREFIX . "bug WHERE version = " . $bugsys->in['versionid']);

	build_versions();

	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'deleteversion')
{
	$admin->page_confirm(T('Are you sure you want to delete this version? Doing so will do <strong>delete all the bugs with this version</strong>.'), 'product.php', 'killversion', array('versionid' => $bugsys->input_clean('versionid', TYPE_UINT)));
}

// ###################################################################

if ($_REQUEST['do'] == 'insertversion')
{
	$db->query("INSERT INTO " . TABLE_PREFIX . "version (productid, version, displayorder, obsolete) VALUES (" . $bugsys->input_clean('productid', TYPE_UINT) . ", '" . $bugsys->input_escape('version') . "', " . $bugsys->input_clean('displayorder', TYPE_UINT) . ", " . $bugsys->input_clean('obsolete', TYPE_UINT) . ")");
	build_versions();
	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'addversion')
{
	NavLinks::productsEdit($bugsys->input_clean('productid', TYPE_UINT));
	$navigator->set_focus('link', 'products-edit-version', 'products-edit');

	if ($bugsys->in['productid'] != -1)
	{
		$product = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $bugsys->in['productid']);
		if (!is_array($product))
		{
			$admin->error(L_INVALID_ID);
		}
	}
	else
	{
		$bugsys->in['productid'] = 0;
	}

	$admin->page_start(T('Add Version'));

	$admin->form_start('product.php', 'insertversion');
	$admin->form_hidden_field('productid', $bugsys->in['productid']);
	$admin->table_start();
	$admin->table_head(T('Add New Version'));
	$admin->row_input(T('Version Number<div><dfn>This is the version string for this product.</dfn></div>'), 'version');
	$admin->row_input(T('Display Order<div><dfn>The order in which the versions are displayed.</dfn></div>'), 'displayorder');
	$admin->row_yesno(T('Obsolete<div><dfn>An obsolete version cannot have new bugs filed against it, but existing ones can be set to it.</dfn></div>'), 'obsolete', false);
	$admin->row_submit();
	$admin->table_end();
	$admin->form_end();

	$admin->page_end();
}

// ###################################################################

if ($_REQUEST['do'] == 'updateversion')
{
	if (empty($bugsys->in['version']))
	{
		$admin->error(T('Please fill in a version number.'));
	}

	$db->query("UPDATE " . TABLE_PREFIX . "version SET version = '" . $bugsys->input_escape('version') . "', displayorder = " . $bugsys->input_clean('displayorder', TYPE_UINT) . ", obsolete = " . $bugsys->input_clean('obsolete', TYPE_UINT) . " WHERE versionid = " . $bugsys->input_clean('versionid', TYPE_UINT));
	build_versions();

	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'editversion')
{
	$admin->page_start(T('Edit Version'));

	$version = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "version WHERE versionid = " . $bugsys->input_clean('versionid', TYPE_UINT));
	if (!is_array($version))
	{
		$admin->error(L_INVALID_ID);
	}

	$admin->form_start('product.php', 'updateversion');
	$admin->form_hidden_field('versionid', $version['versionid']);
	$admin->table_start();
	$admin->table_head(sprintf(T('Edit Version - %1$s'), $version['version']));
	$admin->row_input(T('Version Number<div><dfn>This is the version string for this product.</dfn></div>'), 'version', $version['version']);
	$admin->row_input(T('Display Order<div><dfn>The order in which the versions are displayed.</dfn></div>'), 'displayorder', $version['displayorder']);
	$admin->row_yesno(T('Obsolete<div><dfn>An obsolete version cannot have new bugs filed against it, but existing ones can be set to it.</dfn></div>'), 'obsolete', $version['obsolete']);
	$admin->row_submit();
	$admin->table_end();
	$admin->form_end();

	$admin->page_end();
}

// ###################################################################

if ($_REQUEST['do'] == 'killproduct')
{
	$bugsys->input_clean('productid', TYPE_UINT);
	$allprods = $db->query("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $bugsys->in['productid'] . " OR parentid = " . $bugsys->in['productid']);
	while ($prod = $db->fetch_array($allprods))
	{
		$list[] = $prod['productid'];
	}

	$db->query("DELETE FROM " . TABLE_PREFIX . "product WHERE productid IN (" . implode(', ', $list) . ")");
	$db->query("DELETE FROM " . TABLE_PREFIX . "version WHERE productid IN (" . implode(', ', $list) . ")");
	$db->query("DELETE FROM " . TABLE_PREFIX . "bug WHERE product IN (" . implode(', ', $list) . ")");

	build_products();

	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'deleteproduct')
{
	$admin->page_confirm(T('Are you sure you want to delete this product and all of it\'s versions and components <strong>and any bugs that have been assigned those products or components</strong>?'), 'product.php', 'killproduct', array('productid' => $bugsys->input_clean('productid', TYPE_UINT)));
}

// ###################################################################

if ($_REQUEST['do'] == 'insertproduct')
{
	if (empty($bugsys->in['title']))
	{
		$admin->error(T('Please go back and fill in the title field.'));
	}

	$db->query("
		INSERT INTO " . TABLE_PREFIX . "product
			(title, parentid, description, displayorder)
		VALUES
			('" . $bugsys->input_escape('title') . "', " . $bugsys->input_clean('parentid', TYPE_UINT) . ",
			'" . $bugsys->input_escape('description') . "', " . $bugsys->input_clean('displayorder', TYPE_UINT) . "
		)"
	);
	build_products();

	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'addproduct')
{
	if ($bugsys->input_clean('productid', TYPE_UINT))
	{
		NavLinks::productsEdit($bugsys->in['productid']);
	}
	else
	{
		NavLinks::productsAdd();
	}
	$navigator->set_focus('link', 'products-add', 'products');

	$admin->page_start(T('Add New Product'));

	$admin->form_start('product.php', 'insertproduct');
	$admin->form_hidden_field('parentid', $bugsys->in['productid']);
	$admin->table_start();
	$admin->table_head(T('Add Product'));
	$admin->row_input(T('Title'), 'title');
	$admin->row_textarea(T('Description<div><dfn>A short description of this product.</dfn></div>'), 'description');
	$admin->row_input(T('Display Order<div><dfn>The order in which the products are displayed.</dfn></div>'), 'displayorder');
	$admin->row_submit();
	$admin->table_end();
	$admin->form_end();

	$admin->page_end();
}

// ###################################################################

if ($_REQUEST['do'] == 'updateproduct')
{
	if (empty($bugsys->in['title']))
	{
		$admin->error(T('Please go back and fill in the title field.'));
	}

	if (empty($bugsys->in['productid']))
	{
		$admin->error(L_INVALID_ID);
	}

	$db->query("UPDATE " . TABLE_PREFIX . "product SET title = '" . $bugsys->input_escape('title') . "', description = '" . $bugsys->input_escape('description') . "', displayorder = " . $bugsys->input_clean('displayorder', TYPE_UINT) . " WHERE productid = " . $bugsys->input_clean('productid', TYPE_UINT));
	build_products();

	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'editproduct')
{
	NavLinks::productsEdit($bugsys->input_clean('productid', TYPE_UINT));
	$navigator->set_focus('link', 'products-edit', 'products-edit');

	$admin->page_start(T('Edit Product'));

	$product = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "product WHERE productid = " . $bugsys->in['productid']);
	if (!is_array($product))
	{
		$admin->error(L_INVALID_ID);
	}

	$admin->form_start('product.php', 'updateproduct');
	$admin->form_hidden_field('productid', $product['productid']);
	$admin->table_start();
	$admin->table_head(sprintf(T('Edit Product - %1$s'), $product['title']));
	$admin->row_input(T('Title'), 'title', $product['title']);
	$admin->row_textarea(T('Description<div><dfn>A short description of this product.</dfn></div>'), 'description', $product['description']);
	$admin->row_input(T('Display Order<div><dfn>The order in which the products are displayed.</dfn></div>'), 'displayorder', $product['displayorder']);
	$admin->row_submit();
	$admin->table_end();
	$admin->form_end();

	$admin->page_end();
}

// ###################################################################

if ($_POST['do'] == 'displayorder')
{
	$bugsys->input_clean('displayorder', TYPE_UINT);
	if (is_array($bugsys->in['displayorder']))
	{
		foreach ($bugsys->in['displayorder'] AS $namebit => $displayorder)
		{
			$name = explode('_', $namebit);
			if ($name[0] == 'product' OR $name[0] == 'version')
			{
				$id = $bugsys->clean($name[1], TYPE_UINT);
				$order = $displayorder;
				$db->query("UPDATE " . TABLE_PREFIX . "$name[0] SET displayorder = $order WHERE $name[0]id = $id");
			}
		}
		build_products();
		build_versions();
	}
	$admin->redirect('product.php?do=modify');
}

// ###################################################################

if ($_REQUEST['do'] == 'modify')
{
	$navigator->set_focus('link', 'products-manage', 'products');

	$admin->page_start(T('Products and Versions'));

	$javascript = <<<EOF
<script type="text/javascript">
<!--
	function exec_action(name)
	{
		window.location = document.getElementById(name).value;
	}
//-->
</script>
EOF;

	$admin->page_code($javascript);

	$admin->form_start('product.php', 'displayorder');

	$products_get = $db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
	$products = array();
	while ($prod = $db->fetch_array($products_get))
	{
		if (!$prod['parentid'])
		{
			$products["$prod[productid]"] = $prod;
		}
		else
		{
			$components["$prod[parentid]"][] = $prod;
		}
		$version["$prod[productid]"] = array();
	}

	$versions_get = $db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder ASC");
	$versions = array();
	while ($vers = $db->fetch_array($versions_get))
	{
		$versions["$vers[productid]"]["$vers[versionid]"] = $vers;
	}

	$admin->table_start();
	$admin->table_head(T('Products / Versions'));

	// Handle our global versions
	if (is_array($versions['0']))
	{
		$admin->row_text(T('Global Versions'), construct_option_list(T('Product'), 'product', -1, 0, 0, 1, 0, -1), 'middle', 2, 'alt3');
		foreach ($versions['0'] AS $version)
		{
			$admin->row_text('-- ' . $version['version'], construct_option_list(T('Version'), 'version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
		}
	}

	// Now let's do the rest of the versions
	foreach ($products AS $product)
	{
		// Product
		$admin->row_text("<a href=\"product.php?do=editproduct&amp;productid=$product[productid]\">$product[title]</a>", construct_option_list(T('Product'), 'product', $product['productid'], 1, 1, 1, 1, $product['displayorder']), 'middle', 2, 'alt3');

		// Versions
		if (is_array($versions["$product[productid]"]))
		{
			foreach ($versions["$product[productid]"] AS $version)
			{
				$admin->row_text('-- ' . $version['version'], construct_option_list(T('Version'), 'version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
			}
		}

		// Components
		if (is_array($components["$product[productid]"]))
		{
			foreach ($components["$product[productid]"] AS $comp)
			{
				$admin->row_text("-- <a href=\"product.php?do=editproduct&amp;productid=$comp[productid]\">$comp[title]</a>", construct_option_list(T('Component'), 'component', $comp['productid'], 1, 0, 1, 1, $comp['displayorder']), 'middle', 2, 'alt1');

				// Component versions
				if (is_array($versions["$comp[productid]"]))
				{
					foreach ($versions["$comp[productid]"] AS $version)
					{
						$admin->row_text('---- ' . $version['version'], construct_option_list(T('Version'), 'version', $version['versionid'], 1, 0, 0, 1, $version['displayorder']), 'middle', 2, 'alt2');
					}
				}
			}
		}
	}

	$admin->row_submit(null, T('Save Display Order'), null);

	$admin->table_end();

	$admin->form_end();

	$admin->page_end();
}

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>