<?php

function onaction_publish_post_notify($new_status, $old_status, $post)
{
	if ($new_status == 'publish'){
		do_PostNotifications($post->ID,'newpost');
	}
}

function onaction_edit_post_notify($post_id)
{
	do_PostNotifications($post_id,'editpost');
}

function onaction_comment_notify($commentID){
	global $wpdb;
	$status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $commentID");
	$post_id = $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = $commentID");

	if ($status ==1){
		do_CommentNotifications($commentID, $post_id);
	} else {
		do_CommentWaiting($commentID, $post_id);
	}
}

function onaction_comment_apobe_notify($commentID){
	global $wpdb;
	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
	$status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $commentID");
	if(is_null($status)) return;
	if ($status == 1){
			onaction_comment_notify($commentID);
	}
}

function Notification_triggerEvent($category, $item_id, $event, $extra_tags=array(), $user_list=array(), $omit_user_id=null)
{
	global $xoops_db,$xoops_config;
	//When notifying by a private message, 
	//it is evaded that the data base becomes read-only as a result of the check on the referrer and the method. 
	if (is_wp_cron_page_call() ){
		$_SERVER['HTTP_REFERER'] = 'http://'. $_SERVER[HTTP_HOST]  . $_SERVER['PHP_SELF'];
		$_SERVER['REQUEST_METHOD'] = 'POST';
	}
	if (is_xmlrpc_call() ){
		$_SERVER['HTTP_REFERER'] = 'http://'. $_SERVER[HTTP_HOST]  . $_SERVER['PHP_SELF'];
		$_SERVER['REQUEST_METHOD'] = 'POST';
	}
//	set_error_handler("xpress_error_handler");
	require_once $xoops_config->xoops_mainfile_path;	// load XOOPS System
	$module_id = get_xpress_modid() ;
	$notification_handler =& xoops_gethandler( 'notification' ) ;
	$notification_handler->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
}

function do_CommentWaiting($commentID, $comment_post_ID)
{
//	require_once XOOPS_ROOT_PATH . '/include/notification_functions.php' ;
//	$notification_handler =& xoops_gethandler( 'notification' ) ;
	Notification_triggerEvent( 'global' , 0 , 'waiting') ;
}


function do_CommentNotifications($commentID, $comment_post_ID)
{
	global $xoops_config , $xoops_db;

	$xpress_prefix = get_wp_prefix() ;

	$table_term_relationships = $xpress_prefix .'term_relationships';
	$table_term_taxonomy = $xpress_prefix .'term_taxonomy';
	$table_terms = $xpress_prefix .'terms';
	$table_categories = $xpress_prefix .'categories';
	$wp_post = $xpress_prefix .'posts';
	$wp_options = $xpress_prefix .'options';
	$wp_users  = $xpress_prefix .'users';
	$wp_comments  = $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');

	// query
	$sql = "SELECT post_author FROM ".$wp_post." WHERE ID=$comment_post_ID ";
	$post_author = $xoops_db->get_var($sql);

	$sql = "SELECT display_name  FROM $wp_users WHERE ID ='$post_author'";
	$user_name = $xoops_db->get_var($sql);

	$comment_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;
	Notification_triggerEvent('global' , 0 , 'comment' , $comment_tags , false);
	Notification_triggerEvent('author' , $post_author , 'comment' , $comment_tags , false);
	Notification_triggerEvent('post' , $comment_post_ID , 'comment' , $comment_tags , false);

	// categorie notification
	if (get_xpress_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')";		
	}
	$categories = $xoops_db->get_results($sql);
	foreach($categories as $categorie){
		$cat_id = $categorie->cat_ID;
		$cat_name = $categorie->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_triggerEvent('category' , $cat_id , 'comment' , $comment_tags , false);

	}
}

function do_PostNotifications($post_id,$not_event)
{
	global $xoops_config, $xoops_db;
	


	 // $not_event:		newpost,editpost ; $commentID, $comment_post_ID)
	 
	$xpress_prefix = get_wp_prefix() ;

	$table_term_relationships = $xpress_prefix .'term_relationships';
	$table_term_taxonomy = $xpress_prefix .'term_taxonomy';
	$table_terms = $xpress_prefix .'terms';
	$table_categories = $xpress_prefix .'categories';
	$wp_post = $xpress_prefix .'posts';
	$wp_options = $xpress_prefix .'options';
	$wp_users  = $xpress_prefix .'users';
	$wp_comments  = $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 post_author FROM ".$wp_post." WHERE ID=$post_id ";
	$post_author = $xoops_db->get_var($sql);

	$sql = "SELECT display_name  FROM $wp_users WHERE ID ='$post_author'";
	$user_name = $xoops_db->get_var($sql);

	$posts_tags = array( 'XPRESS_AUTH_NAME' =>$user_name,'XPRESS_BLOG_NAME' =>$blog_name,'XPRESS_POST_TITLE' => $post_title , 'XPRESS_POST_URL' => $post_url ) ;

	switch ($not_event) {
		case 'newpost' :
			Notification_triggerEvent('global' , 0 , 'newpost' , $posts_tags , false);
			Notification_triggerEvent('author' , $post_author , 'newpost' , $posts_tags , false);

			// categorie notification
			if (get_xpress_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')";		
			}
			$categories = $xoops_db->get_results($sql);
			foreach($categories as $categorie){
				$cat_id = $categorie->cat_ID;
				$cat_name = $categorie->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_triggerEvent('category' , $cat_id , 'newpost' , $posts_tags , false);
			}
			break;
		case 'editpost' :
			Notification_triggerEvent('post' , $post_id , 'editpost' , $posts_tags , false);
			break;
		default :
	}
}		

?>