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

// --- category table ---
//  cid int(5)
//  pid int(5)
//  title varchar(50)
//  imgurl varchar(150)

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_CAT    = 100;
$MAX_PARENT = 10;

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

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

$imgurl_dir = XOOPS_URL."/modules/weblinks/images/category/";

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

// contant
	$pid = 0;

for ($i=0; $i<$MAX_PARENT; $i++)
{

// randum data
	$title  = "main_". get_randum_title();
	$imgurl = $imgurl_dir . sprintf( "%01d", rand(0, 9) ) . ".gif";

// ctegory table
	insert_category($table, $pid, $title, $imgurl);
}

//$newid = $xoopsDB->getInsertId();

echo "<br>";

// contant
	$imgurl = '';

for ($i=0; $i<($MAX_CAT - $MAX_PARENT); $i++)
{

// randum data
	$title  = "sub_". get_randum_title();

	$max_pid = intval( ($MAX_PARENT + $i) / 2 );
	$pid     = rand(1, $max_pid);

// ctegory table
	insert_category($table, $pid, $title, $imgurl);

}

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



function insert_category($table, $pid, $title, $imgurl)
{
// ctegory table
	$sql  = "INSERT INTO $table ";
	$sql .= "(pid, title, imgurl)";
	$sql .= "VALUES ";
	$sql .= "($pid, '$title', '$imgurl')";

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

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 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;
}


?>