<?php

require_once 'modules/admin/models/Announcements.php';

/**
* @package Admin
*/
class PublicAnnouncements extends NE_View
{
    var $showHeader = true;
    var $permission = '';

    function dispatch()
    {
        // Check the URL input
        if (@isset($_GET['ann_id']) && !@is_numeric($_GET['ann_id'])) {
            $this->logger->log(1, '****XSS Attack Detected at index.php?fuse=admin&view=PublicAnnouncements. Currently logged in user:'.$this->user->getId().'('.$this->user->getFullName().')');
            die("XSS Attack Detected. Script Execution has been Halted.");
        }
        
        $error = false;
        $this->tpl->define (array('body' => "publicAnnouncements.tpl"));

        $this->tpl->setKey(array(
            'COMPANYNAME'   => $this->settings->get('Company Name')
        ));

        $announcements = new Announcements($this->user);
        $annList = $announcements->getHomeAnnouncements($this->user->getId(), $this->user->isAdmin(), false);

        if ((!isset($_GET['ann_id'])) || ($_GET['ann_id'] == "")) {
            if (count($annList) > 0) {
                foreach ($annList as $row) {
                    $author = new User($row['authorid']);
                    $this->tpl->assign('DATE', NE_Lib::db_to_form($row['postdate'], $this->settings->get('Date Format'), '-', true));
                    $this->tpl->assign('TITLE', $row['title']);
                    if(!$this->user->isAdmin() && !$this->user->isAnonymous()){
                        require_once 'newedge/classes/NE_MailGateway.php';
                        $row['excerpt'] = NE_MailGateway::replaceMailTags($row['excerpt'], $this->user);
                    }
                    $this->tpl->assign('EXCERPT', $row['excerpt']);
                    $this->tpl->assign('POSTED_BY', $author->getFirstName() . ' ' . $author->getLastName());
                    $this->tpl->assign('ANNID', $row['id']);
                    $this->tpl->parse('ANNOUNCEMENT','.announcement');            
                }
                $this->tpl->parse('ANNOUNCEMENTLIST','announcementlist');  
            } else {
                $errorMsg = $this->lang("Currently there are not any announcements to show");
                $error = true;
            }
        } else {
            if ($row = $this->_getAnnFromList($annList, $_GET['ann_id'])) {
                $author = new User($row['authorid']);
                $this->tpl->assign('TITLE', $row['title']);
                $this->tpl->assign('DATE', NE_Lib::db_to_form($row['postdate'], $this->settings->get('Date Format'), '-', true));
                if(!$this->user->isAdmin() && !$this->user->isAnonymous()){
                    require_once 'newedge/classes/NE_MailGateway.php';
                    $row['post'] = NE_MailGateway::replaceMailTags($row['post'], $this->user);
                }
                $this->tpl->assign('POST', $row['post']);
                $this->tpl->assign('POSTED_BY', $author->getFirstName() . ' ' . $author->getLastName());
                $this->tpl->parse('ANNOUNCEMENTDETAIL','announcementdetail');                
            } else {
                $errorMsg = $this->lang("Can not display the selected announcement");
                $error = true;
            }
        }
        if ($error) {
            $this->tpl->assign("ERRORMSG", $errorMsg);
            $this->tpl->parse("ERROR", "error");
        }
        $this->tpl->parse("BODY", "body");
        $this->tpl->FastPrint("BODY");
    }

    function _getAnnFromList(&$anns, $annId)
    {
        foreach ($anns as $ann) {
            if ($ann['id'] == $annId) {
                return $ann;
            }
        }

        return false;
    }
}

?>
