- Moderated by:
- Support Team
-
- rank:
-
Helper
- registered:
- November 2002
- Status:
- offline
- last visit:
- 29.05.07
- Posts:
- 203
I need some of my tables to be populated when my module is initialized. What is the best, most acceoted method of doint this? Should I put the SQL code into pninit.php? Should I make a call to the adminapi.php to use the create function for that table? Should I do something else that I haven't mentioned?
I'm currently doing it in the pninit.php, but it struck me that it may not be the best way. Input appreciated. -
- rank:
-
Helper
- registered:
- November 2002
- Status:
- offline
- last visit:
- 29.05.07
- Posts:
- 203
Code
function pngwbattletracker_init()
{
// Get a database connection handle
$dbconn =& pnDBGetConn(true);
// Get a handle to the $pntable global variable, populated with our module settings
$pntable =& pnDBGetTables();
// Get the table options array. These options apply to all tables, so we only
// need to load once
$taboptarray =& pnDBGetTableOptions();
//
//Create the guild_names table
//
// Get a pointer to the table
$guild_names_table = &$pntable['gwbt_guild_names'];
// Get a pointer to the column definition array
$guild_names_column = &$pntable['gwbt_guild_names_column'] ;
// Define the field data types
$flds = "
$guild_names_column[gnid] I AUTOINCREMENT PRIMARY,
$guild_names_column[name] C (64) NOTNULL DEFAULT '',
$guild_names_column[tag] C (4) NOTNULL DEFAULT '',
$guild_names_column[image] C (255)
";
// Create a new data dictionary object and get a pointer to it
$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($guild_names_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEGNFAILED);
return;
}
// Define the index name
$idxname = "GNIndex";
// Define the indexed column(s)
$idxflds = $guild_names_column['name'];
// Create the $sqlarray for later execution
$sqlarray = $dict->CreateIndexSQL($idxname, $guild_names_table, $idxflds);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSetSessionVar('errormsg', _GWBTCREATEGNIDXFAILED);
return;
}
//
//Create the guild_halls table
//
// Get a pointer to the table
$guild_halls_table = &$pntable['gwbt_guild_halls'];
// Get a pointer to the column definition array
$guild_halls_column = &$pntable['gwbt_guild_halls_column'] ;
// Define the field data types
$flds = "
$guild_halls_column[ghid] I AUTOINCREMENT PRIMARY,
$guild_halls_column[name] C (64) NOTNULL DEFAULT '',
$guild_halls_column[has_flag] I NOTNULL DEF '0',
$guild_halls_column[has_siege] I NOTNULL DEF '0',
$guild_halls_column[has_seed] I NOTNULL DEF '0',
$guild_halls_column[has_tele] I NOTNULL DEF '0',
$guild_halls_column[ghnotes_id] I NOTNULL DEF '',
$guild_halls_column[image] C (255)
";
// Create a new data dictionary object and get a pointer to it
//$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($guild_halls_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEGHFAILED);
return;
}
// Define the index name
$idxname = "GHIndex";
// Define the indexed column(s)
$idxflds = $guild_halls_column['name'];
// Create the $sqlarray for later execution
$sqlarray = $dict->CreateIndexSQL($idxname, $guild_halls_table, $idxflds);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSetSessionVar('errormsg', _GWBTCREATEGHIDXFAILED);
return;
}
//Create the guild_halls_notes table
//
// Get a pointer to the table
$guild_halls_notes_table = &$pntable['gwbt_guild_halls_notes'];
// Get a pointer to the column definition array
$guild_halls_notes_column = &$pntable['gwbt_guild_halls_notes_column'] ;
$flds = "
$guild_halls_notes_column[gnid] I AUTOINCREMENT PRIMARY,
$guild_halls_notes_column[fulltext] X
";
// Create a new data dictionary object and get a pointer to it
//$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($guild_halls_notes_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEGHNFAILED);
return;
}
// No index for the summary text. Primary key is auto-indexed
//
//Create the my_guilds table
//
// Get a pointer to the table
$my_guilds_table = &$pntable['gwbt_my_guilds'];
// Get a pointer to the column definition array
$my_guilds_column = &$pntable['gwbt_my_guilds_column'] ;
// Define the field data types
$flds = "
$my_guilds_column[mgid] I AUTOINCREMENT PRIMARY,
$my_guilds_column[name] C (64) NOTNULL DEFAULT '',
$my_guilds_column[tag] C (4) NOTNULL DEFAULT '',
$my_guilds_column[image] C (255)
";
// Create a new data dictionary object and get a pointer to it
//$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($my_guilds_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEMGFAILED);
return;
}
// Define the index name
$idxname = "MGIndex";
// Define the indexed column(s)
$idxflds = $my_guilds_column['name'];
// Create the $sqlarray for later execution
$sqlarray = $dict->CreateIndexSQL($idxname, $my_guilds_table, $idxflds);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSetSessionVar('errormsg', _GWBTCREATEMGIDXFAILED);
return;
}
//
//Create the match_notes table
//
// Get a pointer to the table
$match_notes_table = &$pntable['gwbt_match_notes'];
// Get a pointer to the column definition array
$match_notes_column = &$pntable['gwbt_match_notes_column'] ;
$flds = "
$match_notes_column[mnid] I AUTOINCREMENT PRIMARY,
$match_notes_column[fulltext] X
";
// Create a new data dictionary object and get a pointer to it
//$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($match_notes_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEMNFAILED);
return;
}
// No index for match_notes. Primary key is auto-indexed
//
//Create the matches table
//
// Get a pointer to the table
$matches_table = &$pntable['gwbt_matches'];
// Get a pointer to the column definition array
$matches_column = &$pntable['gwbt_matches_column'] ;
// Define the field data types
$flds = "
$matches_column[mid] I AUTO PRIMARY,
$matches_column[home_id] I NOTNULL DEFAULT '',
$matches_column[away_id] I NOTNULL DEFAULT '',
$matches_column[hall_id] I,
$matches_column[victor_id] I NOTNULL DEFAULT '',
$matches_column[elapsed_time] C (10),
$matches_column[match_date] D NOTNULL DEFDATE,
$matches_column[rating_change] I,
$matches_column[notes_id] I
";
// Create a new data dictionary object and get a pointer to it
//$dict = &NewDataDictionary($dbconn);
// Create the SQL array
$sqlarray = $dict->CreateTableSQL($matches_table, $flds, $taboptarray);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEMFAILED);
return;
}
// Define the index name
$idxname = "MIndex";
// Define the indexed column(s)
$idxflds = $matches_column['away_id'];
// Create the $sqlarray for later execution
$sqlarray = $dict->CreateIndexSQL($idxname, $matches_table, $idxflds);
// Execute the SQL array and check for errors.
// 0 = failed, 1 = executed all with errors, 2 = success
// If error, set the errormsg session variable and exit the initialization
if($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _GWBTCREATEMIDXFAILED);
return;
}
// Insert the primary guild into the my_guilds table
//
// check for the session variables
$my_guild = pnSessionGetVar('gwbt_my_guild'); // guild name
$my_tag = pnSessionGetVar('gwbt_my_tag'); // tag
// create the SQL statement
if( ($my_guild<>false) && ($my_tag<>false) ) {
// generate the next available id (for oracle-type databases)
$nextId = $dbconn->GenId($my_guilds_table);
// generate the SQL insert commands
$sql = "INSERT INTO $my_guilds_table
VALUES (
'" .(int)$nextId."',
'".pnVarPrepForStore($my_guild)."',
'".pnVarPrepForStore($my_tag)."',
''
)";
// Execute the SQL
$dbconn->Execute($sql);
// Check for an error with the database code, and if so set an
// appropriate error message and return
if ($dbconn->ErrorNo() != 0) {
pnSessionSetVar('errormsg', _GWBTADDMYGUILDFAILED);
return false;
}
}
// Insert guild hall info into the database
//
$hall_data = array(
'0' => array('Druid\'s Hall', '0', '0', '1', '0', '0', 'druid.jpg'),
'1' => array('Hunter\'s Hall', '1', '1', '0', '0', '0', 'hunter.jpg'),
'2' => array('Warrior\'s Hall', '1', '1', '0', '0', '0', 'warrior.jpg'),
'3' => array('Wizard\'s Hall', '1', '1', '0', '0', '0', 'wizard.jpg'),
'4' => array('Dead Hall', '1', '0', '0', '0', '0', 'dead.jpg'),
'5' => array('Nomad\'s Hall', '1', '0', '0', '1', '0', 'nomad.jpg'),
'6' => array('Frozen Hall', '1', '0', '0', '0', '0', 'frozen.jpg'),
'7' => array('Burning Hall', '1', '0', '0', '0', '0', 'burning.jpg')
);
$hall = 0;
foreach($hall_data as $hall) {
// generate the next id for non auto-incrementing DBs
$nextId = $dbconn->GenId($guild_halls_table);
// Create the SQL statement
$sql = "INSERT INTO $guild_halls_table
VALUES (
'".(int)$nextId."',
'".pnVarPrepForStore($hall[0])."',
'".$hall[1]."',
'".$hall[2]."',
'".$hall[3]."',
'".$hall[4]."',
'".$hall[5]."',
'".pnVarPrepForStore($hall[6])."'
)";
// Execute the SQL
$dbconn->Execute($sql);
// Check for an error with the database code, and if so set an
// appropriate error message and return
if ($dbconn->ErrorNo() != 0) {
pnSessionSetVar('errormsg', _GWBTADDGHFAILED);
return false;
}
}
// Check for variables from the interactive init
$user_ipp_long = pnSessionGetVar('gwbt_user_ipp_long'); // user items per page short
$user_ipp_short = pnSessionGetVar('gwbt_user_ipp_short'); // user items per page long
$admin_ipp = pnSessionGetVar('gwbt_admin_ipp'); // admin items per page
// Set default values if they were not gotten from interactive
pnModSetVar('pnGWBattleTracker', 'user_ipp_short', (($user_ipp_short<>false) ? $user_ipp_short : 25));
pnModSetVar('pnGWBattleTracker', 'user_ipp_long', (($user_ipp_long<>false) ? $user_ipp_long : 10));
pnModSetVar('pnGWBattleTracker', 'admin_ipp', (($admin_ipp<>false) ? $admin_ipp : 25));
pnModSetVar('pnGWBattleTracker', 'my_guild', (($my_guild<>false) ? $my_guild : 'Default Guild Name'));
pnModSetVar('pnGWBattleTracker', 'my_tag', (($my_tag<>false) ? $my_tag : 'DEF'));
// Clean up
pnSessionDelVar('gwbt_user_ipp_long');
pnSessionDelVar('gwbt_user_ipp_short');
pnSessionDelVar('gwbt_admin_ipp');
pnSessionDelVar('gwbt_my_guild');
pnSessionDelVar('gwbt_my_tag');
// Initialization Succesful
return true;
} -
- rank:
-
Steering Committee
- registered:
- December 2002
- Status:
- offline
- last visit:
- 23.11.08
- Posts:
- 13415
In all the modules in CVS, they have a specific ModName_defaultdata() function which installs the default data. This function is then called from the init function.
The init function is then only about creating tables and setting modvars.
--
Regards,
Simon
itbegins.co.uk - Zikula Consulting
Please read the Support Guide -
- rank:
-
Steering Committee
- registered:
- December 2002
- Status:
- offline
- last visit:
- 23.11.08
- Posts:
- 13415
Quote
This function works, but as you can see, it's pretty long.
One of the reasons I didn't read it in detail ;)
--
Regards,
Simon
itbegins.co.uk - Zikula Consulting
Please read the Support Guide -
- rank:
-
Helper
- registered:
- November 2004
- Status:
- offline
- last visit:
- 12.03.07
- Posts:
- 387
The latest example module has an "interactive installation" method that might work for this. Not sure what the data you're populating is, but you could give the user the option to insert any or all of the basics, as well as doing a bit more customization. -
- rank:
-
Helper
- registered:
- November 2002
- Status:
- offline
- last visit:
- 29.05.07
- Posts:
- 203
It is fully interactive. I moved the default data portions to the pnGWBattletracker_defaultdata() function. I pass it the guild name and tag, taken from the interactive init, which I put in the my_guilds table. I am painfully familiar with the interactive init in the example module. I think I wrote ten or twelve pages on it. ;)
