<?php

/**
* @package Support
*/
class Support_menu extends NE_MenuHook
{

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

        if ( (NE_Lib::isPublicDir() && $this->user->isAdmin()) ||
              ($this->user->getStatus() == USER_STATUS_CANCELLED) ||
              ($this->user->getStatus() == USER_STATUS_FRAUD) )
        {
                return;
        }

        if (NE_Lib::isPublicDir()){
           //View Tickets
           $menuItem = new NE_MenuItem($this->user->lang('View Tickets'),"index.php?fuse=support&amp;view=TicketsOverview");
           $menuItem->addViews("Overview");
           $menuItem->addPermissions("support_view");
           $this->addItem($menuItem);
}else{
            //View Tickets
           $menuItem = new NE_MenuItem($this->user->lang('Ticket List'),"index.php?fuse=support&amp;view=ViewTickets");
           $menuItem->addViews(array('ViewViewTickets','ViewTicketDetails','ViewAdmin_ViewTicketMessage','ViewAdmin_TicketEvents','AdminTicketTypeArticles','AdminTicketTypeNotes'));
           $menuItem->addPermissions("support_view");
           $this->addItem($menuItem);

           //Canned Responses
            $menuItem = new NE_MenuItem($this->user->lang("Canned") . '&nbsp;' . $this->user->lang("Responses"),"index.php?fuse=support&amp;view=CannedResponses");
            $menuItem->addViews("ViewCannedResponses");
            $menuItem->addPermissions("support_view");
            $this->addItem($menuItem);
           
            //KB Article List
            $menuItem = new NE_MenuItem($this->user->lang("KB Articles"),"index.php?fuse=knowledgebase&amp;view=ViewKBArticles");
            $menuItem->addViews("ViewKBArticles");
            $menuItem->addPermissions("knowledgebase_view");
            $this->addItem($menuItem);

            //KB Comments
            $menuItem = new NE_MenuItem($this->user->lang("KB Comments"),"index.php?fuse=knowledgebase&amp;view=KB_manageCommentsView");
            $menuItem->addViews("ViewKB_manageCommentsView");
            $menuItem->addPermissions("knowledgebase_manageComments");
            $this->addItem($menuItem);
        }

        if(!$this->user->isAdmin()){
            //Submit Ticket
           $menuItem = new NE_MenuItem($this->user->lang('Submit Ticket'),"index.php?fuse=support&amp;view=SubmitTicketForm");
           $menuItem->addViews("SubmitTicketForm");
           $menuItem->addPermissions(array('support_view','support_submit_ticket'));
           $this->addItem($menuItem);
        }

        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;
              }

              if(!$this->user->isAdmin()){
                    $viewableTickets = $cTickets->getViewableTickets( $this->customer->getId(),
                    $this->user,
                    $departmentIds,$canSeeAll);
                      //only draw these if the customer is viewing otherwise we want to parse in the quicklinks
                      while ($tTicket = $viewableTickets->fetch())
                      {
                                //Ticket #'s
                                $menuItem = new NE_MenuItem($this->user->lang('Ticket').' #'.$tTicket->getId(),"index.php?fuse=support&amp;ticketID=".$tTicket->getId()."&amp;view=ViewTicketDetails");
                                $menuItem->addPermissions("support_view");
                                $this->addItem($menuItem);

                      }
               }
        }// end if customer block

    }
}
?>
