If you remember, we will create these as terms for the "Subject" vocabulary. My data file has an unique ID assigned to each category. Although not required, I will use this unique ID to identify my categories instead of using the name. If your categories do not have a unique ID, make sure their names are unique.
The following assumes that your import script is inside a folder named "import" on the same level as our Drupal/Ubercart Installation which is inside "cart", and a "data" directory that contains the data files with a list of products to import. Check to see that your "Subject" vocabulary ID is correct, in my setup it was 2. You can see it by looking at the link it points to when you click on "edit vocabulary" in the categories section in the site administration area.
<?php
//Execute without actually writing to the DB
$TEST = false;
//Change to the Drupal Directory
chdir("../cart");
//Load and init the Drupal System
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Vocabulary ID is 2 for Subject
$vocabID = 2;
//Open the Data File
$handle = fopen("../data/subjects.txt", "r");
?>To help keep my code simple to understand, I've created some convenience functions that will be utilized later on. It allows me to get the database ID from the term/category or in this case book subject that I am importing. This prevents duplicates and allows the synchronization to work. The variable, "ExtID" refers to the ID that is assigned to the subjects in the datafile.
The following two functions allow me to get the internal Drupal database ID of a term using either it's name or unique ID (ExtID) from the data file.
<?php
function getTermIdFromTitle($termTitle, $vocabID){
$query = "SELECT * FROM term_data WHERE vid = $vocabID AND name = \"$termTitle\" LIMIT 1";
$queryResult = db_query($query);
$tid = false;
echo "Looking up term ID for \"$termTitle\"" . "<br />\n";
if ($term = db_fetch_array($queryResult)) {
$tid = $term['tid'];
echo "Getting term ID for \"$termTitle\", Found: tid=" . $tid . "<br />\n";
}
return $tid;
}
function getTermIdFromExtID($extID, $vocabID){
$query = "SELECT * FROM term_data WHERE vid = $vocabID AND description = \"$extID\" LIMIT 1";
$queryResult = db_query($query);
$tid = false;
echo "Looking up term ID for \"$extID\"" . "<br />\n";
if ($term = db_fetch_array($queryResult)) {
$tid = $term['tid'];
echo "Getting term ID for \"$extID\", Found: tid=" . $tid . "<br />\n";
}
return $tid;
}
?>In this example, the categories are a standard CSV file. So we will iterate through each line until the end of the file, make sure the current line is not empty, and process the data.
<?php
while (($data = fgetcsv($handle, 0)) !== FALSE) {
if (!empty($data[0])) {
?>Create the data structure to hold and organize our term data according to Drupal's requirements. In this example, I am assigning the ExtID to the description field. This is OK because that field will not be visible to the costumer. If you do not have an ID just leave the description as NULL. Obviously we assign the "name" as the subject's name as we want it to appear to the costumer.
<?php
//Construct Term Data
$values = array();
$values['name'] = trim($data[1]);
$values['description'] = trim($data[0]);
$values['weight'] = 0;
$values['synonyms'] = '';
$values['vid'] = $vocabID; //Vocabulary ID set previously
$values['relations'] = 0;
?>This next piece of code serves two purposes, it makes sure that no duplicate categories are created, and it allows the synchronization to work. We utilize the convenience function that we created earlier to retrieve the internal Drupal database ID of the the vocabulary term (tid). In this example I am getting it using the category ID from the data file, but you may use the name instead by selecting the appropriate function.
If the term is found, we will just update the term's data using the term's ID (tid) as a reference. If the term is not found a new term is created by setting the term's ID (tid) equal to 0.
<?php
//First see if this already exists in the Database
if (($tid = getTermIdFromExtID($values['description'], $vocabID)) !== false) {
//It Exists, Update it by using the existing term ID
$values['tid'] = $tid;
echo "Updating: " . $values['description'] . "<br />\n";
}
else {
//Does not Exist, Make new term by using tid = 0
$values['tid'] = 0;
echo "Creating: " . $values['description'] . "<br />\n";
}
?>The code saves the term to the database. Either updating it (tid != 0) or creating a new term (tid = 0) using the Drupal API function "taxonomy_save_term()".
<?php
//Save Term
if (!$TEST){$result = taxonomy_save_term($values);}
echo "Subject: \"" . $values['name'] . "\" Saved<br />\n";
?>We close the IF block and the WHILE loop, then proceed to the next line in the data file. To be on the safe side, we also tell PHP to give us more time to process the script just in case it's a big file and it will take longer than the default execution time limit as set in the php configuration. In this example, we are giving ourselves an extra 60 seconds each time we process a line.
<?php
//Counter
$row++;
//Reset Timelimit
echo "*************Resetting PHP Execution Time Limit********** <br />\n";
set_time_limit(60);
}
}
?>Finally we close the data file.
<?php
echo "Closing File" . "<br />\n";
fclose($handle);
?>