<?php

include './core.php';

$epp = 20;

if( isset( $_REQUEST['entryID'] ) )
{
	if( isset( $_POST['send_form'] ) && $user->can_access( 'library' ) )
    {
    	$MySQL->query( sprintf("UPDATE library SET entry = '%s', keywords = '%s' WHERE entryID = %d", me_escape( $_POST['entry'] ), me_escape( $_POST['keywords'] ), intval( $_REQUEST['entryID'] ) ));
    }
    elseif( isset( $_POST['delete_entry'] ) && $user->can_access( 'library' ) )
    {
    	$MySQL->query( sprintf("DELETE FROM library WHERE entryID = %d", intval( $_REQUEST['entryID'] ) ));
    	set_location( 'library.php?'.SID );
    }

    $entry = $MySQL->query_first( sprintf("SELECT * FROM library WHERE entryID = %d", intval( $_REQUEST['entryID'] ) ));
    $Smarty->assign( 'entry', $entry );

    $page['content'] = $Smarty->fetch( 'library_item.tpl' );

    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );

	die();
}
elseif( isset( $_REQUEST['new_entry'] ) && $user->can_access( 'library' ) )
{
    if( isset( $_POST['send_form'] ) )
    {
    	$form_errors = array();

	    if( !check_fields( 'title', 'entry' ) )
	        $form_errors[] = form_error( 'uncomplete' );

	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'title', 'entry' );
	    }
	    else
	    {
	        $MySQL->query( sprintf("INSERT INTO library (title, entry, keywords) VALUES ('%s', '%s', '%s')", me_escape( $_POST['title'] ), me_escape( $_POST['entry'] ), me_escape( $_POST['keywords'] ) ));
	    }
    }

    $page['content'] = $Smarty->fetch( 'library_new_entry.tpl' );

    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );

    die();
}

$charlist = Array( '#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' );
$Smarty->assign( 'charlist', $charlist );

if( isset( $_REQUEST['char'] ) )
	$char = $_REQUEST['char'];
else
	$char = $charlist[1];
$Smarty->assign( 'char', $char );

// start page navigation

if( !isset( $_REQUEST['page_num'] ) || $_REQUEST['page_num'] < 1 )
	$page_num = 1;
else
	$page_num = intval( $_REQUEST['page_num'] );

$Smarty->assign( 'page_num', $page_num );

list( $entry_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM library WHERE title LIKE '".me_escape($char)."%'" );
$pages = ceil( $entry_count / intval( $epp ) );

$Smarty->assign( 'pages', $pages );

$page_navigation = make_page_navigation( "library.php?char=".$char."&amp;".SID, $page_num, $pages, 2 );
$Smarty->assign( 'page_navigation', $page_navigation );

// end page navigation

if( $char == '#' )
	$entries = $MySQL->fetch_complete( sprintf("SELECT * FROM library WHERE SUBSTRING(title,1,1) NOT IN ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z') ORDER BY `title` ASC LIMIT %d, %d", ( intval( $epp ) * ( $page_num - 1 ) ), $epp ));
else
	$entries = $MySQL->fetch_complete( "SELECT * FROM library WHERE title LIKE '".me_escape($char)."%' ORDER BY `title` ASC LIMIT ".( intval( $epp ) * ( $page_num - 1 ) ).", ".$epp );
$Smarty->assign( 'entries', $entries );

$page['content'] = $Smarty->fetch( 'library.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>