<?php
/*
 * @version $Id: ticket.form.php 20130 2013-02-04 16:55:15Z moyo $
 -------------------------------------------------------------------------
 GLPI - Gestionnaire Libre de Parc Informatique
 Copyright (C) 2003-2013 by the INDEPNET Development Team.

 http://indepnet.net/   http://glpi-project.org
 -------------------------------------------------------------------------

 LICENSE

 This file is part of GLPI.

 GLPI 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.

 GLPI 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 GLPI. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

// ----------------------------------------------------------------------
// Original Author of file:
// Purpose of file:
// ----------------------------------------------------------------------

define('GLPI_ROOT', '..');
include (GLPI_ROOT . "/inc/includes.php");

Session::checkLoginUser();
$fup   = new TicketFollowup();
$track = new Ticket();

if (!isset($_GET['id'])) {
   $_GET['id'] = "";
}

if (isset($_POST["add"])) {
   $track->check(-1,'w',$_POST);

   if (isset($_POST["_my_items"]) && !empty($_POST["_my_items"])) {
      $splitter = explode("_",$_POST["_my_items"]);
      if (count($splitter) == 2) {
         $_POST["itemtype"] = $splitter[0];
         $_POST["items_id"] = $splitter[1];
      }
   }
   $track->add($_POST);
   Html::back();

} else if (isset($_POST['update'])) {
   $track->check($_POST['id'],'w');

   if (isset($_POST["_my_items"]) && !empty($_POST["_my_items"])) {
      $splitter = explode("_",$_POST["_my_items"]);
      if (count($splitter) == 2) {
         $_POST["itemtype"] = $splitter[0];
         $_POST["items_id"] = $splitter[1];
      }
   }
   $track->update($_POST);
   Event::log($_POST["id"], "ticket", 4, "tracking", $_SESSION["glpiname"]." ".$LANG['log'][21]);

   // Copy solution to KB redirect to KB
   if (isset($_POST['_sol_to_kb']) && $_POST['_sol_to_kb']) {
      Html::redirect($CFG_GLPI["root_doc"]."/front/knowbaseitem.form.php?id=new&itemtype=Ticket&items_id=".
                   $_POST["id"]);
   } else {
      if ($track->can($_POST["id"],'r')) {
         Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_POST["id"]);
      }
      Session::addMessageAfterRedirect($LANG['job'][26], true, ERROR);
      Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.php");
   }

} else if (isset($_POST['delete'])) {
   $track->check($_POST['id'],'d');
   if ($track->delete($_POST)) {
      Event::log($_POST["id"], "ticket", 4, "tracking",
                 $_SESSION["glpiname"]." ".$LANG['log'][22]." ".$track->getField('name'));
   }
   $track->redirectToList();

} else if (isset($_POST['purge'])) {
   $track->check($_POST['id'],'d');
   if ($track->delete($_POST, 1)) {
      Event::log($_POST["id"], "ticket", 4, "tracking",
                 $_SESSION["glpiname"]." ".$LANG['log'][24]." ".$track->getField('name'));
   }
   $track->redirectToList();

} else if (isset($_POST["restore"])) {
   $track->check($_POST['id'], 'd');
   if ($track->restore($_POST)) {
      Event::log($_POST["id"], "ticket", 4, "tracking",
                 $_SESSION["glpiname"]." ".$LANG['log'][23]." ".$track->getField('name'));
   }
   $track->redirectToList();
/*
} else if (isset($_POST['add']) || isset($_POST['add_close']) || isset($_POST['add_reopen'])) {
   Session::checkSeveralRightsOr(array('add_followups'     => '1',
                                       'global_add_followups' => '1',
                                       'show_assign_ticket' => '1'));
   $newID = $fup->add($_POST);

   Event::log($_POST["tickets_id"], "ticket", 4, "tracking",
              $_SESSION["glpiname"]." ".$LANG['log'][20]." $newID.");
   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".
               $_POST["tickets_id"]."&glpi_tab=1&itemtype=Ticket");

*/

} else if (isset($_POST['sla_delete'])) {
   $track->check($_POST["id"],'w');

   $track->deleteSLA($_POST["id"]);
   Event::log($_POST["id"], "ticket", 4, "tracking", $_SESSION["glpiname"]." ".$LANG['log'][21]);

   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_POST["id"]);

} else if (isset($_REQUEST['delete_link'])) {
   $ticket_ticket = new Ticket_Ticket();
   $ticket_ticket->check($_REQUEST['id'],'w');

   $ticket_ticket->delete($_REQUEST);

   Event::log($_REQUEST['tickets_id'], "ticket", 4, "tracking",
              $_SESSION["glpiname"]." ".$LANG['log'][120]);
   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_REQUEST['tickets_id']);

} else if (isset($_REQUEST['delete_user'])) {
   $ticket_user = new Ticket_User();
   $ticket_user->check($_REQUEST['id'], 'w');
   $ticket_user->delete($_REQUEST);

   Event::log($_REQUEST['tickets_id'], "ticket", 4,
              "tracking", $_SESSION["glpiname"]." ".$LANG['log'][122]);
   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_REQUEST['tickets_id']);

} else if (isset($_REQUEST['delete_group'])) {
   $group_ticket = new Group_Ticket();
   $group_ticket->check($_REQUEST['id'], 'w');
   $group_ticket->delete($_REQUEST);

   Event::log($_REQUEST['tickets_id'], "ticket", 4, "tracking",
              $_SESSION["glpiname"]." ".$LANG['log'][122]);
   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_REQUEST['tickets_id']);

} else if (isset($_REQUEST['addme_observer'])) {
   $ticket_user = new Ticket_User();
   $track->check($_REQUEST['tickets_id'], 'r');
   $input = array('tickets_id'       => $_REQUEST['tickets_id'],
                  'users_id'         => Session::getLoginUserID(),
                  'use_notification' => 1,
                  'type'             => Ticket::OBSERVER);
   $ticket_user->add($input);

   Event::log($_REQUEST['tickets_id'], "ticket", 4, "tracking",
              $_SESSION["glpiname"]." ".$LANG['log'][21]);
   Html::redirect($CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$_REQUEST['tickets_id']);

}

if (isset($_GET["id"]) && $_GET["id"]>0) {
   if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
      Html::helpHeader($LANG['Menu'][5],'',$_SESSION["glpiname"]);
   } else {
      Html::header($LANG['Menu'][5],'',"maintain","ticket");
   }

   $available_options = array('load_kb_sol');
   $options = array();
   foreach ($available_options as $key) {
      if (isset($_GET[$key])) {
         $options[$key] = $_GET[$key];
      }
   }
   $track->showForm($_GET["id"],$options);

} else {
   Html::header($LANG['job'][13],'',"maintain","ticket");

   $track->showForm(0);
}


if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
   Html::helpFooter();
} else {
   Html::footer();
}
?>