<?php

include './core.php';

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

$ppp = 10;

if( isset( $_REQUEST['cat_edit'] ) )
{
    if( isset( $_POST['send_form'] ) )
    {
        if( check_fields( 'checked' ) )
        {
        	foreach( $_POST['checked'] as $screenshotID )
	        {
                $imgurl = combine_path( $settings['screenshots_dir'], 'image'.$screenshotID.'.jpg' );
    			$thumburl = combine_path( $settings['screenshots_dir'], 'thumb'.$screenshotID.'.gif' );
            	unlink( $imgurl );
                unlink( $thumburl );

                $MySQL->query( sprintf("DELETE FROM screenshots WHERE screenshotID = %d", intval( $screenshotID ) ));
            }
        }
        else if( check_fields( 'title' ) )
        {
            $form_errors = array();

        	if( !check_fields( 'title', 'description' ) || !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' );
	        }
	        else
	        {
        		$MySQL->query( sprintf("INSERT INTO screenshots (categoryID, title, timestamp, description) VALUES (%d, '%s', ".time().", '%s')", $_REQUEST['cat_edit'], me_escape( $_POST['title'] ), me_escape( $_POST['description'] ) ));
		   		$newID = $MySQL->insert_id();
                $imgpath = combine_path( getcwd(), $settings['screenshots_dir'], 'image'.$newID.'.jpg' );
                $thumbpath = combine_path( getcwd(), $settings['screenshots_dir'], 'thumb'.$newID.'.gif' );

                //$handle = imagick_readimage( $_FILES['file']['tmp_name'] );
                //imagick_writeimage( $handle, $imgpath );

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

                //move_uploaded_file( $_FILES['file']['tmp_name'], $imgpath );
                //@chmod ( $imgpath, 0777 );

                make_thumb( $_FILES['file']['tmp_name'], $thumbpath );
            }
        }
    }

    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( $screenshot_count ) = $MySQL->query_first( sprintf("SELECT COUNT(*) FROM screenshots WHERE categoryID = %d", $_REQUEST['cat_edit'] ));
	$pages = ceil( $screenshot_count / $ppp );

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

	$page_navigation = make_page_navigation( "screenshots_admin.php?cat_edit=".$_REQUEST['cat_edit']."&amp;".SID, $page_num, $pages, 2 );
	$Smarty->assign( 'page_navigation', $page_navigation );

    $Smarty->assign( 'cat_edit', $_REQUEST['cat_edit'] );
    $screenshots = $MySQL->fetch_complete( sprintf("SELECT * FROM screenshots WHERE categoryID = %d ORDER BY title LIMIT %d, %d", $_REQUEST['cat_edit'], ( intval( $ppp ) * ( $page_num - 1 ) ), $ppp ));
    $Smarty->assign( 'screenshots', $screenshots );

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

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

    die();
}

if( isset( $_REQUEST['up'] ) )
{
	$change_cat = $MySQL->query_first( sprintf("SELECT * FROM screenshot_categories WHERE categoryID = %d", intval( $_REQUEST['up'] ) ));
	$MySQL->query( sprintf("UPDATE screenshot_categories SET `order`=`order`+1 WHERE `order` = %d",( $change_cat['order'] - 1 ) ));
    $MySQL->query( sprintf("UPDATE screenshot_categories SET `order`=`order`-1 WHERE categoryID = %d", $change_cat['categoryID'] ));
}
else if( isset( $_REQUEST['down'] ) )
{
	$change_cat = $MySQL->query_first( sprintf("SELECT * FROM screenshot_categories WHERE categoryID = %d", intval( $_REQUEST['down'] ) ));
	$MySQL->query( sprintf("UPDATE screenshot_categories SET `order`=`order`-1 WHERE `order` = %d", ( $change_cat['order'] + 1 ) ));
    $MySQL->query( sprintf("UPDATE screenshot_categories SET `order`=`order`+1 WHERE categoryID = %d", $change_cat['categoryID'] ));
}
else if( isset( $_REQUEST['delete_cat'] ) )
{
	$MySQL->query( sprintf("DELETE FROM screenshot_categories WHERE categoryID = %d", intval( $_REQUEST['delete_cat'] ) ));
	$screens = $MySQL->query( sprintf("SELECT screenshotID FROM screenshots WHERE categoryID = %d", intval( $_REQUEST['delete_cat'] ) ));
    while( $row = $MySQL->fetch_array( $screens ) )
    {
    	$imgurl = combine_path( $settings['screenshots_dir'], 'image'.$row['screenshotID'].'.jpg' );
    	$thumburl = combine_path( $settings['screenshots_dir'], 'thumb'.$row['screenshotID'].'.gif' );

        unlink( $imgurl );
        unlink( $thumburl );
    }
    $MySQL->query( sprintf("DELETE FROM screenshots WHERE categoryID = %d",intval( $_REQUEST['delete_cat'] ) ));

    $cats = $MySQL->query( "SELECT categoryID FROM screenshot_categories ORDER BY `order`" );
    $i = 1;
    while( $row = $MySQL->fetch_array( $cats ) )
    {
        $MySQL->query( sprintf("UPDATE screenshot_categories SET `order` = ".$i." WHERE categoryID = %d", $row['categoryID'] ));
        $i++;
    }

    $Smarty->assign( 'deleted', true );
}
elseif( isset( $_REQUEST['send_form'] ) && check_fields( 'name' ) )
{
	$last_order_cat = $MySQL->query_first( "SELECT `order` FROM screenshot_categories ORDER BY `order` DESC" );
    $MySQL->query( sprintf("INSERT INTO screenshot_categories (name, `order`) VALUES ('%s', %d)", me_escape( $_POST['name'] ), ( $last_order_cat['order'] + 1 ) ));
}

$categories = $MySQL->fetch_complete( "SELECT * FROM screenshot_categories ORDER BY `order`" );
$Smarty->assign( 'row_count', count( $categories ) );
$Smarty->assign( 'categories', $categories );

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

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

?>