<?php
session_start();
error_reporting(0);
include("../includes/site_config.php");
include('../session_handler.inc');
include('../error.php');include('../includes/functions.php');
include_once __INCLUDE_CLASS_PATH."/class.CropCanvas.php";

$db = new db();



$id = $_SESSION['id'];
$scale = $_REQUEST['scale'];
$x1 = $_REQUEST['x1'];
$x2 = $_REQUEST['x2'];
$y1 = $_REQUEST['y1'];
$y2 = $_REQUEST['y2'];
$file = $_REQUEST['file'];
$act = $_REQUEST['act'];
$mode = $_REQUEST['mode'];
$id   = $_REQUEST['id'];
$title     = form_get('title');
$body      = form_get('body');

unset($_SESSION['id']);

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 ($id) {
        $sql_query = "UPDATE stories SET title = '$title', body = '$body' WHERE story_id = '$id'";
    } else {
      echo  $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";
           	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;
	}
}






//print_r($_SESSION);
/*
if ($Sess_UserType != "A") {
    exit;
}*/
//echo "<pre>";
//print_r($_REQUEST);








if($act == 'save' or isset($file)) {

    if ($file) {
        $img = new ScmsImage(true);
		$_scale = ($scale == 0)?100:$scale;
		$img->loadImage("../$file");
		$img->redimToPercent ( 100,100 );
		$img->applyChangesToOriginal();
		$x1 = (int)((100 / $_scale) * $x1);
		$y1 = (int)((100 / $_scale) * $y1);
		$x2 = (int)((100 / $_scale) * $x2);
		$y2 = (int)((100 / $_scale) * $y2);
		$img->cropToDimensions ($x1,$y1,$x2,$y2);
     	$img->saveImage("../stories/story_$id.jpg");
        //unlink("http://www.mydatingsoftware.com/new_dating/$file");
        //clearCrop("../stories/$file");
    }
}
elseif($_GET['act'] == 'remove')
{
    $sde_eventid = $_GET['id'];
	$filename="story_$sde_eventid.jpg";
    $sql_query = "DELETE FROM stories WHERE story_id = $_GET[id]";
    mysql_query($sql_query);
	if($filename!="")
	{
		$filepath = "../stories/".$filename;
		unlink ($filepath);
	}
}

if($_GET['act'])
    header("Location: $CONST_LINK_ROOT/admin/admin_stories.php");

$_SESSION['title'] = $_SESSION['body'] = $_SESSION['id'] = "";

# retrieve the template
$area = 'member';

$sql_result = mysql_query("SELECT * FROM stories order by story_id DESC", $link);

?>
<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>
<?php include("include/header.php"); ?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Stories Admin</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="650px" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
        <tr>
      <td align="right">
      <?php 	  //require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");	  require_once("../user_status.inc.php");	  ?>      </td>
    </tr>
    <tr>
      <td class="admin-head">&nbsp;</td>
    </tr>
    <tr>
      <td>

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

    <!--<tr>
      <td colspan="3" align="center" class="tdhead">
        <input type="normal" class='normal' onclick="document.location.href = '<?=$_SERVER['PHP_SELF']?>'" value="<?=SD_ADM_STORIES_ADD?>">
      </td>
    </tr>-->
    <!--<tr class="tdtoprow">
      <td>
        <?=SD_ADM_STORIES_TITLE?>
      </td>

      <td >
        <?=SD_ADM_STORIES_IMAGE?>
      </td>


      <td >
        <?=GENERAL_DELETE?>
      </td>
         </tr>-->
        <?php
                    while($story = mysql_fetch_object($sql_result))
                    {$change_to_timestamp = strtotime(htmlspecialchars($story->story_date));
											$new_date = date("d-m-Y",$change_to_timestamp);
                        ?>
						
  <tr>
    <td align="center" width="120px"><? if(file_exists("../stories/story_$story->story_id.jpg")){?><img src="<?=$CONST_LINK_ROOT?>/stories/story_<?=$story->story_id?>.jpg?<?=time()?>" width="100" height="100">
		<? }else{?>
	<img src="<?=$CONST_LINK_ROOT?>/templates/default/images/site_stories.jpg" width="100" height="100">
		<? }?>
	<br /><a href="<?=$CONST_LINK_ROOT?>/admin/admin_stories.php?act=remove&id=<?=$story->story_id?>" onClick="if (confirm('<?=SD_ADM_STORIES_TEXT1?>')) {return true;} else {return false;}" ><?=GENERAL_DELETE?></a>&nbsp;|&nbsp;<a href="<?=$CONST_LINK_ROOT?>/admin/admin_stories.php?act=edit&id=<?=$story->story_id?>"><?=GENERAL_EDIT?></a></td>
	
    <td colspan="2" valign="top"><a href="<?=$CONST_LINK_ROOT?>/admin/admin_story_edit.php?id=<?=$story->story_id?>">
        <strong><?=htmlspecialchars($story->title)?></a><br /><?=$new_date?></strong>
        <br /><br /><?=htmlspecialchars($story->body)?></td>
  </tr>
<tr><td colspan="3"><hr class="line"/></td></tr>

    <!--<tr align=center class="normal" <?if ($event->sde_places*2 < $event->ticket_count ){?>style="background-color: #FFAAAA"<?}?>>
      <td><a href="<?=$CONST_LINK_ROOT?>/admin/admin_story_edit.php?id=<?=$story->story_id?>">
        <?=htmlspecialchars($story->title)?>
        </a></td>

      <td>
        


       <img src="<?=$CONST_LINK_ROOT?>/stories/story_<?=$story->story_id?>.jpg?<?=time()?>" width="100" height="100">      </td>

      <td><a href="<?=$CONST_LINK_ROOT?>/admin/admin_stories.php?act=remove&id=<?=$story->story_id?>" onClick="if (confirm('<?=SD_ADM_STORIES_TEXT1?>')) {return true;} else {return false;}" >[
        <?=GENERAL_DELETE?>
        ]</a></td>
        </tr>-->
        <?php } ?>
					 <tr>
      <td colspan="3" align="center" class="tdfoot">&nbsp; </td>
    </tr>
  </table>	  </td>
    </tr>
</table>


<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"><?php 
		//require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");
		  //require_once("../user_status.inc.php");
	  ?>
    </td>
  </tr>
  <tr>
    <td class="admin-head"><?= $storyid ? SD_ADM_STORY_EDIT_SECTION_NAME : SD_ADM_STORY_ADD_SECTION_NAME ?>
    </td>
  </tr>
  <tr>
    <td><table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
	<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">
      <?php
	 if ($act == 'edit'){
	
        $sql_result = mysql_query("SELECT * FROM stories WHERE story_id='".$_REQUEST['id']."'", $link);
        $story = mysql_fetch_object($sql_result);?>
		<input type="hidden" name="id" value="<?=$_REQUEST['id']?>">
       <?  } ?>
		<? echo $story->title; ?>
      
        <input type="hidden" name="act" value="save">
        
        <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="tdfoot"><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 mysql_close( $link ); ?>
<?php include("include/footer.php"); ?>