<?php

/**
* @package Admin
*/
class PublicAnnouncements extends NE_View
{
    var $showPublicHeader   = true;

    function dispatch()
    {
        $error = false;
        $this->tpl->define (array('body' => "publicAnnouncements.tpl"));

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

        if ((!isset($_GET['ann_id'])) || ($_GET['ann_id'] == "")) {
            $result = $this->db->query("SELECT a.id, a.title, a.excerpt, a.post, a.postdate, a.publish, u.firstname, u.lastname "
                                       ."FROM announcement a, users u "
                                       ."WHERE a.authorid = u.id AND a.recipient = '0' AND a.publish='1' "
                                       ."ORDER BY a.postdate DESC");
            if ($result->getNumRows() > 0) {
                while ($row = $result->fetch()) {
                    $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', $row['firstname'] . ' ' . $row['lastname']);
                    $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 {
            $result = $this->db->query("SELECT a.id, a.title, a.excerpt, a.post, a.postdate, a.publish, u.firstname, u.lastname "
                                       ."FROM announcement a, users u "
                                       ."WHERE a.authorid = u.id AND a.recipient = '0' AND a.publish='1' AND a.id =?", $_GET['ann_id']);
            if ($result->getNumRows() > 0) {
                $row = $result->fetch();
                $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['excerpt'] = NE_MailGateway::replaceMailTags($row['excerpt'], $this->user);
                    $row['post'] = NE_MailGateway::replaceMailTags($row['post'], $this->user);
                }
                $this->tpl->assign('EXCERPT', $row['excerpt']);
                $this->tpl->assign('POST', $row['post']);
                $this->tpl->assign('POSTED_BY', $row['firstname'] . ' ' . $row['lastname']);
                $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");
    }
}

?>
