<?php
  // This form requires: CMSB v2.04 Build 1
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  
  $tableName       = 'news';
  $recordNum       = null; // you must set either $recordNum or $preSaveTempId to null
  $preSaveTempId   = @$_REQUEST['preSaveTempId'] ? $_REQUEST['preSaveTempId'] : uniqid('x');
  $errorsAndAlerts = '';

  ### upload actions
  if (@$_REQUEST['submitForm'] and !preg_match("/multipart\/form-data/", @$_SERVER['CONTENT_TYPE'])) { die("Upload Error: &lt;form&gt; tag must have enctype=\"multipart/form-data\""); }
   // print_r($_FILES);
  foreach (getUploadInfoArrays() as $uploadInfo) { // add uploads
    $errorsAndAlerts .= saveUpload($tableName, $uploadInfo['_fieldname'], $recordNum, $preSaveTempId, $uploadInfo, $newUploadNums);
  }
  if (@$_REQUEST['removeUpload']) { // delete upload
    $uploadNum = @$_REQUEST['removeUpload'];
    removeUpload($uploadNum, $recordNum, $preSaveTempId);
  }

  ### insert record
  $onlyUploadAction = @$_REQUEST['submitUpload'] || @$_REQUEST['removeUpload']; // don't save the record when the user uploads a file
  if (@$_REQUEST['submitForm'] and !$onlyUploadAction) {

    // error checking
    if (!@$_REQUEST['title'])    { $errorsAndAlerts .= "Please enter a Title<br/>\n"; }
    if (!@$_REQUEST['content'])     { $errorsAndAlerts .= "Please enter a Story<br/>\n"; }
    
	// update record
    if (!@$errorsAndAlerts) {
      mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
      $query = "INSERT INTO `{$TABLE_PREFIX}$tableName` SET
                createdDate      = NOW(),
                createdByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',
                updatedDate      = NOW(),
                updatedByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',
                title            = '".mysql_real_escape_string( $_REQUEST['title'] )."',
                content          = '".mysql_real_escape_string( $_REQUEST['content'] )."'";
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $recordNum = mysql_insert_id();

      // adopt temp uploads
      adoptUploads($tableName, $preSaveTempId, $recordNum);
      removeExpiredUploads(); // erase old expired uploads

      // display thanks message and clear form
      $errorsAndAlerts = "Thanks, we've added your story!";
      $_REQUEST      = array();
      $preSaveTempId = uniqid('x');
      $recordNum     = null;
    }
  }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: red; font-weight: bold; font-size: 16px;"><br/>
    <?php echo $errorsAndAlerts; ?>
  </div>
<?php endif ?>

<h1>Upload Your Images</h1>
<hr/>

<form method="post" name="uploadForm" action="?preSaveTempId=<?php echo $preSaveTempId ?>" enctype="multipart/form-data">
<input type="hidden" name="submitForm" value="1" />
<input type="hidden" name="num" value="<?php echo $recordNum ?>" />
<input type="hidden" name="preSaveTempId" value="<?php echo $preSaveTempId ?>" />
<input type="hidden" name="removeUpload" id="removeUpload" value="0" />

<table border="0" cellspacing="0" cellpadding="2">

  <tr>
    <td valign="top">Title</td>
    <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']) ?>" size="30" /></td>
  </tr>
  <tr>
    <td valign="top">Content</td>
    <td><input class="text-input medium-input" type="text" name="content" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']) ?>" size="30" /></td>
  </tr>
  <tr>
    <td valign="top">Uploads</td>
    <td>
 
      <?php // load uploads
        $fieldName     = 'uploads';
        $tempUploads   = getUploadRecords($tableName, $fieldName, $recordNum, $preSaveTempId);
      ?>
 
      <?php foreach ($tempUploads as $upload): ?>
        <?php if ($upload['hasThumbnail']): ?>
          <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
        <?php endif ?>
 
        <?php echo $upload['filename'] ?>
        <a href="#" onclick="document.uploadForm.removeUpload.value='<?php echo $upload['num'] ?>';document.uploadForm.submit();">remove</a><br/><br/>
 
      <?php endforeach ?>
 
      <?php if (!$tempUploads ): ?>
        You haven't uploaded any images yet.<br/><br/>
      <?php endif ?>
 
 
      Upload Files<br />
      <input type="file" name="<?php echo $fieldName ?>[]"/><br/>
      <!--<input type="file" name="<?php echo $fieldName ?>[]"/><br/>-->
      <input type="submit" name="submitUpload" value="Upload" /><br/>
 
    </td>
  </tr> 

</table>
<br/>
<input class="button" type="submit" name="submitForm" value="Add Record &gt;&gt;" />

</form>

</body>
</html>
