<?php
// $Id: submit.php,v 1.4 2005/10/28 11:56:02 ohwada Exp $

// 2005-10-20 K.OHWADA
// REQ 3028: send new link email to admin
// add send_newlink_email_to_admin()
// REQ 3110: Add in this category

// 2005-07-12 K.OHWADA
// BUG 2772: dont show notify when preview

// 2005-01-20 K.OHWADA
// add $weblinksConfig

// 2004-12-26 K.OHWADA
// use table_config

// 2004-12-05 K.OHWADA
// check_access()

// 2004-11-28 K.OHWADA
// makeSubmitDisplay()

// 2004-10-24 K.OHWADA
// URL-less mode
// indispensable item
// delete message of Notice Undefined index
// redirect to single link
// RSS/ATOM URL
// uid, name, mail
// bug fix: approve notify mail dont send

// 2004/08/10 K.OHWADA
// enable to install this module two or more. 
// change template to XoopsThemeForm
// preview mode, but not completed


//================================================================
// submit link information
// use class weblinksCategory, weblinksLink
// 2004/01/23 K.OHWADA
//================================================================

include "header.php";
include_once XOOPS_ROOT_PATH."/include/xoopscodes.php";
include_once XOOPS_ROOT_PATH."/include/xoopscodes.php";
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
include_once $MODULE_ROOT."/class/xoops_form_extend.php";
include_once $MODULE_ROOT."/include/submit_form.php";

$tbl_modify =& table_modify::getInstance();
$eh = new ErrorHandler; //ErrorHandler object

$FLAG_EVENT_ANONYMOUS = 1;	// send email for anonymous

check_access();

$op = '';
if     (isset($_POST['post']))     $op = 'post';
elseif (isset($_POST['preview']))  $op = 'preview';
elseif (isset($_POST['ignore']))   $op = 'ignore';

$notify = 0;
if (isset($_POST['notify']))  $notify = $_POST['notify'];

// start
$tbl_category->getAll();

// Notify of new link (anywhere) and new link in category.
$notification_handler =& xoops_gethandler('notification');

// add preview mode
if (($op == 'post')||($op == 'ignore')) 
{
// add URL-less mode
// check form
	$flag_check = $tbl_link->checkFormAddLink($_POST);

	$flag = 0;
	if (($op == 'post')&&($flag_check >= 1))
	{	$flag = 1;	}
	if (($op == 'ignore')&&($flag_check == 1)) 
	{	$flag = 1;	}

	if ($flag)
	{
		include XOOPS_ROOT_PATH."/header.php";

		print_submit_title();
		echo "<hr>\n";
		echo $tbl_link->getErrorMsgAddLink();
		echo "<hr>\n";
		print_submit_comment();

		$arr_sub = make_preview_in_form();
		$arr_sub['post'] = _REGISTER;

		printSubmitForm($arr_sub);

		include XOOPS_ROOT_PATH.'/footer.php';
		exit();
	}

// auto approve
	if ( $is_auto_approve )
	{
// insert link
		$tbl_link->addLink($_POST) or $eh->show("0013");
		$newid = $tbl_link->getNewid();

// notification
		list($tags, $cid) = $tbl_link->makeTagsAddLink($newid);
		$notification_handler->triggerEvent('global', 0, 'new_link', $tags);
		$notification_handler->triggerEvent('category', $cid, 'new_link', $tags);

// redirect to single link
		redirect_header("singlelink.php?lid=".$newid, 2, _WLS_RECEIVED."<br />"._WLS_ISAPPROVED."");
	}

// approve
	else
	{
// insert link	
		$tbl_modify->addLinkLink($_POST) or $eh->show("0013");

// bug fix: approve notify mail dont send
		$modify_newid = $tbl_modify->getNewid();

// send email to admin, new link notification for user
		list($tags,$cid) = $tbl_modify->makeTagsAddLink($modify_newid);

		$tags['WAITINGLINKS_URL'] = $MODULE_URL."/admin/link_manage.php?op=listNewLinks";
		$notification_handler->triggerEvent('global', 0, 'link_submit', $tags);
		$notification_handler->triggerEvent('category', $cid, 'link_submit', $tags);

// register to waiting notification for user
		if ($notify) 
		{
			include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';

// bug fix: approve notify mail dont send
			$notification_handler->subscribe('link', $modify_newid, 'approve', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
		}

// send email to admin, new link notification for anonymous
// REQ 3028: send new link email to admin
// TODO: anonymous can choice recieve or not email
		if ( $FLAG_EVENT_ANONYMOUS && ($_POST['uid'] == 0) && !empty($_POST['mail']) )
		{
			send_newlink_email_to_admin($_POST);
		}

		redirect_header("index.php",2,_WLS_RECEIVED);
	}

	exit();
}

// preview mode, but not completed
elseif ($op == 'preview')
{
	include XOOPS_ROOT_PATH."/header.php";

	print_submit_title();
	echo "<hr>\n";

// check form
	$flag_check = $tbl_link->checkFormAddLink($_POST);

	if ( $flag_check ) 
	{
		echo $tbl_link->getErrorMsgAddLink();
	}
	else
	{
		echo "<h4>"._PREVIEW."</h4>\n";
		$arr_prev = make_preview();

		$TEMPLATE = 'db:'.$MODULE_DIRNAME.'_link.html';
		$tpl = new XoopsTpl();
		$tbl_link->assignDisplayLink($tpl);
		$tpl->assign('link', $arr_prev);
		$tpl->display($TEMPLATE);
	}

	echo "<hr>\n";
	print_submit_comment();

	$arr_sub = make_preview_in_form();
	$arr_sub['post'] = _REGISTER;

	printSubmitForm($arr_sub);

	include XOOPS_ROOT_PATH.'/footer.php';
	exit();
}
else 
{

// change template to XoopsThemeForm
	include XOOPS_ROOT_PATH."/header.php";

	print_submit_title();
	print_submit_comment();

	$arr_sub = make_init_display();

	printSubmitForm($arr_sub);

	include XOOPS_ROOT_PATH.'/footer.php';
	exit();
}

exit();
// --- end of main ---


//---------------------------------------------------------
// function
//---------------------------------------------------------
function check_access()
{
	global $is_xoops_user_admin, $xoops_user_uid;
	global $is_auto_approve, $notify_show, $password_show;
	global $check_double;

	global $weblinksConfig;
	$use_passwd      = $weblinksConfig['use_passwd'];
	$check_double    = $weblinksConfig['check_double'];
	$is_permit       = $weblinksConfig['is_auth_submit'];
	$is_auto_approve = $weblinksConfig['is_auth_submit_auto'];

	$notify_show     = 0;
	$password_show   = 0;

// admin
	if ( $is_xoops_user_admin )
	{
		$url = "./admin/link_manage.php?op=addLink";

// REQ 3110: Add in this category
		if ( isset($_GET['cid']) )
		{
			$url .= "&amp;cid=".$_GET['cid'];
		}

		redirect_header($url, 2, _WLS_GOTOADMIN);	
		exit();
	}

// not permit
	if ( !$is_permit ) 
	{
		if ($xoops_user_uid) 
		{
			redirect_header("index.php", 1, _NOPERM);
			exit();
		}
// anonymous
		else
		{
			redirect_header(XOOPS_URL."/user.php", 2, _WLS_MUSTREGFIRST);
			exit();
		}
	}

	if ( !$is_auto_approve && ( $xoops_user_uid != 0 ) )
	{
		$notify_show = 1;
	}

	if ($use_passwd)
	{
		$password_show = 1;
	}
}

// change template to XoopsThemeForm
function print_submit_title()
{
	global $MODULE_NAME;
	echo "<h3 align=center>".$MODULE_NAME."</h3>\n";
}

function print_submit_comment()
{
	global $is_auto_approve;
	global $check_double;

	echo "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"4\" border=\"0\">";
	echo "<tr><td><ul>";

// check_double
	if ( $check_double )
	{
		echo "<li>"._WLS_SUBMITONCE."</li>\n";
	}

	echo "<li>"._WLS_DONTABUSE."</li>\n";
	echo "<li>"._WLS_TAKESHOT."</li>\n";

	if ( !$is_auto_approve )
	{
		echo "<li>". _WLS_ALLPENDING."</li>\n";
	}

// indispensable item
	echo "<li>"._WLS_SUBMIT_INDISPENSABLE."</li>\n";

// user comment
	echo "<li>"._WLS_SUBMIT_USER_COMMENT."</li>\n";

	echo "</ul></td></tr></table>\n";
}

function make_preview()
{
	global $tbl_link;

	$tbl_link->clear_items(); 
	$tbl_link->set_items( $_POST );
	$tbl_link->make_addional_data_for_display();

	$arr_out = $tbl_link->make_data_4_preview();
	return $arr_out;
}

function make_init_display()
{
	global $myts;
	global $tbl_category;
	global $xoops_user_uid, $xoops_user_uname, $xoops_user_email;
	global $notify_show, $password_show;

// REQ 3110: Add in this category
	$cid_arr = array();
	if ( isset($_GET['cid']) )
	{
		$cid_arr = array($_GET['cid']);
	}

	$arr_out = array(
// for submit
		'linkhead' => _WLS_SUBMITLINKHEAD,
		'post'     => _REGISTER,

		'notify_show'      => $notify_show,
		'password_show'    => $password_show,
		'usercomment_show' => 1,

		'notify'   => 1,
		'passwd'   => '',
		'request'  => '',

// user
		'nameflag'  => 0,
		'mailflag'  => 0,
		'uid'   => $xoops_user_uid,
		'name'  => $myts->makeTboxData4Edit( $xoops_user_uname ),
		'mail'  => $myts->makeTboxData4Edit( $xoops_user_email ),

// init
// REQ 3110: Add in this category
		'cid_arr'  => $cid_arr,

		'url'      => "http://",
		'banner'   => "http://",
		'rss_url'  => "http://",
		'rss_flag' => 3,	// auto discovery

// table field
		'lid'          => '',
		'recommend'    => '',
		'mutual'       => '',
		'title'        => '',
		'company'      => '',
		'zip'          => '',
		'state'        => '',
		'city'         => '',
		'addr'         => '',
		'addr2'        => '',
		'tel'          => '',
		'fax'          => '',
		'description'  => '',
		'usercomment'  => '',
		'admincomment' => '',

		);

	return $arr_out;
}

function make_preview_in_form()
{
	global $tbl_link;
	global $notify_show, $password_show;
	global $notify;

	$tbl_link->clear_items(); 
	$tbl_link->set_items( $_POST );
	$tbl_link->make_name_mail_edit_for_owner();

	$arr_out = $tbl_link->make_preview_in_form();
	$arr_out['linkhead'] = _WLS_SUBMITLINKHEAD;
	$arr_out['post']     = _REGISTER;
	$arr_out['usercomment_show'] = 1;
	$arr_out['lid']          = '';
	$arr_out['recommend']    = '';
	$arr_out['mutual']       = '';
	$arr_out['admincomment'] = '';

// BUG 2772: dont show notify when preview
	$arr_out['notify_show']   = $notify_show;
	$arr_out['notify']        = $notify;
	$arr_out['password_show'] = $password_show;

	return $arr_out;
}


//---------------------------------------------------------
// REQ 3028: send new link email to admin
// Notification of new wating link to admn
// added by SnAKes
//---------------------------------------------------------
function send_newlink_email_to_admin(&$post) 
{
	global $xoopsConfig;
	global $MODULE_URL;

	$file_tpl = "link_waiting_notify_admin.tpl";
	$dir_tpl  = get_dir_mail_template($file_tpl);

	$xoopsMailer =& getMailer();
	$xoopsMailer->useMail();
	$xoopsMailer->assign("WAITINGLINKS_URL", $MODULE_URL."/admin/link_manage.php?op=listNewLinks");
	$xoopsMailer->assign("SITE_NAME",  $post['title']);
	$xoopsMailer->assign("SITE_URL",   $post['url']);
	$xoopsMailer->assign("SITE_EMAIL", $post['mail']);
	$xoopsMailer->assign("UNAME", '');
	$xoopsMailer->assign("ULINK", '');
	$xoopsMailer->setTemplateDir($dir_tpl);
	$xoopsMailer->setTemplate($file_tpl);
	$xoopsMailer->setFromEmail($post['mail']);
	$xoopsMailer->setFromName($xoopsConfig['sitename']);
	$xoopsMailer->setToEmails($xoopsConfig['adminmail']);
	$xoopsMailer->setSubject(_WLS_LINKSWAITING);
	$success = $xoopsMailer->send();
}

?>