<?php

/**
* @package Support
*/
class Support_menu extends NE_MenuHook
{
    var $menuDescription = /*T*/'Current and closed tickets'/*/T*/;

    function Support_menu($action, &$user, &$customer)
    {
        parent::NE_MenuHook($action, $user, $customer);

        $this->modulesViews[] = array('support', 'Overview');
        $this->modulesViews[] = array('support', 'ViewClosedTickets');
        $this->modulesViews[] = array('support', 'ViewSubmitTicketForm');
        $this->modulesViews[] = array('support', 'ViewSubmitTicketForm');
        $this->modulesViews[] = array('support', 'ViewTicketDetails');
        $this->modulesViews[] = array('support', 'TicketTypeEditor');
        $this->modulesViews[] = array('support', 'Support_ViewEventLog');
        $this->modulesViews[] = array('support', 'TicketFilter');

    }

    function getMenuArr()
    {
        if (NE_Lib::isPublicDir() && $this->user->isAdmin()) {
            return;
        }

        if ($this->user->getStatus() == USER_STATUS_ACTIVE) {
            $tArray = array();
            $tSubArray = array();
            $navlinks = array();

            if ($this->user->hasPermission('support_view')) {
                if (NE_Lib::isPublicDir()) {
                    $overviewText = $this->user->lang("tickets");
                } else {
                    $overviewText = $this->user->lang("overview");
                }
                if ($this->action == 'Overview') {
                     $tSubArray[0] ="<a href=\"index.php?fuse=support&amp;view=Overview\" class='sel'>".strtolower($overviewText)."</a>";
                     $tSubArray[1] = 0;
                     $tArray[] = $tSubArray;
                } else {
                    $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=Overview\" >".strtolower($overviewText)."</a>";
                    $tSubArray[1] = 0;
                    $tArray[] = $tSubArray;
                }
                
                if ($this->user->hasPermission('support_view_closed_tickets') && $this->user->isAdmin()) {
                    if ($this->action == 'ViewClosedTickets') {
                         $tSubArray[0] ="<a href=\"index.php?fuse=support&amp;view=ViewClosedTickets\" class='sel'>".strtolower($this->user->lang("Closed Tickets"))."</a>";
                         $tSubArray[1] = 0;
                         $tArray[] = $tSubArray;
                    } else {
                        $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=ViewClosedTickets\" >".strtolower($this->user->lang("Closed Tickets"))."</a>";
                        $tSubArray[1] = 0;
                        $tArray[] = $tSubArray;
                    }
                }
                
                if ($this->user->hasPermission('support_submit_ticket')) {
                    if ($this->action == 'ViewSubmitTicketForm') {
                         $tSubArray[0] ="<a href=\"index.php?fuse=support&amp;view=ViewSubmitTicketForm\" class='sel'>".strtolower($this->user->lang("Submit ticket"))."</a>";
                         $tSubArray[1] = 0;
                         $tArray[] = $tSubArray;
                    } else {
                        $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=ViewSubmitTicketForm\" >".strtolower($this->user->lang("Submit ticket"))."</a>";
                        $tSubArray[1] = 0;
                        $tArray[] = $tSubArray;
                    }
                }
                
                if ($this->customer) {
                      //get open tickets
                      include_once("modules/support/models/TicketGateway.php");
                      $cTickets = new TicketGateway();
                      $departmentIds = array();
                      if (  !$this->user->hasPermission('support_view_assigned_department_tickets')
                            || !$this->user->hasPermission('support_view_assigned_otherdepartment_tickets'))
                      {
                          $myDepartmentIds = $this->user->getDepartmentsIds();
                          if ($this->user->hasPermission('support_view_assigned_department_tickets')) {
                              $departmentIds = $myDepartmentIds;
                          }
                          if ($this->user->hasPermission('support_view_assigned_otherdepartment_tickets')) {
                              require_once 'modules/support/models/DepartmentGateway.php';
                              $departmentGateway = new DepartmentGateway();
                              foreach ($departmentGateway->getDepartmentIds() as $depId) {
                                  if (!in_array($depId, $myDepartmentIds)) {
                                      $departmentIds[] = $depId;
                                  }
                              }
                          }
                          $canSeeAll = false;
                      } else {
                          $canSeeAll = true;
                      }
                      $viewableTickets = $cTickets->getViewableTickets( $this->customer->getId(),
                                                                        $this->user,
                                                                        $departmentIds,
                                                                        $canSeeAll);
                      while ($tTicket = $viewableTickets->fetch())                                                      
                      {
                            $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;ticketID=".$tTicket->getId()."&amp;view=ViewTicketDetails\">".strtolower($this->user->lang('Ticket #').' '.$tTicket->getId())."</a>";
                            $tSubArray[1] = 0;
                            $tArray[] = $tSubArray;
                      }
                }
            }

            if (in_array($this->action, array(  'Overview',
                                                'ViewClosedTickets',
                                                'ViewSubmitTicketForm',
                                                'ViewTicketDetails',
                                                'TicketTypeEditor',
                                                'TicketFilter',
                                                'KB_MainView',
                                                'KB_CategoriesView',
                                                'KB_SearchArticlesView',
                                                'KB_AddArticle',
                                                'KB_viewArticle')))
            {
                if ($this->user->hasPermission('support_view')) {
                    if (($this->action == 'Overview')) {
                        $tSubArray[0] = strtolower($this->user->lang("viewing overview"));
                        $tSubArray[1] = 1;
                        $tSubArray[2] = 1;
                        $tArray[] = $tSubArray;
                        $navlinks[] = $tSubArray[0];
                    } else {
                        $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=Overview\" class='sel'>".strtolower($this->user->lang("overview"))."</a>";
                        $tSubArray[1] = 1;
                        $tSubArray[2] = 0;
                        $tArray[] = $tSubArray;
                    }                    

                    if ($this->user->hasPermission('support_view_closed_tickets')) {
                        if (($this->action == 'ViewClosedTickets')) {
                            $tSubArray[0] = strtolower($this->user->lang("Viewing Closed Tickets"));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        } else {
                            $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=ViewClosedTickets\" class='sel'>".$this->user->lang("Closed Tickets")."</a>";
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 0;
                            $tArray[] = $tSubArray;
                        }
                    }
                    
                    if ($this->user->hasPermission('support_submit_ticket')) {
                        if (($this->action=='ViewSubmitTicketForm')){
                            $tSubArray[0] = strtolower($this->user->lang("Submitting Support Ticket"));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        } else {
                            $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=ViewSubmitTicketForm\" class='sel'>".$this->user->lang("Submit ticket")."</a>";
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 0;
                            $tArray[] = $tSubArray;
                        }
                    }

                    if (($this->action == 'ViewTicketDetails')) {
                        $tSubArray[0] = strtolower($this->user->lang("Viewing Ticket Details"));
                        $tSubArray[1] = 1;
                        $tSubArray[2] = 1;
                        $tArray[] = $tSubArray;
                        $navlinks[] = $tSubArray[0];
                    }

                    if ($this->action == 'TicketFilter') {
                        $tSubArray[0] = strtolower($this->user->lang("Viewing ticket filters"));
                        $tSubArray[1] = 1;
                        $tSubArray[2] = 1;
                        $tArray[] = $tSubArray;
                        $navlinks[] = $tSubArray[0];
                    }
                    
                }
            }

            return array('navlinks' => $navlinks, 'menus' => $tArray);        
        }
    }
}
?>
