<?php
  // This form requires: CMSB v2.04 Build 1
  $libraryPath = 'MY/viewer_functions.php';
  $dirsToCheck = array('C:/MYSITE/www/','','../','../../','../../../');
  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       = 'images';
  $recordNum       = '1'; // 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     = '1';
    }
  }

?><!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" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="tinybox2/style.css" />
<script type="text/javascript" src="tinybox2/tinybox.js"></script>
<script src="uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
  <link rel="stylesheet" href="3rdParty/SimplaAdmin/css/style.css" type="text/css" media="screen" /><!-- Main Stylesheet -->

  <link rel="stylesheet" href="3rdParty/SimplaAdmin/css/blue.css" type="text/css" media="screen" /><!-- options: green, blue, red -->
  <!--[if lte IE 7]><link rel="stylesheet" href="3rdParty/SimplaAdmin/css/ie.css" type="text/css" media="screen" /><![endif]-->
  <link rel="stylesheet" href="3rdParty/jqueryPlugins/thickbox.css" type="text/css" media="screen" />
  <link rel="stylesheet" href="uploadify/uploadify.css" type="text/css" media="screen" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>
<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" align="center" cellpadding="2" cellspacing="0">
 <tr>
   <td valign="top"></td>
   <td align="center">
     
     <?php // load uploads
      $fieldName     = 'images';
      $tempUploads   = getUploadRecords($tableName, $fieldName, $recordNum, $preSaveTempId);
    ?>
    <div id="images" style="width:700px;">
 <table border="0" cellspacing="0" cellpadding="0" class="data sortable uploadlist">
 <thead>
 <tr class="nodrag nodrop">

  <th width="60" style="text-align: center">Preview</th>
  <th width="260">Details</th>

  <th width="130" style="text-align: center" colspan="2">Commands</th>

 </tr>
 </thead>
 <?php foreach ($tempUploads as $upload): ?>
    <tr class="uploadRow">

     <td width="60" style="text-align: center; vertical-align: middle; padding: 2px;"><?php if ($upload['hasThumbnail']): ?>
     <img src="<?php echo $upload['thumbUrlPath3'] ?>" width="<?php echo $upload['thumbWidth3'] ?>" height="<?php echo $upload['thumbHeight3'] ?>" alt="" />
     <?php endif ?></td>
     <td width="260" style="vertical-align: top" valign="top"><?php echo $upload['filename'] ?></td>
       <td width="130" style="text-align: center; vertical-align: middle;"></td>
       <td width="130" style="text-align: center; vertical-align: middle;"><a href="#" onclick="document.uploadForm.removeUpload.value='<?php echo $upload['num'] ?>';document.uploadForm.submit();">remove</a></td>
    </tr> <?php endforeach ?>
</table>       
 
  <?php if (!$tempUploads): ?>
     There are no uploads yet.<br/><br/>
     <?php endif ?></div>

    <input id="file_upload" name="file_upload" type="file" multiple>

	<script type="text/javascript">
		$(function() {
			$('#file_upload').uploadify({
				'buttonText' : 'SELECT FILES',
				'swf'      : '/uploadify/uploadify.swf',
				'uploader' : 'uploadaddform2.php?preSaveTempId=<?php echo $preSaveTempId ?>'
			});
		});
	</script>
    
     </td>
</tr>
</table>
</form>

</body>
</html>
