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

// 2005-09-04 K.OHWADA
// BUG 2932: dont work correctly when register_long_arrays = off

// 2005-07-12 K.OHWADA
// BUG 2773: dont show _WLS_MODIFY

// 2005-02-18 K.OHWADA
// bug fix: undefined function, forget parameter

// 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
// use $xoops_user_uid

// 2004-10-24 K.OHWADA
// URL-less mode
// redirect to single link
// use system lang file
//   _WLS_US_xx -> _US_xx
// user comment

// 2004/08/10 K.OHWADA
// use class weblinksCatlink
// enable to install this module two or more. 
// change template to XoopsThemeForm

// 2004/02/05 K.OHWADA
// not public mode of name & mail when edit

//================================================================
// modify link information
// use class weblinksCategory, weblinksLink, weblinksModify
// 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

// BUG 2932: dont work correctly when register_long_arrays = off
$lid         = get_post_get_int('lid');
$request     = get_post('request');
$passwd_post = get_post('passwd');
$code        = get_get('code');

$passwd_in = '';
if ($passwd_post)
{
	$passwd_in = $passwd_post;
}
elseif ($code)
{
	$passwd_in = $code;
}

// start
$tbl_category->getAll();

$record = $tbl_link->getRowByLid($lid);
$cids   = $record['cids'];
$uid    = $record['uid'];
$passwd = $record['passwd'];

$form = check_access($lid, $uid, $passwd, $passwd_in, $request);

if ($form)
{
// enable to install this module two or more. 
	$xoopsOption['template_main'] = $MODULE_DIRNAME."_passwd.html";

	include XOOPS_ROOT_PATH."/header.php";
	assignIndex();
	print_form_password($lid);
	include XOOPS_ROOT_PATH.'/footer.php';

	exit();
}

$usercomment_show = 0;
if ( $is_owner )
{
	$usercomment_show = 1;
}

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

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

$notification_handler =& xoops_gethandler('notification');

// add preview mode
if ($op == 'post') 
{
// add URL-less mode
// check form
	if ( $tbl_link->checkFormModLink($_POST, $is_owner) ) 
	{
		include XOOPS_ROOT_PATH."/header.php";

// bug fix: undefined function
		print_modify_title();

		echo $tbl_link->getErrorMsgModLink();
		echo "<hr>\n";

// bug fix: undefined function, forget parameter
		print_modify_comment();
		$arr_sub = make_preview_in_form($_POST);

		printSubmitForm($arr_sub);

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

// auto modify approve
	if ( $is_auto_approve )
	{
		$tbl_link->modLink( $_POST ) or  $eh->show("0013");

// redirect to single link
		redirect_header("singlelink.php?lid=".$lid, 2, _WLS_MODIFYAPPROVED);
	}

// modify approve
	else
	{
		$tbl_modify->modReqLink( $_POST ) or $eh->show("0013");

		$tags = array();
		$tags['MODIFYREPORTS_URL'] = $MODULE_URL."/admin/index.php?op=listModReq";

		$notification_handler->triggerEvent('global', 0, 'link_modify', $tags);

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

	exit();
}

// preview mode, but not completed
elseif ($op == 'preview')
{
	include XOOPS_ROOT_PATH."/header.php";
	print_modify_title();
	echo "<hr>\n";

// check form
	if ( $tbl_link->checkFormModLink($_POST, $is_owner) ) 
	{
		echo $tbl_link->getErrorMsgModLink();
    }
	else
	{
		echo "<h4>"._PREVIEW."</h4>\n";
		$arr_prev = make_preview( $record );

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

	echo "<hr>\n";

	$arr_sub = make_preview_in_form( $record );
	$arr_sub['passwd']           = $passwd_in;
	$arr_sub['request']          = $request;
	$arr_sub['usercomment_show'] = $usercomment_show;

	if ($is_auto_approve)
	{
		$arr_sub['post'] = _WLS_MODIFY;
	
	}

	print_modify_comment();
	printSubmitForm($arr_sub);

	include XOOPS_ROOT_PATH.'/footer.php';

}

// modify form
else 
{

	include XOOPS_ROOT_PATH."/header.php";
	print_modify_title();

// BUG 2773: dont show _WLS_MODIFY
	$arr_sub = $tbl_link->makeEditDisplay($record, $is_owner);
	$arr_sub['passwd']           = $passwd_in;
	$arr_sub['request']          = $request;
	$arr_sub['usercomment_show'] = $usercomment_show;

	if ($is_auto_approve)
	{
		$arr_sub['post'] = _WLS_MODIFY;
	}

	print_modify_comment();
	printSubmitForm($arr_sub);

	include XOOPS_ROOT_PATH.'/footer.php';
}

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


//---------------------------------------------------------
// function
//---------------------------------------------------------
function check_access($lid, $uid, $passwd, $passwd_in, $request)
{
	global $is_xoops_user_admin, $xoops_user_uid;
	global $is_auto_approve, $is_owner;
	global $tbl_config;

	global $weblinksConfig;
	$use_passwd       = $weblinksConfig['use_passwd'];
	$is_permit        = $weblinksConfig['is_auth_modify'];
	$is_auto_approve  = $weblinksConfig['is_auth_modify_auto'];

	$is_owner = false;

// owner
	if ( ( $uid != 0 )&&( $uid == $xoops_user_uid) )
	{
		$is_owner  = true;

		if ( $tbl_config->check_in_array( WEBLINKS_SUBMITTER_ID, 'auth_modify_arr' ) )
		{
			$is_permit = true;
		}

		if ( $tbl_config->check_in_array( WEBLINKS_SUBMITTER_ID, 'auth_modify_auto_arr' ) )
		{
			$is_auto_approve = true;
		}
	}

// password
	if ( $use_passwd && ( $passwd == md5($passwd_in) ) )
	{
		$is_permit       = true;
		$is_auto_approve = true;
		$is_owner        = true;
	}

// admin
	if ( $is_xoops_user_admin )
	{
		$url = "./admin/link_manage.php?op=modLink&lid=".$lid;
		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();
		}
	}

// not owner
	if ( $use_passwd && !$is_owner && !$is_auto_approve && !$request )
	{
		return true;	// password form
	}

	return false;

}

function print_form_password($lid)
{
	global $xoopsTpl;

	$xoopsTpl->assign('lang_requestmod',     _WLS_REQUESTMOD);
	$xoopsTpl->assign('lang_password',       _PASSWORD);
	$xoopsTpl->assign('lang_submitter',      _WLS_LINKSUBMITTER);
	$xoopsTpl->assign('lang_pleasepassword', _WLS_PLEASEPASSWORD);
	$xoopsTpl->assign('lang_registered',     _WLS_REGSTERED);
	$xoopsTpl->assign('lang_registered_dsc', _WLS_REGSTERED_DSC);
	$xoopsTpl->assign('lang_lostpassword',   _US_LOSTPASSWORD);
	$xoopsTpl->assign('lang_noproblem',      _US_NOPROBLEM);
	$xoopsTpl->assign('lang_youremail',      _US_YOUREMAIL);
	$xoopsTpl->assign('lang_sendpassword',   _US_SENDPASSWORD);
	$xoopsTpl->assign('lang_modify',         _WLS_MODIFY);
	$xoopsTpl->assign('lid', $lid);

}

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

function print_modify_comment()
{
	global $is_auto_approve, $is_owner;

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

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

		echo "<li>"._WLS_MODIFY_PENDING."</li>\n";
	}

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

function make_preview( $rec )
{
	global $tbl_link;

	$tbl_link->clear_items(); 
	$tbl_link->make_field_value( $rec );
	$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_preview_in_form( $rec )
{
	global $tbl_link;

	$tbl_link->clear_items(); 
	$tbl_link->make_field_value( $rec );
	$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_REQUESTMOD;
	$arr_out['post']             = _EDIT;

	return $arr_out;
}

?>
