<?
$pre     = '';
include "common.php";
$title   = "Paper Request / Update";
$logo    = false;
$success = false;
$email   = false;

if( !empty($_POST) && !(eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['contact_email']) ||
    eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4},\ *[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['contact_email'])) ){
   $indication = 'A valid email address is required.';
}
else{
   $email = true;
}
if($_POST[but] == "Submit" && $email){
   $data = $_POST;
   $data['title'] = set_paper_title($data['title']);
   $data['journal_title'] = get_journal_title($data['journal_id']);
   $id = $data['contrib_num'];
   $suf = ''; // compiles the SET statement
   foreach($data as $key => $value){
      if($key == "but" || $key == "contrib_num")
         $value = '';
      else
         $value = addslashes($data[$key]);

      if(!empty($value))
         $suf .= $key . "='" . $value . "', ";
      if(empty($value) && $key != "contrib_num" && $key != "but")
         $suf .= $key . "=NULL, ";
   } // foreach($data as $key => $value)

   $suf = substr($suf, 0, -2); //cuts the last ", " off

   $tbname  = TABLE_PAPERS_PURG;
   $primary = "contrib_num";

   $query   = "SELECT $primary FROM $tbname WHERE $primary=$id";
   $result  = $db->sql_query($query);
   $num     = $db->sql_numrows($result);
   if($num == 1){
      $query  = "UPDATE $tbname SET $suf WHERE contrib_num=$id";
      //echo $query;
      $result = $db->sql_query($query);
      if(!$result){
         $indication = "unknown error please try again";
      }
      else{
         $indication = get_paper_title($data['title']) . ' Update Submitted!<br /><br />
            <input type="submit" value="Done" onClick="window.close()">';
         $message    = 'An update to the paper "' . get_paper_title($data['title']) . '" has been submitted:' . 
            "\n\nhttp://www.usglobec.org/admin/paper_approve.php?ind=" . $data['contrib_num'];
         //echo $message;
         mail(PAPER_ADMIN,"Paper Update",$message,FROM);
         $success = true;
      }
   } // if($num == 1)
   else{
      if(empty($id)){
         $id      = get_next_free_contrib_num();
         $type    = ' Request ';
         $message = 'A contribution number request for "';
      }
      else{
         $type    = ' Update ';
         $message = 'An update for "';
      }

      $suf   .= ", contrib_num=$id";

      $query  = "INSERT INTO $tbname SET $suf";
      //echo $query;
      $result = $db->sql_query($query);
      if(!$result){
         $indication = "unknown error please try again";
      }
      else{
         $indication = get_paper_title($data['title']) . $type . 'Submitted!<br /><br />
            <input type="submit" value="Done" onClick="window.close()">';
         $message   .= get_paper_title($data['title']) . '" has been submitted:' . 
            "\n\nhttp://www.usglobec.org/admin/paper_approve.php?ind=" . $id;
         mail(PAPER_ADMIN,"Paper" . $type,$message,FROM);
         $success = true;
      }
   } // else
} // if($_POST[but] == "Submit")
$primary="contrib_num";
$id = (!empty($_GET['ind'])) ? $_GET['ind'] : '';

if(!empty($indication) && $indication != "unknown error please try again"){
   $query = "SELECT * FROM " . TABLE_PAPERS_PURG . " WHERE $primary=" . $id;
}
else{
   $query = "SELECT * FROM " . TABLE_PAPERS . " WHERE $primary=" . $id;
}

if(!empty($_POST)){
   $row = $_POST;
}
else{
   $result = $db->sql_query($query);
   $row    = $db->sql_fetchrow($result);
}
//echo $query;
include HEAD;
?>
<?
echo '<h3>' . $indication . '</h3>';
if($success){
   echo '</body></html>';
   exit;
}
$indication = '';
if($_POST['but'] == "Preview"){
   echo display_paper($_POST);
   echo "\n<hr />\n<br />\n";   
}
?>
<form name="mainform" method="post" action="<?=PUBLIC_PAPER . linker($_GET)?>">
<table>
<?
if(isset($row['contact_email']))
   $tmp=stripslashes($row['contact_email']);
else
   $tmp='';
?>
<tr>
   <td>
      <font class="NORMAL">*Contact Email:</font>
   </td>
   <td>
      <input type="text" name="contact_email" value="<?=$tmp?>" size="40" maxlength="255" onkeypress="return noenter(this,event)" />
   </td>
</tr>

<?
if(!empty($id)){
?>

<tr>
   <td valign="baseline">
      <font class="NORMAL">Contribution Number: </font>
   </td>
   <td valign="baseline">
      <?=$id?> <input type="hidden" name="contrib_num" value="<?=$id?>">
   </td>
</tr>

<?
}
?>

<tr>
   <td valign="middle">
      <font class="NORMAL">Title:</font>
   </td>
   <td valign="baseline">
<?
      //echo '<form name="mainform" method="post" action="paper_edit.php' . linker($_GET) . "\">\n";
      echo '<textarea name="title" cols="40" rows="3">' . get_paper_title($row['title']) . '</textarea>';
?>
   </td>
</tr>
<?
if(isset($row['reference']))
   $tmp=stripslashes($row['reference']);
else
   $tmp='';
?>
<tr>
   <td>
      <font class="NORMAL">Reference:</font>
   </td>
   <td>
      <input type="text" name="reference" value="<?=$tmp?>" size="40" maxlength="255" onkeypress="return noenter(this,event)" />
   </td>
</tr>
<?
if(isset($row['status']))
   $tmp=stripslashes($row['status']);
else
   unset($tmp);
?>
<tr>
   <td>
      <font class="NORMAL">Status:</font>
   </td>
   <td>
      <select size="1" name="status">
         <option value="0">-----Select One-----</option>
         <option value="Accepted" <?  if(isset($tmp) && $tmp=="Accepted")  echo 'selected'; ?>>Accepted</option>
         <option value="In Press" <?  if(isset($tmp) && $tmp=="In Press")  echo 'selected'; ?>>In Press</option>
         <option value="Published" <? if(isset($tmp) && $tmp=="Published") echo 'selected'; ?>>Published</option>
      </select>
   </td>
</tr>
<?
if(isset($row['date']))
   $tmp=stripslashes($row['date']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Date(YYYY):</font>
   </td>
   <td>
      <input type="text" name="date" value="<?=$tmp?>" size="20" maxlength="255" onkeypress="return noenter(this,event)" />
   </td>
</tr>
<?
if(isset($row['authors']))
   $tmp=stripslashes($row['authors']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Authors:</font>
   <td>
      <textarea name="authors" cols="40" rows="3"><?=$tmp?></textarea>
   </td>
</tr>
<?
if(isset($row['type']))
   $tmp=$row['type'];
else
   unset($tmp);
?>
<tr>
   <td>
      <font class="NORMAL">Type:</font>
   </td>
   <td>
      <select size="1" name="type">
         <option value="">-----Select One-----</option>
         <option value="Journal" <?      if(isset($tmp) && $tmp=="Journal")      echo 'selected'; ?>>Journal</option>
         <option value="Dissertation" <? if(isset($tmp) && $tmp=="Dissertation") echo 'selected'; ?>>Dissertation</option>
         <option value="Book" <?         if(isset($tmp) && $tmp=="Book")         echo 'selected'; ?>>Book</option>
         <option value="Chapter" <?      if(isset($tmp) && $tmp=="Chapter")      echo 'selected'; ?>>Chapter(s)</option>
         <option value="Report" <?       if(isset($tmp) && $tmp=="Report")       echo 'selected'; ?>>Report</option>
         <option value="Other" <?        if(isset($tmp) && $tmp=="Other")        echo 'selected'; ?>>Other</option>
      </select>
   </td>
</tr>
<tr>
   <td>
      <font class="NORMAL">Journal:</font>
   </td>
   <td>
<?
      $query   = "SELECT id,title FROM journals ORDER BY title";
      $result  = $db->sql_query($query);
      $jour[0] = "N/A";
      $i = 1;
      while($jrow = $db->sql_fetchrow($result)){
         $jind[$i] = $jrow['id'];
         $jour[$i] = $jrow['title'];
         $i++;
      }
      $num=count($jour);
      echo '<select name="journal_id">';
      for($i=0;$i<$num;$i++){
         if($jind[$i]!=$row['journal_id']){
            echo "<option value=\"$jind[$i]\">".stripslashes($jour[$i])."</option>\n";
         }
         else{
            echo "<option selected value=\"$jind[$i]\">".stripslashes($jour[$i])."</option>\n";
         }
      }
?>
      </select>
   </td>
</tr>
<?
if(isset($row['pub_title']))
   $tmp=stripslashes($row['pub_title']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Publication/Issue Title:</font>
   </td>
   <td>
      <textarea name="pub_title" cols=40 rows=3><?=$tmp?></textarea>
   </td>
</tr>
<?
if(isset($row['doi']))
   $tmp=stripslashes($row['doi']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">DOI Number:</font>
   </td>
   <td>
      <input type="text" name="doi" value="<?=$tmp?>" size="20" maxlength="255" onkeypress="return noenter(this,event)" />
   </td>
</tr>
<?
if(isset($row['volume']))
   $tmp=stripslashes($row['volume']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Volume:</font>
   </td>
   <td>
      <input type="text" name="volume" value="<?=$tmp?>" size="20" maxlength="45" onkeypress="return noenter(this,event)" />
   </td>
</tr>
<?
if(isset($row['pages']))
   $tmp=stripslashes($row['pages']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Pages:</font>
   </td>
   <td>
      <input type="text" name="pages" value="<?=$tmp?>" size="20" maxlength="50" onkeypress="return noenter(this,event)" />
   </td>
</tr>
<?
if(isset($row['link']))
   $tmp=stripslashes($row['link']);
else
   $tmp="";
?>
<tr>
   <td>
      <font class="NORMAL">Link:</font>
   </td>
   <td>
      <input type="text" name="link" value="<?=$tmp?>" size="50" maxlength="255" onkeypress="return noenter(this,event)" />
   </td>
</tr>
</table>
<br /><br />
<input type="submit" name="but" value="Preview" /> <input type="submit" name="but" value="Submit" />
</form>
<?
if(!empty($_GET['act']) && $_GET['act'] == 'upd'){
   echo '<form name="cancel" method="post" onSubmit="window.close()">';
}
else{
   echo '<form name="cancel" method="post" action="index.php">';
}
?>
   <input type="submit" name="but" value="Cancel" />
</form>
<?
if(!empty($_GET['ind'])){
?>
<b>NOTES:</b><br />
<b>*:</b> This email address is only used to notify you when/if your changes are accepted.
You may enter up to <b>2</b> email addresses separated by a comma.
<?
}
else{
?>
<b>NOTES:</b><br />
<b>*:</b> This email address is where the contribution number will be sent when/if your paper is approved.
You may enter up to <b>2</b> email addresses separated by a comma.
<?
}
include FOOT;
?>
