<?php
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;

eval( '
function b_'.$mydirname.'_comments_show($options){
	return _b_comments_show($options) ;
}
function b_'.$mydirname.'_comments_edit($options){
	return _b_comments_edit($options) ;
}
' ) ;


if( ! defined( 'XPRESS_COMMENTS_BLOCK_INCLUDED' ) ) {
	define( 'XPRESS_COMMENTS_BLOCK_INCLUDED' , 1 ) ;
	function _b_comments_edit($options)
	{
		$mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
		$disp_count = empty( $options[1] ) ? '10' : $options[1] ;
		$disp_length = empty( $options[2] ) ? '30' : $options[2] ;
	    $selected = array_slice($options,5); // get allowed cats
		$this_template = empty( $options[6] ) ? 'db:'.$mydirname.'_block_comments.html' : trim( $options[4] );
		$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;

		require_once(XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/blocks/block_common.php');
		
		$form  = "MyDirectory <input type='text' name='options[0]' value='" . $mydirname . "' /><br />";
		$form .= _MB_XPRESS_COUNT .": <input type='text' name='options[1]' value='" . $disp_count . "' /><br />";
		$form .= _MB_XPRESS_LENGTH .": <input type='text' name='options[2]' value='" . $disp_length . "' />";
	    $form .= "<br /><br />" . _MB_XPRESS_COM_TYPE ;
	    $isAll = (count($selected)==0||empty($selected[0]))?true:false;
	    $form .= "<br />&nbsp;&nbsp;<select name=\"options[]\" multiple=\"multiple\">";
		    $form .= "<option value=\"0\" ";
		    if ($isAll) $form .= " selected=\"selected\"";
		    $form .= ">"._MB_XPRESS_ALL ."</option>";

		    $form .= "<option value=\"1\" ";
		    if (in_array(1, $selected))
				$form .= " selected=\"selected\"";
		    $form .= ">"._MB_XPRESS_COMMENT ."</option>";

		    $form .= "<option value=\"2\" ";
		    if (in_array(2, $selected))
				$form .= " selected=\"selected\"";
		    $form .= ">"._MB_XPRESS_TRUCKBACK ."</option>";

		    $form .= "<option value=\"3\" ";
		    if (in_array(3, $selected))
				$form .= " selected=\"selected\"";
		    $form .= ">"._MB_XPRESS_PINGBACK ."</option>";
		$form .= "</select><br />";
		//	    $form .="<br /><input type='text' size='60' name='options[5]' id='this_template' value='".htmlspecialchars($this_template,ENT_QUOTES)."' />";


		return $form;
	}
	
	function _b_comments_show($options)
	{
		$mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
		$disp_count = empty( $options[1] ) ? '10' : $options[1] ;
		$disp_length = empty( $options[2] ) ? '30' : $options[2] ;
		$selected = array_slice($options,5); // get allowed cats
		$this_template = empty( $options[6] ) ? 'db:'.$mydirname.'_block_comments.html' : trim( $options[4] );
		$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
		

		$myts =& MyTextSanitizer::getInstance();

		$this_url = '/modules/'. $mydirname;
		$call_url = $_SERVER['REQUEST_URI'];
		
		$disp_all = in_array('0',$selected);
		$disp_comment = in_array('1',$selected);
		$disp_trackback = in_array('2',$selected);
		$disp_pingback = in_array('3',$selected);
		
		$type_select = '';
		if (!$disp_all){			
			if ($disp_comment){
				$in_where =  "''";
			}
			if ($disp_trackback){
				if (empty($in_where)) $in_where =  "'trackback' "; else $in_where .=  ",'trackback'";
			}
					
			if ($disp_pingback){
				if (empty($in_where)) $in_where =  "'pingback' "; else $in_where .=  ",'pingback'";
			}
			
			if (! empty($in_where)){
				$type_select = " AND comment_type IN($in_where) ";				
			}
		}
		
		global $wpdb;
		$block = array( 
		'mydirname' => $mydirname ,
		'mod_url' => XOOPS_URL.'/modules/'.$mydirname ,
		) ;
			
		if (!is_null($wpdb)){

			$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments LEFT JOIN $wpdb->posts ON  $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_type = 'post'  AND post_status = 'publish' $type_select ORDER BY comment_date_gmt DESC LIMIT $disp_count");
			$format = get_settings('date_format') . ' ' . get_settings('time_format');				

			if ( $comments ) {
				$output .= '<ul>';
				foreach ($comments as $comment){
					$comment_content = $comment->comment_content;
					$comment_excerpt = ($disp_length>0 ? xoops_substr($comment_content, 0, $disp_length): $comment->comment_content);

					$comment_link = get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID ;
					$comment_title = $comment_excerpt;
					$comment_title_link = "<a href='$comment_link' rel='external nofollow' class='url'>$comment_tytle</a>";

					$post_link = get_comment_link($comment->comment_ID);
					$post_title = get_the_title($comment->comment_post_ID);
					$post_title_link = '<a href="'. $post_link . '">' . $post_title . '</a>';
					
					$author_link = $comment->comment_author_url;
					$author_name = $comment->comment_author;
					$author_name_link = (( empty( $author_link ) || 'http://' == $author_link ) ? $author_name : "<a href='$author_link' rel='external nofollow' class='url'>$author_name</a>");

					$comment_type = (empty($comment->comment_type) ? 'comment': $comment->comment_type);
					$from_auther_to_post = sprintf(__('%1$s on %2$s'), $author_name_link , $post_title_link );

					$row_data = array(
						'comment_ID' 		=> $comment->comment_ID ,
						'comment_post_ID'	=> $comment->comment_post_ID ,
						'comment_date' 		=> $comment->comment_date ,
						'comment_content' 	=> $comment_content ,
						'comment_excerpt' 	=> $comment_excerpt ,	
						'comment_link' 		=> $comment_link,
						'comment_title' 	=> $ccomment_title ,
						'comment_title_link' => $comment_title_link ,
						'post_link' 		=> $post_link,
						'post_title' 		=> $post_title,
						'post_title_link' 	=> $post_title_link,
						'author_link' 		=> $author_link,
						'author_name' 		=> $author_name,
						'author_name_link' 	=> $author_name_link,		
						'comment_type' 		=> $comment_type,																																		
						'from_auther_to_post' => $from_auther_to_post
					);
					
					$block['contents'][] = $row_data;
				}
			}
		} else {
//			$block['contents'] = "out of  module";
		}
			return $block ;

	}
}
?>