<?php

function b_d3news_relations_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_relations_show($options) {

	$mydirname = empty( $options[0] ) ? $mytrustdirname : $options[0] ;
	require dirname( dirname( __FILE__ ) ).'/include/configs.inc.php';

	$block = array();
	$url = $_SERVER["REQUEST_URI"];
	$splits = preg_split("/[?&]/", $url);

	if($splits[1] == 'page=article'){
		$myids = preg_split("/=/", $splits[2]);
		$myid = $myids[1];

		$sql  = "select * from  " . $table_stories . " as a inner join " . $table_relations . " as b on a.storyid = b.linkedid where b.storyid=" . $myid . " and a.type > 0 AND a.published > 0 AND (a.expired = 0 OR a.expired > " . time() . ") AND a.block = 1 order by a.published";
		$result = $xoopsDB->query($sql);
		$rowcount = icms::$xoopsDB->getRowsNum($result);


		$sdata = array();
		if($rowcount){
			while ( $myrow2 = $xoopsDB->fetchArray($result) ) {
				$deach = array();
				$deach['storyid'] = $myrow2['storyid'];
				$deach['title'] = $myrow2['title'];
				$deach['date'] = date('Y-m-d', $myrow2['published']);
				if($myrow2['image'])
					$deach['image'] = $myrow2['image'];
				else
					$deach['image'] = 'noimage.png';

				$sdata[] = $deach;
			}

			$block['myid'] = $myid[1];
			$block['sdata'] = $sdata;

			// require_once XOOPS_ROOT_PATH.'/class/template.php' ;
			$tpl = new icms_view_Tpl() ;
			$tpl->assign( 'block' , $block ) ;
			$ret['content'] = $tpl->fetch( 'db:'.$mydirname.'_block_relations.html') ;
			return $ret ;
		} else {
			return $block;
		}
	} else {
		// just assign it
		return $block ;
	}


}

function b_d3news_relations_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_relations_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_relations_order() ) ,
		'selected_order' => $selected_order ,
		'yn_options' => array( 0 => _NO , 1 => _YES ) ,
	) ) ;
	return $tpl->fetch( 'db:'.$mydirname.'_blockedit_relations.html' ) ;
}
?>
