Location: PHPKode > projects > ACSoc Comics Library Management System > odin-0.589/inven.php
<?php
	require_once "odincom.php";
	require_once "$ui_dir/uicom.inc";
	require_once "dbo/Item.php";
	require_once "dbo/ItemList.php";
	require_once "dbo/Series.php";
	require_once "dbo/SeriesList.php";
	require_once "$ui_dir/renderer.inc";
	require_once "$ui_dir/dispinven.inc";

	$script	="inven.php";
	odin_staff_login();

	if ($_GET['nvr']) {
		$_SESSION['navbar']->register("root_inven");
	}

	if ($_POST['action'])
		$action = $_POST['action'];
	elseif ($_GET['action'])
		$action = $_GET['action'];
	elseif ($_GET['by'])
		$action = "query";

	switch($action) {
		case "query":
			$renderObj = query_series();
			$previousQuery = $_GET['query'];
			$mainForm = "query";
			break;
		case "browse":
			$renderObj = query_series(1);
			$mainForm = "query";
			break;
		case "add_series":
			if ($sid = add_series()) {
				$_SESSION['navbar']->unregister("addseries");
				$renderObj = series_info($sid);
				$mainForm = "seriesmenu";
			}
			else {
				$formPreload = $_POST;
				$mainForm = "series";
			}
			break;
		case "add_volume":
			$sid = $_POST['series'];
			if ($vid = add_volume()) {
				$_SESSION['navbar']->unregister("addvolume");
				$renderObj = series_info($sid);
				$mainForm = "seriesmenu";
			}
			else {
				$formPreload = $_POST;
				$mainForm = "vol";
			}				
			break;
		case "getid":
			$formPreload = $_POST;
			$mainForm = "series";
			if ($newid = allocate_seriesno($_POST['prefix'])) {
				$formPreload['seriesno'] = $newid;
			}
			break;
		case "edit":
			$sid = $_GET['sid'];
			$vid = $_GET['vid'];
			if ( $vid && $sid ) {
				$item = new Item();
				$item->id = $vid;
				$item->loadFromDB();
				$formPreload = $item->getFields();
				//print "svol: $item->svolume";
				$_SESSION['navbar']->register("volumeinfo", "{$GLOBALS['script']}?vid={$vid}", $item->svolume);
				$_SESSION['navbar']->register("editvolume", "get", $item->svolume);
				if ($_GET['sv'] == "toc") $mainForm ="voltoc";
				else $mainForm = "vol";
			}
			elseif ( $sid ) {
				$series = new Series();
				$series->id = $sid;
				$series->loadFromDB();
				$formPreload = $series->getFields();
				$sidtext = $formPreload['seriesno'] ." ". $formPreload['title'];
				$_SESSION['navbar']->register("seriesinfo", "{$GLOBALS['script']}?sid=$sid", trim("$series->seriesno $series->title"),1);
				$_SESSION['navbar']->register("editseries", "get", $sidtext);
				if ($_GET['sv'] == "toc") $mainForm ="seriestoc";
				else $mainForm = "series";
			}
			else {
				$_SESSION['navbar']->register("root_inven");
				$mainForm = "query";
			}
			break;
		case "new":
			if ($sid = $_GET['sid']) {
				//$series = new Series();
				//$series->id = $sid;
				//$series->loadFromDB();
				//$_SESSION['navbar']->register("seriesinfo", "{$GLOBALS['script']}?sid=$sid", trim("$series->seriesno $series->title"));
				$_SESSION['navbar']->register("newvolume", "get");
				$formPreload['series'] = $sid;
				$mainForm = "vol";
			}
			else {
				$_SESSION['navbar']->register("newseries", "get");
				$mainForm = "series";
			}
			break;
		case "change_status":
			$sid = $_POST['sid'];
			$vid = $_POST['vid'];
			change_status($sid,$vid);
			if ($vid) {
				$renderObj = volume_info($vid);
			} else {
				$renderObj = series_info($sid);
				$mainForm = "seriesmenu";
			}
			break;
		case "edit_series":
			$sid = $_POST['id'];
			$failForm = ($_POST['fields'] == "toc")? "seriestoc" : "series";
			if ( edit_series($sid,$_POST['fields']) ) {
				$_SESSION['navbar']->unregister("editseries");
				$renderObj = series_info($sid);
				$mainForm = "seriesmenu";
			}
			else {
				$formPreload = $_POST;
				$mainForm = $failForm;
			}
			break;
		case "edit_volume":
			$vid = $_POST['id'];
			$failForm = ($_POST['fields'] == "toc")? "voltoc" : "vol";
			if ( edit_volume($vid, $_POST['fields']) ) {
				$_SESSION['navbar']->unregister("editvolume");
				$renderObj = volume_info($vid);
			}
			else {
				$formPreload = $_POST;
				$mainForm = $failForm;
			}
			break;
		case "del_volume":
			$vid = $_POST['vid'];
			$sid = $_POST['sid'];
			del_volume($sid,$vid);
			$renderObj = series_info($sid);
			$mainForm = "seriesmenu";
			break;
		case "del_series":
			$sid = $_POST['sid'];
			if (del_series($sid)) {
				$_SESSION['navbar']->register("root_inven");
				$mainForm = "query";
			} else {
				$renderObj = series_info($sid);
				$mainForm = "seriesmenu";
			}
			break;
		default:
			$sid = $_GET['sid'];
			$vid = $_GET['vid'];
			if ($vid) {
				$renderObj = volume_info($vid);
			}
			elseif ($sid) {
				if ($renderObj = series_info($sid)) {
					$mainForm = "seriesmenu";
				} else {
					$mainForm = "query";
					$_SESSION['navbar']->register("root_inven");
				}
			}
			else {
				$_SESSION['navbar']->register("root_inven");
				$mainForm = "query";
			}
	}

	print_header();
	$renderer = new Renderer;
	$renderer->is_admin = $renderer->is_admin = check_staff_permission('writebooklist',0);
	if (!$deferRenderObj && $renderObj) {
		$renderer->render($renderObj);
		unset($renderObj);
	}
	switch($mainForm) {
		case "seriesmenu" :
			series_menu($sid);
			break;
		case "series" :
			series_form($formPreload);
			break;
		case "vol" :
			volume_form($formPreload);
			break;
		case "seriestoc" :
			series_toc_form($formPreload);
			break;
		case "voltoc" :
			volume_toc_form($formPreload);
			break;
		case "query" :
			main_menu($previousQuery);
			break;
		default:
			default_form();
	}
	if ($renderObj) {
		$renderer->render($renderObj);
	}
	print_footer();
	exit;

//////////////////////////////////////////////////////////////////////////

function add_series() {

	$series = new Series();
	$series->putFields($_POST);
	$error = $series->validate();
	if (count($error) > 0) {
		myresult(0, join("<br>\n", $error));
		return;
	}
	if ($series->insertIntoDB()) {
		myresult (1, "Add Series Successful\n");
		return $series->id;
	}
	else {
		myresult (0, "Add Series error");
	}
	return;
}


//////////////////////////////////////////////////////////////////////////

function del_series($sid) {

	$series = new Series();
	$series->id = $sid;
	if ($series->deleteFromDB()) {
		myresult (1, "Delete Series Successful");
		return 1;
	}
	else {
		myresult (0, "Delete Series error");
	}
	return;
}

//////////////////////////////////////////////////////////////////////////
function allocate_seriesno($prefix) {
	$series = new Series();
	$series->allocate($prefix);
	return $series->seriesno;
}



//////////////////////////////////////////////////////////////////////////

function change_status($sid,$vid) {

	if (!$_POST['status']) return;
	if (!isset($sid) || !is_numeric($sid)) {
		myresult (0, "Unknown series input $sid");
		return;
	}

	odin_connect();

	$query = "UPDATE odin_item_status SET status='".$_POST['status']."' ";
	if (!isset($vid)) {
		$query .= "WHERE id IN (SELECT id FROM odin_item WHERE series=$sid)";
	} else {
		$query .= "WHERE id=$vid";
	}

	if (!mysql_query($query)) {
		myresult (0, "Update odin_item_status failed : ". mysql_error() ."<br>$query");
	} elseif (!mysql_affected_rows()) {
		myresult (1, "Update Status : nothing changed");
	} else {
		myresult (1, "Update Status Success");
	}

}

//////////////////////////////////////////////////////////////////////////

function edit_series($sid, $fields=NULL) {
	$series = new Series();
	if ($fields == "toc") {
		$series->fields = array('abstract', 'staffteam');
		$series->putFields($_POST);
		$series->id = $sid;
		$logType = 'SeriesAbstract';
	} else {		
		$series->fields = array('seriesno', 'author', 'authoralt', 'title', 'titlealt', 'subtitle',
			'publisher', 'pubcity',	'pubyear', 'language', 'shelf', 'numofvolumes', 
			'classify', 'subject', 'itemno_fmt', 'coverimage', 'note');
		$series->putFields($_POST);
		$series->id = $sid;
		$error = array();
		$error = $series->validate();
		if (count($error) > 0) {
			myresult(0, join("<br>\n", $error));
			return;
		}
		$logType = 'EditSeries';
	}
	if ($series->updateToDB($logType)) {
		myresult (1, "Edit Series Successful\n");
		unset ($GLOBALS["_POST"]);
		return 1;
	}
	else {
		myresult (0, "Edit Series error");
	}
	return;
}

//////////////////////////////////////////////////////////////////////////

function edit_volume($vid,$fields=NULL) {
	$item = new Item();
	if ($fields == "toc") {
		$item->fields = array('abstract', 'toc', 'staffteam');
		$item->putFields($_POST);
		$item->id = $vid;
		$logType = 'VolumeAbstract';
	} else {		
		$series->fields = array('itemno', 'series', 'volume', 'copy', 'volumeintext', 'subtitle',
		'ipublisher', 'publishdate', 'purchasedate', 'listprice', 'purchaseprice', 
		'supplier', 'isbn', 'barcode', 'shelf', 'note');
		$item->putFields($_POST);
		$item->id = $vid;
		$error = array();
		$error = $item->validate();
		if (count($item->volume) > 1) {
			$error[] = "Cannot edit one volume into multiple volumes";
			$GLOBALS['_POST']['volume'] = '';
		}
		$logType = 'EditVolume';
	}

	if (count($error) > 0) {
		myresult(0, join("<br>\n", $error));
		return;
	}
	if ($item->updateToDB($logType)) {
		myresult (1, "Edit Volume Successful");
		unset ($GLOBALS["_POST"]);
		return 1;
	} 
	else {
		myresult (0, "Edit volume error");
	}
	return;
}


//////////////////////////////////////////////////////////////////////////


function del_volume($sid, $vid) {
	$item = new Item();
	$item->id = $vid;
	$item->series = $sid;
	if ($item->deleteFromDB()) {
		myresult (1, "Delete Volume Successful");
		return 1;
	}
	else {
		myresult (0, "Delete volume error");
	}
	return;
}


//////////////////////////////////////////////////////////////////////////
function add_volume() {

	$item = new Item();
	$item->putFields($_POST);
	$error = $item->validate();
	if (count($error) > 0) {
		myresult(0, join("<br>\n", $error));
		return;
	}
	if ($volok = $item->insertIntoDB()) {
		myresult (1, "Add volume $volok Successful");
		return $item->id;
	}
	else {
		myresult (0, "Add volume error");
	}
	return;

}


//////////////////////////////////////////////////////////////////////////
function query_series($browse=0) {
	global $lang;

	if (!$browse && empty($_GET['query'])) return;

	$_SESSION['search'] = array();
	if ($_GET['by'] == "cn" || $_GET['by'] == "ct" || $_GET['by'] == "co" || ($browse && $_GET['status'] != "" )) {
		$list = new ItemList;
		$_SESSION['search']['class'] = "Item";
	} else {
		$list = new SeriesList;
		$_SESSION['search']['class'] = "Series";
	}
	$list->lim = $_GET['lim'];
	$list->page = $_GET['page'];
	$list->search_status = $_GET['status'];
	$list->orderby = $_GET['ord'];
	$list->orderdesc = $_GET['odn'];

	switch ($_GET['by']) {
		case "vid" : 
			$list->search_by_ids(trim($_GET['query']));
			break;
		case "sid" :
			$list->search_by_id(trim($_GET['query']));
			break;
		default:
			if ($browse && !$_GET['by']) $list->browse();
			else $list->search($_GET['by'], trim($_GET['query']));
	}

	if (!$browse && $list->no_of_match==1)
	{
		// if only one match, jump directly to info page
		if (is_a($list, "ItemList")) {
			redirect("{$GLOBALS['script']}?vid={$list->ids[0]}");
		}
		else {
			redirect("{$GLOBALS['script']}?sid={$list->ids[0]}");
		}
	}
	else if ($list->no_of_match>1) {
		$displaystr = "";
		if ($_GET['query']) $displaystr .= " '{$_GET['query']}'";
		if ($_GET['page']) $displaystr .= " pg. {$_GET['page']}";
		if ($browse) {
			$_SESSION['navbar']->register("browse", url_strip_qs('luck'), $displaystr);
		} else {
			$_SESSION['navbar']->register("result", url_strip_qs('luck'), $displaystr);
			$_SESSION['navbar']->addaction("startover", "{$GLOBALS['script']}");
		}
		
		$_SESSION['search']['url'] = url_strip_qs('luck');
		$_SESSION['search']['page'] = $list->page;
		$_SESSION['search']['maxpage'] = ceil($list->no_of_match/$list->limit);
		$_SESSION['search']['ids'] = $list->ids;
		if (($x=$_GET['luck']) != '')
		{
			if ($x == -1) $x = count($list->ids)-1;
			if (is_a($list, "ItemList")) {
				redirect("{$GLOBALS['script']}?vid={$list->ids[$x]}&x=$x");
			}
			else {
				redirect("{$GLOBALS['script']}?sid={$list->ids[$x]}&x=$x");
			}
		}
		return $list;
	}
	else
	{
		myresult(1, $lang[60]);
	}

}


//////////////////////////////////////////////////////////////////////////
function volume_info($vid) {
	global $lang;

	$item = new Item();
	$item->id = $vid;
	if (!$item->loadFromDB()) return;
	$_SESSION['navbar']->register("seriesinfo", "{$GLOBALS['script']}?sid={$item->series}", trim("$item->seriesno $item->title"));
	$_SESSION['navbar']->register("volumeinfo", "{$GLOBALS['script']}?vid={$vid}", $item->svolume);
	$_SESSION['navbar']->addaction("editvolume", "{$GLOBALS['script']}?action=edit&sid={$item->series}&vid=$vid");
	$_SESSION['navbar']->addaction("edittoc", "{$GLOBALS['script']}?action=edit&sid={$item->series}&vid=$vid&sv=toc");
	$_SESSION['navbar']->addaction("uploadcover", "{$GLOBALS['script_thumbnail']}?t=item&i=$vid");

	$ix = $_GET['x'];
	if (is_numeric($ix) && ($sr = $_SESSION['search']) && $sr['class'] == "Item") {
		if ($ix > 0) { 
			$prurl = url_replace_qs('x', $ix-1);
			$prurl = url_replace_qs('vid', $sr['ids'][$ix-1], $prurl);
		} else {
			if ($sr['page'] > 1) {
				$prurl = url_replace_qs('page', $sr['page']-1, $sr['url']);
				$prurl = url_replace_qs('luck', '-1', $prurl);
			} else {
				$prurl = '';
			}
		}
		if ($ix < count($sr['ids'])-1) { 
			$nxurl = url_replace_qs('x', $ix+1);
			$nxurl = url_replace_qs('vid', $sr['ids'][$ix+1], $nxurl);
		} else {
			if ($sr['page'] < $sr['maxpage']) {
				$nxurl = url_replace_qs('page', $sr['page']+1, $sr['url']);
				$nxurl = url_replace_qs('luck', '0', $nxurl);
			} else {
				$nxurl = '';
			}
		}
		$_SESSION['navbar']->addaction("prev", $prurl);
		$_SESSION['navbar']->addaction("returntoresult", $sr['url']);
		$_SESSION['navbar']->addaction("next", $nxurl);
	}
	
	return $item;
}

//////////////////////////////////////////////////////////////////////////
function series_info($sid) {
	global $lang;

	$series = new Series();
	$series->id = $sid;
	if (!$series->loadFromDB()) return;
	$series->loadVolumes();
	$_SESSION['navbar']->register("seriesinfo", "{$GLOBALS['script']}?sid=$sid", trim("$series->seriesno $series->title"));
	$_SESSION['navbar']->addaction("newvolume", "{$GLOBALS['script']}?action=new&sid=$sid");
	$_SESSION['navbar']->addaction("editseries", "{$GLOBALS['script']}?action=edit&sid=$sid");
	$_SESSION['navbar']->addaction("edittoc", "{$GLOBALS['script']}?action=edit&sid=$sid&sv=toc");
	$_SESSION['navbar']->addaction("uploadcover", "{$GLOBALS['script_thumbnail']}?t=series&i=$sid");
	
	$ix = $_GET['x'];
	if (is_numeric($ix) && ($sr = $_SESSION['search']) && $sr['class'] == "Series") {
		if ($ix > 0) { 
			$prurl = url_replace_qs('x', $ix-1);
			$prurl = url_replace_qs('sid', $sr['ids'][$ix-1], $prurl);
		} else {
			if ($sr['page'] > 1) {
				$prurl = url_replace_qs('page', $sr['page']-1, $sr['url']);
				$prurl = url_replace_qs('luck', '-1', $prurl);
			} else {
				$prurl = '';
			}
		}
		if ($ix < count($sr['ids'])-1) { 
			$nxurl = url_replace_qs('x', $ix+1);
			$nxurl = url_replace_qs('sid', $sr['ids'][$ix+1], $nxurl);
		} else {
			if ($sr['page'] < $sr['maxpage']) {
				$nxurl = url_replace_qs('page', $sr['page']+1, $sr['url']);
				$nxurl = url_replace_qs('luck', '0', $nxurl);
			} else {
				$nxurl = '';
			}
		}
		$_SESSION['navbar']->addaction("prev", $prurl);
		$_SESSION['navbar']->addaction("returntoresult", $sr['url']);
		$_SESSION['navbar']->addaction("next", $nxurl);
	}
	
	return $series;

}

?>
Return current item: ACSoc Comics Library Management System