Location: PHPKode > scripts > Impleo Music Collection Script > impleo/admin/add-record.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

session_start();

// Login control:
if (!$_SESSION['login'] == 1)
{
	header("Location: ./");
	exit;
}

header("content-type:text/html;charset=utf-8");

require_once(dirname(__FILE__) . '/../classes/ImpleoTemplate.class.php');
require_once(dirname(__FILE__) . '/../classes/Factory.class.php');
require_once(dirname(__FILE__) . '/../classes/Image.class.php');

$IMAGE_PATH = "../images/uploads/";

$tpl = new ImpleoTemplate("add-record.tpl");
$factory = Factory::getInstance();

$arrDetails = $factory->getDetailInfo();
$arrGeneralSettings = $factory->getGeneralSettings();

$posted = false;
if (isset($_POST['send']))
{
	$posted = true;
	$tpl->assign('postValues', $_POST);
	
	$boolErrors = true;
	$arrErrors = validate($arrDetails);
	
	if (count($arrErrors) == 0)
	{
		$boolErrors = false;
		$arrInsertDetail = array();
		foreach ($arrDetails as $arrDetail)
		{
			if (strlen($_POST[$arrDetail['key']]) > 0)
				$arrInsertDetail[$arrDetail['name']] = $_POST[$arrDetail['key']];
		}
		
		// Upload image
		$imgName = null;
		if (strlen($_FILES['image']['name']) > 0)
		{
			$image = new Image($_FILES['image']['tmp_name'], $_FILES['image']['name']);
			if ($image->getWidth() > $arrGeneralSettings['image_width'])
				 $image->resize($arrGeneralSettings['image_width']);
			$imgName = $image->saveImage($IMAGE_PATH);
		}
		
		$factory->insertRecord($_POST['artist'], $_POST['title'], $_POST['year'], $arrInsertDetail, $imgName);
	}
	else
	{
		$tpl->assign('errors', $arrErrors);
	}
}

// Validate form input:
function validate($arrDetails)
{
    $arrAllowedImageTypes = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/pjpeg');

	$arrErrors = array();
	if (empty($_POST['artist']))
		$arrErrors[] = "Artist must be specified.";
	if (empty($_POST['title']))
		$arrErrors[] = "Title must be specified.";
	if (empty($_POST['year']))
		$arrErrors[] = "Release year must be specified.";
	else if (!is_numeric($_POST['year']))
		$arrErrors[] = "Release year must be numeric.";
	foreach ($arrDetails as $arrDetail)
	{
		if (empty($_POST[$arrDetail['key']]) && $arrDetail['required'] == 1)
			$arrErrors[] = "{$arrDetail['name']} must be specified";
	}
	if (strlen($_FILES['image']['name']) > 0)
	{
        if (!in_array($_FILES['image']['type'], $arrAllowedImageTypes))
			$arrErrors[] = "Image must be of type JPG, PNG or GIF.";
	}
	return $arrErrors;
}

$tpl->assign('posted', $posted);
$tpl->assign('hasErrors', $boolErrors);
$tpl->assign('details', $arrDetails);

$tpl->displayTemplate();

?>
Return current item: Impleo Music Collection Script