<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php
  require_once 'server_functions.php';

  globalize_request_keys('comment_id', 'submit', 'comments', 'del', 'query_id');

  # be sure to validate comment_id

  if (!$comment_id) {
    print "no comment to edit.";
    return;
  }

  require_once 'comment.php';

  $comment = new Comment();

  $crec = $comment->Info_By_Comment_ID ($comment_id);

  $ticket_id = $crec->Get ('ticket_id');

  if (!$ticket_id) {
    print "unable to edit comment (can't get ticket_id).";
    return;
  }

  require_once 'ticket.php';

  $ticket = new Ticket();

  $trec = $ticket->Info_by_Ticket_ID ($ticket_id);

  require_once 'person.php';

  $person = new Person();

  $remote_user_p_rec = $person->Info_by_Map_ID ($REMOTE_USER_PCRM_MAP_ID);

  $mod_tickets = $remote_user_p_rec->Get ('modify_tickets');

  $edit_flag = false;

  ## Y always modifies
  ## R modifies if they own the ticket
  ## N no modifiable

  if ($trec->Get ('owner_crm_id') == $REMOTE_USER_CRM_ID) {
    if ($mod_tickets == 'Y') {
      $edit_flag = true;

    } elseif ($mod_tickets == 'R') {

      if (($REMOTE_USER == $trec->Get ('support_person_id')) or
          ($REMOTE_USER == $trec->Get ('escalated_to_person_id'))) {
        $edit_flag = true;
      }
    }
  }

  if (!$edit_flag) {
    print <<<EOF
You don't have permission to edit this ticket.  If you think you should be able to modify this ticket make sure you are logged into the proper <a href="change_crm">RMS</a>.
EOF;
    return;
  }

  if ($submit) {
    $deleted_attachment_ids = array();
    $saved_attachment = false;
    $did_update = false;

    $errors = array ();

    $f = $_FILES['afile'];

    if (is_uploaded_file ($f['tmp_name'])) {
      $file = '';
      $fp = fopen ($f['tmp_name'], 'r');
      while (!feof ($fp)) {
        $file .= fread ($fp, 1000000);
      }
      fclose ($fp);

      unlink ($f['tmp_name']);

      if (!$comment->Save_Attachment ($comment_id, basename($f['name']), 
	  $f['type'], $REMOTE_USER, $file)) {
        $errors[] = "unable to upload file.";
      } else {
	$saved_attachment = true;
      }
    }

    if (!count($errors) and $comments and ($comments != $crec->Get ('comments'))) {
      $crec->SaveState();
      $crec->Set ('comments', $comments);
      $crec->Set ('updated_by', $REMOTE_USER);
      $ok = $crec->Update ();

      if (!$ok) {
	$errors[] = "unable to update comment.";
      } else {
	$did_update = true;
      }

      # refetch to make sure it saved
      $crec = $comment->Info_By_Comment_ID ($comment_id);
    }

    ## need to validate these
    if (!count($errors) and count($del)) {
      foreach ($del as $attach_id) {
        if (!$comment->Delete_Attachment ($attach_id)) {
          $errors[] = "unable to delete attachment";
	  break;
        }
	$deleted_attachment_ids[] = $attach_id;
      }
    }

    if (count($errors)) {

      if ($saved_attachment && $errors[] = "deleting last saved attachment" && !$comment->DeleteLastSavedAttachment()) {
	$errors[] = "Unable to delete last saved attachment.";
      }
      if ($did_update) {
	$errors[] = "restoring previous state";
	$crec->RestoreState();
	if (!$crec->Update()) {
	  $errors[] = "Unable to undo comment update.";
	}
      }
      foreach ($deleted_attachment_ids as $attach_id) {
	$errors[] = "undeleting record $attach_id";
	$comment->Undelete_Attachment_Record($attach_id);
      }

      array_unshift ($errors, "No changes saved:");

      foreach ($errors as $e) {
        print "<font color=\"red\">$e</font><br />\n";
      }

    }

  }

  $attachments = $comment->Attachments ($comment_id);

  $comments = htmlentities ($crec->Get ('comments'));

  print <<<EOF

<form action="$_SERVER[PHP_SELF]" method="post" enctype="multipart/form-data">
<input type="hidden" name="comment_id" value="$comment_id" />
<input type="hidden" name="ticket_id" value="$ticket_id" />
<input type="hidden" name="query_id" value="$query_id" />
<center>
<a href="edit_ticket?ticket_id=$ticket_id&amp;query_id=$query_id" class="textmed">« back to ticket $ticket_id</a>
</center>

<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>
EOF;

  print Sub_Heading ('Attachments');

  if (count($attachments)) {
    print <<<EOF

<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>

<table border="0" bgcolor="#E0E0E2" cellpadding="0" cellspacing="0">
<tr>
<td class="textsm" align="left" nowrap="nowrap" width="100%">

<table cellpadding="2" border="0" cellspacing="1" width="100%">
<tr>
  <th class="textsm" align="left">Del</th>
  <th class="textsm" align="left">Name - Type</th>
</tr>
EOF;
  }

  foreach ($attachments as $a) {
    $aname = htmlentities ($a['attachment_name']);

    print <<<EOF
<tr>
  <td bgcolor="#ffffff"><input type="checkbox" name="del[]" value="$a[attachment_id]" /></td>
  <td bgcolor="#ffffff"><a href="../email/viewattachment/$aname?attach_id=$a[attachment_id]" class="textsm" target="_blank">$aname - $a[attachment_type]</a></td>
</tr>
EOF;
  }

  if (count($attachments)) {
    print <<<EOF
  </table></td>
</tr>
</table>
<br />
<input type="submit" name="submit" class="textsm" value="Delete Attachments" />
</form>
<form action="$_SERVER[PHP_SELF]" method="post" enctype="multipart/form-data">
<input type="hidden" name="comment_id" value="$comment_id" />
<input type="hidden" name="ticket_id" value="$ticket_id" />
<input type="hidden" name="query_id" value="$query_id" />
EOF;
  }

  $sub_heading = Sub_Heading ('Comment');

  print <<<EOF

<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>
Attach a new file to this comment:<br />
<input name="afile" type="file" class="textsm" /> <input type="submit" name="submit" class="textsm" value="Upload" />

<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>

$sub_heading

<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>
<textarea name="comments" cols="79" rows="9" class="text">$comments</textarea>



<table width="10" border="0" cellpadding="0" cellspacing="0">
<tr>  <td class="textsm" align="left" nowrap="nowrap" width="10">
<img alt="" src="../../img/cldot.gif" width="1" height="1" />
</td>  </tr>
</table>

<center><table cellspacing="0" cellpadding="2" border="0" bgcolor="#de0000">
<tr>
<td><input type="submit" name="submit" class="textsm" value="Save Changes" /></td>
</tr>
</table></center>

</form>

<center>
<a href="edit_ticket?ticket_id=$ticket_id&amp;query_id=$query_id" class="textmed">« back to ticket $ticket_id</a>
</center>

EOF;

?>
