<?php

function b_d3news_new_allowed_order()
{
	return array(
		_MB_D3NEWS_DATE . ' DESC' => 'published DESC' ,
		_MB_D3NEWS_HITS . ' DESC' => 'counter DESC' ,
		_MB_D3NEWS_DATE . ' ASC' => 'published ASC' ,
		_MB_D3NEWS_HITS . ' ASC' => 'counter ASC' ,
		'title' => 'title' ,
		'created DESC' => 'created DESC' ,
		'created ASC' => 'created ASC' ,
		'expired DESC' => 'expired DESC' ,
		'expired ASC' => 'expired ASC' ,
		'comments DESC' => 'comments DESC' ,
	) ;
}

function b_d3news_new_show($options) {

	$mydirname = $options[0] ;
	if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;
	$selected_order = empty( $options[1] ) || ! in_array( $options[1] , b_d3news_new_allowed_order() ) ? 'published DESC' : $options[1] ;

	require dirname( dirname( __FILE__ ) ).'/include/configs.inc.php';

	$module_handler  =& xoops_gethandler('module');
	$module = $module_handler->getByDirname($mydirname);

	$block = array();

	//
	if($options[4] > 0){

		$sql  = sprintf('SELECT s.storyid, s.topicid, s.title, s.image, s.hometext, s.bodytext, s.published, s.expired, s.counter, s.comments, s.uid, s.topicimg, s.html, s.smiley, s.br, s.xcode, t.topic_title, t. topic_imgurl FROM %s s, %s t WHERE s.type > 0 AND s.published < %u AND s.published > 0 AND (s.expired = 0 OR s.expired > %3$u) AND s.topicid = t.topic_id AND s.block = 1 ORDER BY weight desc, %s', $table_stories, $table_topics, time(), $selected_order);

		$result = $xoopsDB->query($sql,$options[4],0);


		while ( $myrow = $xoopsDB->fetchArray($result) ) {
			$fullstory = array() ; // GIJ
			$fullstory['id']       = $myrow['storyid'];
			$fullstory['posttime'] = formatTimestamp($myrow['published'], $d3news_date_format);
			$fullstory['date']     = formatTimestamp($myrow['published'], $d3news_date_format);
			$fullstory['published'] = intval($myrow['published']);
			$fullstory['topicid']  = $myrow['topicid'];
			$fullstory['topic']    = $myts->makeTboxData4Show($myrow['topic_title']);
			$fullstory['title']    = $myts->makeTboxData4Show($myrow['title']);
			$fullstory['text']     = $myts->displayTarea($myrow['hometext'],$myrow['html'],$myrow['smiley'],$myrow['xcode'],1,$myrow['br']);
			$fullstory['hits']     = $myrow['counter'];
			$fullstory['title_link'] = true;
			//
			$fullstory['uid']      = $myrow['uid'];
			$fullstory['uname']    = icms_member_user_Object::getUnameFromId($myrow['uid']);
			$fullstory['realname'] = icms_member_user_Object::getUnameFromId($myrow['uid'], 1);
			$fullstory['morelink'] = '';

			//
			if ( myStrlenText($myrow['bodytext']) > 1 ) {
				$fullstory['bytes']    = sprintf(_MB_D3NEWS_BYTESMORE, myStrlenText($myrow['bodytext']));
				$fullstory['readmore'] = true;
			}else{
				$fullstory['bytes']    = 0;
				$fullstory['readmore'] = false;
			}

			//
			$ccount = $myrow['comments'];
			if( $ccount == 0 ){
				$fullstory['comentstotal'] = _MB_D3NEWS_COMMENTS;
			}elseif( $ccount == 1 ) {
				$fullstory['comentstotal'] = _MB_D3NEWS_ONECOMMENT;
			}else{
				$fullstory['comentstotal'] = sprintf(_MB_D3NEWS_NUMCOMMENTS, $ccount);
			}

			//
			$fullstory['adminlink'] = 0;

			//
			if ( $myrow['topicimg'] ) {
				$fullstory['topic_url'] = makeTopicImgURL($d3news_topicon_path, $myrow['topic_imgurl']);
				$fullstory['align']     = topicImgAlign($myrow['topicimg']);
			}

			if($myrow['image'])
				$fullstory['titleimg'] = XOOPS_URL . "/uploads/d3news/t_" . $myrow['image'];
			else
				$fullstory['titleimg'] = XOOPS_URL . "/uploads/d3news/noimage.png";


			$block['fullstories'][] = $fullstory;

		}
	}

	if( $options[2] - $options[4] > 0 ){

//		$sql  = sprintf('SELECT storyid, title, image, published, expired, counter, uid FROM %s WHERE type > 0 AND published < %u AND published > 0 AND (expired = 0 OR expired > %2$u) AND block = 1 ORDER BY %s', $table_stories, time(), $selected_order);
		$sql  = sprintf('SELECT s.storyid, s.topicid, s.title, s.image, s.hometext, s.bodytext, s.published, s.expired, s.weight, s.counter, s.comments, s.uid, s.topicimg, s.html, s.smiley, s.br, s.xcode, t.topic_title, t. topic_imgurl FROM %s s, %s t WHERE s.type > 0 AND s.published < %u AND s.published > 0 AND (s.expired = 0 OR s.expired > %3$u) AND s.topicid = t.topic_id AND s.block = 1 ORDER BY s.weight desc, %s', $table_stories, $table_topics, time(), $selected_order);

		$result = $xoopsDB->query($sql,$options[2]-$options[4],$options[4]);

		while ( $myrow = $xoopsDB->fetchArray($result) ) {
			$story = array();

			//
			$story['title']    = $myts->makeTboxData4Show(icms_core_DataFilter::icms_substr($myrow['title'], 0 ,$options[3] + 3, '...'));
			$story['id']       = $myrow['storyid'];
			$story['date']     = formatTimestamp($myrow['published'], $d3news_date_format);
			$story['published'] = intval($myrow['published']);
			$story['hits']     = $myrow['counter'];
			$story['topic_title']      = $myrow['topic_title'];
			$story['desc']      = mb_substr(strip_tags($myrow['hometext']),0,100) . "... ";
			$story['uname']    = icms_member_user_Object::getUnameFromId($myrow['uid']);
			$story['uid']      = $myrow['uid'];
			$story['realname'] = icms_member_user_Object::getUnameFromId($myrow['uid'], 1);
			if($myrow['image'])
				$story['image'] = XOOPS_URL . "/uploads/d3news/t_" . $myrow['image'];
			else
				$story['image'] = XOOPS_URL . "/uploads/d3news/noimage.png";

			$block['stories'][] = $story;
		}

	}

	if( ! empty( $block ) ) {
		$block['mod_config'] = @$d3news_configs ;
		$block['lang_postedby'] = _POSTEDBY;
		$block['lang_on']       = _ON;
		$block['lang_reads']    = _READS;
		$block['lang_readmore'] = _MB_D3NEWS_READMORE;
		$block['type']  = $selected_order;
		$block['mydirurl'] = XOOPS_URL.'/modules/'.$mydirname;;
		$block['mydirname'] = $mydirname;
	}

	$tpl = new icms_view_Tpl() ;
	$tpl->assign( 'block' , $block ) ;
	$ret['content'] = $tpl->fetch( 'db:'.$mydirname.'_block_new.html') ;
	return $ret ;

}



function b_d3news_new_edit($options) {

	$mydirname = $options[0] ;
	if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;
	$selected_order = empty( $options[1] ) || ! in_array( $options[1] , b_d3news_new_allowed_order() ) ? 'published DESC' : $options[1] ;

	require_once XOOPS_ROOT_PATH.'/class/template.php' ;
	$tpl = new icms_view_Tpl() ;
	$tpl->assign( array(
		'mydirname' => $mydirname ,
		'options' => $options ,
		'order_options' => array_flip( b_d3news_new_allowed_order() ) ,
		'selected_order' => $selected_order ,
	) ) ;
	return $tpl->fetch( 'db:'.$mydirname.'_blockedit_new.html' ) ;
}

?>
