<?php

require_once dirname(dirname(__FILE__))."/class/formselecttime.php";

$time = time();

$form = new icms_form_Theme(_MD_SUBMITNEWS, 'storyform', '?page=submit');

if( $storyid > 0 ){
	if( $story->isActiveUser() ){
		$form->addElement(new icms_form_elements_Label(_MD_POSTEDBY, $story->getUname()));
	}else{
		$form->addElement(new icms_form_elements_Label(_MD_POSTEDBY, $story->getUname().'&nbsp;('.$story->getVar('hostname').')'));
	}
}else{
	$form->addElement(new icms_form_elements_Label(_MD_POSTEDBY, icms_member_user_Object::getUnameFromId($my_uid) ));
}

$form->addElement(new icms_form_elements_Text(_MD_TITLE, 'title', 50, 255, $story->getVar('title', 'f') ), true); // GIJ
$bt = new D3newsTopic( $mydirname ) ; // GIJ
$topic_select = new icms_form_elements_Label( _MD_TOPIC , $bt->makeTopicSelBox( false , $story->getVar('topicid') , 'topicid' ) ) ; // GIJ
$form->addElement($topic_select);

$topicalign_select = new icms_form_elements_Select(_MD_TOPIC_IMAGE, 'topicimg', $story->getVar('topicimg') );
$topicalign_select->addOptionArray(array('1' => _MD_TOPIC_RIGHT, '2' => _MD_TOPIC_LEFT, '0' => _MD_TOPIC_DISABLE));
$form->addElement($topicalign_select);

// bodytext
$bodytext_tray = new icms_form_elements_Tray(_MD_THESCOOP, '');
if( empty( $xoopsModuleConfig['use_ckeditor'] ) || ! $gperm->group_perm(4) ) {
	$bodytext_tray->addElement(new icms_form_elements_Dhtmltextarea('', 'text', $story->getVar('text', 'f'), $d3news_post_tray_row, $d3news_post_tray_col), true);
} else {
	// ckeditor
	$wysiwyg_header = '
		<script type="text/javascript" src="'.XOOPS_URL.'/editors/ckeditor/ckeditor.js"></script>';
	$xoopsTpl->assign( 'xoops_module_header' , $xoopsTpl->get_template_vars( "xoops_module_header" ) . "\n" . $wysiwyg_header ) ;
	$bodytext_tray->addElement(new icms_form_elements_Label('', '<textarea id="bodytext" class="ckeditor" cols="80"  rows="10" name="text">'.$story->getVar('text', 'f').'</textarea>' ));
}
$bodytext_tray->addElement(new icms_form_elements_Label('', '<div>'._MULTIPAGE.'</div>'));
$form->addElement($bodytext_tray);


// if user has right to set date.
if( $gperm->group_perm(3) ){
	// autodate
	$autodate_tray = new icms_form_elements_Tray(_MD_PUBLISHED, '');
	$autodate_checkbox = new icms_form_elements_Checkbox('', 'autodate', $story->getVar('autodate') );
	$autodate_checkbox->addOption(1, _MD_SETDATETIME);
	$autodate_tray->addElement($autodate_checkbox);
	$autodate_tray->addElement(new icms_form_elements_Label('', '<span style="font-size:0.8em;">'._MD_SETDATETIME_DESC.'</span><br />'));
	$autodate_tray->addElement(new icms_form_select_time('', 'auto', $auto, _MD_DATE_FORMAT));
	$form->addElement($autodate_tray);

	// auto expire
	$autoexpdate_tray = new icms_form_elements_Tray(_MD_EXPIRED, '');
	$autoexpdate_checkbox = new icms_form_elements_Checkbox('', 'autoexpdate', $story->getVar('autoexpdate') );
	$autoexpdate_checkbox->addOption(1, _MD_SETEXPDATETIME);
	$autoexpdate_tray->addElement($autoexpdate_checkbox);
	$autoexpdate_tray->addElement(new icms_form_elements_Label('', '<span style="font-size:0.8em;">'._MD_SETEXPDATETIME_DESC.'</span><br />'));
	$autoexpdate_tray->addElement(new icms_form_select_time('', 'autoexp', $autoexp, _MD_DATE_FORMAT));
	$form->addElement($autoexpdate_tray);
}

// relation
if( $gperm->group_perm(7) && $d3news_use_relations ){
	$relation_content  = '<div id="relation">';
	foreach( $relations as $relation ){
		$relation_content .= '<input type="checkbox" name="storyidR[]" value="'.intval($relation['linkedid']).'" />';
		$relation_content .= '<input type="hidden" name="titleR[]" value="'.$story->getRelatedTitle($relation['linkedid'], $relation['dirname']).'" />';
		$relation_content .= '<input type="hidden" name="dirnameR[]" value="'.htmlspecialchars($relation['dirname']).'" />';
		$relation_content .= '<input type="hidden" name="storyidRH[]" value="'.intval($relation['linkedid']).'" />';
		$relation_content .= ''.$story->getRelatedTitle($relation['linkedid'], $relation['dirname']).'<br />';
	}
	$relation_content .= '</div>';
	$relation_content .= '<input type="button" value="'._MD_ADD_RELATION.'" name="opensub" onclick="window.open(\'index.php?page=search\',\'sub\',\'width=400,height=500\');">';
	$relation_content .= '<input type="button" value="'._DELETE.'" name="updatevar" onclick="updateRelations(\'storyform\', false)">';
	$form->addElement(new icms_form_elements_Label(_MD_RELATION, $relation_content));
}

// options
$option_tray = new icms_form_elements_Tray(_OPTIONS,'<br />');

// event notifaction (if mode is edit, not display.)
if ($icms_member_user_Object && $storyid == 0 && !$gperm->group_perm(2)) {
	$notify_checkbox = new icms_form_elements_Checkbox('', 'notifypub', $story->getVar('notifypub') );
	$notify_checkbox->addOption(1, _MD_NOTIFYPUBLISH);
	$option_tray->addElement($notify_checkbox);
}

// If user have right to use HTML
if( $gperm->group_perm(4) ){
	$html_checkbox = new icms_form_elements_Checkbox('', 'html', $story->getVar('html') );
	$html_checkbox->addOption(1, _MD_USE_HTML);
	$option_tray->addElement($html_checkbox);
}

// br (GIJ)
$br_checkbox = new icms_form_elements_Checkbox('', 'br', $story->getVar('br') );
$br_checkbox->addOption(1, _MD_USE_BR);
$option_tray->addElement($br_checkbox);

$smiley_checkbox = new icms_form_elements_Checkbox('', 'smiley', $story->getVar('smiley') );
$smiley_checkbox->addOption(1, _MD_USE_SMILEY);
$option_tray->addElement($smiley_checkbox);
$xcode_checkbox = new icms_form_elements_Checkbox('', 'xcode', $story->getVar('xcode') );
$xcode_checkbox->addOption(1, _MD_USE_XCODE);
$option_tray->addElement($xcode_checkbox);
$block_checkbox = new icms_form_elements_Checkbox('', 'block', $story->getVar('block') );
$block_checkbox->addOption(1, _MD_DISP_BLOCK);
$option_tray->addElement($block_checkbox);
$ihome_checkbox = new icms_form_elements_Checkbox('', 'ihome', $story->getVar('ihome') );
$ihome_checkbox->addOption(1, _MD_PUBINHOME);
$option_tray->addElement($ihome_checkbox);
if( $gperm->group_perm(2) ){
	$approve_value = isset($_POST['approve']) ? $story->getVar('approve') : $story->getVar('type');
	$approve_checkbox = new icms_form_elements_Checkbox('', 'approve', $approve_value );
	$approve_checkbox->addOption(1, _MD_APPROVE);
	$option_tray->addElement($approve_checkbox);
}
$form->addElement($option_tray);

// for edit
if( $storyid > 0){
	$form->addElement(new icms_form_elements_Hidden('storyid', $storyid));
}

if( $return == 1){
	$form->addElement(new icms_form_elements_Hidden('return', 1));
}
$button_tray = new icms_form_elements_Tray('' ,'');
$button_tray->addElement(new icms_form_elements_Button('', 'preview', _PREVIEW, 'submit'));
$button_tray->addElement(new icms_form_elements_Button('', 'post', _MD_POST, 'submit'));
$form->addElement($xoopsGTicket->getTicketIcmsForm( __LINE__ ));
$form->addElement($button_tray);
$form->display();

$xoopsTpl->assign('xoops_module_header', '<script type="text/javascript" src="'.$mydirurl.'/index.php?page=javascript"></script>' . $xoopsTpl->get_template_vars( "xoops_module_header" ));
?>
