/[drupal]/contributions/modules/maqum/maqum.module
ViewVC logotype

Contents of /contributions/modules/maqum/maqum.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sun Jun 8 15:17:57 2008 UTC (17 months, 2 weeks ago) by brianpuccio
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of MAQUM. Metadata Aware Quick Upload Module: a module which creates image nodes with EXIF and IPTC metadata.
1 <?php
2 // $Id$
3
4 define('MAQUM_VERSION', shell_exec(variable_get('MAQUM_path', '/usr/bin/').'exiftool -ver'));
5
6 /**
7 * CORE HOOKS
8 */
9
10 /**
11 * Implementation of hook_access
12 */
13 function MAQUM_access($op, $node) {
14 if ($op == 'create') {
15 return user_access('create MAQUM nodes');
16 }
17 } // MAQUM_access
18
19 /**
20 * Implementation of hook_form
21 */
22 function MAQUM_form(&$node, &$param) {
23 _image_check_settings();
24
25 // Set form parameters so we can accept file uploads.
26 $form['#attributes'] = array("enctype" => "multipart/form-data");
27
28 $sizes = _image_get_sizes();
29 $form['images']['#tree'] = TRUE;
30
31 $form['images']['_original'] = array('#type' => 'hidden', '#value' => $node->images['_original']);
32 foreach ($sizes as $size) {
33 $form['images'][$size['label']] = array('#type' => 'hidden', '#value' => $node->images[$size['label']]);
34 }
35
36 $form['thumbnail']['#after_build'] = array('image_form_add_thumbnail');
37 $form['image'] = array('#type' => 'file', '#title' => t("Image"), '#description' => t("Click \"Browse...\" to select an image to upload."));
38
39 return $form;
40 } // MAQUM_form
41
42 /**
43 * Implementation of hook_help
44 */
45 function MAQUM_help($section) {
46 switch ($section) {
47 // This function is no longer needed here but is still useful
48 // for displaying help on pages so I kept it here
49 }
50 } // MAQUM_help
51
52 /**
53 * Implementation of hook_insert
54 */
55 function MAQUM_insert($node) {
56 image_insert($node);
57 } // MAQUM_insert
58
59 /**
60 * Implementation of hook_menu
61 */
62 function MAQUM_menu($may_cache) {
63 $items = array();
64 if ($may_cache) {
65 $items[] = array('path' => 'node/add/maqum',
66 'title' => t('MAQUM'),
67 'access' => user_access('create MAQUM nodes'),
68 );
69 $items[] = array('path' => 'admin/settings/maqum',
70 'title' => t('MAQUM Settings'),
71 'description' => t('Change settings for MAQUM module here.'),
72 'access' => user_access('create MAQUM nodes'),
73 'callback' => 'drupal_get_form',
74 'callback arguments' => array('MAQUM_settings'),
75 );
76 }
77 return $items;
78 } // MAQUM_menu
79
80 /**
81 * Implementation of hook_node_info
82 */
83 function MAQUM_node_info() {
84 return array('MAQUM' => array('name' => t('MAQUM'), 'module' => 'MAQUM', 'description' => t('Create an image node with EXIF and IPTC metadata.')));
85 } // MAQUM_node_info
86
87 /**
88 * Implementation of hook_perm
89 */
90 function MAQUM_perm() {
91 return array('create MAQUM nodes');
92 } // MAQUM_perm
93
94 /**
95 * Implementation of hook_prepare
96 */
97 function MAQUM_prepare(&$node, $field_name) {
98 image_prepare(&$node, $field_name);
99 } // MAQUM_prepare
100
101 /**
102 * Implementation of hook_settings
103 */
104 function MAQUM_settings() {
105 if (!MAQUM_VERSION) drupal_set_message(t("Exiftool not found - please check your path."), 'error');
106 foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
107 $options[$vid] = $vocab->name;
108 }
109 $form['MAQUM_exiftool'] = array(
110 '#type' => 'item',
111 '#value' => t("Exiftool version: <strong>%version</strong>", array('%version' => MAQUM_VERSION)),
112 '#description' => t("If this version number is blank then your installation path is incorrect"),
113 );
114 $form['MAQUM_path'] = array(
115 '#type' => 'textfield',
116 '#title' => t("Path to exiftool"),
117 '#default_value' => variable_get('MAQUM_path', '/usr/bin/'),
118 '#description' => t("Including trailing slash, eg. /usr/bin/"),
119 '#attributes' => !MAQUM_VERSION ? array('class' => 'error') : '',
120 );
121 $form['MAQUM_args'] = array(
122 '#type' => 'textarea',
123 '#title' => t("exiftool arguments"),
124 '#default_value' => variable_get('MAQUM_args', '-ObjectName -CreateDate -Caption-Abstract -ShutterSpeed -Aperture -ISO -FocalLength -ExposureProgram -MeteringMode -Flash -WhiteBalance -Country-PrimaryLocationName -Province-State -City -Keywords -Make -Model'),
125 '#description' => t("Including dashes, eg. -ObjectName -CreateDate -Caption-Abstract -ShutterSpeed -Aperture -ISO"),
126 '#cols' => 50,
127 '#rows' => 1,
128 );
129 $form['group1'] = array('#type' => 'fieldset', '#title' => t("Automation Settings:"), '#collapsible' => TRUE, '#collapsed' => FALSE);
130 $form['group1']['MAQUM_title'] = array(
131 '#type' => 'textfield',
132 '#title' => t("IPTC field to use for node title"),
133 '#default_value' => variable_get('MAQUM_title', 'Object Name'),
134 '#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"),
135 );
136 $form['group1']['MAQUM_date'] = array(
137 '#type' => 'textfield',
138 '#title' => t("IPTC field to use for node creation timestamp"),
139 '#default_value' => variable_get('MAQUM_date', 'Create Date'),
140 '#description' => t("This must match the IPTC fieldname EXACTLY"),
141 );
142 $form['group1']['MAQUM_body'] = array(
143 '#type' => 'textfield',
144 '#title' => t("IPTC field to use for node body"),
145 '#default_value' => variable_get('MAQUM_body', 'Caption-Abstract'),
146 '#description' => t("This must match the IPTC fieldname EXACTLY"),
147 );
148 $form['group1']['MAQUM_exif'] = array(
149 '#type' => 'textarea',
150 '#title' => t("A list of EXIF tags to use for your taxonomies"),
151 '#default_value' => variable_get('MAQUM_exif', "Shutter Speed\nAperture\nISO\nFocal Length"),
152 '#description' => t("Please separate each tag by a newline"),
153 '#cols' => 50,
154 '#rows' => 25,
155 );
156
157 return system_settings_form($form);
158 } // MAQUM_settings
159
160 /**
161 * Implementation of hook_submit
162 */
163 function MAQUM_submit(&$node) {
164 $node->type = 'image';
165 $info = _MAQUM_exiftool($node->images['_original']);
166 if (!$info) {
167 drupal_set_message(t('Error using exiftool.'));
168 watchdog('MAQUM', t("MAQUM: error using exiftool."), WATCHDOG_ERROR);
169 return;
170 }
171
172 $test = _MAQUM_set_metadata(&$node, $info);
173 if (!$test) {
174 drupal_set_message(t('Error setting taxonomy.'));
175 watchdog('MAQUM', t("MAQUM: error setting taxonomy."), WATCHDOG_ERROR);
176 }
177
178 if ($info[variable_get('MAQUM_title', 'Object Name')]) {
179 $node->title = $info[variable_get('MAQUM_title', 'Object Name')];
180 }
181 else {
182 $node->title = basename($node->images['_original']);
183 }
184 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')]));
185 if ($info[variable_get('MAQUM_body', 'Caption-Abstract')]) $node->body = $info[variable_get('MAQUM_body', 'Caption-Abstract')];
186 } // MAQUM_submit
187
188 /**
189 * MODULE CUSTOM FUNCTIONS
190 */
191
192 /**
193 * Creates a new term set in your default vocabulary
194 */
195 function _MAQUM_create_term($name, $vid = NULL) {
196 if (!$vid) $vid = variable_get('MAQUM_vid', '1');
197 $output->tid = db_next_id('{term_data}_tid');
198 $output->name = $name;
199 $output->vid = $vid;
200 db_query("INSERT INTO {term_data} (tid, name, vid) VALUES (%d, '%s', %d)", $output->tid, $output->name, $output->vid);
201 db_query("INSERT INTO {term_hierarchy} (tid) VALUES (%d)", $output->tid);
202 return array($output);
203 } // _MAQUM_create_term
204
205 /**
206 * Creates a new vocabulary
207 */
208 function _MAQUM_create_vocabulary($name) {
209 $vid = db_next_id('{vocabulary}_vid');
210 db_query("INSERT INTO {vocabulary} (vid, name, hierarchy, multiple, module) VALUES (%d, '%s', %d, %d, '%s')", $vid, $name, '1', '1', 'taxonomy');
211 db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vid, 'image');
212 return $vid;
213 } // _MAQUM_create_vocabulary
214
215 /**
216 * Returns the exiftool information as an array
217 */
218 function _MAQUM_exiftool($path) {
219 $temp = shell_exec(variable_get('MAQUM_path', '/usr/bin/').'exiftool '.variable_get('MAQUM_args', '-ObjectName -CreateDate -Caption-Abstract -ShutterSpeed -Aperture -ISO -FocalLength -ExposureProgram -MeteringMode -Flash -WhiteBalance -Country-PrimaryLocationName -Province-State -City -Keywords -Make -Model').' '.escapeshellarg($path));
220 $temp = explode("\n", $temp);
221 foreach ($temp as $item) {
222 $pos = strpos($item, ':');
223 $info[trim(substr($item, 0, $pos))] = trim(substr($item, $pos+1));
224 }
225 return $info;
226 } // _MAQUM_exiftool
227
228 /**
229 * Creates the vocabularies (if required) and sets the terms
230 */
231 function _MAQUM_set_metadata(&$node, $info) {
232 $taxonomy = $node->taxonomy;
233 $items = explode("\n", variable_get('MAQUM_exif', "Shutter Speed\nAperture\nISO\nFocal Length"));
234 foreach ($items as $item) {
235 $item = trim($item);
236 if (isset($info[$item])) {
237 unset($vid);
238 unset($term);
239 $fetch = db_fetch_object(db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $item));
240 $vid = $fetch->vid;
241 if (!$vid) $vid = _MAQUM_create_vocabulary($item);
242 $prefix = NULL;
243 switch ($item) {
244 case "Keywords":
245 $keywords = explode("," , $info[$item]);
246 foreach ($keywords as $keyword) {
247 $keyword = $prefix.trim($keyword);
248 $term = taxonomy_get_term_by_name($keyword);
249 if (!$term) $term = _MAQUM_create_term($keyword, $vid);
250 $taxonomy[$term[0]->tid] = $term[0];
251 }
252 break;
253 default:
254 $term = taxonomy_get_term_by_name($prefix.$info[$item]);
255 if (!$term) $term = _MAQUM_create_term($prefix.$info[$item], $vid);
256 $taxonomy[$term[0]->tid] = $term[0];
257 } // end switch
258 }
259 }
260 if (isset($taxonomy)) {
261 $node->taxonomy = $taxonomy;
262 return TRUE;
263 }
264 } // _MAQUM_set_metadata

  ViewVC Help
Powered by ViewVC 1.1.2