<?php
function do_CommentNotifications($commentID, $comment_post_ID)
{
	$xpress_prefix = $mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;
	if ($xpress_prefix == 'wordpress') $xpress_prefix = 'wp';
	$db =& Database::getInstance() ;
	$myts =& MyTextsanitizer::getInstance() ;

	$table_term_relationships = $db->prefix($xpress_prefix."_term_relationships");
	$table_term_taxonomy = $db->prefix($xpress_prefix."_term_taxonomy");
	$table_terms = $db->prefix($xpress_prefix."_terms");
	$table_categories = $db->prefix($xpress_prefix."_categories");
	$wp_post = $db->prefix($xpress_prefix."_posts");
	$wp_options = $db->prefix($xpress_prefix."_options");
	$wp_users  = $db->prefix($xpress_prefix."_users");
	$wp_comments  = $db->prefix($xpress_prefix."_comments");
	$post_id = $comment_post_ID;

	$post_title = get_the_title($post_id);
	$post_url = get_permalink($post_id). '#comment';
	$blog_name = get_bloginfo('name');

	/*
	$sql = "SELECT option_value  FROM $wp_options WHERE option_name ='blogname'";
	$blog_row = $db->fetchArray( $db->query( $sql ) ) ;
	if( empty( $blog_row ) ) return false;
	$blog_name = $blog_row['option_value'];
	*/								
	// query
	$sql = "SELECT * FROM ".$wp_post." WHERE ID=$comment_post_ID ";
	$post_row = $db->fetchArray( $db->query( $sql ) ) ;
	if( empty( $post_row ) ) return false;
	//			$post_title = $post_row['post_title'];
	$post_author = $post_row['post_author'];

	$sql = "SELECT display_name  FROM $wp_users WHERE ID ='$post_author'";
	$blog_row = $db->fetchArray( $db->query( $sql ) ) ;
	if( empty( $blog_row ) ) return false;
	$user_name = $blog_row['display_name'];

	require_once XOOPS_ROOT_PATH . '/include/notification_functions.php' ;
	// non-module integration returns false quickly
	
	if( ! is_object($GLOBALS["xoopsModule"]) ) return false ;
	$not_modid = $GLOBALS["xoopsModule"]->getVar('mid') ;

	$comment_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;
	$notification_handler =& xoops_gethandler( 'notification' ) ;
	$notification_handler->triggerEvent( 'global' , 0 , 'comment' , $comment_tags , false , $not_modid ) ;
	$notification_handler->triggerEvent( 'author' , $post_author , 'comment' , $comment_tags , false , $not_modid ) ;
	$notification_handler->triggerEvent( 'post' , $comment_post_ID , 'comment' , $comment_tags , false , $not_modid ) ;

	// categorie notification
	include(XOOPS_ROOT_PATH . '/modules/'.$mydirname . '/wp-includes/version.php');
	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=".$comment_post_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 =" . $comment_post_ID.") AND ($table_term_taxonomy.taxonomy='category')";		
	}
	$res2 = $db->query($sql2);
	while($row2 = $db->fetchArray($res2)){
		$cat_id = $row2['cat_ID'];
		$cat_name = $row2['cat_name'];
		$comment_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_CAT_TITLE' => $cat_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;
		$notification_handler->triggerEvent( 'category' , $cat_id , 'comment' , $comment_tags , false , $not_modid ) ;
	}
}

function do_PostNotifications($post_id,$not_event)
{
	 // $not_event:		newpost,editpost ; $commentID, $comment_post_ID)
	 
	$xpress_prefix = $mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;
	if ($xpress_prefix == 'wordpress') $xpress_prefix = 'wp';
	$db =& Database::getInstance() ;
	$myts =& MyTextsanitizer::getInstance() ;

	$table_term_relationships = $db->prefix($xpress_prefix."_term_relationships");
	$table_term_taxonomy = $db->prefix($xpress_prefix."_term_taxonomy");
	$table_terms = $db->prefix($xpress_prefix."_terms");
	$table_categories = $db->prefix($xpress_prefix."_categories");
	$wp_post = $db->prefix($xpress_prefix."_posts");
	$wp_options = $db->prefix($xpress_prefix."_options");
	$wp_users  = $db->prefix($xpress_prefix."_users");
	$wp_comments  = $db->prefix($xpress_prefix."_comments");

	$post_title = get_the_title($post_id);
	$post_url = get_permalink($post_id). '#comment';
	$blog_name = get_bloginfo('name');

	// query
	$sql = "SELECT * FROM ".$wp_post." WHERE ID=$post_id ";
	$post_row = $db->fetchArray( $db->query( $sql ) ) ;
	if( empty( $post_row ) ) return false;
	//			$post_title = $post_row['post_title'];
	$post_author = $post_row['post_author'];

	$sql = "SELECT display_name  FROM $wp_users WHERE ID ='$post_author'";
	$blog_row = $db->fetchArray( $db->query( $sql ) ) ;
	if( empty( $blog_row ) ) return false;
	$user_name = $blog_row['display_name'];

	require_once XOOPS_ROOT_PATH . '/include/notification_functions.php' ;
	// non-module integration returns false quickly
	
	if( ! is_object($GLOBALS["xoopsModule"]) ) return false ;
	$not_modid = $GLOBALS["xoopsModule"]->getVar('mid') ;

	$posts_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;

	$notification_handler =& xoops_gethandler( 'notification' ) ;	
	switch ($not_event) {
		case 'newpost' :
			$notification_handler->triggerEvent( 'global' , 0 , 'newpost' , $posts_tags , false , $not_modid ) ;
			$notification_handler->triggerEvent( 'author' , $post_author , 'newpost' , $posts_tags , false , $not_modid ) ;

			// categorie notification
			include(XOOPS_ROOT_PATH . '/modules/'.$mydirname . '/wp-includes/version.php');
			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=".$post_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 =" . $post_id.") AND ($table_term_taxonomy.taxonomy='category')";		
			}
			$res2 = $db->query($sql2);
			while($row2 = $db->fetchArray($res2)){
				$cat_id = $row2['cat_ID'];
				$cat_name = $row2['cat_name'];
				$posts_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_CAT_TITLE' => $cat_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;
				$notification_handler->triggerEvent( 'category' , $cat_id , 'newpost' , $posts_tags , false , $not_modid ) ;
			}
			break;
		case 'editpost' :
			$notification_handler->triggerEvent( 'post' , $post_id , 'editpost' , $posts_tags , false , $not_modid ) ;
			break;
		default :
	}
}		



	
?>