<?php

/*  Alternate WebKit
(C) 2011 by Alternate Tools
-----------------------------------------------------

    filename: class_news.php
*/
include "class_storagefile.php";

if (!defined ("CLASS_NEWS"))
{
    define ("CLASS_NEWS", "defined");

    class cNews
    {
        var $m_newsId;
        var $m_newsDate;
        var $m_newsPubDate;
        var $m_newsNameDE;
        var $m_newsNameEN;
        var $m_newsLocked;
        var $m_newsFolder;

        function cNews()  // PHP 4
        {
            $this->Init();
        }

        function __construct()
        {
            $this->Init();
        }

        function Init()
        {
            $this->m_newsId = "0";
            $this->m_newsDate = "";
            $this->m_newsPubDate = "";
            $this->m_newsNameDE = "";
            $this->m_newsNameEN = "";
            $this->m_newsLocked = "0";

            $this->m_newsFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_newsId;
        }
        
        function GetDate()
        {
            return $this->m_newsDate;
        }
        
        function SetPubDate($date)
        {
            $this->m_newsPubDate = $date;
        }
        function GetPubDate()
        {
            return $this->m_newsPubDate;
        }

        function SetNameDE($nameDE)
        {
            $this->m_newsNameDE = $nameDE;
        }
        function GetNameDE()
        {
            return $this->m_newsNameDE;
        }

        function SetNameEN($nameEN)
        {
            $this->m_newsNameEN = $nameEN;
        }
        function GetNameEN()
        {
            return $this->m_newsNameEN;
        }
        
        function IsLocked()
        {
            return ($this->m_newsLocked == "1");
        }

        function SetLocked($bLock)
        {
            $this->m_newsLocked = "0";
            if ($bLock == true)
                $this->m_newsLocked = "1";
            return $this->SaveNews();
        }

        function SetFolder($folder)
        {
            $this->m_newsFolder = $folder;
        }
        
        // Main methods
        function CreateNews()
        {
            $this->Init();
            $this->m_newsId = uniqid("new", FALSE);
            $this->m_newsDate = date("Y-m-d H:i:s");
        }

        function DeleteNewsFinal()
        {
            return unlink($this->m_newsFolder . $this->m_newsId . ".new");
        }

        function DeleteNewsFinalFromId($newsId)
        {
            return unlink($this->m_newsFolder . $newsId . ".new");
        }


        function LoadNewsFromId($newsId)
        {
            $this->m_newsId = $newsId;
            return $this->LoadNews();
        }

        function LoadNews()
        {
            $fileName = $this->m_newsFolder . $this->m_newsId  . ".new";
            //echo  $fileName;

            if ($this->m_newsId == "0" || $this->m_newsFolder == "")
                return false;

            if (file_exists($fileName) == false)
                return false;

            $cStorage = new cStorageFile;
            if ($cStorage->LoadStorageFromFile($fileName) == false)
                return false;

            if ($cStorage->GetData("Id") == "")
                return false;

            $this->m_newsId = $cStorage->GetData("Id");
            $this->m_newsDate = $cStorage->GetData("Date");
            $this->m_newsPubDate = $cStorage->GetData("PubDate");
            $this->m_newsNameDE = $cStorage->GetData("NameDE");
            $this->m_newsNameEN = $cStorage->GetData("NameEN");
            $this->m_newsLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveNews()
        {
            $newRow = "\r\n";
            $fileName = $this->m_newsFolder . $this->m_newsId  . ".new";
            // echo $fileName;

            if ($this->m_newsId == "0" || $this->m_newsFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("news");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_newsId);
            $cStorage->AddData("Date", $this->m_newsDate);
            $cStorage->AddData("PubDate", $this->m_newsPubDate);
            $cStorage->AddData("NameDE", $this->m_newsNameDE);
            $cStorage->AddData("NameEN", $this->m_newsNameEN);
            $cStorage->AddData("Locked", $this->m_newsLocked, false);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}