Drupal 7 and my Address Book
I have been using Drupal for a number of years to make all my websites. I originally used Drupal 6 but I am now exclusively using Drupal 7. I have run a web based address book for a while but I wanted to transfer it to Drupal as it should be easier to maintain and also share with others.
The design of the address book allows other members of the family to see all the entries and add them to their own mailing lists if required. Different mailing lists can also be created for Christmas Cards and special family events.
The starting point was to create a new content type that had 6 text fields added for each line of the address. The title of the node is the first line of the address label i.e. the mailing name (Mr & Mrs Jones). I also added a taxonomy field which is used to tag the entry for inclusion in a mailing list.
The views module is the key to displaying the address book in a flexible way but one of the problems I came across was sorting the entries alphabetically. I wanted to sort by surname so I needed to split out the surname from the mailing name. To do this I used a computed field. To split our the surname I used the following php code
// Get the last word and trim any punctuation:
$entity_field[0]['value'] = trim($words[count($words) - 1], '.?![](){}*');
$entity->title is the title of the node which is my mailing name.
I don't display this field as I just use it for sorting in the views.
I use views to display the node title and the associated fields in a table with the node title being a link to allow the node to be edited.
A critical requirement of this implementation was the ability to print address onto labels. To achieve this I have implemented two output methods. The first uses views data export to output a CSV file of the current view. I input this into glabels which is a great litttle program to print labels. You define an output structure and then you can use the CSV file to create all the labels.
Once this was working I looked to see if I could print to labels directly from drupal. I found the module mailing labels which, unfortunately, did not have an official D7 version. Luckily someone has produced the first verison of a D7 port and after a bit of playing about and a bit of coding I was able to get it performing well. It does not yet have the flexibility I would like but it does a good job of printing standard sheets of labels.
I already had a couple of address databases that I wanted to import into this new system so I wrote a small program to create nodes from a CSV file. This program is run from the command line and must be edited to match the structure of the CSV file and the names of the text fields in the nodes. The program is below.
/**
* - Drupal 7 - import CSV file into nodes
*/
$_SERVER['HTTP_HOST'] = 'your.site.address.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$node = new stdClass();
$node->type = 'address';
$file_handle = fopen("yourcsvfile.csv","r");
if (!$file_handle)
{
die("Failed to open file");
}
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
// $line_of_text[0] is the first element. I wanted to print the 6th element as it was imported
print "Reading line ".$line_of_text[5]."\n";
$node = new stdClass();
$node->type = 'address'; // change this to the name of your content type
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->title = $line_of_text[5];
$node->field_address_line_1[$node->language][0]['value'] = $line_of_text[7];
$node->field_address_line_2[$node->language][0]['value'] = $line_of_text[8];
$node->field_address_town[$node->language][0]['value'] = $line_of_text[9];
$node->field_county[$node->language][0]['value'] = $line_of_text[10];
$node->field_postcode[$node->language][0]['value'] = $line_of_text[11];
$node->field_country[$node->language][0]['value'] = $line_of_text[12];
node_save($node);
}
fclose($file_handle);
?>
The program should be placed in the root of your drupal site. It would, of course, be possible to turn this into a module but I only needed to run it a couple of times so it was not worth the effort.
- Log in to post comments
