<?php

include("../includes/site_config.php");
include('../session_handler.inc');
//include('../functions.php');
include('../includes/functions.php');
include('../error.php');
//print_r($_SESSION);
include_once("../lib/class.ItemManager.php");
$option_manager = new OptionManager();
/*

if ($Sess_UserType != "A") {
    exit;
}*/

$db = new db();


######################


######################



# retrieve the template
$area = 'member';
$id        = $_REQUEST['id'];
$title     = $_REQUEST['title'];
$body      = $_REQUEST['body'];
$act      = $_REQUEST['act'];


if (isset($_FILES['imgFile'])) {
    # check whether immediate authorisation
    if ($_FILES['imgFile']['name'] != "") {
        if ($_FILES['imgFile']['size'] > $option_manager->GetValue('maxpicsize')) {
            $max_size=round($max_size/1024);
            error_page(sprintf(PRGPICADMIN_ERROR2,$max_size),GENERAL_USER_ERROR);
        }
        if ($_FILES['imgFile']['type'] == "image/pjpeg" || $_FILES['imgFile']['type'] == "image/jpeg") {
            if ( $_FILES['imgFile']['type'] == "image/pjpeg"  || $_FILES['imgFile']['type'] == "image/jpeg") { $extension=".jpg"; }
           
        	
        } else {
            error_page(PRGPICADMIN_ERROR3,GENERAL_USER_ERROR);
        }
    }
}

if ($act == 'save'){
    if (isset($id)) {
     $sql_query = "UPDATE stories SET title = '$title', body = '$body' WHERE story_id = '$id'";
    } else {
     $sql_query = "INSERT INTO stories SET title = '$title', body = '$body', story_date = now()";

    }

    $db->query($sql_query);
    $id = ($id) ? $id : $db->insert_id;
    $_SESSION['id'] = $id;

 $tempusername="story_$id";
$filename="$tempusername"."$extension";
           	$targetfile="stories/"."$filename";
			createThumbs(115, 115, $_FILES['imgFile']['tmp_name'], 'jpg', "../$targetfile");
           	//copy($_FILES['imgFile']['tmp_name'],"../$targetfile");
			

	if (isset($_FILES['imgFile']) && $_FILES['imgFile']['error'] == 0) {?>

<script language="javascript">
window.location='<? echo $CONST_LINK_ROOT?>/admin/admin_stories.php?thePic=<?php echo $targetfile; ?>&area=stories';
   
</script>


<?php 

	   // header("Location: $CONST_LINK_ROOT/crop.php?thePic=$targetfile&area=stories");
	   // exit;
	} else {
?>
<script language="javascript">
window.location='admin_stories.php';
   
</script>


<?php
	    //header("Location: $CONST_ADMIN_LINK_ROOT/admin_stories.php");
	    //exit;
	}
}




?>
<?php include("include/header.php"); ?>
<script language=javascript>
function CheckFile(){
	form=document.getElementById('storyForm');
	if (form.imgFile.value) {
		form.action="<?=$CONST_LINK_ROOT?>/admin/admin_story_edit.php";
	} else {
		form.action="<?=$CONST_LINK_ROOT?>/admin/admin_stories.php";
	}
	return true;
}
</script>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Edit Story</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
        <tr>
      <td align="right">
      </td>
    </tr>
        <tr>
      <td><table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
        <?php
        $sql_result = mysql_query("SELECT * FROM stories WHERE story_id='$id'", $link);
        $story = mysql_fetch_object($sql_result);
        ?>
        <form method="post" id="storyForm" action="<?php echo $CONST_LINK_ROOT?>/admin/admin_story_edit.php<?php print("?PHPSESSID=".session_id()); ?>" enctype="multipart/form-data">
          <input type="hidden" name="act" value="save">
          <input type="hidden" name="id" value="<?=$id?>">
          <input type="hidden" name="MAX_FILE_SIZE" value="<?$CONST_STORYIMAGE_WEIGHT?>000">
          <tr>
            <td align="center">

	   <table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">

                <tr>
                  <td colspan="2" class="tdhead">&nbsp;</td>
                </tr>
                <tr class="normal">
                  <td>
                    <?=SD_ADM_STORY_TITLE?>
                  </td>
                  <td> <input name="title" type="text" class="input" value="<?=htmlspecialchars($story->title)?>"></td>
                </tr>
                <tr class="normal">
                  <td>
                    <?=SD_ADM_STORY_BODY?>
                  </td>
                  <td> <textarea name="body" cols="50" rows="10" wrap="soft" class="inputl"><?=htmlspecialchars($story->body)?></textarea></td>
                </tr>
                <tr class="normal">
                  <td>
                    <?=SD_ADM_STORY_IMAGE?>
                  </td>
                  <td> <input name="imgFile" type="file" > <br></td>
                </tr>
                <tr>
                  <td colspan="2" align="center" class="normal"> <input name="submit" type="submit" class="normal" value="<?=GENERAL_SAVE?>">
                    <input name="normal" type="normal" class="normal" onclick="window.location = '<?=$CONST_LINK_ROOT?>/admin/admin_stories.php'" value="<?=GENERAL_CANCEL?>">
                  </td>
                </tr>
              </table></td>
          </tr>
        </form>
      </table></td>
    </tr>
</table></div><div class="admin-page-bottom"></div></div>
<?php include("include/footer.php"); ?>
