<?php
//================================================================
// genarete 1000 link data for weblinks
// for debug
// 2005-03-28 K.OHWADA
//================================================================

// --- link table ---
//  lid int(11)
//  uid int(11)
//  title varchar(100)
//  url   varchar(250)
//  time_create int(10)
//  time_update int(10)
//  hits int(11)
//  rating double(6,4)
//  votes int(11)
//  comments int(11)
//  width  int(5)
//  height int(5)

// --- catlink table ---
//  jid int(11)
//  cid int(4)
//  lid int(11)

include '../../../../mainfile.php';
include '../../../../include/cp_header.php';

global $xoopsDB, $xoopsUser, $xoopsModule;
xoops_cp_header();

// check permission
$flag = false;
if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) 
{	$flag = true;	}

if (!$flag)
{
	echo "<font color=red>you are not admin</font><br>";
	xoops_cp_footer();
	exit();
}

$MAX_LINK = 1000;
$MAX_CAT  = 100;

// table name
$table_links = $xoopsDB->prefix("weblinks_link");

echo "<h3>start</h3>";

srand( (double)microtime()*1000000 );

// constant
	$rating   = 0;
	$votes    = 0;
	$comments = 0;
	$width    = 12;
	$height   = 15;

for ($i=0; $i<$MAX_LINK; $i++)
{
// randum data
	$uid    = rand(0, 10);
	$hits   = rand(0, 100);
	$title  = get_randum_title();
	$banner = get_randum_banner();
	$passwd = get_randum_passwd();

	list($time_create, $time_update) = get_randum_time();

// other
	$url    = "http://$title/";
	$desc   = "$title\n $time_create\n";
	$search = "$url $title $desc";

// link table
	$sql  = "INSERT INTO $table_links (";
	$sql .= "uid, title, url, description, ";
	$sql .= "search, passwd, time_create, time_update, ";
	$sql .= "hits, rating, votes, comments, ";
	$sql .= "banner, width, height";
	$sql .= ") VALUES (";
	$sql .= "$uid, '$title', '$url', '$desc', ";
	$sql .= "'$search', '$passwd', $time_create, $time_update, ";
	$sql .= "$hits, $rating, $votes, $comments, ";
	$sql .= "'$banner', $width, $height";
	$sql .= ")";

	echo "$sql <br>";
	sql_exec($sql);

	$newid  = $xoopsDB->getInsertId();
	$catnum = rand(1,3);

	for ($i=1; $i<$catnum; $i++)
	{
		$cid = rand(1, $MAX_CAT);
		insert_catlink($cid, $newid);
	}

}

echo "<h3>end</h3>";
xoops_cp_footer();
exit();
// ---

function get_randum_time()
{
	$time  = time();
	$rand1 = rand(0, 365*24*60*60);	// 1 year
	$rand2 = rand(0, intval( $rand1/2 ) );
	$time_create = $time - $rand1;
	$time_update = $time - $rand2;

	return array($time_create, $time_update);
}

function get_randum_char()
{
	return chr( rand(97,122) );
}

function get_randum_title()
{
	$title = get_randum_char() . sprintf( "%06d", rand(0,999999) );
	return $title;
}

function get_randum_banner()
{
	$banner_dir = XOOPS_URL."/modules/weblinks/images/category/";
	$banner = $banner_dir . sprintf( "%01d", rand(0, 9) ) . ".gif";

//	$size   = GetImageSize($banner);
//	if ($size)
//	{
//		$width  = (int)$size[0];
//		$height = (int)$size[1];
//	}

	return $banner;
}

function get_randum_passwd()
{
	return md5( rand(10000000,99999999) );
}

function insert_catlink($cid, $lid)
{
	global $xoopsDB;

	$sql  = "INSERT INTO ".$xoopsDB->prefix("weblinks_catlink")." (";
	$sql .= "cid, lid";
	$sql .= ") VALUES (";
	$sql .= "$cid, $lid";
	$sql .= ")";

	sql_exec($sql);
}

function sql_exec($sql)
{ 
	global $xoopsDB;

	$ret = $xoopsDB->queryF($sql);
	if ($ret != false ) { return $ret; }

	$error = $xoopsDB->error();
	echo "<font color=red>$sql<br>$error</font><br>";

	return false;
}


?>