<?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_ACTIVE) )
                {
                        return;
                }

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

                            //View Ticket Search
                           $menuItem = new NE_MenuItem($this->user->lang('Ticket Search'),"index.php?fuse=support&amp;view=TicketSearch");
                           $menuItem->addViews(array('TicketSearch'));
                           $menuItem->addPermissions("support_view");
                           $this->addItem($menuItem);
                }

                if(!$this->user->isAdmin()){
                            //Submit Ticket
                           $menuItem = new NE_MenuItem($this->user->lang('Submit Ticket'),"index.php?fuse=support&amp;view=ViewSubmitTicketForm");
                           $menuItem->addViews("ViewSubmitTicketForm");
                           $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;
                          }

                          $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
                                    
                                    if(!$this->user->isAdmin()){
                                            $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);
                                    }else{
                                            $menuItem = new NE_MenuItem($this->user->lang('Ticket').' #'.$tTicket->getId(),"index.php?fuse=support&amp;ticketID=".$tTicket->getId()."&amp;view=Admin_ViewTicketMessage");
                                            $menuItem->addPermissions("support_view");
                                            $this->tpl->setKey('openitemname',$menuItem->getURL());
                                            $this->tpl->parse('openitem_out','.openitem');
                                    }
                          }

                          //check to see if there are closed tickets
                          $ticketCount = $cTickets->GetTicketCountByCustomerId($this->customer->getId(),true);
                          if( ($ticketCount>0) && ($this->user->isAdmin()) )
                          {
                                if ($this->user->isAdmin()) {
                                    $ticketCount .= "/".$cTickets->GetTicketCountYouHaveAnsweredByCustomerId($this->user->getId(),$this->customer->getId(),true);
                                }

                                $dispClosedTicketCount = "<span style='font-size:9px''>".$ticketCount."</span>";
                                
                                $this->tpl->setKey('openitemname',"<a href='index.php?fuse=support&amp;view=ViewTickets&amp;filter=closed&amp;customerid=".$this->customer->getId()."'>Closed Tickets (".$dispClosedTicketCount.")</a>");
                                $this->tpl->parse('openitem_out','.openitem');
                          }
                }// end if customer block

                if ( ($this->user->getStatus() == USER_STATUS_ACTIVE) && (!NE_Lib::isPublicDir()) ) {

                        //KB Article List
                        $menuItem = new NE_MenuItem($this->user->lang("KB Article List"),"index.php?fuse=knowledgebase&amp;view=ViewKBArticles");
                        $menuItem->addViews("ViewKBArticles");
                        $this->addItem($menuItem);

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

                }
    }
}
?>
