I've posted twice regarding the creation of my module and found a developer. However, upon upgrading to Drupal 5, the part of the module that gets the date and overrides it no longer seems to work. The developer shot me a few emails to point me in the right direction, but it was all stuff I had tried in one way or another. First, here's the MAQUM.module:
// $Id$
define('MAQUM_VERSION', shell_exec(variable_get('MAQUM_path', '/usr/bin/').'exiftool -ver'));
/**
* CORE HOOKS
*/
/**
* Implementation of hook_access
*/
function MAQUM_access($op, $node) {
if ($op == 'create') {
return user_access('create MAQUM nodes');
}
} // MAQUM_access
/**
* Implementation of hook_form
*/
function MAQUM_form(&$node, &$param) {
_image_check_settings();
// Set form parameters so we can accept file uploads.
$form['#attributes'] = array("enctype" => "multipart/form-data");
$sizes = _image_get_sizes();
$form['images']['#tree'] = TRUE;
$form['images']['_original'] = array('#type' => 'hidden', '#value' => $node->images['_original']);
foreach ($sizes as $size) {
$form['images'][$size['label']] = array('#type' => 'hidden', '#value' => $node->images[$size['label']]);
}
$form['thumbnail']['#after_build'] = array('image_form_add_thumbnail');
$form['image'] = array('#type' => 'file', '#title' => t("Image"), '#description' => t("Click \"Browse...\" to select an image to upload."));
return $form;
} // MAQUM_form
/**
* Implementation of hook_help
*/
function MAQUM_help($section) {
switch ($section) {
// This function is no longer needed here but is still useful
// for displaying help on pages so I kept it here
}
} // MAQUM_help
/**
* Implementation of hook_insert
*/
function MAQUM_insert($node) {
image_insert($node);
} // MAQUM_insert
/**
* Implementation of hook_menu
*/
function MAQUM_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/add/MAQUM',
'title' => t('MAQUM'),
'access' => user_access('create MAQUM nodes'),
);
$items[] = array('path' => 'admin/settings/MAQUM',
'title' => t('MAQUM Settings'),
'description' => t('Change settings for MAQUM module here.'),
'access' => user_access('create MAQUM nodes'),
'callback' => 'drupal_get_form',
'callback arguments' => array('MAQUM_settings'),
);
}
return $items;
} // MAQUM_menu
/**
* Implementation of hook_node_info
*/
function MAQUM_node_info() {
return array('MAQUM' => array('name' => t('MAQUM'), 'module' => 'MAQUM', 'description' => t('Create an auto-tagged image node.')));
} // MAQUM_node_info
/**
* Implementation of hook_perm
*/
function MAQUM_perm() {
return array('create MAQUM nodes');
} // MAQUM_perm
/**
* Implementation of hook_prepare
*/
function MAQUM_prepare(&$node, $field_name) {
image_prepare(&$node, $field_name);
} // MAQUM_prepare
/**
* Implementation of hook_settings
*/
function MAQUM_settings() {
if (!MAQUM_VERSION) drupal_set_message(t("Exiftool not found - please check your path."), 'error');
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
$options[$vid] = $vocab->name;
}
$form['MAQUM_exiftool'] = array(
'#type' => 'item',
'#value' => t("Exiftool version: <strong>%version</strong>", array('%version' => MAQUM_VERSION)),
'#description' => t("If this version number is blank then your installation path is incorrect"),
);
$form['MAQUM_path'] = array(
'#type' => 'textfield',
'#title' => t("Path to exiftool"),
'#default_value' => variable_get('MAQUM_path', '/usr/bin/'),
'#description' => t("Including trailing slash, eg. /usr/bin/"),
'#attributes' => !MAQUM_VERSION ? array('class' => 'error') : '',
);
$form['group1'] = array('#type' => 'fieldset', '#title' => t("Mode:"), '#collapsible' => TRUE, '#collapsed' => FALSE);
$form['group1']['MAQUM_mode'] = array(
'#type' => 'select',
'#title' => t("Mode"),
'#options' => array(1 => t('Terms'), 2 => t('Vocabularies')),
'#default_value' => variable_get('MAQUM_mode', 1),
'#description' => t("Which mode to operate in;<br><ul><li>Terms : Creates metadata as terms in one specified vocabulary</li><li>Vocabularies : Creates new vocabularies for metadata and populates terms with the associated values (eg. ISO --> 100, 200, 400, etc)</li></ul>"),
);
if (variable_get('MAQUM_mode', 1) == 1) {
$form['group1']['MAQUM_vid'] = array(
'#type' => 'select',
'#title' => t("Vocabulary"),
'#options' => $options,
'#default_value' => variable_get('MAQUM_vid', '1'),
'#description' => t("Select the vocabulary you wish to auto-add EXIF terms to"),
);
}
$form['group2'] = array('#type' => 'fieldset', '#title' => t("Automation Settings:"), '#collapsible' => TRUE, '#collapsed' => FALSE);
$form['group2']['MAQUM_date'] = array(
'#type' => 'textfield',
'#title' => t("IPTC field to use for node creation timestamp"),
'#default_value' => variable_get('MAQUM_date', 'Create Date'),
'#description' => t("This must match the IPTC fieldname EXACTLY"),
);
$form['group2']['MAQUM_title'] = array(
'#type' => 'textfield',
'#title' => t("IPTC field to use for node title"),
'#default_value' => variable_get('MAQUM_title', 'Object Name'),
'#description' => t("This must match the IPTC fieldname EXACTLY<br>Note that if this IPTC field is not populated then the images filename will be used as the node title"),
);
$form['group2']['MAQUM_body'] = array(
'#type' => 'textfield',
'#title' => t("IPTC field to use for node body"),
'#default_value' => variable_get('MAQUM_body', 'Caption-Abstract'),
'#description' => t("This must match the IPTC fieldname EXACTLY"),
);
$form['group2']['MAQUM_exif'] = array(
'#type' => 'textarea',
'#title' => t("A list of EXIF tags to use for your taxonomies"),
'#default_value' => variable_get('MAQUM_exif', "Camera Model Name\nMake\nISO"),
'#description' => t("Please separate each tag by a newline"),
'#cols' => 50,
'#rows' => 25,
);
return system_settings_form($form);
} // MAQUM_settings
/**
* Implementation of hook_submit
*/
function MAQUM_submit(&$node) {
$node->type = 'image';
$info = _MAQUM_exiftool($node->images['_original']);
if (!$info) {
drupal_set_message(t('Error using exiftool.'));
watchdog('MAQUM', t("MAQUM: error using exiftool."), WATCHDOG_ERROR);
return;
}
$test = _MAQUM_set_metadata(&$node, $info);
if (!$test) {
drupal_set_message(t('Error setting taxonomy.'));
watchdog('MAQUM', t("MAQUM: error setting taxonomy."), WATCHDOG_ERROR);
}
if ($info[variable_get('MAQUM_title', 'Object Name')]) {
$node->title = $info[variable_get('MAQUM_title', 'Object Name')];
}
else {
$node->title = basename($node->images['_original']);
}
if ($info[variable_get('MAQUM_date', 'Create Date')]) $node->created = strtotime(preg_replace("/(\d+):(\d+):(\d+)\s/", "$1/$2/$3", $info[variable_get('MAQUM_date', 'Create Date')]));
if ($info[variable_get('MAQUM_body', 'Caption-Abstract')]) $node->body = $info[variable_get('MAQUM_body', 'Caption-Abstract')];
} // MAQUM_submit
/**
* MODULE CUSTOM FUNCTIONS
*/
/**
* Creates a new term set in your default vocabulary
*/
function _MAQUM_create_term($name, $vid = NULL) {
if (!$vid) $vid = variable_get('MAQUM_vid', '1');
$output->tid = db_next_id('{term_data}_tid');
$output->name = $name;
$output->vid = $vid;
db_query("INSERT INTO {term_data} (tid, name, vid) VALUES (%d, '%s', %d)", $output->tid, $output->name, $output->vid);
db_query("INSERT INTO {term_hierarchy} (tid) VALUES (%d)", $output->tid);
return array($output);
} // _MAQUM_create_term
/**
* Creates a new vocabulary
*/
function _MAQUM_create_vocabulary($name) {
$vid = db_next_id('{vocabulary}_vid');
db_query("INSERT INTO {vocabulary} (vid, name, hierarchy, multiple, module) VALUES (%d, '%s', %d, %d, '%s')", $vid, $name, '1', '1', 'taxonomy');
db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vid, 'image');
return $vid;
} // _MAQUM_create_vocabulary
/**
* Returns the exiftool information as an array
*/
function _MAQUM_exiftool($path) {
$temp = shell_exec(variable_get('MAQUM_path', '/usr/bin/').'exiftool --list '.escapeshellarg($path));
$temp = explode("\n", $temp);
foreach ($temp as $item) {
$pos = strpos($item, ':');
$info[trim(substr($item, 0, $pos))] = trim(substr($item, $pos+1));
}
return $info;
} // _MAQUM_exiftool
/**
* Creates the vocabularies (if required) and sets the terms
*/
function _MAQUM_set_metadata(&$node, $info) {
$taxonomy = $node->taxonomy;
$items = explode("\n", variable_get('MAQUM_exif', "Camera Model Name\nMake\nISO"));
foreach ($items as $item) {
$item = trim($item);
if (isset($info[$item])) {
unset($vid);
unset($term);
if (variable_get('MAQUM_mode', 1) == 2) {
$fetch = db_fetch_object(db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $item));
$vid = $fetch->vid;
if (!$vid) $vid = _MAQUM_create_vocabulary($item);
$prefix = NULL;
}
else {
$prefix = $item.' : ';
}
switch ($item) {
case "Keywords":
$keywords = explode("," , $info[$item]);
foreach ($keywords as $keyword) {
$keyword = $prefix.trim($keyword);
$term = taxonomy_get_term_by_name($keyword);
if (!$term) $term = _MAQUM_create_term($keyword, $vid);
$taxonomy[$term[0]->tid] = $term[0];
}
break;
default:
$term = taxonomy_get_term_by_name($prefix.$info[$item]);
if (!$term) $term = _MAQUM_create_term($prefix.$info[$item], $vid);
$taxonomy[$term[0]->tid] = $term[0];
} // end switch
}
}
if (isset($taxonomy)) {
$node->taxonomy = $taxonomy;
return TRUE;
}
} // _MAQUM_set_metadata
Here's MAQUM.info:
; $Id$
name = MAQUM
description = A simple module which creates automatically categorized image nodes.
I know, I need to add the dependencies in there, but that's merely aesthetics. My problem is with this line:
if ($info[variable_get('MAQUM_date', 'Create Date')]) $node->created = strtotime(preg_replace("/(\d+):(\d+):(\d+)\s/", "$1/$2/$3", $info[variable_get('MAQUM_date', 'Create Date')]));
My server does not recognize "2007:01:07 22:04:39" as a date format. So it needed to be reformatted, hence the preg_replace. However, the date of node creation still is not overridden to the date found in the metadata while using Drupal 5.
I looked at the next line which sets the body of the node to the caption field found in the metadata and changed it to read like so:
if ($info[variable_get('MAQUM_date', 'Create Date')]) $node->body = $info[variable_get('MAQUM_date', 'Create Date')];
This should just set the body of the node to the date, not reformat it and not convert it to a unix timestamp. Instead, I get a totally empty body. I tried removing the conditional if statement. Same result.
It seems to me that the module is no longer capable, for whatever reason, of reading the date field and hence is unable to convert it to the right date format and override the node creation date. Can anyone see why?
Comments
Just to be clear here, this
Just to be clear here, this module (my own work) worked fine when it was used for 4.7 (only necessary things were updated for version 5.0, neither the date nor the way the exiftool information is parsed was touched) - this 5.0 version works fine on my server (using PHP5 - although I do note that Brian has altered some of my defaults, although that should be pretty irrelevant) with and without the preg_replace for the strtotime function. I can only assume there's some sort of configuration problem here as we're testing using the same photographs?
This is proving to be difficult to debug as like I said, it's fine my end and I do not have shell access to Brians server.
Pobster