<?php

$storyid   = isset($_GET['storyid']) ? intval($_GET['storyid']) : 0 ;
$storypage = isset($_GET['storypage']) ? intval($_GET['storypage']) : 0 ;

$article = new D3news( $mydirname , $storyid);

// 記事が存在しない場合
if( !$article->isPublishedExists( $mydirname , $storyid) ){
	redirect_header($mydirurl.'/index.php',2,_MD_NOSTORY);
	exit();
}

//テンプレート
$xoopsOption['template_main'] = "{$mydirname}_article.html";

require_once XOOPS_ROOT_PATH.'/header.php';


$story['id']       = $storyid;
$story['posttime'] = formatTimestamp($article->getVar('published'), $d3news_date_format);
$story['topicid']  = $article->getVar('topicid');
$story['topic']    = $article->topic_title();
$story['title']    = $article->getVar('title');
$story['image']    = $article->getVar('image');
$story['text']     = $article->getVar('hometext');
$story['hits']     = $article->getVar('counter') + 1; // To disp real view
$bodytext = $article->getVar('bodytext');

if ( $bodytext != '' ) { // [pagebreak]がある
/*

	$articletext = explode('[pagebreak]', $bodytext);
	$story_pages = count($articletext);
	$storypage   = ( $story_pages - 1 >= $storypage ) ? $storypage : 0 ;

	// [pagebreak]で複数ページのコンテンツが構成されている場合
	if ($story_pages > 1 ) {
		//require_once XOOPS_ROOT_PATH.'/class/pagenav.php';
		$pagenav = new icms_view_PageNav($story_pages, 1, $storypage, 'storypage', 'page=article&storyid='.$storyid);
		$xoopsTpl->assign('pagenav', $pagenav->renderNav());

		if ($storypage == 0) {
			$story['text'] = $story['text'].'<br /><br />'.$articletext[$storypage];
		} else {
			$story['text'] = $articletext[$storypage];
		}
	} else {
		$story['text'] = $story['text'].'<br />ここ<br />'.$bodytext;
    }
*/
//		$story['text'] = $story['text'] .'<input id="togglebtn" type="button" value="続きを見る"><br /><div class="bodytext">' . $bodytext . '</div>';
		$story['text'] = $story['text'] .'<hr style="width: 50%;"><div class="bodytext">' . $bodytext . '</div>';

}

//ユーザ情報をアサイン
$story['uid']      = $article->getVar('uid');
$story['uname']    = $article->getUname();
$story['realname'] = $article->getRealname();
$story['morelink'] = '';


$xoopsTpl->assign('og_title', $article->getVar('title'));
if(mb_strlen(strip_tags($article->getVar('hometext')))>100) {
	$obdesc = mb_substr(strip_tags($article->getVar('hometext')),0,100) . "...";
} else {
	$obdesc = strip_tags($article->getVar('hometext'));
}

$xoopsTpl->assign('og_description', $obdesc);
$xoopsTpl->assign('og_url', XOOPS_URL . "/modules/" . $mydirname . "/index.php?page=article&storyid=" . $storyid);


if($story['image']){
	$imgurldata = XOOPS_URL . "/uploads/d3news/" . $story['image'];
	$story['titleimg'] = $imgurldata;
	$xoopsTpl->assign('og_image', $imgurldata);
} else {
	$story['titleimg'] = "";
	$xoopsTpl->assign('og_image', XOOPS_URL . "/images/logo.png");
}

$story['adminlink'] = 0;
if ( $xoopsUser && $xoopsUser->isAdmin($xoopsModule->getVar('mid')) ) {
	$story['adminlink'] = 1;
}

if ( $article->showTopicimg()  ) {
	$story['topic_url'] = $article->imglink($d3news_topicon_path);
	$story['align']     = $article->getTopicalign();
}

if(!$d3news_kanren_block){
	// 関連記事
	if($d3news_use_relations){
		$relations = $article->getRelated();
		foreach($relations as $relation){
			$relation_asign = array();
			$relation_asign['storyid'] = $relation->getVar('storyid');
			$relation_asign['title'] = $relation->getVar('title');
			$relation_asign['date'] = formatTimestamp($relation->getVar('published'), $d3news_date_format);
			if($relation->getVar('image'))
				$relation_asign['image'] = $relation->getVar('image');
			else
				$relation_asign['image'] = 'noimage.png';
			$relation_asign['uid'] = $relation->getVar('uid');
			$relation_asign['uname'] = $relation->getUname();
			$relation_asign['realname'] = $relation->getRealname();
			$relation_asign['topicid'] = $relation->getVar('topicid');
			$relation_asign['topic'] = $relation->topic_title();
			$relation_asign['counter'] = $relation->getVar('counter');
			$relation_asign['comments'] = $relation->getVar('comments');
			$relation_asign['dirname'] = $relation->getVar('dirname');
			$relation_asign['url'] = XOOPS_URL.'/modules/'.$relation->getVar('dirname');
			$xoopsTpl->append('relations', $relation_asign);
		}
		$xoopsTpl->assign('kanren_block', 1);
	}
}
if(!$d3news_in_category_block){
	// カテゴリの最新記事
	if($d3news_disp_list_of_cat && $d3news_stories_of_cat > 0){
		$category_storeis = $article->getAllPublished( $mydirname , $d3news_stories_of_cat, 0, $article->getVar('topicid'), 0);
		foreach($category_storeis as $category_story){
			$category_story_asign['storyid']  = $category_story->getVar('storyid');
			$category_story_asign['title']    = $category_story->getVar('title');
			if($category_story->getVar('image'))
				$category_story_asign['image']    = $category_story->getVar('image');
			else
				$category_story_asign['image'] = 'noimage.png';

			$category_story_asign['date']     = formatTimestamp($category_story->getVar('published'), $d3news_date_format);
			$category_story_asign['uid']      = $category_story->getVar('uid');
			$category_story_asign['uname']    = $category_story->getUname();
			$category_story_asign['realname'] = $category_story->getRealname();
			$category_story_asign['counter']  = $category_story->getVar('counter');
			$category_story_asign['comments'] = $category_story->getVar('comments');
			$xoopsTpl->append('category_storeis', $category_story_asign);
		}
		$xoopsTpl->assign('in_category_block', 1);
	}
}

// Tell A Frinedを使う場合
if($d3news_use_tell_a_frined){
	$mail_link = XOOPS_URL.'/modules/tellafriend/index.php?target_uri='.rawurlencode( "$mydirurl/index.php?page=article&storyid=$storyid" ).'&amp;subject='.rawurlencode(sprintf(_MD_INTARTFOUND,$xoopsConfig['sitename'])) ;
}else{
	$mail_link = 'mailto:?subject='.rawurlencode(sprintf(_MD_INTARTICLE,$xoopsConfig['sitename'])).'&amp;body='.rawurlencode(sprintf(_MD_INTARTFOUND, $xoopsConfig['sitename']).':  '.$mydirurl.'/index.php?page=article&storyid='.$storyid);
}

$xoopsTpl->assign('story', $story);
$xoopsTpl->assign('mail_link', $mail_link);
$xoopsTpl->assign('disp_print_icon', $d3news_disp_print_icon);
$xoopsTpl->assign('disp_tell_icon', $d3news_disp_tell_icon );
// パンくずリスト
if($d3news_use_pankuzu) $xoopsTpl->assign('pankuzu', $article->topics->makePankuzuForHTML($article->getVar('topicid')) );

if( $d3news_titile_as_sitename ) $xoopsTpl->assign('xoops_pagetitle', $article->getVar('title'));

require_once XOOPS_ROOT_PATH.'/include/comment_view.php';

if($d3news_assing_rssurl_head){
	$xoopsTpl->assign('xoops_module_header', $rss_feed . $xoopsTpl->get_template_vars( "xoops_module_header" ));
}
$xoopsTpl->assign($assing_array);

// 閲覧数をカウントアップする
if (empty($_GET['com_id']) && !isset($_GET['storypage'])) {
	$article->updateCounter();
}

// GIJ
$breadcrumbs = array( array( 'name' => $xoopsModule->getVar('name') , 'url' => XOOPS_URL.'/modules/'.$mydirname.'/' ) ) ;
$pankuzu4assign = $article->topics->makePankuzuForHTML($article->getVar('topicid')) ;
foreach( $pankuzu4assign as $p4a ) {
	$breadcrumbs[] = array( 'name' => $p4a['topic_title'] , 'url' => XOOPS_URL.'/modules/'.$mydirname.'/index.php?storytopic='.$p4a['topic_id'] ) ;
}
$breadcrumbs[] = array( 'name' => $article->getVar('title') ) ;
$xoopsTpl->assign( 'xoops_breadcrumbs' , $breadcrumbs ) ;
$xoopsTpl->assign( 'mod_config' , $xoopsModuleConfig ) ;

require_once XOOPS_ROOT_PATH.'/footer.php';
?>
