/** * Module initialization. * * @return true on successful module initialization, else false. */ function Meds_init() { // Get database connection and tables references. $dbconn =& pnDBGetConn(true); $pntable =& pnDBGetTables(); // Assign table/columns used in this module. $company_table =& $pntable['rx_company']; $company_field =& $pntable['rx_company_column']; $preserve_table =& $pntable['rx_preserve']; $preserve_field =& $pntable['rx_preserve_column']; $chem_table =& $pntable['rx_chem']; $chem_field =& $pntable['rx_chem_column']; $moa_table =& $pntable['rx_moa']; $moa_field =& $pntable['rx_moa_column']; $meds_table =& $pntable['rx_meds']; $meds_field =& $pntable['rx_meds_column']; // Start a new data object. $dict =& NewDataDictionary($dbconn); // Get table creation options. $options =& pnDBGetTableOptions(); // Initialize a variable to hold the modules' table/columns schema. $schema = array(); // Adding another table/columns to $schema. $schema[$preserve_table] = "\n {$preserve_field['pres_id']} I AUTO PRIMARY,\n {$preserve_field['name']} C(48) NOT NULL DEFAULT '',\n {$preserve_field['comments']} X2\n "; // Adding a first table/columns to $schema. $schema[$company_table] = "\n {$company_field['comp_id']} I AUTO PRIMARY,\n {$company_field['name']} C(48) NOT NULL DEFAULT '',\n {$company_field['phone']} C(15) NOT NULL DEFAULT '',\n {$company_field['street']} C(96) NOT NULL DEFAULT '',\n {$company_field['city']} C(48) NOT NULL DEFAULT '',\n {$company_field['state']} C(2) NOT NULL DEFAULT '',\n {$company_field['zip']} C(12) NOT NULL DEFAULT '',\n {$company_field['email']} C(64) NOT NULL DEFAULT '',\n {$company_field['url']} C(64) NOT NULL DEFAULT '',\n {$company_field['comments']} X2\n "; // Adding another table/columns to $schema. $schema[$chem_table] = "\n {$chem_field['chem_id']} I AUTO PRIMARY,\n {$chem_field['name']} C(48) NOT NULL DEFAULT '',\n {$chem_field['moa_id']} I(6) DEFAULT NULL\n "; // Adding another table/columns to $schema. $schema[$moa_table] = "\n {$moa_field['moa_id']} I AUTO PRIMARY,\n {$moa_field['name']} C(48) NOT NULL DEFAULT '',\n {$moa_field['comments']} X2\n "; // Loop through $schema array. foreach ($schema as $table => $fields) { // Assemble each SQL array to CREATE the populated, optioned table. $sqlarray = $dict->CreateTableSQL($table, $fields, $options); // Run SQL query, checking for errors along the way. if ($dict->ExecuteSQLArray($sqlarray) != 2) { pnSessionSetVar('errormsg', $table . ' - ' . mysql_error()); return false; } // If no error was encountered in the table creation loop, each table now // needs an index for better performance. Since all the tables don't have // an identical field to use as an index, a single assigment won't do for // all iterations of the loop. To deal with this, a switch() is used and // the indexes defined on a case by case basis. switch ($table) { case $preserve_table: $index_alias = 'PreserveIndex'; break; case $company_table: $index_alias = 'CompanyIndex'; break; case $chem_table: $index_alias = 'ChemIndex'; break; case $moa_table: $index_alias = 'MOAIndex'; break; default: pnSessionSetVar('errormsg', $index_alias . ' - ' . mysql_error()); $failure = true; break; } if ($failure) { return; } $index_field = 'pn_name'; // Create an array of SQL to INDEX the table $sqlarray = $dict->CreateIndexSQL($index_alias, $table, $index_field); // Run each INDEX query, checking for errors along the way. if ($dict->ExecuteSQLArray($sqlarray) != 2) { pnSessionSetVar('errormsg', $table . ' - ' . mysql_error()); return false; } } // This table requires an "enum" column type, which cannot be // handled with the data dictionary object. This table will have // to be built the old way (a la the "Template" module.) $sql = "CREATE TABLE {$meds_table} (\n {$meds_field['med_id']} int(10) NOT NULL auto_increment,\n {$meds_field['trade']} varchar(48) NOT NULL DEFAULT '',\n {$meds_field['comp_id']} int(4) DEFAULT NULL,\n {$meds_field['medType1']} varchar(48) DEFAULT NULL,\n {$meds_field['medType2']} varchar(48) DEFAULT NULL,\n {$meds_field['preg']} char(1) NOT NULL DEFAULT '',\n {$meds_field['schedule']} varchar(4) NOT NULL DEFAULT '',\n {$meds_field['generic']} enum('yes','no','unknown') DEFAULT NULL,\n {$meds_field['image1']} varchar(48) NOT NULL DEFAULT '',\n {$meds_field['image2']} varchar(48) NOT NULL DEFAULT '',\n {$meds_field['dose']} text,\n {$meds_field['peds']} varchar(32) DEFAULT NULL,\n {$meds_field['ped_text']} varchar(8) DEFAULT NULL,\n {$meds_field['nurse']} varchar(96) NOT NULL DEFAULT '',\n {$meds_field['pres_id1']} int(4) DEFAULT NULL,\n {$meds_field['pres_id2']} int(4) DEFAULT NULL,\n {$meds_field['comments']} text,\n {$meds_field['rxInfo']} varchar(200) DEFAULT NULL,\n {$meds_field['med_url']} varchar(200) NOT NULL DEFAULT '',\n {$meds_field['updated']} varchar(10) NOT NULL DEFAULT '',\n {$meds_field['display']} enum('true','false') DEFAULT 'true',\n {$meds_field['conc1']} varchar(20) DEFAULT NULL,\n {$meds_field['chem_id1']} int(4) DEFAULT NULL,\n {$meds_field['moa_id1']} int(4) DEFAULT NULL,\n {$meds_field['conc2']} varchar(20) DEFAULT NULL,\n {$meds_field['chem_id2']} int(4) DEFAULT NULL,\n {$meds_field['moa_id2']} int(4) DEFAULT NULL,\n {$meds_field['conc3']} varchar(20) DEFAULT NULL,\n {$meds_field['chem_id3']} int(4) DEFAULT NULL,\n {$meds_field['moa_id3']} int(4) DEFAULT NULL,\n {$meds_field['conc4']} varchar(20) DEFAULT NULL,\n {$meds_field['chem_id4']} int(4) DEFAULT NULL,\n {$meds_field['moa_id4']} int(4) DEFAULT NULL,\n {$meds_field['form1']} enum('capsule','gel','intravenous','ointment','solution','emulsion','suspension','syrup','tablet','unknown','other') DEFAULT NULL,\n {$meds_field['size1']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['cost1']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['form2']} enum('capsule','gel','intravenous','ointment','solution','emulsion','suspension','syrup','tablet','unknown','other') DEFAULT NULL,\n {$meds_field['size2']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['cost2']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['form3']} enum('capsule','gel','intravenous','ointment','solution','emulsion','suspension','syrup','tablet','unknown','other') DEFAULT NULL,\n {$meds_field['size3']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['cost3']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['form4']} enum('capsule','gel','intravenous','ointment','solution','emulsion','suspension','syrup','tablet','unknown','other') DEFAULT NULL,\n {$meds_field['size4']} varchar(20) NOT NULL DEFAULT '',\n {$meds_field['cost4']} varchar(20) NOT NULL DEFAULT '',\n PRIMARY KEY({$meds_field['med_id']}))"; // Execute SQL. $dbconn->Execute($sql); // Check for any db errors. if ($dbconn->ErrorNo() != 0) { pnSessionSetVar('errormsg', '<strong>' . $meds_table . '</strong> - ' . mysql_error()); return false; } // Create the SQL to create a table index. $sqlarray = $dict->CreateIndexSQL('MedsIndex', $meds_table, 'pn_trade'); // Run SQL query and check for database error. if ($dict->ExecuteSQLArray($sqlarray) != 2) { // Set a detailed error message. pnSessionSetVar('errormsg', '<strong>' . $meds_table . '</strong> - ' . mysql_error()); // Report failure. return false; } // Set a default number for per-page meds. pnModSetVar('Meds', 'per_page', (int) 10); // -------------------------------------------------- // POPULATE TABLES WITH DATA // -------------------------------------------------- // Define the path/file that holds the SQL inserts. $inserts = dirname(__FILE__) . '/pnSQL/inserts.sql'; // Check if such a file exists and... if (@file_exists($inserts)) { // ...if so, get all INSERTs from file into an easily queryable array and then... $sqlarray = file($inserts); // ...run each INSERT query, checking for errors along the way. if ($dict->ExecuteSQLArray($sqlarray) != 2) { pnSessionSetVar('errormsg', 'Error Inserting Default Data - ' . mysql_error()); return false; } } // Installation success. return true; }
function Lenses_init() { // Security check. if (!pnSecAuthAction(0, 'Lenses::', '::', ACCESS_ADMIN)) { pnSessionSetVar('errormsg', _MODULENOAUTH); return false; } // Get reference to database connection and PN tables. $dbconn =& pnDBGetConn(true); $pntable =& pnDBGetTables(); // Create a new data object and define table options. $dict =& NewDataDictionary($dbconn); $taboptarray =& pnDBGetTableOptions(); // Table/column references. $lenses_table =& $pntable['lenses']; $lenses_field =& $pntable['lenses_column']; $companies_table =& $pntable['lenses_companies']; $companies_field =& $pntable['lenses_companies_column']; $polymers_table =& $pntable['lenses_polymers']; $polymers_field =& $pntable['lenses_polymers_column']; $stats_table =& $pntable['lenses_stats']; $stats_field =& $pntable['lenses_stats_column']; $zero_table =& $pntable['lenses_zero']; $zero_field =& $pntable['lenses_zero_column']; // TIP: Another way to save on code is to setup all the table information // inside an array first, and then loop through the array afterward, // creating all the module tables with only a single block of table- // creation code. Shaves many, many lines of code off this function. // Though, note that now that the module only has 3 tables, the number // of lines of code saved is far less than previously saved when there // where many tables in this module. // Schema for companies table. $schema[$companies_table] = "\n {$companies_field['comp_tid']} I AUTO PRIMARY,\n {$companies_field['comp_name']} C(50) NOTNULL DEFAULT '',\n {$companies_field['logo']} C(100) DEFAULT NULL,\n {$companies_field['phone']} C(15) DEFAULT NULL,\n {$companies_field['address']} C(100) DEFAULT NULL,\n {$companies_field['city']} C(50) DEFAULT NULL,\n {$companies_field['state']} C(50) DEFAULT NULL,\n {$companies_field['zip']} C(15) DEFAULT NULL,\n {$companies_field['url']} C(100) DEFAULT NULL,\n {$companies_field['email']} C(100) DEFAULT NULL,\n {$companies_field['comp_desc']} X2\n "; // Schema for polymers table. $schema[$polymers_table] = "\n {$polymers_field['poly_tid']} I AUTO PRIMARY,\n {$polymers_field['fda_grp']} I(1) DEFAULT NULL,\n {$polymers_field['h2o']} N(3.1) DEFAULT NULL,\n {$polymers_field['poly_name']} C(50) DEFAULT NULL,\n {$polymers_field['poly_desc']} X2\n "; // Schema for stats table. $schema[$stats_table] = "\n {$stats_field['id']} \t\t\tI(4) PRIMARY,\n {$stats_field['total']} \t\tI(7) \tNOTNULL DEFAULT '',\n {$stats_field['last_month']} \tI(5) \tNOTNULL DEFAULT '',\n {$stats_field['this_month']} \tI(5) NOTNULL DEFAULT '',\n\t\t\t{$stats_field['month']} \t\tI(2) NOTNULL DEFAULT '',\n "; // Schema for stats table. $schema[$zero_table] = "\n {$zero_field['id']} \t\t\tI(4) AUTO PRIMARY,\n\t\t\t{$zero_field['phrase']} \t\tC(40) AUTO PRIMARY,\n {$zero_field['total']} \t\tI(7) \tNOTNULL DEFAULT '',\n {$zero_field['last_month']} \tI(5) \tNOTNULL DEFAULT '',\n {$zero_field['this_month']} \tI(5) NOTNULL DEFAULT '',\n\t\t\t{$zero_field['month']} \t\t\tI(2) NOTNULL DEFAULT '',\n "; $schema[$lenses_table] = "\n {$lenses_field['tid']} I(10) NOT NULL auto_increment,\n {$lenses_field['name']} C(100) NOT NULL DEFAULT '',\n {$lenses_field['aliases']} X2 NOT NULL,\n {$lenses_field['comp_id']} I(4) DEFAULT NULL,\n {$lenses_field['poly_id']} I(4) DEFAULT NULL,\n {$lenses_field['visitint']} L NOT NULL DEFAULT 1,\n {$lenses_field['ew']} L NOT NULL DEFAULT 0,\n {$lenses_field['ct']} N(4.2) DEFAULT NULL,\n {$lenses_field['dk']} N(3.1) DEFAULT NULL,\n {$lenses_field['oz']} N(3.1) DEFAULT NULL,\n {$lenses_field['process_text']} C(64) NOT NULL DEFAULT '',\n {$lenses_field['process_simple']} C(64) NOT NULL DEFAULT '',\n {$lenses_field['qty']} C(32) DEFAULT NULL,\n {$lenses_field['replace_simple']} I(3) NOT NULL DEFAULT 365,\n {$lenses_field['replace_text']} C(128) NOT NULL DEFAULT 'conventional',\n {$lenses_field['wear']} C(64) NOT NULL DEFAULT 'daily wear',\n {$lenses_field['price']} X2 DEFAULT NULL,\n {$lenses_field['markings']} C(64) DEFAULT NULL,\n {$lenses_field['fitting_guide']} C(255) DEFAULT NULL,\n {$lenses_field['website']} C(255) DEFAULT NULL,\n {$lenses_field['image']} C(255) DEFAULT NULL,\n {$lenses_field['other_info']} X2,\n {$lenses_field['discontinued']} L NOT NULL DEFAULT 0,\n {$lenses_field['display']} L NOT NULL DEFAULT 1,\n {$lenses_field['special']} X2 NOT NULL,\n {$lenses_field['bc_simple']} C(30) DEFAULT NULL,\n\t\t\t{$lenses_field['bc_all']} C(100) DEFAULT NULL,\n {$lenses_field['max_plus']} N(4.2) NOT NULL DEFAULT '0.00',\n {$lenses_field['max_minus']} N(4.2) NOT NULL DEFAULT '0.00',\n {$lenses_field['max_diam']} N(4.1) NOT NULL DEFAULT '0.00',\n {$lenses_field['min_diam']} N(4.1) NOT NULL DEFAULT '0.00',\n {$lenses_field['diam_1']} C(90) NOT NULL DEFAULT '0.00',\n {$lenses_field['base_curves_1']} C(90) NOT NULL DEFAULT '',\n {$lenses_field['powers_1']} C(160) NOT NULL DEFAULT '',\n {$lenses_field['diam_2']} C(90) DEFAULT NULL,\n {$lenses_field['base_curves_2']} C(90) NOT NULL DEFAULT '',\n {$lenses_field['powers_2']} C(160) NOT NULL DEFAULT '',\n {$lenses_field['diam_3']} C(90) DEFAULT NULL,\n {$lenses_field['base_curves_3']} C(90) NOT NULL DEFAULT '',\n {$lenses_field['powers_3']} C(160) NOT NULL DEFAULT '',\n\t\t\t{$lenses_field['sph_notes']} C(200) NOT NULL DEFAULT '',\n\n {$lenses_field['toric']} L NOT NULL DEFAULT 0,\n {$lenses_field['toric_type']} C(128) NOT NULL DEFAULT '',\n {$lenses_field['toric_type_simple']} C(80) NOT NULL DEFAULT '',\n {$lenses_field['cyl_power']} C(128) NOT NULL DEFAULT '',\n {$lenses_field['max_cyl_power']} N(5.2) DEFAULT NULL,\n {$lenses_field['cyl_axis']} C(128) NOT NULL DEFAULT '',\n {$lenses_field['cyl_axis_steps']} L DEFAULT NULL,\n {$lenses_field['oblique']} I(2) DEFAULT NULL,\n\t\t\t{$lenses_field['cyl_notes']} C(200) NOT NULL DEFAULT '',\n\n {$lenses_field['bifocal']} L NOT NULL DEFAULT 0,\n {$lenses_field['bifocal_type']} C(128) NOT NULL DEFAULT '',\n {$lenses_field['add_text']} C(64) NOT NULL DEFAULT '',\n {$lenses_field['max_add']} N(5.2) DEFAULT NULL,\n {$lenses_field['cosmetic']} L NOT NULL DEFAULT 0,\n {$lenses_field['enh_names']} C(255) NOT NULL DEFAULT '',\n {$lenses_field['enh_names_simple']} C(255) NOT NULL DEFAULT '',\n {$lenses_field['opaque_names']} C(255) NOT NULL DEFAULT '',\n {$lenses_field['opaque_names_simple']} C(255) NOT NULL DEFAULT '',\n {$lenses_field['updated']} D NOT NULL DEFAULT '2003-01-03'\n "; // Now all tables are defined inside $schema, so // we let fly with the table-creation loop here. // Loop through $schema array. foreach ($schema as $table => $fields) { // Create SQL for creating a table. $sqlarray = $dict->CreateTableSQL($table, $fields, $taboptarray); // Run SQL query and check for database error. if ($dict->ExecuteSQLArray($sqlarray) != 2) { // Set a detailed error message. pnSessionSetVar('errormsg', '<strong>Table creation failure in ' . $table . '</strong> - ' . mysql_error()); // Report failure. return false; } // If no error was encountered in the table creation loop, each table now // needs an index for better performance. Since all the tables don't have // an identical field to use as an index, a single assigment won't do for // all iterations of the loop. To deal with this, a switch() is used and // the indexes defined on a case by case basis. switch ($table) { case $lenses_table: $index_alias = 'LensIndex'; $index_field = 'pn_name'; break; case $companies_table: $index_alias = 'CompanyIndex'; $index_field = 'pn_comp_name'; break; case $polymers_table: $index_alias = 'PolymerIndex'; $index_field = 'pn_poly_name'; break; default: pnSessionSetVar('errormsg', '<strong>Case failure in ' . $table . '</strong> - ' . mysql_error()); return false; //break; } // Create the SQL to create a table index. $sqlarray = $dict->CreateIndexSQL($index_alias, $table, $index_field); // Run SQL query and check for database error. if ($dict->ExecuteSQLArray($sqlarray) != 2) { // Set a detailed error message. pnSessionSetVar('errormsg', '<strong>Error indexing table ' . $table . '</strong> - ' . mysql_error()); // Report failure. return false; } } // Now all the tables are created and indexed. This next block // is to pre-populate the module with the 432 initial records. // Define the file that holds the default records to insert. $inserts = dirname(__FILE__) . '/pnSQL/inserts.sql'; // Check if such a file exists. if (@file_exists($inserts)) { // Get all INSERTs from file into an easily queryable array. $sqlarray = file($inserts); // Run SQL query and check for database error. if ($dict->ExecuteSQLArray($sqlarray) != 2) { // Set a detailed error message. pnSessionSetVar('errormsg', '<strong>Error Inserting Default Data</strong> - ' . mysql_error()); // Report failure. return false; } } // Module installation complete. Return success. return true; }