<?/*
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 'dd.php';
  require_once 'ticket.php';
  require_once 'person.php';
  require_once 'comment.php';
  require_once 'crypt.php';
  require_once 'config.php';
  require_once 'server_functions.php';

  globalize_request_keys('comments');

  $dd->Set_Edit_Flag (0);

  $ticket_p = new Ticket ();
  $comment = new Comment ();
  $crypt = new Crypt ();
  $config = new Config ();

  $params = explode ('/', $_SERVER['PATH_INFO']);
  $enc_id = $params[count($params)-1];

  $ticket_id = $crypt->decrypt ($enc_id);

  $ticket = $ticket_p->Info_by_Ticket_ID ($ticket_id);
  $user_pid = $ticket->Get ('user_person_id');

  if (!$ticket->Get ('ticket_id')) {
    print "Invalid ticket\n";
    return;
  }

  global $REMOTE_USER_CRM_ID;
  $REMOTE_USER_CRM_ID = $ticket->Get('crm_id');

  $person_p = new Person ();

  if ($comments) {
    $person = $person_p->Info_by_Person_ID ($user_pid);
    $updated_ticket = false;

    #### ROLLBACK CODE STARTS HERE

    $com_id = $comment->Add (array (
      'comments' => $comments,
      'person_id' => $user_pid,
      'updated_by' => $user_pid,
      'ticket_id' => $ticket_id,
    ));

    $errors = array ();
  
    if ($com_id and count($_FILES)) {

      foreach ($_FILES as $f) {

        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']);
    
print "<pre>browse.php: calling Save_Attachment()</pre>";
          if (!$comment->Save_Attachment ($com_id, basename($f['name']), 
	      $f['type'], "new ticket - $user_pid", $file)) {
            $errors[] = "unable to upload file: ".$comment->error;
print "<pre>Error saving attachment:\n";
print_r($errors);
print "</pre>";
	    break;
          }
        }
      }
    }

    $ok = !count($errors);

    if ($ok && $ticket->Get ('status') == 'Closed') {
      $ticket->SaveState();
      $ticket->Set ('status', 'ReOpen');
      $ok = $ticket->Update ();
      $updated_ticket = true;
      $ticket = $ticket_p->Info_by_Ticket_ID ($ticket_id);
    }

    if (!$ok or !$com_id or count($errors)) {

      ## Delete the comment and any attachments.
      if ($com_id) {
	$comment->DeleteAllAttachments($com_id);
	$comment->Delete($com_id);
      }

      ## Undo any ticket updates.
      if ($updated_ticket) {
	$ticket->RestoreState();
	$ticket->Update();
      }

      print "Unable to save changes.";
    }

    #### ROLLBACK CODE STOPS HERE
  }

  if (is_null($comment_id_list = $comment->List_By_Ticket ($ticket_id, 1, '%I:%i%p %b %d %Y'))) die('browse.php: ' . $comment->error);

  $dd->Set_Edit_Flag (0);

  $problem = $dd->problem_types ($ticket->Get('problem_id'));

  require_once 'template.php';

  $template = new Template ();

  include $template->load ('status.php');
?>
