<?php

include './core.php';

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

if( isset( $_REQUEST['cat_edit'] ) )
{
    if( isset( $_POST['send_form'] ) )
    {
        if( check_fields( 'checked' ) )
        {
        	foreach( $_POST['checked'] as $faqID )
	        {
                $MySQL->query( sprintf("DELETE FROM faqs WHERE faqID = %d", intval( $faqID ) ));
            }
        }
        else if( check_fields( 'mode' ) && $_POST['mode'] == "new" )
        {
            $form_errors = array();

        	if( !check_fields( 'question', 'answer' ) )
	            $form_errors[] = form_error( 'uncomplete' );

	        if( count( $form_errors ) > 0 )
	        {
	            $Smarty->assign( 'form_errors', $form_errors );
                assign_formvalues( 'question', 'answer' );
	        }
	        else
	        {
        		$MySQL->query( sprintf("INSERT INTO faqs (categoryID, question, answer) VALUES (%d, '%s', '%s')", $_REQUEST['cat_edit'], me_escape( $_POST['question'] ), me_escape( $_POST['answer'] ) ));
            }
        }
        else if( check_fields( 'mode' ) && $_POST['mode'] == "edit" )
        {
            $form_errors = array();

        	if( !check_fields( 'question', 'answer' ) )
	            $form_errors[] = form_error( 'uncomplete' );

	        if( count( $form_errors ) > 0 )
	        {
	            $Smarty->assign( 'form_errors', $form_errors );
                assign_formvalues( 'question', 'answer' );
	        }
	        else
	        {
        		$MySQL->query( sprintf("UPDATE faqs SET question = '%s', answer = '%s' WHERE faqID = %d", me_escape( $_POST['question'] ), me_escape( $_POST['answer'] ), intval( $_POST['question_edit'] )  ));
            	unset( $_REQUEST['question_edit'] );
            }
        }
    }

    $Smarty->assign( 'cat_edit', $_REQUEST['cat_edit'] );
    $faqs = $MySQL->fetch_complete( sprintf("SELECT * FROM faqs WHERE categoryID = %d", $_REQUEST['cat_edit'] ));
    $Smarty->assign( 'faqs', $faqs );

    if( isset( $_REQUEST['question_edit'] ) )
    {
        $faq = $MySQL->query_first( sprintf("SELECT * FROM faqs WHERE faqID = %d", intval( $_REQUEST['question_edit'] ) ));
        $formvalues = array();
        $formvalues['faqID'] = $faq['faqID'];
        $formvalues['question'] = stripslashes( $faq['question'] );
        $formvalues['answer'] = stripslashes( $faq['answer'] );
        $Smarty->assign( 'formvalues', $faq );
        $page['content'] = $Smarty->fetch( 'faq_admin_item_edit.tpl' );
    }
    else
    	$page['content'] = $Smarty->fetch( 'faq_admin_item.tpl' );

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

    die();
}

if( isset( $_REQUEST['up'] ) )
{
	$change_cat = $MySQL->query_first( sprintf("SELECT * FROM faq_categories WHERE categoryID = %d", intval( $_REQUEST['up'] ) ));
    $MySQL->query( sprintf("UPDATE faq_categories SET `order`=`order`+1 WHERE `order` = %d", ( $change_cat['order'] - 1 ) ));
    $MySQL->query( sprintf("UPDATE faq_categories SET `order`=`order`-1 WHERE categoryID = %d", $change_cat['categoryID'] ));
}
else if( isset( $_REQUEST['down'] ) )
{
	$change_cat = $MySQL->query_first( sprintf("SELECT * FROM faq_categories WHERE categoryID = %d",intval( $_REQUEST['down'] ) ));
	$MySQL->query( sprintf("UPDATE faq_categories SET `order`=`order`-1 WHERE `order` = %d", ( $change_cat['order'] + 1 ) ));
    $MySQL->query( sprintf("UPDATE faq_categories SET `order`=`order`+1 WHERE categoryID = %d", $change_cat['categoryID'] ));
}
else if( isset( $_REQUEST['delete_cat'] ) )
{
	$MySQL->query( sprintf("DELETE FROM faq_categories WHERE categoryID = %d", intval( $_REQUEST['delete_cat'] ) ));
    $MySQL->query( sprintf("DELETE FROM faqs WHERE categoryID = %d", intval( $_REQUEST['delete_cat'] ) ));

    $cats = $MySQL->query( "SELECT categoryID FROM faq_categories ORDER BY `order`" );

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

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

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

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

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

?>