<?/*
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(
    'save_change', 'submit_button', 'query_id', 'ticket_id', 'atts',
    'last_name', 'first_name', 'company', 'work_phone', 'email',
    'info1', 'info2', 'account_id', 'additional_info', 'subject',
    'status', 'support_person_id', 'escalated_to_person_id',
    'problem_id', 'use_help', 'mass_queue', 'move_to_crm_id', 'next', 'back',
    'forward', 'send_user_a', 'send_support_a', 'send_internal_a',
    'send_other_a', 'template_id');

  if ($save_change && !$submit_button) {
    $submit_button = $save_change;
  }

  $USER_COMMENT = 'com-customer';
  $ESCALATED_COMMENT = 'com-escalated';
  $SUPPORT_COMMENT = 'com-owner';
  $OTHER_CRM_COMMENT = 'com-other';

  require_once 'dd.php';
  require_once 'db.php';
  require_once 'ticket.php';
  require_once 'person.php';
  require_once 'comment.php';
  require_once 'display.php';
  require_once 'crm.php';
  require_once 'attribute_display.php';

  $ticket_p = new Ticket ();
  $person_p = new Person ();
  $comment = new Comment ();
  $crm = new CRM ();
  $attv = new Attribute_Value ();

  if (in_array ($submit_button, array('SAVE','Compose'))) {

    if (count($atts)) $attv->add_cleared_att_vals ($ticket_id, $atts);

    $ticket = $ticket_p->Info_by_Ticket_ID ($ticket_id);
    $person = $person_p->Info_by_Person_ID ($ticket->Get ('user_person_id'));

    foreach (array (
      'last_name',
      'first_name',
      'company',
      'work_phone',
      'email'
    ) as $val) {
      $person->Set ($val, $$val);
    }
    $person->Set ('updated_by', $REMOTE_USER);
    $person->Update ();

    ## re fetch to make sure update took
    $person = $person_p->Info_by_Person_ID ($ticket->Get ('user_person_id'));

    foreach (array (
      'info1',
      'info2',
      'account_id',
      'additional_info',
      'subject',
    ) as $val) {
      $ticket->Set ($val, $$val);
    }

    foreach (array (
      'status',
      'support_person_id',
      'escalated_to_person_id',
      'problem_id',
      'use_help',
      'mass_queue',
    ) as $val) {
      if ($$val) $ticket->Set ($val, $$val);
    }

    $ticket->Set ('updated_by', $REMOTE_USER);

    if (!$ticket->Update ()) {
      echo("<font color=\"red\">Unable to update ticket: <pre>\n" .
	htmlentities($ticket->error) . "\n</pre></font>");
      return;
    }

    ## re fetch to make sure update took
    $ticket = $ticket_p->Info_by_Ticket_ID ($ticket_id);

    if ($move_to_crm_id) {
      $ok = $ticket->Move_To ($ticket_id, $move_to_crm_id, $REMOTE_USER_CRM_ID, $REMOTE_USER);

      if (!$ok) {
        print '<font class="errors">Unable to move ticket, please make sure the other RMS is completely setup.</font>';
        return;
      }
    }

    if (!$move_to_crm_id and ($submit_button == 'Compose')) {
      ## There's probably some less dumb way to do this which involves
      ## $$varname or whatever.
      $ctypes = '';
      if ($send_user_a)     $ctypes .= "&send_user=$send_user_a";
      if ($send_support_a)  $ctypes .= "&send_support=$send_support_a";
      if ($send_internal_a) $ctypes .= "&send_internal=$send_internal_a";
      if ($send_other_a)    $ctypes .= "&send_other=$send_other_a";

      redirect ("../email/review_email?ticket_id=$ticket_id&query_id=$query_id&template_id=$template_id$ctypes", 1);
    }
  }

  if ($query_id) {

    require_once 'search_result.php';
    $sc = new Search_Result ();

    if ($submit_button == 'GO') {
      if (eregi ('back', $next)) {
        $next = '-'.$back;
      } elseif (eregi ('forward', $next)) {
        $next = $forward;
      }

      $ticket_id = $sc->Get_ID ($query_id, $ticket_id, $next);
    }

    $dec_params = "?ticket_id=$ticket_id&amp;submit_button=GO&amp;query_id=$query_id&amp;next=";
    $dec_url = $_SERVER['PHP_SELF'].$dec_params;

  $current_ticket_count = $sc->Current_Ticket ($ticket_id, $query_id);
  $total_result_count = $sc->Total_Results ($query_id);

  $result_diff_count = $total_result_count - $current_ticket_count;

$back_next_text = <<<EOF

<!-- begin ticket nav -->

<table width="100%" border="0" cellspacing="0" cellpadding="3">
<form action="$_SERVER[PHP_SELF]" method="get">
<input type="hidden" name="ticket_id" value="$ticket_id" />
<input type="hidden" name="query_id" value="$query_id" />
<input type="hidden" name="submit_button" value="GO" />

<tbody>
<tr valign="middle">
  <td align="left" valign="middle" nowrap="nowrap" class="textsm"><a href="$_SERVER[PHP_SELF]?ticket_id=$ticket_id&amp;submit_button=GO&amp;query_id=$query_id&amp;next=-1"><b>&laquo;</b> back</a></td>
<!--
  <td align="left" valign="middle" nowrap="nowrap" class="textsm"><a href="$_SERVER[PHP_SELF]?ticket_id=$ticket_id&amp;submit_button=GO&amp;query_id=$query_id&amp;next=back&back=$total_result_count"><b>&laquo;</b> back</a></td>
-->
  <td align="center" valign="middle" nowrap="nowrap" class="textsm">[showing &nbsp; $current_ticket_count of $total_result_count | <a href="list?query_id=$query_id">see list</a>]
</td>
  <td align="right" valign="middle" nowrap="nowrap" class="textsm"><a href="$_SERVER[PHP_SELF]?ticket_id=$ticket_id&amp;submit_button=GO&amp;query_id=$query_id&amp;next=1">next &raquo;</a></td>
<!--
  <td align="right" valign="middle" nowrap="nowrap" class="textsm"><a href="$_SERVER[PHP_SELF]?ticket_id=$ticket_id&amp;submit_button=GO&amp;query_id=$query_id&amp;next=$result_diff_count">next &raquo;</a></td>
-->
</tr>
<tr valign="middle">
  <td align="left" valign="middle" nowrap="nowrap" class="textsm"><input size="2" name="back" class="textsm" />&nbsp;<input type="submit" value="back" name="next" class="textsm" /></td>
  <td align="center" valign="middle" nowrap="nowrap" class="textsm"></td>
  <td align="right" valign="middle" nowrap="nowrap" class="textsm"> <input type="submit" name="next" value="forward" class="textsm" />&nbsp;<input size="2" name="forward" class="textsm" /></td>
</tr>

</tbody>
</form>
</table>



<!-- end ticket nav -->
EOF;

    print $back_next_text;

  } else {
    $back_next_text = '';

    $dec_params = "?ticket_id=$ticket_id&submit_button=GO&query_id=$query_id&next=";
  }

  $ticket = $ticket_p->Info_by_Ticket_ID ($ticket_id);
  $person = $person_p->Info_by_Person_ID ($ticket->Get ('user_person_id'));

  if (!$ticket->Get ('ticket_id')) {
    print "No Records found.\n";
    return;
  }

  ## determine read/write status

  $remote_user_p_rec = $person_p->Info_by_Map_ID ($REMOTE_USER_PCRM_MAP_ID);
  if (!$remote_user_p_rec) {
    echo("<font color=\"red\">edit_ticket.php: <pre>\n" 
      . htmlentities($person_p->error) . "</pre>\n");
    return;
  }

  $display = new Display ();

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

  $edit_flag = 0;

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

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

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

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

  $display->Set_Edit_Flag ($edit_flag);
  $dd->Set_Edit_Flag ($edit_flag);

  $crm_access = $crm->Access_To ($REMOTE_USER_CRM_ID, DB_FETCHMODE_ASSOC);
  $crm_list = array ();

  foreach ($crm_access as $c) {
    $crm_list[] = array ($c['access_to_crm_id'], $c['access_to_crm_name']);
  }

  $crm_dd = $dd->Multi_Array_Drop_Down ($crm_list);
?>

<!-- begin new ticket area -->

<script type="text/javascript">
  function submit_page () {
    document.mainform.save_change.value = 'SAVE';
    document.mainform.submit ();
    return 1;
  }
</script>



<table width="100%" border="0" cellpadding="0" cellspacing="0">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="mainform">
<input type="hidden" name="ticket_id" value="<?php echo $ticket_id; ?>" />
<input type="hidden" name="query_id" value="<?php echo $query_id; ?>" />
<input type="hidden" name="save_change" value="" />

<?php if ($query_id) { ?>
<tr>
  <td align="center" background="../../img/dotline.gif"><img alt="" src="../../img/cldot.gif" width="1" height="14" /></td>
</tr>
<?php } ?>


<!-- New Compose Email Header Starts

<tr>
  <td align="center"><img alt="" src="../../img/cldot.gif" width="1" height="5" /></td>
</tr>

<tr>
  <td align="center">

<table class='emailbox' width='100%' cellpadding="0" border="0" cellspacing="0">
<tr><td class="composebox" valign="middle" align="center">
<?php
  echo $display->Value ('<b class="commentsatextmedgrn">Send comments:&nbsp;</b></font>
    <input type="checkbox" name="comment_type[]" value="user"><font class="commentstxt">user</font></input>
    <input type="checkbox" name="comment_type[]" value="internal"><font class="commentstxt">internal</font></input>
    <input type="checkbox" name="comment_type[]" value="support"><font class="commentstxt">support</font></input>
    <input type="checkbox" name="comment_type[]" value="other"><font class="commentstxt">other rms</font></input>

&nbsp;<input type="submit" value="Compose Email" name="submit_button" class="btn-on" />', '&nbsp;');?>

</td>

</table>

</td>
</tr>

<tr>
  <td align="center"><img alt="" src="../../img/cldot.gif" width="1" height="5" /></td>
</tr>

 New Compose Email Header Ends -->


<tr>
  <td align="center">
    <table width="100%" border="0" cellpadding="3" cellspacing="0">
    <tr>
      <td align="center" valign="top">
        <table border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td rowspan="3"><img alt="" src="../../img/ticket-lf.gif" width="8" height="70" /></td>
          <td valign="top" colspan="2" height="3" background="../../img/ticket-top.gif"><img alt="" src="../../img/cldot.gif" width="1" height="3" /></td>
          <td rowspan="3"><img alt="" src="../../img/ticket-rt.gif" width="8" height="70" /></td>
        </tr>
        <tr>
          <td valign="middle" align="center" bgcolor="C6E192" height="64" nowrap="nowrap" class="ticket">&nbsp;TICKET&nbsp;</td>
          <td valign="middle" align="center" bgcolor="EDEDEE" height="64" nowrap="nowrap" class="ticketnumber">&nbsp;<?php echo $ticket_id; ?>&nbsp;</td>
        </tr>
        <tr>
          <td valign="bottom" colspan="2" height="3" background="../../img/ticket-btm.gif"><img alt="" src="../../img/cldot.gif" width="1" height="3" /></td>
        </tr>
        </table>

<!--Start Nav-->

        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#E6F0CE">
        <tr>
          <td><img alt="" src="../../img/elipsefill-top-corn-left.gif" width="16" height="15" /></td>
          <td background="../../img/elipsefill-vert-line-top.gif"><img alt="" src="../../img/cldot.gif" width="1" height="15" /></td>
          <td> <img alt="" src="../../img/elipsefill-top-corn-right.gif" width="15" height="15" /></td>
        </tr>
        <tr>
          <td background="../../img/elipsefill-vert-line-left.gif" rowspan="5"><img alt="" src="../../img/cldot.gif" width="16" height="1" /></td>
          <td bgcolor="#E6F0CE" class="textsm" align="left">

<!-- begin included edit page -->

<?php
  $att = new Attribute_Display ($dbi);

  require_once 'template.php';

  $template = new Template ();

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

<!-- end included edit page -->

          </td>
          <td background="../../img/elipsefill-vert-line-right.gif" rowspan="5"><img alt="" src="../../img/cldot.gif" width="15" height="1" /></td>
        </tr>
        <tr>
          <td class="textsm" align="left" background="../../img/dotline.gif"><img alt="" src="../../img/cldot.gif" width="1" height="14" /></td>
        </tr>
        <tr>
          <td class="textsm" align="left"><b>Browser:</b> <?php echo $ticket->Get ('browser'); ?><br />
<b>Source:</b>
<?php
  $old_ronly = $dd->Editable ();
  $dd->Set_Edit_Flag (0);
  echo $dd->sources ($ticket->Get ('source_id'));
  $dd->Set_Edit_Flag ($old_ronly);
?>
          </td>
        </tr>
        <tr>
          <td class="textsm" align="left" background="../../img/dotline.gif"><img alt="" src="../../img/cldot.gif" width="1" height="14" /></td>
        </tr>
        <tr>
          <td class="textsm" align="center">
	   <img width="110" height="100" src="../../img/comments-key.gif"/>
          </td>
        </tr>
        <tr>
          <td><img alt="" src="../../img/elipsefill-btm-corn-left.gif" width="16" height="15" /></td>
          <td background="../../img/elipsefill-vert-line-btm.gif"><img alt="" src="../../img/cldot.gif" width="1" height="15" /></td>
          <td> <img alt="" src="../../img/elipsefill-btm-corn-right.gif" width="15" height="15" /></td>
        </tr>
        </table>

<!--End Nav-->

      </td>
      <td align="center" valign="top">

<!-- begin included edit page rnav -->

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

<!-- end included edit page rnav -->

      </td>
    </tr>
    </table>
  </td>
</tr>

<tr>
  <td align="center" background="../../img/dotline.gif"><img alt="" src="../../img/cldot.gif" width="1" height="14" /></td>
</tr>

<tr>
  <td align="center"><img alt="" src="../../img/cldot.gif" width="1" height="5" /></td>
</tr>
<!--
<tr>
  <td align="center">

<table class='emailbox' width='100%' cellpadding="0" border="0" cellspacing="0">
<tr><td class="composebox" valign="middle" align="center">
<?php
  echo $display->Value ('<b class="commentsatextmedgrn">Send comments:&nbsp;</b></font>
    <input type="checkbox" name="comment_type[]" value="user"><font class="commentstxt">user</font></input>
    <input type="checkbox" name="comment_type[]" value="internal"><font class="commentstxt">internal</font></input>
    <input type="checkbox" name="comment_type[]" value="support"><font class="commentstxt">support</font></input>
    <input type="checkbox" name="comment_type[]" value="other"><font class="commentstxt">other rms</font></input>

&nbsp;<input type="submit" value="Compose Email" name="submit_button" class="btn-on" />', '&nbsp;');?>

</td>

</table>

</td>
</tr>-->

<tr>
  <td align="center"><img alt="" src="../../img/cldot.gif" width="1" height="5" /></td>
</tr>

<?php if ($query_id) { ?>
<!--<tr>
  <td align="center" background="../../img/dotline.gif"><img alt="" src="../../img/cldot.gif" width="1" height="14" /></td>
</tr>-->
<?php } ?>
</form>
</table>


<!-- end new ticket area -->

<?php print $back_next_text; ?>
