Location: PHPKode > projects > PHP SiteBuilder > include/menu.php
<?php
/**
 * Functions in this file:
 * - function getNewsArray()
 * - function getWebboardArray()
 * - function readArrayFromFile()
 * - function truncateLongTitle()
 * - function computeMoreLink()
 *
 * HTML Output functions:
 * - function writeNewsBox()
 * - function writeNewsList()
 * - function writeLink()
 *
 *	
 * $Id: menu.php,v 1.16 2001/10/16 02:45:16 zaiborg Exp $
 */
if 	(defined('__menu')) return;
	define('__menu', 1 );
if (DEBUG) { echo "<!-- Start of file: menu  -->\n"; }


/**
 * returns array with news postings
 * 
 * @name	getNewsArray
 * @date	2000-10-30
 * @author	Jesper Rønn <hide@address.com>
 * @author	Tobias H. Michaelsen <hide@address.com>
 * @calls		readArrayFromFile
 * @calledby	global.php, morenews.php
 */
function getNewsArray($newsFile, $count = null, $offset = 0, $splitter = "|")
{
	$newsArray = readArrayFromFile($newsFile);

	if (is_array($newsArray) and !empty($newsArray)) {
		// Slice out the wanted part of the array, if a $count is given...
		if (!empty($count)) {
			$newsArray = array_slice($newsArray, $offset, $count);
		}
		// Explode each text-line to an array...
		#$newsArray = explodeArray($newsArray, $splitter);
		foreach ($newsArray as $key => $val) {
			$newsArray[$key] = explode($splitter, $val);
		}
		
		$result = array();
		foreach ($newsArray as $news) {
			if (!empty($news)) {
				$tmp['date'] = $news[1];
				$tmp['text'] = $news[2];
				$tmp['link'] = $news[3];
				$tmp['auth'] = "Webmaster";
				$result[] = $tmp;
			}
		}		
		return $result;
	} else {
		$errorMsg = '<p class="news"><b>Error:</b> ';
		$errorMsg .= 'File "'.$newsFile.'" not found or newsArray empty.</p>';
		if (is_string($newsArray)) {
			$errorMsg .= $newsArray;
		}
		return $errorMsg;
	}
}


/**
 * Read an xml-news-file, and return an array with $numNews newest news.
 *
 * @author	Tobias H. Michaelsen <hide@address.com>
 * @date	2001-10-11
 * @param	string	$filename	for xml-news-file.
 * @param	integer	$numNews	number of news to return.
 * @param	integer	$offset		optional offset from newest news.
 * @return	array
 */
function getNewsArrayFromXml($filename, $numNews, $offset = 0)
{
	include_once('XML/Tree.php');
	$xmlTree = new XML_Tree($filename);
	if (PEAR::isError($err = $xmlTree->getTreeFromFile())) {
		echo $err->getMessage()."<br />\n";
		return array();
	} else {
		// Extract $numNews newest news...
		$xmlNews = array_slice($xmlTree->root->children, 1 + $offset, $numNews);
		// initialize...
		$item = array();
		$res = array();
		// Extract information...
		foreach ($xmlNews as $news) {
			$item['date'] = $news->getAttribute('date');
			$item['text'] = $news->getAttribute('text');
			$item['link'] = $news->getAttribute('href');
			$item['auth'] = 'Webmaster';
			$res[] =  $item;
		}
		return $res;
	}
}


/**
 * Returns text array with links.
 * Note: the text array is returned with the newest post as the first element:
 * newest: text[0], text[1] ... text[n] -> oldest.
 *
 * @name	getWebboardArray
 * @date	2000-10-30
 * @author	Jesper Rønn <hide@address.com>
 * @author	Tobias H. Michaelsen <hide@address.com>
 * @calls		readArrayFromFile
 * @calledby	global.php, morenews.php
 */
function getWebboardArray($newsFile, $count = null, $offset = 0, $splitter = "|:|")
{
	global $design;
	
	// read all postings
	$newsArray = readArrayFromFile($newsFile);

	if (is_array($newsArray) and !empty($newsArray)) {
		// pop the first element of $newsArray
		array_shift($newsArray);

		// Explode each line to an array, and sort at the same time..,
		#$newsArray = explodeArray($newsArray, $splitter, true, $sortColumn);
		foreach ($newsArray as $key => $val) {
			$newsArray[$key] = explode($splitter, $val);
			settype($newsArray[$key][1], "integer");
		}
		array_columnsort($newsArray, 1);

		// Reverse the arry (get the newest postings first) and
		// slice out the part of the array that we need...
		$newsArray = array_reverse($newsArray);
		if (!empty($count)) {
			$newsArray = array_slice($newsArray, $offset, $count);
		}

		// Create array with information to return
		$result = array();
		foreach ($newsArray as $news) {
			if (!empty($news)) {
				$tmp['date'] = date("d/m-y", $news[5]);
				$tmp['text'] = $news[2];
				$tmp['link'] = "webboard/?action=display&id=".$news[1]."&group=../".$newsFile;
				// Truncate the message-body and strip HTML tags:
				$tmp['summ'] = truncateLongTitle($news[6], $design['truncDescLength'], true);
				$tmp['auth'] = $news[3];
				$result[] = $tmp;
			}
		}
		return $result;
  	} else {
		$errorMsg = '<p class="news"><b>Error:</b> ';
		$errorMsg .= 'Could not read from file "'.$newsFile.'"</p>';
		if (is_string($newsArray)) {
			$errorMsg .= $newsArray;
		}
		return $errorMsg;
	}
}


/**
 * Read a textfile and return its contents as an array,
 * with comment-line and empty lines removed.
 *
 * @name	readArrayFromFile
 * @date	2000-10-30
 * @author	Jesper Rønn <hide@address.com>
 * @author	Tobias H. Michaelsen <hide@address.com>
 * @param	string	$filename	Name of the file to read.
 * @calls	
 * @calledby	getNewsArray, getWebboardArray
 */
function readArrayFromFile($filename)
{
	if (file_exists($filename)) {
		$tmpArray = file($filename);
		$result = array();
		foreach ($tmpArray as $line) {
			// Strip comments and empty lines.
			$line = trim($line);
			if (substr($line,0,1) != "#" and !empty($line)) {
				$result[] = $line;
			}
		}
		return $result;
	} else {
		return "File not found!\n[".$filename."]";
	}
}


/**
 * Check if a string is longer than a given max-length.
 * If it is, it is truncated and three dots are added at the end to imply
 * that this is a shortened version of a longer string.
 * 
 * @name	truncateLongTitle
 * @date	2000-05-02
 * @author	Jesper Rønn <hide@address.com>
 * @param	string	$str	The string to check and possibly truncate.
 * @param	integer	$length	Maximal lengt for returned string.
 * $param	boolean	$stripTags	Whether to strip HTML tags from the string
 * 								before truncating.
 * @returns	string			A text-string of max {$length} chars.
 * @see		getWebboardArray(), writeNewsMini(), writeNewsClean()
 */
function truncateLongTitle($str, $length="", $stripTags=false){
	global $design;
	if ($length=="")	
		$length = $design['debatQuoteLength'];
	if ($stripTags == true)
		$str = trim(strip_tags($str));
	if (strlen($str) > $length )	{	// hvis orig. længere end angivet længde
		$tmp = substr($str,0,$length);
		$tmp .= "...";//4 tilføj prikker
		return $tmp;
	} else {
		return $str;
	}
}


/**
 *	
 *	@name	computeMoreLink
 *	@date	2000-10-30
 *	@author	Jesper Rønn (JROE)
 *	@calls		none
 *	@calledby	global.php, morenews.php
 */
function computeMoreLink($type, $linkTxt, $nextOffset = 0, $pos = null)
{
        $str  = '<a href="?a=morenews.php&amp;type='.$type;
        $str .= '&amp;offset='.$nextOffset;
        $str .= !empty($pos) ? '&amp;position='.$pos : '';
        $str .= '">'.$linkTxt.'</a>';
        return $str;
}

/**
 * Create an xml-file containing news from an array.
 *
 * @name	createXMLNewsFile
 * @author	Tobias H. Michaelsen <hide@address.com>
 * @date	2001-10-01
 * @param	string	$filename	Name of the xml-file.
 * @param	string	$title		Title for news.
 * @param	array	$newsArray	Array containing the news.
 * @see		XML/Tree.php
 */
function createXMLNewsFile($filename, $title, $newsArray)
{
	include_once('XML/Tree.php');
	$newsXml = new XML_Tree($filename, '1.0', 'iso-8859-1');
	$newsXml->addRoot('news');
	$newsXml->root->addChild('title', $title);
	foreach ($newsArray as $item) {
		$attributes['date'] = $item['date'];
		$attributes['text'] = $item['text'];
		$attributes['href'] = $item['link'];
		$newsXml->root->addChild('item', '', $attributes);
	}
	$err = $newsXml->writeFile();
	if (PEAR::isError($err)) {
		echo $err->getMessage()."<br />\n";
	}
}



/************************** HTML output functions below ************/


/*	
 *	$moreLink 	 = maybe should be URL or false
 *	$layoutstyle = ( clean | mini | content[the new one for overview morenews page])
 */
function writeNewsMini($newsTitle, $text, $moreLink = null) 
{
	if (is_array($text)) {
		$textArray = $text;
	} elseif (is_string($text)) {
		$errorMsg = $text;
	}
	
	if (DEBUG) {
		echo '<pre>';
		var_dump($text);
		echo '</pre>';
	}

	// the class "news" constrains the width of the table
	// (explicitly set in the stylesheet)
	$str = '<table class="news">'."\n";
	$str .= '  <tr><th class="news">'.$newsTitle."</th></tr>\n";

	if (isset($textArray)) {
		$newsCount = count($textArray);
		foreach ($textArray as $news) {
			// check for empty line
			if (!empty($news['text'])) {
				$str .= '  <tr><td class="news">';
				$news['trunc']	= truncateLongTitle($news['text']);
				$str .= writeLink($news);
				$str .= "<br />\n    <i>(".$news['date'].")</i></td></tr>\n";
			}
		}
		if (DEBUG) { var_dump($textArray[$j]); }
		if (DEBUG) { echo $cfg['count']." : ".$newsCount."\t(line:". __LINE__ .")"; }
		if (!empty($moreLink)) {
			$str .= '  <tr><td class="news" style="text-align: right">'.$moreLink.'</td></tr>';
		}
	} elseif ($errorMsg) {
		$str .= '  <tr><td class="news">';
		$str .= $errorMsg."</td></tr>\n";
	} else {
		// virtually impossible situation
		$str .= '  <tr><td class="news">Unknown error!</td></tr>'."\n";
	}
	$str .= "</table>\n";
	return $str;
}	# end function writeNewsMini


/**
 *	$textArray[$j]['auth']	author
 *	$textArray[$j]['summ']	brief summary 
 *	$textArray[$j]['date']	
 *	$textArray[$j]['text'] 	= title
 *	$textArray[$j]['date']	human time, e.g. 2000-10-30
 */
function writeNewsClean($newsTitle, $textArray, $errorMsg = false, $moreLink = false) // just titles & dates
{ 
	global $design,  $cfg;
	
	if (DEBUG) { echo "<hr /><br />"; print_r($textArray); echo "<hr />"; }

	$str = '<h2>'.$newsTitle."</h2>\n";

	if (is_array($textArray)) {
		$newsCount = count($textArray);	
		$str .= '<dl>';
		foreach ($textArray as $news) { // ($j = 0; $j < $newsCount; $j++) { 
			// check for empty line
			if (!empty($news['text'])) {
				$str .= '<dt  class="news">'; // term
				$news['trunc'] = truncateLongTitle($news['text'], $design['truncTitleLength']); // used for writeLink function
				$str .= writeLink($news).'</dt>'."\n";
				$str .= '<dd class="news">';
				if (!empty($news['summ'])) {
					$str .= $news['summ'].'<br />';
				}
				$str .= '<i>('.$news['auth'].', '.$news['date'].')</i><br />';
				$str .= '</dd>'."\n";
			}
		}
		$str .= "</dl>\n";
		// {{{ DEBUG
		if (DEBUG) {
			var_dump($textArray[$j]);
			echo $cfg['count']. " : ". $newsCount ."\t(line:". __LINE__ .")";
		}
		// }}}
		if ($moreLink) {
			$str .= '<p class="news" style="text-align: right">'.$moreLink.'</p>';
		}
		// {{{ DDEBUG
		if (DEBUG) {
			echo "<hr /><br />";
			var_dump($str);
		}
		// }}}
	} elseif (is_string($textArray)) {
		$str .= '<p class="news">'.$textArray."</p>\n";
	} else {
		// "virtually impossible situation":
		$str .= '<p class="news">Unknown error!</p>'."\n";
	}
#	$str .= "\n\t\t\n";
	return $str;

}	# end function writeNewsClean



/**
 * Create a text string containing a link if available.
 *
 * @param	array	$arr	containing 'text', 'link' and 'trunc'.
 */
function writeLink ($arr) 
{
	if ($arr['link'] != "") {
		$str = '<a href="'.htmlspecialchars($arr['link']).'"';
		$str .= ' title="'.htmlspecialchars($arr['text']).'"';
		$str .= '>'.htmlspecialchars($arr['trunc']).'</a>';
	} else {
		$str = $arr['text'];
	}
	return $str;
} // end function  writeLink ($arr) 


if (DEBUG) { echo "<!-- End  of file: menu  -->\n"; }
?>
Return current item: PHP SiteBuilder