<?php
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;

eval( '

function '.$mydirname.'_new($limit=0, $offset=0){
	return _xpress_new("'.$mydirname.'" ,$limit, $offset ) ;
}

function '.$mydirname.'_num(){
	return _xpress_num("'.$mydirname.'") ;
}

function '.$mydirname.'_data($limit=0, $offset=0){
	return _xpress_data("'.$mydirname.'" ,$limit, $offset ) ;
}
' ) ;

if (!function_exists('_xpress_new')) {
//================================================================
// What's New Module
// get aritciles from module
// xpress 0.20 <http://www.toemon.com>
// 2007-07-17 toemon 
//================================================================

// --- function start ---
function _xpress_new($mydirname, $limit=0, $offset=0) 
{
	global $xoopsDB;

	if ($mydirname == 'wordpress'){
		$wp_prefix = 'wp';
	} else {
		$wp_prefix = $mydirname;
	}
	require_once (XOOPS_ROOT_PATH . '/modules/'.$mydirname . '/include/general_functions.php');
	include(XOOPS_ROOT_PATH . '/modules/'.$mydirname . '/wp-includes/version.php');

	$modules_table = $xoopsDB->prefix('modules');
	$modSQL ="SELECT mid FROM " . $modules_table . " WHERE dirname LIKE '" . $mydirname . "'";
	$modRes = $xoopsDB->query($modSQL, 0, 0);
	$modRow = $xoopsDB->fetchArray($modRes);
	$module_id = $modRow['mid'];	

	$table_config = $xoopsDB->prefix('config');
	$confSQL ="SELECT conf_value FROM " . $table_config . " WHERE (conf_modid = " . $module_id . ") AND (conf_name LIKE 'whatsnew_use_mod_date')";
	$confRes = $xoopsDB->query($confSQL, 0, 0);
	$confRow = $xoopsDB->fetchArray($confRes);
	$use_modified_date = $confRow['conf_value'];

	$url_mod = XOOPS_URL."/modules/".$mydirname;

	$table_posts      = $xoopsDB->prefix($wp_prefix . "_posts");
	$table_categories = $xoopsDB->prefix($wp_prefix . "_categories");
	$table_post2cat   = $xoopsDB->prefix($wp_prefix . "_post2cat");
	$table_views   = $xoopsDB->prefix($wp_prefix . "_views");
	$table_term_relationships = $xoopsDB->prefix($wp_prefix . "_term_relationships");
	$table_term_taxonomy = $xoopsDB->prefix($wp_prefix . "_term_taxonomy");
	$table_terms = $xoopsDB->prefix($wp_prefix . "_terms");

//	$sql = "SELECT ID, post_title, post_content, UNIX_TIMESTAMP(post_date) AS unix_post_date, post_status FROM ".$xoopsDB->prefix($wp_prefix . "_posts")." WHERE post_status='publish' ORDER BY post_date DESC";

	$sql1 = "SELECT ID, post_author, post_title, post_content, post_type, comment_count, UNIX_TIMESTAMP(post_date) AS unix_post_date, UNIX_TIMESTAMP(post_modified) AS unix_post_modified, post_status FROM ".$table_posts." WHERE (post_status='publish') AND (UNIX_TIMESTAMP(post_date) <= UNIX_TIMESTAMP()) ORDER BY post_date DESC";

	
	$res1 = $xoopsDB->query($sql1, $limit, $offset);

	$i = 0;
	$ret = array();

	while($row1 = $xoopsDB->fetchArray($res1))
	{
		$id = $row1['ID'];
		if ($wp_db_version < 6124){
			$sql2 = "SELECT c.cat_ID, c.cat_name FROM ".$table_categories." c, ".$table_post2cat." p2c WHERE c.cat_ID = p2c.category_id AND p2c.post_id=".$id;
		} else {
			$sql2  = "SELECT $table_term_relationships.object_id, $table_terms.term_id AS cat_ID, $table_terms.name AS cat_name ";
			$sql2 .= "FROM $table_term_relationships INNER JOIN ($table_term_taxonomy INNER JOIN $table_terms ON $table_term_taxonomy.term_id = $table_terms.term_id) ON $table_term_relationships.term_taxonomy_id = $table_term_taxonomy.term_taxonomy_id ";
			$sql2 .= "WHERE ($table_term_relationships.object_id =" . $id.") AND ($table_term_taxonomy.taxonomy='category')";		
		}
		$row2 = $xoopsDB->fetchArray( $xoopsDB->query($sql2) );
		if ($row1['post_type'] == 'page'){
				$ret[$i]['link']     = $url_mod."/?page_id=".$id;
		} else {
			$ret[$i]['link']     = $url_mod."/index.php?p=".$id;
		}
		$ret[$i]['cat_link'] = $url_mod."/index.php?cat=".$row2['cat_ID'];

		$ret[$i]['title']    = $row1['post_title'];
		$ret[$i]['cat_name'] = $row2['cat_name'];

		$ret[$i]['uid'] = wp_uid_to_xoops_uid($row1['post_author'],$mydirname);
		$ret[$i]['replies'] = $row1['comment_count'];


		if(empty($use_modified_date)) {
			$time = $row1['unix_post_date'];
		} else {

			if ($row1['unix_post_modified'] > $row1['unix_post_date']){
				$time = $row1['unix_post_modified'];
			} else {
				$time = $row1['unix_post_date'];
			}
		}

	   	$ret[$i]['time']     = $time;
		$ret[$i]['modified'] = $time;
		$ret[$i]['issued']   = $row1['unix_post_date'];
		$content=$row1['post_content'];
		$content = strip_tags($content);

		$ret[$i]['description'] = $content;

		$sql3 = "SELECT post_views FROM  " .  $table_views . " WHERE post_id = " . $id;
		$row3 = $xoopsDB->fetchArray( $xoopsDB->query($sql3) );
	   	$ret[$i]['hits']     = $row3['post_views'];



		$i++;
	}

	return $ret;
}

function _xpress_num($mydirname) 
{
	// get $mydirnumber
	if( ! preg_match( '/^(\D+)(\d*)$/' , $mydirname , $regs ) ) echo ( "invalid dirname: " . htmlspecialchars( $mydirname ) ) ;

	global $xoopsDB;

	if ($mydirname == 'wordpress'){
		$wp_prefix = 'wp';
	} else {
		$wp_prefix = $mydirname;
	}

	$sql = "SELECT count(*) FROM ".$xoopsDB->prefix($wp_prefix . "_posts")." ORDER BY ID";
	$array = $xoopsDB->fetchRow( $xoopsDB->query($sql) );
	$num = $array[0];
	if (empty($num)) $num = 0;

	return $num;
}

function _xpress_data($mydirname,$limit=0, $offset=0) 
{
	// get $mydirnumber
	if( ! preg_match( '/^(\D+)(\d*)$/' , $mydirname , $regs ) ) echo ( "invalid dirname: " . htmlspecialchars( $mydirname ) ) ;

	global $xoopsDB;

	if ($mydirname == 'wordpress'){
		$wp_prefix = 'wp';
	} else {
		$wp_prefix = $mydirname;
	}
	$sql = "SELECT ID, post_title, UNIX_TIMESTAMP(post_date) AS unix_post_date, post_status FROM ".$xoopsDB->prefix($wp_prefix . "_posts")." WHERE post_status='publish' ORDER BY ID";
	$result = $xoopsDB->query($sql,$limit,$offset);

	$i = 0;
	$ret = array();

	while($row1 = $xoopsDB->fetchArray($result))
	{
		$id = $row1['ID'];
		$ret[$i]['id'] = $id;
		$ret[$i]['link'] = XOOPS_URL."/modules/xpress/index.php?p=".$id."";
		$ret[$i]['title'] = $row1['post_title'];
		$ret[$i]['time']  = $row1['unix_post_date'];
		$i++;
	}

	return $ret;

}


// --- function end ---

}

?>
