<?php

require_once XOOPS_TRUST_PATH.'/modules/d3forum/class/D3commentAbstract.class.php' ;
require_once XOOPS_ROOT_PATH.'/modules/minidiary/functions.php' ; //added 20090330

// a class for d3forum comment integration
class minidiaryD3commentContent extends D3commentAbstract {


function fetchSummary( $external_link_id )
{
	global $xoopsDB, $xoopsConfig, $xoopsUser;

	$db =& Database::getInstance() ;
	$myts =& MyTextSanitizer::getInstance();
	
	$module_handler =& xoops_gethandler( 'module' ) ;
	$module =& $module_handler->getByDirname( $this->mydirname ) ;
	
	$entryID = intval( $external_link_id ) ;
	$mydirname = $this->mydirname ;
	if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

	if(is_object($xoopsUser)) {
		$uid = $xoopsUser->getVar('uid');
	} else {
		$uid = 0;
	}

	$content_row = $db->fetchArray( $db->query( "SELECT uid, bid, cid, title, diary FROM ".$db->prefix("yd_diary")." WHERE bid =$entryID" ) ) ;
	
	//if( empty( $content_row ) ) {
	//	redirect_header(XOOPS_URL.'/user.php',3,_NOPERM);
	//	exit();
	//}
	$diary_uid = $content_row['uid'];

	//checking permission : if false, redirect
		$sql = "SELECT * FROM ".$xoopsDB->prefix('yd_config')." WHERE uid=".intval($diary_uid);
		$result = $xoopsDB->query($sql);
		while($dbdat = $xoopsDB->fetchArray($result)){ $openarea=$dbdat['openarea']; }

		$permission = $this->check_permission($diary_uid, $uid, $openarea);
		if( $permission != true ) {
			redirect_header(XOOPS_URL.'/user.php',3,_NOPERM);
			exit();
		}

	$categoryID = $content_row['cid'];
	$uri = XOOPS_URL.'/modules/'.$mydirname.'/detail.php?&bid='.$entryID.'&req_uid='.$diary_uid;

	$str = strip_tags($myts->displayTarea($content_row['diary'],0,1,1,0));
	$summary = xoops_substr( $str , 0 , 255 );

	return array(
		'dirname' => $mydirname ,
		'module_name' => $module->getVar( 'name' ) ,
		'subject' => $myts->makeTboxData4Show( $content_row['title'] ) ,
		'uri' => $uri,
		'summary' => $summary,
	) ;
}

// callback on newtopic/edit/reply/delete  overrides function added 2009/03/31
function onUpdate( $mode , $link_id , $forum_id , $topic_id , $post_id = 0 )
{
	global $xoopsDB, $xoopsConfig, $xoopsUser;

	if (($mode != "newtopic") && ($mode != "reply")) return true ;
	
	// non-module integration returns false quickly
	if( ! is_object( $this->module ) ) return false ;
	$not_module =& $this->module ;
	$not_modid = $this->module->getVar('mid') ;
	$not_catinfo =& notificationCommentCategoryInfo( $not_modid ) ;
	// module without 'comment' notification
	if( empty( $not_catinfo ) ) return false ;

	//get uname
	if(is_object($xoopsUser)) {
		$uname = $xoopsUser->getVar('uname');
	}
	
	//get entry's title, bid, uid
	$sql = "SELECT * FROM " . $xoopsDB->prefix('yd_diary') . " WHERE bid = ".intval($link_id);
	$result = $xoopsDB->query($sql);
	while ( $dbdat = $xoopsDB->fetchArray($result)){
		$title = $dbdat['title'];
		$bid = $dbdat['bid'];
		$uid = $dbdat['uid'];
	}

	// Check XSNS dirname
	if(empty($xoopsModuleConfig['xsns_dirname'])){
			$xsns_dirname="xsns";
	}else{
		$xsns_dirname=$xoopsModuleConfig['xsns_dirname'];
	}
	
	// Trigger Notification
	$users2notify = get_users_can_read_entry( $xsns_dirname, $uid );
	$not_handler =& D3NotificationHandler::getInstance() ;
	
	$comment_tags = array( 'ENTRY_TITLE' => $title , 'ENTRY_BLOGGER' => $uname , 'ENTRY_URI' => XOOPS_URL."/modules/minidiary/detail.php?bid=".$link_id."&req_uid=".$uid."#comment" ) ;
	$not_handler->triggerEvent( $this->mydirname , '' , 'blogger' , $uid , 'new_comment' , $comment_tags , $users2notify ) ;
	
	$not_category = $not_catinfo['name'] ;
	$not_itemid = $link_id ;
	$not_handler->triggerEvent( $this->mydirname , '' , $not_category , $not_itemid , 'comment' , $comment_tags , $users2notify ) ;
	
	return true ;
}

function validate_id( $link_id )
{
	$link_id = intval( $link_id ) ;
	$mydirname = $this->mydirname ;

	$db =& Database::getInstance() ;

	list( $count ) = $db->fetchRow( $db->query( "SELECT COUNT(*) FROM ".$db->prefix("yd_diary")." WHERE bid=$link_id" ) ) ;

	if( $count <= 0 ) return false ;
	else return $link_id ;
}

// set forum_dirname from config.comment_dirname
function setD3forumDirname( $d3forum_dirname = '' )
{
	if( ! empty($this->mod_config['comment_dirname'] ) ) {
    		$this->d3forum_dirname = $this->mod_config['comment_dirname'] ;
	} elseif( ! empty( $params['comment_dirname'] ) ) {
		$this->d3forum_dirname = $params['comment_dirname'] ;
	} elseif( $d3forum_dirname ) {
		$this->d3forum_dirname = $d3forum_dirname ;
	} else {
		$this->d3forum_dirname = 'd3forum' ;
	}
}

function check_permission($req_uid, $uid, $openarea)
{
	global $xoopsDB, $xoopsModuleConfig;
	
	if($req_uid == $uid){return true;}
	
	if($openarea == 0){return true;}
	
	if($openarea == 1){
		if($uid==0){return false;}else{return true;}
	}
	
	// xsnsÏ¢·È
	if($xoopsModuleConfig['use_xsns'] == 1){
		if($openarea == 2){
			return is_xsns_friend($req_uid, $uid);
		}
		if($openarea == 3){
			return is_xsns_friend2($req_uid, $uid);
		}
	}
	
	return false;
}

// d3comment for legacy comment callback overrides function added 2009/03/31
function processCommentNotifications( $mode , $link_id , $forum_id , $topic_id , $post_id )
{
	return true ;
}

}
?>