<?php
  // This is a sample form for adding a record and it's uploads

  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 = '';

  ### insert record
  if (@$_REQUEST['submitForm']) {

    // 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". htmlencode(mysql_error()) . "\n");
      $newRecordNum = mysql_insert_id();

      // adopt temp uploads (IMPORTANT - DON'T FORGET THIS STEP!!!)
      adoptUploads($tableName, $preSaveTempId, $newRecordNum);
      removeExpiredUploads(); // erase old expired uploads

      // display thanks message and clear form
      $errorsAndAlerts = "Thanks, we've added your record!";
      $_REQUEST      = array();
    }
  }

?><!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>

<h1>Add Record</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?></div>
<?php endif ?>

<hr/>

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<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 ?>" />

<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td valign="top">PreSaveTempId</td>
  <td><?php echo $preSaveTempId ?></td>
</tr>
 <tr>
  <td valign="top">Title</td>
  <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlencode(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>
 <tr>
  <td valign="top">Uploads</td>
  <td>

    <?php /* TODO: Add security check in uploadForm2_iframe.php to limit access to only allowed uploads */ ?>
    <iframe src='uploadForm3_iframe.php?table=<?php echo $tableName ?>&amp;field=uploads&amp;num=<?php echo $recordNum ?>&amp;preSaveTempId=<?php echo $preSaveTempId ?>'
            height='100' width='600' frameborder='0' scrolling='no'>
    </iframe><br/>
  </td>
</tr>
</table><br/>
<hr/>
<input class="button" type="submit" name="submitForm" value="Add Record &gt;&gt;" />

</form>

</body>
</html>
