Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/NitroNews.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Modules ::  NitroNews					       														 |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2007 June Systems B.V.                                      |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Joris Osterhaus - 	<hide@address.com>			       |
// +---------------------------------------------------------------------------+
//

//
// $Id: NitroNews.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * This file contains the NitroNews  Module
 *
 * @author
 * @copyright		June Systems BV, 2006
 * @version			$Revision: 0.9 $
 * @package			Modules
 */

require_once "Nitro/Module.inc.php";
require_once "Nitro/Libraries/Form.inc.php";
require_once "Nitro/Libraries/Listing.inc.php";

/**
 * @access public
 */

class NitroNews extends NitroModule {
	var $IDString = "NitroNews";
	/**
	 * Constructor function
	 */

	function NitroNews()
	{
	
	  parent::NitroModule();
	}

	/**
	 * Function UserRights
	 *
	 * interprets / alters user session variable
	 */
	function UserRights($Action = FALSE)
	{
		if ($Action !== FALSE && $Action === 'logout') $this->Sess->Logout();
		if ((int)$this->Sess->SecurityGroups[0] === 2) {
			$UserRights = 'admin';
		} else {
			$UserRights = '';
		}
		return $UserRights;
	}

	/**
	 * Function NewsMenuQuery
	 *
	 * returns NewsCategories from db for use in menu 
	 */

	function NewsMenuQuery()
	{
		$MenuItems = array();
		$Query = "
			SELECT
				NewsCategoryID,
				Name,
				Description
			FROM `NewsCategory`
		";
		$Result = $this->DB->query($Query);
		if ($Result->numRows()) {
			while ($Row = $Result->fetchArray()) {
				$MenuItems[$Row['NewsCategoryID']] = $Row;
			}
		}
		return $MenuItems;
	}

	function NewsMenu($NewsCategoryID = False)
	{
		$MenuItems = $this->NewsMenuQuery();
		$NewsMenu = '';
		if (is_array($MenuItems) && count($MenuItems) > 0) {
			foreach ($MenuItems AS $MenuItemID => $MenuItemData) {

				if ($MenuItemID === (int)$NewsCategoryID) {
					$NewsMenu.= '<div class="newsmenuitem">'.Language($MenuItemData['Name']). ($this->UserRights()== 'admin' ? '<br /><a href=\''.NitroGetConfig('Settings/PageURL').$this->CurrentPage.'&mode=editcategory&newscategoryid=' . $MenuItemID. '\'><sup>' . Language('edit') . '</sup></a>' : '') . ' </div>';
				} else {
					$NewsMenu .= '
						<div class="newsmenuitem" onmouseover="this.className=\'newsmenuitemhover\'" onmouseout="this.className=\'newsmenuitem\'">
							<a href="'.NitroGetConfig('Settings/PageURL').$this->CurrentPage.'&mode=newslist&NewsCategoryID='.$MenuItemID.'">'.Language($MenuItemData['Name']).'</a>
						</div>
					';
				}
			}
		}
		$NewsMenu .= '<div class="newsmenuitem"><a href="'.NitroGetConfig('Settings/PageURL').$this->CurrentPage.'&mode=newslist">'.Language('All').'</a></div><br /><br />';
		return $NewsMenu;
	}

	/**
	* function NewsList -  returns list of Newsarticles (in specified category if specified)
	*
	*
	*/

		function NewsList ($NewsCategoryID = False)
			 {
			$Query = "
				SELECT
					Title,
					Leader,
					NewsID,
					N.CreatedOn
				FROM News AS N
				INNER JOIN NewsCategory AS NC
				ON N.NewsCategoryID = NC.NewsCategoryID
				WHERE N.VisibleFrom < NOW()
					AND N.VisibleTill > NOW() "
					. ($NewsCategoryID ?  "AND N.NewsCategoryID = $NewsCategoryID " : '') . "
				ORDER BY N.CreatedOn DESC";


			$Result = $this->DB->query($Query);

			if ($this->UserRights() == 'admin')
			{
				$NewsList .= '<div class = \'floatmenu\'><a href=\'' . NitroGetConfig('Settings/PageURL') . $this->CurrentPage . '&mode=makenews\'>' . Language('Create New Item') . '</a>&nbsp;&bull;<br />';
				$NewsList .= '<a href=\'' .  NitroGetConfig('Settings/PageURL') . $this->CurrentPage . '&mode=makecategory\'>' . Language('Make category') . '</a>&nbsp;&bull;<br />';
				$NewsList .= '<a href=\'' .  NitroGetConfig('Settings/PageURL') . $this->CurrentPage . '&mode=logout\'>' . Language('Logout') . '</a>&nbsp;&bull;</div><br />';
			}
			else
			{
				$NewsList .= "<div class = \"floatmenu\"><a href=\"" . $_SERVER['PHP_SELF']. "?P=NewsLogon\">" . Language('Login') . "</a>&nbsp;&bull;</div>\n";
			}

			if ($Result->numRows()) {
					while ($Row = $Result->fetchArray()) {
					$NewsList .= "<h4>" . $Row['Title']  . "</h4>\n";
							if ($this->UserRights() == 'admin'){		$NewsList .= '<div class = \'floatmenu\'>
																	<a href=\'' .  NitroGetConfig('Settings/PageURL') . $this->CurrentPage  . '&mode=edit&article=' .  $Row['NewsID']. '\'>' . Language('Edit') . '</a>&nbsp;&bull;<br />
																	<a href=\'' . NitroGetConfig('Settings/PageURL') . $this->CurrentPage . '&mode=deletenews&article=' .  $Row['NewsID']. '\'>' . Language('Delete') . '</a>&nbsp;&bull;
																</div>';
							}
								$NewsList .= '<div class=\'newsleader\'>' . $Row['Leader'];
								$NewsList .= '<br /><a href=\'' . NitroGetConfig('Settings/PageURL') . $this->CurrentPage . "&mode=article&article=" .  $Row['NewsID']. '\'>' . Language('Read More') . '</a></div>';
					}
			}
			$NewsList = $this->NewsMenu($NewsCategoryID). $NewsList;
			
			return $NewsList;
	}

	/**
	 * Function SelectArticle
	 *
	 * Retrieves specified article from news table
	 *
	 */


	function SelectArticle ($NewsID)
	{
		$Query = "
								SELECT *
								FROM News AS N
								INNER JOIN NewsCategory AS NC
								ON
									N.NewsCategoryID = NC.NewsCategoryID
								WHERE N.NewsID = " . $NewsID . "
						";

		$Result = $this->DB->getRow($Query);
		//print_r($Result); exit;
		
		return $Result;
		
	}
	
	/**
	 * Function SelectCategory
	 *
	 * Retrieves specified News category from db
	 *
	 */


function SelectCategory ($CategoryID)
	{
		$Query = "
								SELECT * FROM NewsCategory
								WHERE NewsCategoryID = " . $CategoryID . "
						";

		$Result = $this->DB->getRow($Query);
		
		return $Result;
	}


	/**
	 * Function SelectCategory
	 *
	 * Retrieves specified News category from db
	 *
	 */

	function DeleteNews ($NewsID)
	{
		if ($this->UserRights() == 'admin'){
			$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
			$Transaction->addData('News', array('NewsID' => $NewsID), array('NewsID' => $NewsID), 'DELETE');
			$Transaction->Commit();
			$ReturnValue .= '<div class=\'message\'>' . Language('News Item has been deleted!') . '</div>';
		}	else {
		 	$ReturnValue .= $this->Disallow();
		}
			$ReturnValue .= $this->Newslist();
			return $ReturnValue;
	}
	
	/**
	 * Function GetArticle
	 *
	 * Retrieves specified News Article from db
	 *
	 */

	function GetArticle($NewsID)
	{
		$ArticleDetails = $this->SelectArticle($NewsID);

			if (is_array($ArticleDetails))	{
				$NewsArticle = "<h4>" . $ArticleDetails['Title']  . "</h4>\n";
				$NewsArticle .= '<div class=\'article\'>' . $ArticleDetails['Leader']  . '</div>';
				$NewsArticle .= '<div class=\'article\'>' . $ArticleDetails['NewsText']  . '</div>';
			}
			else {
				$NewsArticle = "<h4>" . Language('Error - Article not Found')  . "</h4>\n";
			}
			return $NewsArticle;
	}


	/**
	 * Function PreProcess
	 *
	 * Catches and processes posted variables 
	 *
	 */


	function PreProcess ()
	{
			if ($_REQUEST['NewsFormData']){

			$Transaction = new Transaction($this->DB, 0, FALSE);

				  if ($Transaction->Error) {
				    echo "ERROR: Failed creating a new transaction";
				  } else {
					$Fields ['NewsID']= (int)$_REQUEST['NewsID'];
				  	$Fields ['NewsCategoryID']= (int)$_REQUEST['NewsCategoryID'];
				  	$Fields ['VisibleFrom']= $_REQUEST['VisibleFrom'];
				  	$Fields ['VisibleTill']= $_REQUEST['VisibleTill'];
				  	$Fields ['Title']= $_REQUEST['Title'];
				  	$Fields ['Leader']= $_REQUEST['Leader'];
				  	$Fields ['NewsText']= $_REQUEST['NewsText'];
				  	$Fields ['Published']= $_REQUEST['Published'];

				    $Transaction->addData(News, $Fields);

				    $Transaction->Commit();

							if ($_REQUEST['NewsID']) { 
								$this->Output .= "<div class=\"message\">" . $this->NewsMenu() . Language('News Item Succesfully Updated.') .  "</div>";
							}
							else {
						    $this->Output .= "<div class=\"message\">" . $this->NewsMenu() . Language('News Item Succesfully Created.') .  "</div>";
						  }
						}

			} elseif ($_REQUEST['CategoryFormData']) {
				$Transaction = new Transaction($this->DB, 0, FALSE);
							  if ($Transaction->Error) {
				    echo "ERROR: Failed creating a new transaction";
				  } else {
							$Fields ['NewsCategoryID']= (int)$_REQUEST['NewsCategoryID'];
					  	$Fields ['Name']=($_REQUEST['Name']);
					  	$Fields ['Description']= ($_REQUEST['Description']);

					    $Transaction->addData(NewsCategory, $Fields);
					    $Transaction->Commit();

							if ($_REQUEST['NewsID']) { 

								$this->Output .= "<div class=\"message\">" . $this->NewsMenu() . Language('Category Succesfully Updated.') .  "</div>";
							} else {
						    $this->Output .= "<div class=\"message\">" . $this->NewsMenu() . Language('Category Succesfully Created.') .  "</div>";
							}		
					}
			}
	}

	/**
	 * Function Process
	 *
	 * Retrieves and processes url variables 
	 *
	 */

	function Process ()
	{

		// TODO GetSettings  implementeren
		// if ($this->Settings['Article']) { //if ($_GET("article"))

		switch ($_REQUEST['mode']) {

				case logout:
					$this->UserRights(logout);
					$RV = $this->NewsList();
					break;

				case newslist:
					$RV = $this->NewsList($_REQUEST['NewsCategoryID']);

					break;

				case article:
					$RV = $this->GetArticle($_REQUEST['article']);
					break;

				case edit:
					$RV = $this->EditNews($_REQUEST['article']);
					break;

				case makenews:
					$RV = $this->Makenews();
					break;

				case makecategory:
					$RV = $this->MakeCategory();
					break;

				case editcategory:
					$RV = $this->EditCategory($_REQUEST['newscategoryid']);
					break;

				case savenews:
					$RV = $this->Savenews();
					break;

				case deletenews:
					$RV = $this->DeleteNews($_REQUEST['article']);
					break;

				default:
				$RV = $this->NewsList();
		}

	$this->Output .= $RV;

	$this->Processed = True;

	}

	/**
	 * Function Edit News
	 *
	 * Returns html form for editing selected article
	 *
	 */


	function EditNews ($NewsID)
	{
		if ($this->UserRights() == 'admin'){
			$ArticleDetails = $this->SelectArticle($NewsID);

			if (is_array($ArticleDetails)) {
				$Form =  $this->NewsForm ($ArticleDetails);
			}
		} else {
			$Form = $this->Disallow();
		}
	return $Form;
	}

	/**
	 * Function Disallow
	 *
	 * Returns html message - disallowed  
	 *
	 */

	function Disallow()	{
		return '<div class=\'message\'>' . Language('Disallowed'). '</div>';
	}


	/**
	 * Function NewsForm
	 *
	 * Constructs html form for editing / creating articles
	 *
	 */

	function NewsForm ($ArticleDetails = 0)
	{//print_r($ArticleDetails); exit;

		if ($this->UserRights() == 'admin'){

			$form = new Form("FormName", "");

			$form->AddOptionString("NewsID", "HIDDEN/VALUE=".$ArticleDetails['NewsID']);
			$form->AddOptionString("NewsCategoryID", "SELECT/SELECTED=" . $ArticleDetails['NewsCategoryID'] . "/VALUES=: -- Select a Category -- /STYLE=width: 250/DB=Nitro:NewsCategory:NewsCategoryID:Name/LABLE=News Category");
		 	$form->AddOptionString("Title", "TEXT/VALUE=" . rawurlencode($ArticleDetails['Title']) . "/MAXLENGTH=150/LABLE=" .Language('Headline'). "/SIZE=85");
		 	$form->AddOptionString("Leader", "TEXTAREA/VALUE=". rawurlencode($ArticleDetails['Leader']) . "/LABLE=" .Language('Leader'). "/ROWS=10/COLS=70/HTML_EDITOR=true");
			$form->AddOptionString("NewsText", "TEXTAREA/VALUE=" . rawurlencode($ArticleDetails['NewsText']) . "/LABLE=" .Language('Body text'). "/ROWS=20/COLS=70/HTML_EDITOR=true");
		  $form->AddOptionString("Published", "CHECKBOX/SELECTED=" . $ArticleDetails['Published'] . "/VALUES=1: /LABLE=" . Language('Published'));
			$form->AddOptionString("VisibleFrom", "DATE/VALUE=" . rawurlencode($ArticleDetails['VisibleFrom']) . "/MAXLENGTH=50/LABLE=" .Language('Visible From'). "/STYLE=width: 250");
			$form->AddOptionString("VisibleTill", "DATE/VALUE=" . rawurlencode($ArticleDetails['VisibleTill']) . "/MAXLENGTH=50/LABLE=" .Language('Visible Till'). "/STYLE=width: 250");

		  $form->AddButton('NewsFormData', array('Type' => 'SUBMIT', 'ID' => '', 'Value' => Language('Save')));
			$formOutput = $form->Draw();
		}	else {
			$Form = $this->Disallow();
		}
		//print_r ($formOutput); exit;
		return $formOutput;
		
	}
	
	/**
	 * Function MakeNews
	 *
	 * Returns html form for creating articles
	 *
	 */

	function MakeNews()
	{ if ($this->UserRights() == 'admin'){
			$Form = $this->NewsForm ();
			return $Form;
		}	else {
			$Form = $this->Disallow();
		}
	}

	/**
	 * Function CategoryForm
	 *
	 * constructs html form for editing / creating categories
	 *
	 */

	function CategoryForm ($CategoryDetails = 0)
	{
				$form = new Form("FormName", "");
				$form->AddOptionString("NewsCategoryID", "HIDDEN/VALUE=".$CategoryDetails['NewsCategoryID']);
				$form->AddOptionString("Name", "TEXT/VALUE=" . $CategoryDetails['Name'] . "/MAXLENGTH=50/LABLE=" .Language('Name'). "/STYLE=width: 250");
				$form->AddOptionString("Description", "TEXTAREA/VALUE=". $CategoryDetails['Description'] . "/LABLE=" .Language('Description'). "/ROWS=5/COLS=40");
	

		$form->AddButton('CategoryFormData', array('Type' => 'SUBMIT', 'ID' => '', 'Value' => Language('Save')));
		$FormOutput = $form->Draw();
		return $FormOutput;
	}

	/**
	 * Function MakeCategory()
	 *
	 * returns html form for creating categories
	 *
	 */

	function MakeCategory()
	{
		if ($this->UserRights() == 'admin'){
			$Form = $this->CategoryForm();
		}	else {
		$Form = $this->Disallow();
		}
		return $Form;
	}

/**
	 * Function EditCategory()
	 *
	 * returns html form for editing categories 
	 *
	 */
	function EditCategory($CategoryID)
	{
		if ($this->UserRights() == 'admin'){
			$CategoryDetails = $this->SelectCategory($CategoryID);

			$Form = $this->CategoryForm($CategoryDetails);
		}	else {
		$Form = $this->Disallow();
		}
		return $Form;
	}


	function Draw($RuntimeSettings)
	{

		//print_r($this->Output); exit;
		return $this->Output;

	}

}
?>
Return current item: OpenNitro