Drupal: Adding a geocoding failure message to Location Module

One of the coolest pieces of Drupal is how simple it is to quickly enter a street address and have it show up on a dynamic map on your website using Location and GMap modules. To make it happen, a lot of stuff goes on behind the scenes. Unfortunately, in typical Drupal fashion, when something goes wrong, you aren’t provided much information to fix it.

A big issue if you’re having regular users enter information into your website is malformed addresses that can’t be automatically converted into latitude/longitude coordinates (geocoding) for display on a map. Out of the box, the Location module doesn’t give you a warning if it’s unable to geocode an address. On my website MappingAccess.org–a community maintained directory of Cable Access Television stations—I average about an email a week from a visitor saying “I added my station but it’s not showing up on the map”. Usually it’s a simple matter of using a PO Box or wacky abbreviation, but the website itself should be telling them there is a problem, not me.

So I whipped up a simple module that checks everytime a new station is submitted to see if the address was properly geocoded. If not, it displays a message with some tips on how to correct the issue.

Geocode Warning Message

You can download the module for Drupal 6.x by clicking here.

To be nitpicky, I’d rather the message show up during the validation stage—before the node is submitted—with the option to say “Please edit the address or press submit again to publish with the understanding that it will not show up on the map.” Unfortunately, in Drupal 6 you can’t make changes to node form during the validation stage—which I would use to set a flag in a hidden form element so that the validation message only gets triggered once. The current implementation calls a drupal_set_message in hook_nodeapi’s insert/update operations. It can be enabled on a per-content-type basis (on the Content Type Configuration screen).


Making ReCAPTCHA not suck

I really like using the Drupal CAPTCHA system with ReCAPTCHA (the one that helps scan in books). Both of them suck in the standard “Drupal makes everything ugly and hard to use by default, but it’s still easier than building something from scratch”.

One of ReCaptcha’s problems is that the words are sometimes hard to read. To deal with that, I used this tip from a Stumbleupon developer in the comments of this post entitled ReCAPTCHA’s quality is going down? : putting a link to reload—Recaptcha.reload()—the CAPTCHA in the explanation. To do that, I pasted this into the Challenge Description setting on the CAPTCHA admin page:

<br />
To prevent spam, please type the two words you see below separated by a space. &lt;a href=&quot;javascript:Recaptcha.reload();&quot; title=&quot;Get a new set of words&quot;&gt;Can't read the words?&lt;/a&gt;<br />

I also used CSS to hide the fieldset border box and title from the comments to cut down on the cruft too.


Drupal: theme override for Upload.module&#039;s attachments list

Update: this functionality can now be achieved with the iTweak_upload module . Thanks to Damon for the tip!

I made a custom override for Drupal 6.x’s Upload.module’s attachments table that is displayed at the bottom of a node when you create file attachments. That table is, in my opinion, one of the ugliest common and default presentations in Drupal core. Below is an example of the before and after:

Example of override

Hocus Pocus hd

To use it, unzip and drop the included folder into your active theme’s directory (e.g. /sites/default/all/garland), it should take effect without any other modifications—though you may have to reset the theme cache (goto admin/build/themes and click save without making any other changes).

Click Here to Download (shiny_upload.zip)

Return to Never Land movie

Also, I don’t know what the name is for these types of theme overrides: it’s not a module, and it’s not a whole theme. I posted this to a Drupal Group that, I think, calls them “Themer Packs“.

The icon code is based on the CCK filefield module—but the current 6.0 version is kind’ve clunky and I wanted to port it to the core Upload module. The namespace is “shiny_upload”.

Also, as an aside, the reason island94.org doesn’t currently have this enabled is because it’s still running on Drupal 5.x branch


Conference notes: managing nonprofit technology projects

Notes from Rebecca below on managing nonprofit technology projects

http://aspirationtech.org/events/mntp-sf

http://mntp.aspirationtech.org/index.php/Event_Agenda

Becket divx …maybe I’ll clean this up someday.

—-
!!!Basic Stages of a Project
1. Initiate
* define project
* talk about start and end points, budget, participants/roles, timeline
2. Plan
* defining scope, requirements, use cases
3. Implement

4. Monitor

5. Close
*how do you know when you’re done with the project you’re working on?

*upkeep/maintenance phase?

*in “waterfall” style projects, there is just one of each step
*in “agile” style projects, the plan -> implement -> monitor cycle repeats
*know which style project it is at the beginning!

—-
!!!Content inventories
For redesign processes, figure out what content and navigation is currently present and how it’s organized. Include things like creation dates and web statistics; talk about the value of old content to users. This can affect how much of the content is migrated, and can give organizations insight into how they intend to communicate vs. how they’re actually communicating. For example, one PM working on the ACLU site inventoried 15,000 pages; the ACLU decided to migrate 8,000 of those, and rethought their style of technical & legal language to a more personal approach.

—-
!!!Scope Creep
*postpone things to phase 2
*define project endpoints in the “initiate” phase
*specifications that include what ”won’t” be included
*review scope and recently developed features regularly
*revisit goals to regain focus as scope creep starts to take over
*saying no: make sure that people are aware of the depth of their requests, especially if they’re out of scope. inform people about research or dev time required just to estimate cost for a feature.

—-
!!!Recognizing Impending Doom
*chunking projects: dividing projects into smaller chunks will make overages more obvious.
*development time = developer + QA + PM + client + risk + padding.
*define checkin points in the web plan
*be upfront/honest/immediate when you’re feeling uncomfortable
*be proactive about input–consider what input people will have before you ask them for it
*one attendee mentioned that they “had been trying to stay with this FOSS community which was politically important, but they were making incredibly bad engineering decisions” and they eventually had to break with the FOSS group.
*beware of working with volunteers who aren’t web professionals
*notice the point where transparency starts to drop

—-
!!!Client Panic
*lack of communication: presenting a product that the client hasn’t seen before and they aren’t happy
*role changes on either the client or dev side can trigger less dialouge
*when you’re behind, don’t just work harder: restructure dev roles, reopen lines of communication with client
*sign of panic: abusive emails. one dev mentioned having a “2 strike rule” for abusive communication; he immediately calls higher-ups re: lack of tolerance for abusive/blame email/interaction
*learn how to identify ”good” things about a client relationship

”’recovery stragegy”’: regaining trust is remotely is difficult. Highly structured communication can help; for example, frequent meeting at consistent times with a repeated agenda.

#highly structured communication
#revisit goals
#talk to other members of the client org
#re-structure the project
#fire the client

—-
!!!Turning a Project into a Product
*forces you into more standardization
*documentation becomes exponentially more important
*languages & international users: translators can be your most active outside contributors

—-
!!!Other
*build test cases alongside app development
*involve real-life people–external stakeholders–in the process
*make sure there is someone within the nonprofit who can maintain the solution
*can you narrow your website’s focus? working with the client to focus the project; can be used to reduce the feature set and budget, AND/OR to clarify the client’s communication strategy.
*get clients to take notes at meetings and send them to you so that you know what they’re taking away/expecting
*discussions about “which tools?” can obscure discussions of needs
*blog or message board for a project: for both issues and for cheerleading
*”the smallest organizations need the most hand-holding”
—-
EOF


Who is using Drupal?

Always interesting to see who is using Drupal for their website:


Drupal WYSIWYG Editors

I’m partial to the following WYSIWYG editors for Drupal:

But Super Capers neither of them work well enough for me to want to use them. The trade-off for using these is that it’s easy to make pretty text, but if you ever need to manually edit, it’s incredibly painful. The biggest problem is that they don’t make new lines for paragraph breaks, smashing everything together into one huge, ugly block. And since Drupal has a nice, built-in filter for creating paragraph elements, it’s redundant (and infuriating).

The only markup you need is bold, emphasis, links, pictures and a way to turn it off. WordPress seems to get it.


Unintuitive: check

CCK’s single checkboxes require a strange configuration in the Allowed Values list:

0
1|Checkbox title


Geekout: Video on Maps for Cable Access TV

I recently did some Drupal development work for Cambridge Community Television. As part of the really amazing work they are doing combining new media with traditional Cable Access Television, CCTV has been mapping videos their members produce. They call this project the Mediamap.

I was really excited to work on the Mediamap with CCTV because of my long involvement with Cable Access Television, most notably the now-defunct DigitalBicycle Project and the community maintained directory of Cable Access Stations I built and administer: MappingAccess.com.

Despite CCTV running their website on Drupal, their first proof-of-concept version of the Mediamap was created manually, using the very capable Mapbuilder.net service and copy-and-pasted embedded flash video. While simple from a technological standpoint, they were running to problems optimizing the workflow of updating the map; changes had to be made via the Mapbuilder.net interface, with a single username and password, then manually parsed to remove some coding irregularities, and finally copy and pasted whole into a page on their website.

I was asked to improve the workflow and ultimately take fuller advantage of Drupal’s built-in user management and content management features. For instance, taking advantage of CCTV’s current member submitted video capabilities and flowing them into the map as an integrated report, not a separate and parallel system.

In my discussions with them, a couple of issues came up. Foremost was that CCTV was running an older version of Drupal: 4.7. While still quite powerful, many newer features and contributed modules were not available for this earlier release. The current version of Drupal, 5.1, has many rich, well-developed utilities for creating reports and mapping them: Content Construction Kit (CCK) + Views + Gmap + Location. As it was though, with the older version, I would have to develop the additional functionality manually.

The following is a description, with code examples, of the functionality I created for the Mediamap. Additionally, following this initial development, CCTV upgraded their Drupal installation to 5.1, giving me the opportunity to demonstrate the ease and power of Drupal’s most recent release—rendering blissfully obsolete most of the custom coding I had done.

Location and Gmap was used in both versions for storing geographic data and hooking into the Google Map API. One of Drupal’s great strengths is the both the diversity of contributed modules, and the flexibility with which a developer can use them.

Adding additional content fields

CCTV already has a process in which member’s can submit content nodes. In 4.7, the easiest way to add additional data fields to these was with a custom NodeAPI module. CCTV was interested in using embedded flash video, primarily from Blip.tv, but also Google Video or YouTube if the flexibility was needed. To simplify the process, we decided on just adding the cut-and-paste embed code to a custom content field in existing nodes.

To do this, I created a new module that invoked hook_nodeapi:
/**
* Implementation of hook_nodeapi
*/
function cambridge_mediamap_nodeapi(&amp;$node, $op, $teaser, $page) {
switch ($op) {

case 'validate':
if (variable_get('cambridge_mediamap_'. $node-&gt;type, TRUE)) {
if (user_access('modify node data')) {
if ($node-&gt;cambridge_mediamap['display'] &amp;&amp; $node-&gt;cambridge_mediamap['embed'] == '') {
form_set_error('cambridge_mediamap', t('Media Map: You must enter embed code or disable display of this node on the map'));
}
}
}
break;

case 'load':
$object = db_fetch_object(db_query('SELECT display, embed FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid));

$embed = $object-&gt;embed;
$embed_resize = cambridge_mediamap_resize($embed);

return array(
'cambridge_mediamap' =&gt; array(
'display' =&gt; $object-&gt;display,
'embed' =&gt; $embed,
'embed_resize' =&gt; $embed_resize,
)
);
break;

case 'insert':
db_query("INSERT INTO {cambridge_mediamap} (nid, display, embed) VALUES (%d, %d, '%s')", $node-&gt;nid, $node-&gt;cambridge_mediamap['display'], $node-&gt;cambridge_mediamap['embed']);
break;

case 'update':
db_query('DELETE FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid);
db_query("INSERT INTO {cambridge_mediamap} (nid, display, embed) VALUES (%d, %d, '%s')", $node-&gt;nid, $node-&gt;cambridge_mediamap['display'], $node-&gt;cambridge_mediamap['embed']);
break;

case 'delete':
db_query('DELETE FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid);
break;

case 'view':
break;
}
}

As you can see, there is a considerable amount of coding required, from defining the form, validating input and configuring database storage and retrieval calls.

Now that we have the glue for the custom field, we have to configure what node types that custom field appears on. Additionally, we need to set up administrative settings to configure where that custom field will appear, and lastly insert that field into the node edit screen:
/**
* Implementation of hook_form_alter
*/
function cambridge_mediamap_form_alter($form_id, &amp;$form) {
// We're only modifying node forms, if the type field isn't set we don't need
// to bother.
if (!isset($form['type'])) {
return;
}

//disable the Gmap module's location map for unauthorized users
//unfortunately Gmap.module doesn't have this setting
if (isset($form['coordinates'])) {
if (!user_access('modify node data')) {
unset($form['coordinates']);
}
}

// Make a copy of the type to shorten up the code
$type = $form['type']['#value'];

// Is the map enabled for this content type?
$enabled = variable_get('cambridge_mediamap_'. $type, 0);

switch ($form_id) {
// We need to have a way for administrators to indicate which content
// types should have the additional media map information added.
case $type .'_node_settings':
$form['workflow']['cambridge_mediamap_'. $type] = array(
'#type' =&gt; 'radios',
'#title' =&gt; t('Cambridge Mediamap setting'),
'#default_value' =&gt; $enabled,
'#options' =&gt; array(0 =&gt; t('Disabled'), 1 =&gt; t('Enabled')),
'#description' =&gt; t('Allow the attaching of externally hosted imbedded video to be displayed in a map?'),
);
break;

case $type .'_node_form':

if ($enabled &amp;&amp; user_access('modify node data')) {
//create the fieldset
$form['cambridge_mediamap'] = array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Media Map'),
'#collapsible' =&gt; TRUE,
'#collapsed' =&gt; FALSE,
'#tree' =&gt; TRUE,
);
//insert the embed code
$form['cambridge_mediamap']['embed'] = array(
'#type' =&gt; 'textarea',
'#title' =&gt; t('Video Embed Code'),
'#default_value' =&gt; $form['#node']-&gt;cambridge_mediamap['embed'],
'#cols' =&gt; 60,
'#rows' =&gt; 5,
'#description' =&gt; t('Copy and paste the embed code from an external video or media hosting service'),
);
//enable or disable on map
$form['cambridge_mediamap']['display'] = array(
'#type' =&gt; 'select',
'#title' =&gt; t('Display this node'),
'#default_value' =&gt; $form['#node']-&gt;cambridge_mediamap['display'],
'#options' =&gt; array(
'0' =&gt; t('Disable display'),
'1' =&gt; t('Enable display'),
),
);

}
break;
}
}

As you can see, that’s a lot of lines of code for what we essentially can do, in Drupal 5.1 with CCK. CCK allows you, graphically through the Drupal web-interface, to create a new content field and add it to a node type; it takes about a minute.

Building the Map

The primary goal of rebuilding the Mediamap using native Drupal was workflow optimization: it was frustrating to submit information both within Drupal and then recreate it within Mapbuilder. In essence, the map should be just another report of Drupal content: you may have a short bulleted list of the top five articles, a paginated history with teasers and author information, or a full-blown map, but most importantly, all of it is flowing dynamically out of the Drupal database.

The Gmap module provides many powerful ways to integrate the Google Map API with Drupal. While Gmap for 4.7 provides a default map of content it would not provide the features or customizability we desired with the Mediamap. Instead, one of the most powerful ways to use Gmap is to hook directly into the module’s own API-like functions:

/**
* A page callback to draw the map
*/
function cambridge_mediamap_map() {
$output = '';

//Collect the nodes to be displayed
$results = db_query('SELECT embed, nid FROM {cambridge_mediamap} WHERE display = 1');

//Initialize our marker array
$markers = array();

//check to see what modules are enabled
$location_enabled = module_exist('location');
$gmap_location_enabled = module_exist('gmap_location');

//load each node and set it's attributes in the marker array
while($item = db_fetch_object($results)) {
$latitude = 0;
$longitude = 0;
//load the node
$node = node_load(array('nid' =&gt; $item-&gt;nid));

//set the latitude and longitude
//give location module data preference over gmap module data
if ($location_enabled) {
$latitude = $node-&gt;location['latitude'];
$longitude = $node-&gt;location['longitude'];
}
elseif ($gmap_location_enabled) {
$latitude = $node-&gt;gmap_location_latitude;
$longitude = $node-&gt;gmap_location_longitude;
}

if ($latitude &amp;&amp; $longitude) {
$markers[] = array(
'label' =&gt; theme('cambridge_mediamap_marker', $node),
'latitude' =&gt; $latitude,
'longitude' =&gt; $longitude,
'markername' =&gt; variable_get('cambridge_mediamap_default_marker', 'marker'),
);
}
}

$latlon = explode(',', variable_get('cambridge_mediamap_default_latlong','42.369452,-71.100426'));

$map=array(
'id' =&gt; 'cambridge_mediamap',
'latitude' =&gt; trim($latlon[0]),
'longitude'=&gt; trim($latlon[1]),
'width' =&gt; variable_get('cambridge_mediamap_default_width','100%'),
'height' =&gt; variable_get('cambridge_mediamap_default_height','500px'),
'zoom' =&gt; variable_get('cambridge_mediamap_default_zoom', 13),
'control' =&gt; variable_get('cambridge_mediamap_default_control','Large'),
'type' =&gt; variable_get('cambridge_mediamap_default_type','Satellite'),
'markers' =&gt; $markers,
);

return gmap_draw_map($map);
}

As you can see, this is quite complicated. Drupal 5.1 offers the powerful Views module, which allows one to define custom reports, once again graphically from the Drupal web-interface, in just a couple minutes of configuration. The gmap_views module, which ships with Gmap, allows one to add those custom reports to a Google Map, which is incredibly useful and renders obsolete much of the development work I did.

On displaying video in maps

In my discussions with CCTV, we felt it most pragmatic to use the embedded video code provided by video hosting services such as Blip.tv. While we could have used one of the Drupal video modules, we wanted the ability to host video offsite due to storage constraints. While I was concerned about the danger of code injection via minimally validated inputs, we felt that this would be of small danger because the content would be maintained by CCTV staff and select members.

The markers were themed using the embedded video field pulled from the Drupal database, along with the title and a snippet of the description, all linking back to the full content node.
/**
* A theme function for our markers
*/

function theme_cambridge_mediamap_marker($node) {

$output = '
<div class="mediamap-marker">';
$output .= '
<div class="title">' . l($node-&gt;title, 'node/' . $node-&gt;nid) . '</div>
';
$output .= '
<div class="embed">' . $node-&gt;cambridge_mediamap['embed_resize'] . '</div>
';
$output .= '</div>
';

return $output;
}</pre>
With Drupal 5.1 and Views, we still had to override the standard marker themes, but this was simple and done through the standard methods.

One of the most helpful pieces was some code developed by Rebecca White, who I previously worked with on Panlexicon. She provided the critical pieces of code that parsed the embedded video code and resized it for display on small marker windows.
<pre style="height: 10em;">/**
* Returns a resized embed code
*/
function cambridge_mediamap_resize($embed = '') {
if (!$embed) {
return '';
}

list($width, $height) = cambridge_mediamap_get_embed_size($embed);

//width/height ratio
$width_to_height = $width / $height;

$max_width = variable_get('cambridge_mediamap_embed_width','320');
$max_height = variable_get('cambridge_mediamap_embed_height','240');

//shrink down widths while maintaining proportion
if ($width &gt;= $height) {
if ($width &gt; $max_width) {
$width = $max_width;
$height = (1 / $width_to_height) * $width;
}
if ($height &gt; $max_height) {
$height = $max_height;
$width = ($width_to_height) * $height;
}
}
else {
if ($height &gt; $max_height) {
$height = $max_height;
$width = ($width_to_height) * $height;
}
if ($width &gt; $max_width) {
$width = $max_width;
$height = (1 / $width_to_height) * $width;
}
}

return cambridge_mediamap_set_embed_size($embed, intval($width), intval($height));
}

/**
* find out what size the embedded thing is
*/
function cambridge_mediamap_get_embed_size($html) {
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*width(\s*=\s*"|:\s*)(\d+)/i', $html, $match_width);
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*height(\s*=\s*"|:\s*)(\d+)/i', $html, $match_height);

return array($match_width[2], $match_height[2]);
}

/**
* set the size of the embeded thing
*/
function cambridge_mediamap_set_embed_size($html, $width, $height) {
$html = preg_replace('/(&lt;(embed|object)\s[^&gt;]*width(\s*=\s*"|:\s*))(\d+)/i', '${1}' . $width, $html);
$html = preg_replace('/(&lt;(embed|object)\s[^&gt;]*height(\s*=\s*"|:\s*))(\d+)/i', '${1}' . $height, $html);

return $html;
}

/**
* returns the base url of the src attribute.
* youtube = www.youtube.com
* blip = blip.tv
* google video = video.google.com
*/
function cambridge_mediamap_get_embed_source($html) {
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*src="http:\/\/([^\/"]+)/i', $html, $match_src);
return $match_src[1];
}

The Wrap-Up

While it may not seem so from the lines of code above, developing for Drupal is still relatively easy. Drupal provides a rich set of features for developers, well documented features, and strong coding standards—making reading other people’s code and learning from it incredibly productive.

Below is the entirety of the custom module I developed for the 4.7 version of the CCTV Media Map. Because it was custom and intended to be used in-house, many important, release worthy functions were omitted, such as richer administrative options and module/function verifications.
'cambridge_mediamap',
'title' =&gt; t('Mediamap'),
'callback' =&gt; 'cambridge_mediamap_map',
'access' =&gt; user_access('access mediamap'),
);
}
return $items;
}

/**
* Implementation of hook_nodeapi
*/
function cambridge_mediamap_nodeapi(&amp;$node, $op, $teaser, $page) {
switch ($op) {

case 'validate':
if (variable_get('cambridge_mediamap_'. $node-&gt;type, TRUE)) {
if (user_access('modify node data')) {
if ($node-&gt;cambridge_mediamap['display'] &amp;&amp; $node-&gt;cambridge_mediamap['embed'] == '') {
form_set_error('cambridge_mediamap', t('Media Map: You must enter embed code or disable display of this node on the map'));
}
}
}
break;

case 'load':
$object = db_fetch_object(db_query('SELECT display, embed FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid));

$embed = $object-&gt;embed;
$embed_resize = cambridge_mediamap_resize($embed);

return array(
'cambridge_mediamap' =&gt; array(
'display' =&gt; $object-&gt;display,
'embed' =&gt; $embed,
'embed_resize' =&gt; $embed_resize,
)
);
break;

case 'insert':
db_query("INSERT INTO {cambridge_mediamap} (nid, display, embed) VALUES (%d, %d, '%s')", $node-&gt;nid, $node-&gt;cambridge_mediamap['display'], $node-&gt;cambridge_mediamap['embed']);
break;

case 'update':
db_query('DELETE FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid);
db_query("INSERT INTO {cambridge_mediamap} (nid, display, embed) VALUES (%d, %d, '%s')", $node-&gt;nid, $node-&gt;cambridge_mediamap['display'], $node-&gt;cambridge_mediamap['embed']);
break;

case 'delete':
db_query('DELETE FROM {cambridge_mediamap} WHERE nid = %d', $node-&gt;nid);
break;

case 'view':
break;
}
}

/**
* Returns a resized embed code
*/
function cambridge_mediamap_resize($embed = '') {
if (!$embed) {
return '';
}

list($width, $height) = cambridge_mediamap_get_embed_size($embed);

//width/height ratio
$width_to_height = $width / $height;

$max_width = variable_get('cambridge_mediamap_embed_width','320');
$max_height = variable_get('cambridge_mediamap_embed_height','240');

//shrink down widths while maintaining proportion
if ($width &gt;= $height) {
if ($width &gt; $max_width) {
$width = $max_width;
$height = (1 / $width_to_height) * $width;
}
if ($height &gt; $max_height) {
$height = $max_height;
$width = ($width_to_height) * $height;
}
}
else {
if ($height &gt; $max_height) {
$height = $max_height;
$width = ($width_to_height) * $height;
}
if ($width &gt; $max_width) {
$width = $max_width;
$height = (1 / $width_to_height) * $width;
}
}

return cambridge_mediamap_set_embed_size($embed, intval($width), intval($height));
}

/**
* find out what size the embedded thing is
*/
function cambridge_mediamap_get_embed_size($html) {
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*width(\s*=\s*"|:\s*)(\d+)/i', $html, $match_width);
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*height(\s*=\s*"|:\s*)(\d+)/i', $html, $match_height);

return array($match_width[2], $match_height[2]);
}

/**
* set the size of the embeded thing
*/
function cambridge_mediamap_set_embed_size($html, $width, $height) {
$html = preg_replace('/(&lt;(embed|object)\s[^&gt;]*width(\s*=\s*"|:\s*))(\d+)/i', '${1}' . $width, $html);
$html = preg_replace('/(&lt;(embed|object)\s[^&gt;]*height(\s*=\s*"|:\s*))(\d+)/i', '${1}' . $height, $html);

return $html;
}

/**
* returns the base url of the src attribute.
* youtube = www.youtube.com
* blip = blip.tv
* google video = video.google.com
*/
function cambridge_mediamap_get_embed_source($html) {
preg_match('/<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><embed type="application/x-shockwave-flash" width="100" height="100"></embed></object>]*src="http:\/\/([^\/"]+)/i', $html, $match_src);
return $match_src[1];
}

/**
* Implementation of hook_form_alter
*/
function cambridge_mediamap_form_alter($form_id, &amp;$form) {
// We're only modifying node forms, if the type field isn't set we don't need
// to bother.
if (!isset($form['type'])) {
return;
}

//disable the Gmap module's location map for unauthorized users
//unfortunately Gmap.module doesn't have this setting
if (isset($form['coordinates'])) {
if (!user_access('modify node data')) {
unset($form['coordinates']);
}
}

// Make a copy of the type to shorten up the code
$type = $form['type']['#value'];

// Is the map enabled for this content type?
$enabled = variable_get('cambridge_mediamap_'. $type, 0);

switch ($form_id) {
// We need to have a way for administrators to indicate which content
// types should have the additional media map information added.
case $type .'_node_settings':
$form['workflow']['cambridge_mediamap_'. $type] = array(
'#type' =&gt; 'radios',
'#title' =&gt; t('Cambridge Mediamap setting'),
'#default_value' =&gt; $enabled,
'#options' =&gt; array(0 =&gt; t('Disabled'), 1 =&gt; t('Enabled')),
'#description' =&gt; t('Allow the attaching of externally hosted imbedded video to be displayed in a map?'),
);
break;

case $type .'_node_form':

if ($enabled &amp;&amp; user_access('modify node data')) {
//create the fieldset
$form['cambridge_mediamap'] = array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Media Map'),
'#collapsible' =&gt; TRUE,
'#collapsed' =&gt; FALSE,
'#tree' =&gt; TRUE,
);
//insert the embed code
$form['cambridge_mediamap']['embed'] = array(
'#type' =&gt; 'textarea',
'#title' =&gt; t('Video Embed Code'),
'#default_value' =&gt; $form['#node']-&gt;cambridge_mediamap['embed'],
'#cols' =&gt; 60,
'#rows' =&gt; 5,
'#description' =&gt; t('Copy and paste the embed code from an external video or media hosting service'),
);
//enable or disable on map
$form['cambridge_mediamap']['display'] = array(
'#type' =&gt; 'select',
'#title' =&gt; t('Display this node'),
'#default_value' =&gt; $form['#node']-&gt;cambridge_mediamap['display'],
'#options' =&gt; array(
'0' =&gt; t('Disable display'),
'1' =&gt; t('Enable display'),
),
);

}
break;
}
}

/**
* A page callback to draw the map
*/
function cambridge_mediamap_map() {
$output = '';

//Collect the nodes to be displayed
$results = db_query('SELECT embed, nid FROM {cambridge_mediamap} WHERE display = 1');

//Initialize our marker array
$markers = array();

//check to see what modules are enabled
$location_enabled = module_exist('location');
$gmap_location_enabled = module_exist('gmap_location');

//load each node and set it's attributes in the marker array
while($item = db_fetch_object($results)) {
$latitude = 0;
$longitude = 0;
//load the node
$node = node_load(array('nid' =&gt; $item-&gt;nid));

//set the latitude and longitude
//give location module data preference over gmap module data
if ($location_enabled) {
$latitude = $node-&gt;location['latitude'];
$longitude = $node-&gt;location['longitude'];
}
elseif ($gmap_location_enabled) {
$latitude = $node-&gt;gmap_location_latitude;
$longitude = $node-&gt;gmap_location_longitude;
}

if ($latitude &amp;&amp; $longitude) {
$markers[] = array(
'label' =&gt; theme('cambridge_mediamap_marker', $node),
'latitude' =&gt; $latitude,
'longitude' =&gt; $longitude,
'markername' =&gt; variable_get('cambridge_mediamap_default_marker', 'marker'),
);
}
}

$latlon = explode(',', variable_get('cambridge_mediamap_default_latlong','42.369452,-71.100426'));

$map=array(
'id' =&gt; 'cambridge_mediamap',
'latitude' =&gt; trim($latlon[0]),
'longitude'=&gt; trim($latlon[1]),
'width' =&gt; variable_get('cambridge_mediamap_default_width','100%'),
'height' =&gt; variable_get('cambridge_mediamap_default_height','500px'),
'zoom' =&gt; variable_get('cambridge_mediamap_default_zoom', 13),
'control' =&gt; variable_get('cambridge_mediamap_default_control','Large'),
'type' =&gt; variable_get('cambridge_mediamap_default_type','Satellite'),
'markers' =&gt; $markers,
);

return gmap_draw_map($map);
}

/**
* A theme function for our markers
*/

function theme_cambridge_mediamap_marker($node) {

$output = '
<div class="mediamap-marker">';
$output .= '
<div class="title">' . l($node-&gt;title, 'node/' . $node-&gt;nid) . '</div>
';
$output .= '
<div class="embed">' . $node-&gt;cambridge_mediamap['embed_resize'] . '</div>
';
$output .= '</div>
';

return $output;
}

/**
* Settings page
*/
function cambridge_mediamap_settings() {
// Cambridge data
// latitude = 42.369452
// longitude = -71.100426

$form['defaults']=array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Default map settings'),
);

$form['defaults']['cambridge_mediamap_default_width'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Default width'),
'#default_value' =&gt; variable_get('cambridge_mediamap_default_width','100%'),
'#size' =&gt; 25,
'#maxlength' =&gt; 6,
'#description' =&gt; t('The default width of a Google map. Either px or %'),
);
$form['defaults']['cambridge_mediamap_default_height'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Default height'),
'#default_value' =&gt; variable_get('cambridge_mediamap_default_height','500px'),
'#size' =&gt; 25,
'#maxlength' =&gt; 6,
'#description' =&gt; t('The default height of Mediamap. In px.'),
);
$form['defaults']['cambridge_mediamap_default_latlong'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Default center'),
'#default_value' =&gt; variable_get('cambridge_mediamap_default_latlong','42.369452,-71.100426'),
'#description' =&gt; 'The decimal latitude,longitude of the centre of the map. The "." is used for decimal, and "," is used to separate latitude and longitude.',
'#size' =&gt; 50,
'#maxlength' =&gt; 255,
'#description' =&gt; t('The default longitude, latitude of Mediamap.'),
);
$form['defaults']['cambridge_mediamap_default_zoom']=array(
'#type'=&gt;'select',
'#title'=&gt;t('Default zoom'),
'#default_value'=&gt;variable_get('cambridge_mediamap_default_zoom', 13),
'#options' =&gt; drupal_map_assoc(range(0, 17)),
'#description'=&gt;t('The default zoom level of Mediamap.'),
);
$form['defaults']['cambridge_mediamap_default_control']=array(
'#type'=&gt;'select',
'#title'=&gt;t('Default control type'),
'#default_value'=&gt;variable_get('cambridge_mediamap_default_control','Large'),
'#options'=&gt;array('None'=&gt;t('None'),'Small'=&gt;t('Small'),'Large'=&gt;t('Large')),
);
$form['defaults']['cambridge_mediamap_default_type']=array(
'#type'=&gt;'select',
'#title'=&gt;t('Default map type'),
'#default_value'=&gt;variable_get('cambridge_mediamap_default_type','Satellite'),
'#options'=&gt;array('Map'=&gt;t('Map'),'Satellite'=&gt;t('Satellite'),'Hybrid'=&gt;t('Hybrid')),
);

$markers = gmap_get_markers();

$form['defaults']['cambridge_mediamap_default_marker'] = array(
'#type'=&gt;'select',
'#title'=&gt;t('Marker'),
'#default_value'=&gt;variable_get('cambridge_mediamap_default_marker', 'marker'),
'#options'=&gt;$markers,
);

$form['embed']=array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Default embedded video settings'),
);

$form['embed']['cambridge_mediamap_embed_width'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Default width'),
'#default_value' =&gt; variable_get('cambridge_mediamap_embed_width','320'),
'#size' =&gt; 25,
'#maxlength' =&gt; 6,
'#description' =&gt; t('The maximum width of embedded video'),
);
$form['embed']['cambridge_mediamap_embed_height'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Default height'),
'#default_value' =&gt; variable_get('cambridge_mediamap_embed_height','240'),
'#size' =&gt; 25,
'#maxlength' =&gt; 6,
'#description' =&gt; t('The maximum height of embedded video.'),
);

return $form;
}

/**
* Prints human-readable (html) information about a variable.
* Use: print debug($variable_name);
* Or assign output to a variable.
*/
function debug($value) {
return preg_replace("/\s/", " ", preg_replace("/\n/", "",
print_r($value, true)));
}


A new look

Island94.org has undergone some revision today; I hope you noticed. I upgraded Drupal to 4.7 and rethemed the site.

Drupal 4.7 adds a lot of really great features, the main one, from a ‘nifty’ perspective is free tagging. This gives a lot more granularity to organizing posts. There is also a lot of nice stuff under the hood. I had been a little lax in upgrading my old Drupal install, so the upgrade also fixes a couple of security issues as well.

The theme is a major change from the old design. I had really liked the woodcut motif that I grabbed from Mark Twain’s Life on the Mississippi but it was starting to show its age. Also, looking under the hood I was always reminded of my own inexperience at the time I put it together.

With the new theme I had a couple of things in mind:

First off, I really wanted to better separate posts of a personal nature, from that of a professional or fictional nature. I’m fairly prodigious in creating media, it’s mostly a matter of audience. I don’t want some boss reading a slasher tale thinking I’m a psychopath, or my mom reading halfway through some technical post to realize she doesn’t care. I’m really excited about the hats theme, not only are they simply beautiful, but they help to quickly convey what overall category the post falls into.

Secondly, Drupal’s the standard interface is beautiful and intuitive, but when a couple thousand websites start using it, it has become a little rusty. Much of my time in designing Drupal sites is making sure it doesn’t look like Drupal.

Lastly, I really wanted to fool around with some soft gradients and big bubbly icons. I’ve been pulling from theOpen Clip Art Library for the most part and using Inkscape and Gimpshop for editing.

I hope that my new theme is both easy to use and beautiful. At the moment, I’ve pretty much given up on Internet Explorer compatibility. Maybe something to do later, but frankly, I am a little tired of dealing with its nonstandard quirks.

I started work on the design yesterday morning using Luka Cvrk’s Small Studio as a base, and considering I didn’t get a vacation day today (it is Monday, July 3rd), I am happy with the result. I hope you are as well.