<?php
require_once realpath(dirname(__FILE__)) . '/../include/functions.php';
require_once realpath(dirname(__FILE__)) . '/../class/links_Base.class.php';

function b_links_approval_show($options)
{
	global $xoopsConfig;

	$mydirname = empty($options[0]) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$this_template = empty($options[1]) ? 'db:'. $mydirname .'_block_approval.html' : htmlspecialchars(trim($options[1]), ENT_QUOTES, _CHARSET);

	// $Links OBJ
	$links_block = links_Base::getInstance($mydirname);

	require_once realpath(dirname(__FILE__)) . '/../language/'. $xoopsConfig['language'] .'/waiting.php';
	require_once realpath(dirname(__FILE__)) . '/../include/waiting.plugin.php';
	$block = b_waiting_links($mydirname);

	// require_once XOOPS_ROOT_PATH . '/class/template.php' ;
	$tpl = new icms_view_Tpl();
	$tpl->assign( array(
			'block' => $block,
		)
	);
	$ret['content'] = $tpl->fetch($this_template);

	return $ret;
}



function b_links_approval_edit($options)
{
	$mydirname = empty( $options[0] ) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$this_template = empty($options[1]) ? 'db:'. $mydirname .'_block_approval.html' : htmlspecialchars(trim($options[1]), ENT_QUOTES, _CHARSET);

	$form = "
		<input type='hidden' name='options[0]' value='". htmlspecialchars($mydirname, ENT_QUOTES, _CHARSET) ."' />
		<br />". _MB_LINKS_TEMPLATE ."
		<input type='text' size='60' name='options[1]' value='". htmlspecialchars($this_template, ENT_QUOTES, _CHARSET) ."' />
		<br />\n";

	return $form;
}

//-----------------------------------------------------------------------
function b_links_easypost_show($options)
{
	$mydirname = empty($options[0]) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$cid = empty($options[1]) ? 0 : intval($options[1]) ;
	$this_template = empty($options[2]) ? 'db:'. $mydirname .'_block_easypost.html' : htmlspecialchars(trim($options[2]), ENT_QUOTES, _CHARSET);

	$func_name = str_replace(array('db:','file:','.html'), '', $this_template);

	// $Links OBJ
	$links_block = links_Base::getInstance($mydirname);

	if (! $links_block->post_perm) return;

	if (empty($cid)) {
		if (count($links_block->allkeycid) > 0) {
			$cid = $links_block->allkeycid[0];
		}
	}

	// require_once XOOPS_ROOT_PATH . '/class/template.php' ;
	$tpl = new icms_view_Tpl();
	$tpl->assign( array(
			'postcid'   => $cid ,
			'func_name' => $func_name.$cid,
			'posturl'   => $links_block->makeUrl('easypost', 'index', array()),
			'gticket'   => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_easypost'),
		)
	);
	$ret['content'] = $tpl->fetch($this_template);

	return $ret;
}

function b_links_easypost_edit($options)
{
	$mydirname = empty( $options[0] ) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$cid = empty( $options[1] ) ? 0 : intval($options[1]) ;
	$this_template = empty($options[2]) ? 'db:'. $mydirname .'_block_easypost.html' : htmlspecialchars(trim($options[2]), ENT_QUOTES, _CHARSET);

	// $Links OBJ
	$links_block = links_Base::getInstance($mydirname);
	$selbox = $links_block->Categories->makeMySelBox('corder,cid', $cid, FALSE, 'options[1]', '', '', 0);

	$form = "
		<input type='hidden' name='options[0]' value='". htmlspecialchars($mydirname, ENT_QUOTES, _CHARSET) ."' />
		". _MB_LINKS_SAVE_CATEGORY ."$selbox<br />". _MB_LINKS_TEMPLATE ."
		<input type='text' size='60' name='options[2]' value='". htmlspecialchars($this_template, ENT_QUOTES, _CHARSET) ."' />
		<br />\n";

	return $form;
}

//-----------------------------------------------------------------------

function b_links_category_show($options)
{
	$mydirname = empty($options[0]) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$this_template = empty($options[1]) ? 'db:'. $mydirname .'_block_category.html' : htmlspecialchars(trim($options[1]), ENT_QUOTES, _CHARSET);

	// $Links OBJ
	$links_block = links_Base::getInstance($mydirname);


	$categories = $links_block->Categories->getChildTreeArray(0, 'corder,cid');
	foreach ($categories as $key => $cat) {
		$categories[$key]['prefix']  = str_replace('.', '&nbsp;&nbsp;', substr($cat['prefix'], 1));
		$categories[$key]['prefix2'] = strlen($cat['prefix'])-1;
		$categories[$key]['imgurl']  = links_urlCheckReplace($cat['imgurl']);
		$categories[$key]['itemnum'] = $links_block->Categories->getItemsNum($cat['cid'], 1);
		$categories[$key]['title']   = htmlspecialchars($cat['title'], ENT_QUOTES, _CHARSET);
	}

	// require_once XOOPS_ROOT_PATH . '/class/template.php' ;
	$tpl = new icms_view_Tpl();
	$tpl->register_function('my_make_uri', 'smarty_function_my_make_uri');
	$tpl->assign( array(
			'categories'  => $categories,
			'mydirname'   => htmlspecialchars($mydirname, ENT_QUOTES, _CHARSET),
			'catimgpath'  => htmlspecialchars(links_urlCheckReplace($links_block->module_config['categoryicon_path']), ENT_QUOTES, _CHARSET),
			'uri_options' => $links_block->uri_option_arr,
		)
	);
	$ret['content'] = $tpl->fetch($this_template);

	return $ret;
}

function b_links_category_edit($options)
{
	$mydirname = empty( $options[0] ) ? basename(dirname(dirname(__FILE__))) : $options[0] ;
	$this_template = empty($options[1]) ? 'db:'. $mydirname .'_block_category.html' : htmlspecialchars(trim($options[1]), ENT_QUOTES, _CHARSET);

	$form = "
		<input type='hidden' name='options[0]' value='". htmlspecialchars($mydirname, ENT_QUOTES, _CHARSET) ."' />
		". _MB_LINKS_TEMPLATE ."
		<input type='text' size='60' name='options[1]' value='". htmlspecialchars($this_template, ENT_QUOTES, _CHARSET) ."' />
		<br />\n";

	return $form;
}

//-----------------------------------------------------------------------
function b_links_top_show($options)
{
	$mydirname = empty( $options[0] ) ? basename(dirname(dirname(__FILE__))): $options[0];
	$order = empty( $options[1] ) ? 0: intval($options[1]);
	$linknum = empty( $options[2] ) ? 10: intval($options[2]);
	$length = empty( $options[3] ) ? 25: intval($options[3]);
	$screen_shot = empty( $options[4] ) ? 0: 1;
	$table = empty( $options[5] ) ? 0: 1;
	$colsnum = empty( $options[6] ) ? 1: intval($options[6]);
	$mobile = empty( $options[7] ) ? 0: intval($options[7]);
	$adult = empty( $options[8] ) ? 0: intval($options[8]);
	$category = empty( $options[9] ) ? '': trim($options[9]);
	if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) return;//die('Invalid mydirname');
	$this_template = empty( $options[10] ) ? 'db:'. $mydirname .'_block_links.html': htmlspecialchars(trim($options[10]), ENT_QUOTES, _CHARSET);

	if ($colsnum == 0) $colsnum = 1;

	// $Links OBJ
	$links_block = links_Base::getInstance($mydirname);

	if ($screen_shot == 1) {
		$links_block->screenShotServiceON();
		$links_block->block_capture = TRUE;
	}

	$links_block->clearWhere();
	$links_block->setWhere('l.status', 0, '>');
	$links_block->setWhere('l.date', time(), '<');
	if ($mobile == 1) {
		$links_block->setWhere('l.murl', '', '<>');
	} else if ($mobile == 2) {
		$links_block->setWhere('l.url', '', '<>');
	}
	if ($adult == 1) {
		$links_block->setWhere('l.adult', 0, '=');
	}
	if (! empty($category)) {
		$cat_where = "" ;
		$cats = explode(',', $category);
		$cats = array_map('intval', $cats);
		foreach ($cats as $cid) {
			if ($cat_where != "") $cat_where .= " OR ";
			$cat_where .= " l.cid LIKE '%[$cid]%' ";
		}
		$links_block->setRealWhere(" ( $cat_where ) ");
	}
	//ORDER
	switch ($order) {
		case 1:
			$sql_order = " l.access DESC ";
			break;
		case 2:
			$sql_order = " l.hits DESC ";
			break;
		case 3:
			$sql_order = " l.rating DESC ";
			break;
		case 4:
			$sql_order = " rand() ";
			break;
		case 0:
		default:
			$sql_order = " l.date DESC ";
			break;
	}
	$links_block->setOrder($sql_order) ;
	$links = $links_block->getDatas($linknum, 0);

	if (empty($options['disable_renderer'])) {
		// require_once XOOPS_ROOT_PATH . '/class/template.php';
		$tpl = new icms_view_Tpl();
		$tpl->register_function('my_make_uri', 'smarty_function_my_make_uri');
		$tpl->assign( array(
				'links'             => $links,
				'mydirname'         => $mydirname,
				'b_screen_shot'     => $screen_shot,
				'tablelist'         => $table,
				'length'            => $length,
				'colsnum'           => $colsnum,
				'colwidth'          => intval(100/$colsnum),
				'uri_options'       => $links_block->uri_option_arr,
				'myurl'             => $links_block->myurl,
				'cache_url'         => $links_block->cache_url,
				'screenshotservice' => $links_block->screen_shot_service,
				'order'             => $order,
			)
		);
		$ret['content'] = $tpl->fetch($this_template);
		return $ret ;
	} else {
		return $links;
	}

}

//OPTION
//0-directory     : $mydirname
//1-order by      : 0=date, 1=access, 2=hits, 3=rate, 4=random
//2-number        : 10
//3-title length  : 20
//4-screen shot   : 0=off , 1=on
//5-table display : 0=list, 1=table
//6-cols in table : 1=default
//7-mobile        : 1=only, 2=except, 0=all
//8-adult         : 1=except, 0=all
//9-category      : empty=default
//10-template

function b_links_top_edit($options)
{
	$mydirname = empty( $options[0] ) ? basename(dirname(dirname(__FILE__))): $options[0];
	$order = empty( $options[1] ) ? 0: intval($options[1]);
	$linknum = empty( $options[2] ) ? 10: intval($options[2]);
	$length = empty( $options[3] ) ? 25: intval($options[3]);
	$screenshot = empty( $options[4] ) ? 0: 1;
	$table = empty( $options[5] ) ? 0: 1;
	$colsnum = empty( $options[6] ) ? 1: intval($options[6]);
	$mobile = empty( $options[7] ) ? 0: intval($options[7]);
	$adult = empty( $options[8] ) ? 0: intval($options[8]);
	$category = empty( $options[9] ) ? '': trim($options[9]);
	if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) return;//die('Invalid mydirname');
	$this_template = empty($options[10]) ? 'db:'.$mydirname.'_block_links.html': trim($options[10]);


	// ORDER
	$ocheck0 = $ocheck1 = $ocheck2 = $ocheck3 = $ocheck4 = '';
	$ochk = "ocheck$order" ;
	$$ochk = "checked='CHECKED'";
	// SCREEN SHOT
	$scheck0 = $scheck1 = '' ;
	$schk = "scheck$screenshot" ;
	$$schk = "checked='CHECKED'";
	// TABLE
	$tcheck0 = $tcheck1 = '' ;
	$tchk = "tcheck$table" ;
	$$tchk = "checked='CHECKED'";
	// MOBILE
	$mcheck0 = $mcheck1 = $mcheck2 = '' ;
	$mchk = "mcheck$mobile" ;
	$$mchk = "checked='CHECKED'";
	// ADULT
	$acheck0 = $acheck1 = '' ;
	$achk = "acheck$adult" ;
	$$achk = "checked='CHECKED'";

	$form = "
		<input type='hidden' name='options[0]' value='". htmlspecialchars($mydirname, ENT_QUOTES, _CHARSET) ."' />
		". _MB_LINKS_ORDER ."
		<input type='radio' name='options[1]' value='0' $ocheck0 />". _MB_LINKS_ORDER_DATE ."&nbsp;
		<input type='radio' name='options[1]' value='1' $ocheck1 />". _MB_LINKS_ORDER_ACCESS ."&nbsp;
		<input type='radio' name='options[1]' value='2' $ocheck2 />". _MB_LINKS_ORDER_HITS ."&nbsp;
		<input type='radio' name='options[1]' value='3' $ocheck3 />". _MB_LINKS_ORDER_RATE ."&nbsp;
		<input type='radio' name='options[1]' value='4' $ocheck4 />". _MB_LINKS_ORDER_RANDOM ."<br />
		". _MB_LINKS_DISP ."
		<input type='text' name='options[2]' value='$linknum' /><br />
		". _MB_LINKS_CHARS ."
		<input type='text' name='options[3]' value='$length' /><br />
		". _MB_LINKS_SCREENSHOT ."
		<input type='radio' name='options[4]' value='1' $scheck1 />". _YES ."
		<input type='radio' name='options[4]' value='0' $scheck0 />". _NO ."<br />
		". _MB_LINKS_TABLELIST ."
		<input type='radio' name='options[5]' value='0' $tcheck0 />". _MB_LINKS_TABLELIST_LIST ."
		<input type='radio' name='options[5]' value='1' $tcheck1 />". _MB_LINKS_TABLELIST_TABLE ."
		(". _MB_LINKS_TABLE_COLS ."
		<input type='text' name='options[6]' value='$colsnum' />)<br />
		". _MB_LINKS_MOBILE ."
		<input type='radio' name='options[7]' value='1' $mcheck1 />". _MB_LINKS_MOBILE_ONLY ."
		<input type='radio' name='options[7]' value='2' $mcheck2 />". _MB_LINKS_MOBILE_EXCEPT ."
		<input type='radio' name='options[7]' value='0' $mcheck0 />". _ALL ."<br />
		". _MB_LINKS_ADULT_EXCEPT ."
		<input type='radio' name='options[8]' value='1' $acheck1 />". _YES ."
		<input type='radio' name='options[8]' value='0' $acheck0 />". _NO ."<br />
		". _MB_LINKS_CATEGORY ."
		<input type='text' name='options[9]' value='". htmlspecialchars($category, ENT_QUOTES, _CHARSET) ."' />
		". _MB_LINKS_CATEGORY_DESC ."<br />
		". _MB_LINKS_TEMPLATE ."
		<input type='text' size='60' name='options[10]' value='". htmlspecialchars($this_template, ENT_QUOTES, _CHARSET) ."' />
		<br />\n" ;

	return $form;
}
