<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'wep_admin' ) ) access_error();

$ppp = 5;

if( isset( $_POST['send_form'] ) )
{
    $form_errors = array();

    if( !check_fields( 'title', 'description', 'starttime_DD', 'starttime_MM', 'starttime_YYYY' ) || !isset( $_FILES['file'] ) )
        $form_errors[] = form_error( 'uncomplete' );
    else
    {
        $file_extension = strtolower( substr( strrchr( $_FILES['file']['name'], "." ), 1 ) );
        $formats = array( 'jpg', 'jpeg', 'gif', 'png', 'bmp' );
        if( !in_array( $file_extension, $formats ) )
        	$form_errors[] = form_error( 'no_picture' );
    }

    if( count( $form_errors ) > 0 )
    {
        $Smarty->assign( 'form_errors', $form_errors );
        assign_formvalues( 'title', 'description', 'starttime_DD', 'starttime_MM', 'starttime_YYYY' );
    }
    else
    {
        $starttime = mktime( 0, 0, 0, intval( $_REQUEST['starttime_MM'] ), intval( $_REQUEST['starttime_DD'] ), intval( $_REQUEST['starttime_YYYY'] ) );

        $MySQL->query( sprintf("INSERT INTO weekend_pictures (title, description, starttime) VALUES ('%s', '%s', %d)", me_escape( $_POST['title'] ), me_escape( $_POST['description'] ), $starttime));
        $newID = $MySQL->insert_id();
        $imgpath = combine_path( $settings['wep_dir'], 'image'.$newID.'.jpg' );
        $thumbpath = combine_path( $settings['wep_dir'], 'thumb'.$newID.'.gif' );

        exec( 'E:\ImageMagick/convert -format jpg "'.$_FILES['file']['tmp_name'].'" '.$imgpath );
    	make_thumb( $_FILES['file']['tmp_name'], $thumbpath );
    }
}

if( isset( $_REQUEST['delete'] ) )
{
    $imgurl = combine_path( $settings['wep_dir'], 'image'.$_REQUEST['delete'].'.jpg' );
    $thumburl = combine_path( $settings['wep_dir'], 'thumb'.$_REQUEST['delete'].'.gif' );
    unlink( $imgurl );
    unlink( $thumburl );

    $MySQL->query( sprintf("DELETE FROM weekend_pictures WHERE wepID = %d", intval( $_REQUEST['delete'] ) ));
}

if( isset( $_REQUEST['edit'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'title', 'description', 'starttime_DD', 'starttime_MM', 'starttime_YYYY' ) || !isset( $_FILES['file'] ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    else
	    {
	        if( count( $_FILES ) > 0 && $_FILES['file']['name'] != '' )
	        {
		    	$file_extension = strtolower( substr( strrchr( $_FILES['file']['name'], "." ), 1 ) );
		        $formats = array( 'jpg', 'jpeg', 'gif', 'png', 'bmp' );
		        if( !in_array( $file_extension, $formats ) )
		        	$form_errors[] = form_error( 'no_picture' );
	        }
	    }
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'title', 'description', 'starttime_DD', 'starttime_MM', 'starttime_YYYY' );
	    	
	    	$page['content'] = $Smarty->fetch( 'wep_admin_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $starttime = mktime( 0, 0, 0, intval( $_REQUEST['starttime_MM'] ), intval( $_REQUEST['starttime_DD'] ), intval( $_REQUEST['starttime_YYYY'] ) );
	
	        $MySQL->query( sprintf("UPDATE weekend_pictures SET title = '%s', description = '%s', starttime = %d WHERE wepID = %d", me_escape( $_REQUEST['title'] ), me_escape( $_REQUEST['description'] ), $starttime, intval( $_REQUEST['edit'] ) ));
	        
	        if( count( $_FILES ) > 0 && $_FILES['file']['name'] != '' )
	        { 
		        $imgpath = combine_path( $settings['wep_dir'], 'image'.intval( $_REQUEST['edit'] ).'.jpg' );
		        $thumbpath = combine_path( $settings['wep_dir'], 'thumb'.intval( $_REQUEST['edit'] ).'.gif' );
		
		        exec( 'E:\ImageMagick/convert -format jpg '.$_FILES['file']['tmp_name'].' '.$imgpath );
		    	make_thumb( $_FILES['file']['tmp_name'], $thumbpath );
	        }
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( sprintf("SELECT * FROM weekend_pictures WHERE wepID = %d", intval( $_REQUEST['edit'] ) ));
		$formvalues['starttime_YYYY'] = date( "Y", $formvalues['starttime'] );
		$formvalues['starttime_MM'] = date( "m", $formvalues['starttime'] );
		$formvalues['starttime_DD'] = date( "d", $formvalues['starttime'] );
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'wep_admin_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

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( $wep_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM weekend_pictures" );
$pages = ceil( $wep_count / $ppp );

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

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

$weps_res = $MySQL->query( sprintf("SELECT * FROM weekend_pictures ORDER BY starttime DESC LIMIT %d, %d", ( intval( $ppp ) * ( $page_num - 1 ) ), $ppp ));
$weps = array();
while( $row = $MySQL->fetch_array( $weps_res ) )
{
	$row['thumbpath'] = combine_path( $settings['wep_dir'], 'thumb'.$row['wepID'].'.gif' );
    $weps[] = $row;
}
$Smarty->assign( 'weps', $weps );

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

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

?>