Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/PortalSources/Post.inc.php
<?php
/*
*****************************************************************

 Post.inc.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it 
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************
*/

if (!defined('ENIGMA'))
	die('<b>Access Violation</b><br />Direct Access to this location is not allowed.');

global $context;

$context['submit_news'] = allowedTo('submit_news');
$context['review_news'] = allowedTo('review_news');

function ePost()
{
	global $sourcedir, $portalSources, $context, $modSettings;

	require_once($sourcedir . '/Post.php');
	Post();

	if (!empty($modSettings['enablesitenews']) && !empty($context['current_board']) && !isset($_REQUEST['num_replies']) && $context['submit_news'])
		PostNewsOptions();
}

function ePost2()
{
	global $sourcedir, $portalSources, $context, $topic, $catchRedirect;

	if (isset($_REQUEST['preview']))
		return ePost();

	if (!empty($_POST['news']) && $context['submit_news'])
	{
	     SetupNewsRules();
		if (!empty($_REQUEST['msg']) || empty($topic))
			$catchRedirect = 'CallSiteNews';
	}

	require_once($sourcedir . '/Post.php');
	Post2();

}

function PostNewsOptions()
{

     global $context, $txt, $db_prefix, $topic;

     // if the user cannot submit news or this isn't the first post of a topic, no need for the news stuff
	if (!$context['submit_news'] || empty($context['is_first_post']))
     	return;

	loadPLanguage('News');

	// are we previewing and there aren't any errors?
	if (isset($context['preview_message']) && empty($context['post_error']))
	{
		$context['is_news'] = !empty($_REQUEST['news']) ? ' checked="checked"' : '';
		$newscat = !empty($_REQUEST['newscat']) ? $_REQUEST['newscat'] : NULL;
		$context['news_publish'] = !empty($_REQUEST['news_publish']) ? ' checked="checked"' : '';
		$context['news_feature'] = !empty($_REQUEST['news_feature']) ? ' checked="checked"' : '';
		$context['news_nsource'] = !empty($_REQUEST['nsource']) ? $_REQUEST['nsource'] : NULL;
	}
	// if there is already message content, we must be editting an existing message
	elseif (isset($_REQUEST['msg']))
     {
		$request = db_query("
			SELECT *
			FROM {$db_prefix}news
			WHERE ID_TOPIC = '$topic'", __FILE__, __LINE__);
		$row = mysql_fetch_assoc($request);
		mysql_free_result($request);
     	$context['is_news'] = ((!empty($row['ID_NEWS']) && $row['stage'] != '2' && $row['stage'] != '3') || (!empty($_REQUEST['sub']) && $_REQUEST['sub'] == '1')) ? ' checked="checked"' : '';
          $newscat = (!empty($row['ID_CATEGORY'])) ? $row['ID_CATEGORY'] : NULL;
          $context['news_publish'] = (!empty($row['publish'])) ? ' checked="checked"' : '';
          $context['news_feature'] = (!empty($row['featureFlag'])) ? ' checked="checked"' : '';
          $context['news_nsource'] = (!empty($row['ID_NEWS'])) ? '1' : '0';
     }
     // this must be a new message, so just display the default settings
     else
     {
          $context['is_news'] = '';
          $newscat = '';
          $context['news_publish'] = '';
          $context['news_feature'] = '';
          $context['news_nsource'] = '';
     }

	$context['shownews'] = (int) true;

	// setup the news categories for selection list

	// setup the "None" option
	$context['news_cats'][0] = array(
		'catID'	=> 0,
		'name'	=> $txt['news_109'],
		'selected' => ($newscat == 0) ? ' selected="selected"' : '');

	//Setup the any existing categories
	$g_nCats = mysql_query("
		SELECT *
		FROM {$db_prefix}news_categories");

	while ($rnCat = mysql_fetch_array($g_nCats))
	{
		$context['news_cats'][] = array(
			'catID'	=> $rnCat['ID_CATEGORY'],
			'name'	=> $rnCat['name'],
			'selected' => ($newscat == $rnCat['ID_CATEGORY']) ? ' selected="selected"' : '');
	}

}

function SetupNewsRules()
{

     global $context;

	if ($context['submit_news'])
	{
		// Set up the news array for NewsRules...
		$context['News']['is_news'] = !empty($_REQUEST['news']) ? $_REQUEST['news'] : '0';
		$context['News']['newscat'] = !empty($_REQUEST['newscat']) ? $_REQUEST['newscat'] : NULL;
		$context['News']['publish'] = !empty($_REQUEST['news_publish']) ? $_REQUEST['news_publish'] : '0';
		$context['News']['feature'] = !empty($_REQUEST['news_feature']) ? $_REQUEST['news_feature'] : '0';
		$context['News']['nsource'] = !empty($_REQUEST['nsource']) ? $_REQUEST['nsource'] : NULL;
	}
}

function CallSiteNews()
{

     global $modSettings, $context;

		// if Site News is enabled lets call it
		if ($modSettings['enablesitenews'] && !empty($context['News']) && $context['News']['nsource'] != 9)
			newsRules();

}

function newsRules()
{
	global $context, $db_prefix, $topic, $ID_MEMBER;

	/************************
	* nsource values        *
	* 0 = post news item    *
	* 1 = modify news item  *
	* 4 = approve news item *
	* 5 = reject news item  *
	* 6 = publish item      *
	* 9 = no submission     *
	************************/

	/*******************
	* workflow values  *
	* 0 = pending      *
	* 1 = approved     *
	* 2 = rejected     *
	* 3 = cancelled    *
	* 4 = resubmitted  *
	*******************/

	/********************
	* publish values    *
	* 0 = publish       *
	* 1 = delay publish *
	********************/

	// setup some variables so the queries are easier to process

	// Set up the news array for NewsRules...
	if (!empty($_REQUEST['op']) && $_REQUEST['op'] == 'news2')
	{
		$topic = !empty($_POST['topic']) ? $_POST['topic'] : '';
		$comment = !empty($_POST['comment']) ? $_POST['comment'] : '';
		$news_feature = !empty($_POST['news_feature']) ? $_POST['news_feature'] : '';
		$news_publish = !empty($_POST['news_publish']) ? $_POST['news_publish'] : '';
		$nsource = !empty($_POST['nsource']) ? $_POST['nsource'] : '';
		$newscat = !empty($_POST['newscat']) ? $_POST['newscat'] : '';
	}
	else
	{
		$news = !empty($context['News']['is_news']) ? $context['News']['is_news'] : '';
		$newscat = !empty($context['News']['newscat']) ? $context['News']['newscat'] : '';
		$news_publish = !empty($context['News']['publish']) ? $context['News']['publish'] : '';
		$news_feature = !empty($context['News']['feature']) ? $context['News']['feature'] : '';
		$nsource = !empty($context['News']['nsource']) ? $context['News']['nsource'] : '';
	}

	if (empty($news) || $news == '') { $news = 0; }
	if (empty($newscat) || $newscat == '') { $newscat = 0; }
	$time = time();
	if (empty($news_feature) || $news_feature == '') { $news_feature = 0; }
	if (empty($news_publish) || $news_publish == '') { $news_publish = 0; }

	// figure out who has permissions to do what
	if ($context['submit_news'])
	{
		$reviewFlag = 1;
		$newstage = 0;
	}
	if (($context['review_news']))
	{
		$reviewFlag = 0;
		$newstage = 1;
	}

	//coming directly from a new post with news checked
	if ($nsource == 0 && $news == 1)
	{
		db_query("
			INSERT INTO {$db_prefix}news
			(ID_TOPIC, ID_CATEGORY, ID_POSTER, reviewFlag, featureFlag, publish, stage, dateEntered)
			VALUES ($topic, $newscat, $ID_MEMBER, $reviewFlag, $news_feature, $news_publish, $newstage, $time)", __FILE__, __LINE__);
	}
	//coming from a modified post by a submitter
	else if ($nsource == 1 && $reviewFlag == 1)
	{
		//check to see if a submission record already exists
		$request = db_query("
			SELECT count(*)
			FROM {$db_prefix}news
			WHERE ID_TOPIC = $topic", __FILE__, __LINE__);

		$result = mysql_fetch_array($request);
		mysql_free_result ($request);

		$exists = $result[0];

		if ($exists == 0)
		{
			//it doesn't exist, and they checked news, so we have to insert it into the news table
			if ($news == 1)
			{
				db_query("
					INSERT INTO {$db_prefix}news
					(ID_TOPIC, ID_CATEGORY, ID_POSTER, reviewFlag, featureFlag, stage, dateEntered)
					VALUES ($topic, $newscat, $ID_MEMBER, $reviewFlag, $news_feature, 0, $time)", __FILE__, __LINE__);
			}
		}

		if ($exists > 0)
		{
			//it already exists, but they unchecked news, so we change the status to cancelled
			if ($news == 0)
			{
				db_query("
					UPDATE {$db_prefix}news
					SET stage = 3, ID_CATEGORY = $newscat, dateModified = $time,
						featureFlag = $news_feature, reviewFlag = 1
					WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
			}
			//it already exists and it's been edited, so we change the status to resubmitted
			if ($news == 1)
			{
				db_query("
					UPDATE {$db_prefix}news
					SET stage = 4, ID_CATEGORY = $newscat, dateModified = $time,
						featureFlag = $news_feature, reviewFlag = 1
					WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
			}
		}
	}

	// coming from a modified post from a reviewer
	else if ($nsource == 1 && $reviewFlag == 0)
	{
		//check to see if a submission record already exists
		$request = db_query("
			SELECT count(*)
			FROM {$db_prefix}news
			WHERE ID_TOPIC = $topic", __FILE__, __LINE__);

		$result = mysql_fetch_array($request);
		mysql_free_result ($request);

		$exists = $result[0];

		if ($exists == 0)
		{
			//it doesn't exist, and they checked news, so we have to insert it into the news table
			if ($news == 1)
			{
				db_query("
					INSERT INTO {$db_prefix}news
					(ID_TOPIC, ID_CATEGORY, ID_POSTER, reviewFlag, featureFlag, publish, stage, dateEntered)
					VALUES ($topic, $newscat, $ID_MEMBER, $reviewFlag, $news_feature, $news_publish, 1, $time)", __FILE__, __LINE__);
			}
		}

		if ($exists > 0)
		{
			//if the reviewer unchecked the news box, we change the status to rejected
			if ($news == 0)
			{
				db_query("
					UPDATE {$db_prefix}news
					SET stage = 2, ID_CATEGORY = $newscat, featureFlag = $news_feature,
						publish = $news_publish, dateModified = $time, ID_REVIEWER = $ID_MEMBER,
						reviewFlag = '$reviewFlag'
					WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
			}
			//if the reviewer left the news box checked and edited the post, we assume that it is now approved
			if ($news == 1)
			{
				db_query("
					UPDATE {$db_prefix}news
					SET stage = 1, ID_CATEGORY = $newscat, featureFlag = $news_feature,
						publish = $news_publish, dateModified = $time, ID_REVIEWER = $ID_MEMBER,
						reviewFlag = '$reviewFlag'
					WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
			}
		}

	}

	//the reviewer has approved the item from the submission console
	else if ($nsource == 4 && $reviewFlag == 0)
	{
			db_query("
				UPDATE {$db_prefix}news
				SET stage = 1, featureFlag = $news_feature, publish = $news_publish,
					ID_CATEGORY = $newscat, dateModified = $time, ID_REVIEWER = $ID_MEMBER, comment = '$comment',
					reviewFlag = '$reviewFlag'
				WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
	}

	//the reviewer has rejected the item from the submission console
	else if ($nsource == 5 && $reviewFlag == 0)
	{
			db_query("
				UPDATE {$db_prefix}news
				SET stage = 2, featureFlag = $news_feature, publish = $news_publish,
					ID_CATEGORY = $newscat, dateModified = $time, ID_REVIEWER = $ID_MEMBER, comment = '$comment',
					reviewFlag = '$reviewFlag'
				WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
	}

	//publishing had been delayed, and the reviewer wishes to publish the item now
	else if ($nsource == 6 && $reviewFlag == 0)
	{
			db_query("
				UPDATE {$db_prefix}news
				SET publish = 0, reviewFlag = '$reviewFlag'
				WHERE ID_TOPIC = $topic", __FILE__, __LINE__);
	}
	else
	{
		// fall back through to where ever newsRules was called from...
	}

	// Return to post if newsRules() was not called from Post in the first place
	if(!empty($_REQUEST['op']) && $_REQUEST['op'] == 'news2')
	{
		if (isset($topic))
			redirectexit('topic=' . $topic, true, $context['browser']['is_ie']);
		else
			redirectexit();
	}
}

?>
Return current item: Enigma