<?php

/**
* @package Support
*/
class Support_menu extends NE_MenuHook
{
    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');

        if (isset($this->configuration['modules']['knowledgebase']['installedVersion']) && $this->user->hasPermission('knowledgebase_view')) {
            $this->modulesViews[] = array('knowledgebase', 'KB_MainView');
            $this->modulesViews[] = array('knowledgebase', 'KB_CategoriesView');
            $this->modulesViews[] = array('knowledgebase', 'KB_SearchArticlesView');
            $this->modulesViews[] = array('knowledgebase', 'KB_AddArticle');
            $this->modulesViews[] = array('knowledgebase', 'KB_viewArticle');
        }
    }

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

            if ($this->user->hasPermission('support_view')) {
                if ($this->action == 'Overview') {
                     $tSubArray[0] ="<a href=\"index.php?fuse=support&amp;view=Overview\" class='sel'>".strtolower($this->user->lang("overview"))."</a>";
                     $tSubArray[1] = 0;
                     $tArray[] = $tSubArray;
                } else {
                    $tSubArray[0] = "<a href=\"index.php?fuse=support&amp;view=Overview\" >".strtolower($this->user->lang("overview"))."</a>";
                    $tSubArray[1] = 0;
                    $tArray[] = $tSubArray;
                }          
                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\" class='sel'>".strtolower($this->user->lang('Ticket #').' '.$tTicket->getId())."</a>";
                            $tSubArray[1] = 0;
                            $tArray[] = $tSubArray;
                      }
                }
            }

            if (isset($this->configuration['modules']['knowledgebase']['installedVersion']) && $this->user->hasPermission('knowledgebase_view')) {
                if ($this->action == 'KB_MainView' || $this->action == 'KB_CategoriesView') {
                     $tSubArray[0] ="<a href=\"index.php?fuse=knowledgebase&amp;view=KB_MainView\" class='sel'>".strtolower($this->user->lang('Knowledge base'))."</a>";
                     $tSubArray[1] = 0;
                     $tArray[] = $tSubArray;
                } else {
                    $tSubArray[0] = "<a href=\"index.php?fuse=knowledgebase&amp;view=KB_MainView\" >".strtolower($this->user->lang('Knowledge base'))."</a>";
                    $tSubArray[1] = 0;
                    $tArray[] = $tSubArray;
                }          

                if ($this->user->hasPermission('knowledgebase_add_article')) {
                    if ($this->action == 'KB_AddArticle' && !isset($_GET['articleId'])) {
                         $tSubArray[0] ="<a href=\"index.php?fuse=knowledgebase&amp;view=KB_AddArticle\" class='sel'>".strtolower($this->user->lang('Add article'))."</a>";
                         $tSubArray[1] = 0;
                         $tArray[] = $tSubArray;
                    } else {
                        $tSubArray[0] = "<a href=\"index.php?fuse=knowledgebase&amp;view=KB_AddArticle\" >".strtolower($this->user->lang('Add article'))."</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->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];

                        /*
                        $tSubArray[0] ="<a href=\"index.php?fuse=support&view=Support_ViewEventLog&ticketID=".$_GET['ticketID']."\" class='sel'>".strtolower($this->user->lang("Ticket event log"))."</a>";
                        $tSubArray[1] = 1;
                        $tSubArray[2] = 0;
                        $tArray[] = $tSubArray;
						*/
                    }

                    /*
                    if ($this->action == 'Support_ViewEventLog') {
                        $tSubArray[0] = strtolower($this->user->lang("Viewing ticket event log"));
                        $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];
                    }
                    
                    if (isset($this->configuration['modules']['knowledgebase']['installedVersion']) && $this->user->hasPermission('knowledgebase_view')) {
                        if (($this->action == 'KB_MainView')) {
                            $tSubArray[0] = strtolower($this->user->lang('Knowledge base'));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        } else {
                            $tSubArray[0] = "<a href=\"index.php?fuse=knowledgebase&amp;view=KB_MainView\" class='sel'>".strtolower($this->user->lang('Knowledge base'))."</a>";
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 0;
                            $tArray[] = $tSubArray;
                        }

                        if ($this->action == 'KB_CategoriesView') {
                            $tSubArray[0] = strtolower($this->user->lang('Knowledge base category'));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        }

                        if ($this->action == 'KB_viewArticle') {
                            $tSubArray[0] = strtolower($this->user->lang('Knowledge base article'));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        }

                        if (($this->action == 'KB_AddArticle')) {
                            if (isset($_GET['articleId'])) {
                                $tSubArray[0] = strtolower($this->user->lang('Editing article'));
                            } else {
                                $tSubArray[0] = strtolower($this->user->lang('Add article'));
                            }
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        } elseif ($this->user->hasPermission('knowledgebase_add_article')) {
                            $tSubArray[0] = "<a href=\"index.php?fuse=knowledgebase&amp;view=KB_AddArticle\" class='sel'>".strtolower($this->user->lang('Add article'))."</a>";
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 0;
                            $tArray[] = $tSubArray;
                        }                    

                        if (($this->action == 'KB_SearchArticlesView')) {
                            $tSubArray[0] = strtolower($this->user->lang('Search results'));
                            $tSubArray[1] = 1;
                            $tSubArray[2] = 1;
                            $tArray[] = $tSubArray;
                            $navlinks[] = $tSubArray[0];
                        }
                    }
                }
            }

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