
<?php
  // This form requires: CMSB v2.04 Build 1
  require_once "../lib/viewer_functions.php";
  $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'] && !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'] && !$onlyUploadAction) {

    // error checking
    if (!@$_REQUEST['title'])    { $errorsAndAlerts .= "Please specify title!<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_escape( $_REQUEST['title'] )."'";
      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 record!";
      $_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>Add Record</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">PreSaveTempId</td>
</tr>
 <tr>
  <td valign="top">Title</td>
  <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>" 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): ?>
      There are no uploads 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/>
<hr/>
<input class="button" type="submit" name="submitForm" value="Add Record &gt;&gt;" />

</form>

</body>
</html>
