| 1 |
<?php
|
| 2 |
// $Id: flashfield.module,v 1.8 2009/04/26 06:25:32 stuartgreenfield Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* flashfield core hooks and menu callbacks.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_init().
|
| 11 |
*/
|
| 12 |
function flashfield_init() {
|
| 13 |
|
| 14 |
// Load widget functions
|
| 15 |
module_load_include('inc', 'flashfield', 'flashfield_widget');
|
| 16 |
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_theme().
|
| 21 |
*/
|
| 22 |
function flashfield_theme() {
|
| 23 |
return array(
|
| 24 |
// Theme an image uploaded to flashfield with alt and title.
|
| 25 |
// TODO: Switch to core theme image if possible.
|
| 26 |
'flashfield_image' => array(
|
| 27 |
'arguments' => array('file' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE),
|
| 28 |
),
|
| 29 |
// Theme an flashfield field item. It calls imagefied_image with the proper
|
| 30 |
// item properties as arguments.
|
| 31 |
'flashfield_item' => array(
|
| 32 |
'arguments' => array('item' => NULL),
|
| 33 |
),
|
| 34 |
// flashfield_widget form element type theme function.
|
| 35 |
'flashfield_widget' => array(
|
| 36 |
'arguments' => array('element' => NULL),
|
| 37 |
'file' => 'flashfield_widget.inc',
|
| 38 |
),
|
| 39 |
// Use to generate a preview (admin view) of an flashfield item for use in
|
| 40 |
// field item forms and filefield widgets. Invoked by filefield_widget_process.
|
| 41 |
'flashfield_widget_preview' => array(
|
| 42 |
'arguments' => array('item' => NULL),
|
| 43 |
),
|
| 44 |
// Theme function for the field item elements. allows you to place children
|
| 45 |
// within the context of the parent.
|
| 46 |
'flashfield_widget_item' => array(
|
| 47 |
'arguments' => array('element' => NULL),
|
| 48 |
),
|
| 49 |
// Generates and img tag to the admin thumbnail of an flashfield upload.
|
| 50 |
'flashfield_admin_thumbnail' => array(
|
| 51 |
'arguments' => array('item' => NULL),
|
| 52 |
),
|
| 53 |
);
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_elements().
|
| 58 |
*/
|
| 59 |
function flashfield_elements() {
|
| 60 |
|
| 61 |
// A flashfield is really just a filefield but with some extra processing.
|
| 62 |
$filefield_elements = filefield_elements();
|
| 63 |
$elements['flashfield_widget'] = $filefield_elements['filefield_widget'];
|
| 64 |
$elements['flashfield_widget']['#process'][] = 'flashfield_widget_process';
|
| 65 |
$elements['flashfield_widget']['#element_validate'][] = 'flashfield_widget_validate';
|
| 66 |
$elements['flashfield_widget']['#value_callback'] = 'flashfield_widget_value';
|
| 67 |
|
| 68 |
// Return elements
|
| 69 |
return $elements;
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
///**
|
| 74 |
// * Implementation of hook_file_download.
|
| 75 |
// */
|
| 76 |
//function flashfield_file_download($filepath) {
|
| 77 |
// // Return headers for admin thumbnails if private files are enabled.
|
| 78 |
// if (strpos($filepath, 'flashfield_thumbs') !== FALSE) {
|
| 79 |
// $original_path = str_replace('flashfield_thumbs/', '', $filepath);
|
| 80 |
// $original_full_path = file_create_path($original_path);
|
| 81 |
// $thumb_full_path = file_create_path($filepath);
|
| 82 |
//
|
| 83 |
// // Allow access to temporary thumbnails, since they're not yet associated
|
| 84 |
// // with a node. If not temporary, check access on the original file.
|
| 85 |
// $status = db_result(db_query("SELECT status FROM {files} WHERE filepath = '%s'", $original_full_path));
|
| 86 |
// $access = ($status == 0 || module_invoke_all('file_download', $original_path));
|
| 87 |
// if ($access && $info = getimagesize($thumb_full_path)) {
|
| 88 |
// return array(
|
| 89 |
// 'Content-Type: ' . $info['mime'],
|
| 90 |
// 'Content-Length: ' . filesize($thumb_full_path)
|
| 91 |
// );
|
| 92 |
// }
|
| 93 |
// }
|
| 94 |
//
|
| 95 |
// // Return headers for default images.
|
| 96 |
// if (strpos($filepath, 'flashfield_default_images') !== FALSE) {
|
| 97 |
// $full_path = file_create_path($filepath);
|
| 98 |
// if ($info = getimagesize($full_path)) {
|
| 99 |
// return array(
|
| 100 |
// 'Content-Type: ' . $info['mime'],
|
| 101 |
// 'Content-Length: ' . filesize($full_path)
|
| 102 |
// );
|
| 103 |
// }
|
| 104 |
// }
|
| 105 |
//}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Implementation of CCK's hook_widget_info().
|
| 109 |
*/
|
| 110 |
function flashfield_widget_info() {
|
| 111 |
$module_path = drupal_get_path('module', 'flashfield');
|
| 112 |
return array(
|
| 113 |
'flashfield_widget' => array(
|
| 114 |
'label' => t('Flash'),
|
| 115 |
'field types' => array('filefield'),
|
| 116 |
'multiple values' => CONTENT_HANDLE_CORE,
|
| 117 |
'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
|
| 118 |
'description' => t('An edit widget for flash files.'),
|
| 119 |
),
|
| 120 |
);
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Implementation of CCK's hook_widget_settings().
|
| 125 |
*/
|
| 126 |
function flashfield_widget_settings($op, $widget) {
|
| 127 |
switch ($op) {
|
| 128 |
case 'form':
|
| 129 |
return flashfield_widget_settings_form($widget);
|
| 130 |
case 'validate':
|
| 131 |
return flashfield_widget_settings_validate($widget);
|
| 132 |
case 'save':
|
| 133 |
return flashfield_widget_settings_save($widget);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Implementation of CCK's hook_widget().
|
| 139 |
*
|
| 140 |
* Assign default properties to item and delegate to FileField.
|
| 141 |
*/
|
| 142 |
function flashfield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
|
| 143 |
// Add default values to items.
|
| 144 |
// TODO: use CCK's default value callback.
|
| 145 |
if (empty($items[$delta])) {
|
| 146 |
$items[$delta] = array(
|
| 147 |
'height' => '',
|
| 148 |
'width' => '',
|
| 149 |
'alt' => '',
|
| 150 |
'flashvars' => '',
|
| 151 |
'base' => '',
|
| 152 |
'params' => '',
|
| 153 |
);
|
| 154 |
}
|
| 155 |
|
| 156 |
// Start with the FileField widget as a basic start.
|
| 157 |
// Note that FileField needs to modify $form by reference.
|
| 158 |
$element = filefield_widget($form, $form_state, $field, $items, $delta);
|
| 159 |
|
| 160 |
// Add flashfield specific validators.
|
| 161 |
$element['#upload_validators'] = array_merge($element['#upload_validators'], flashfield_widget_upload_validators($field));
|
| 162 |
|
| 163 |
return $element;
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Get the additional upload validators for an image field.
|
| 168 |
*
|
| 169 |
* @param $field
|
| 170 |
* The CCK field array.
|
| 171 |
* @return
|
| 172 |
* An array suitable for passing to file_save_upload() or the file field
|
| 173 |
* element's '#upload_validators' property.
|
| 174 |
*/
|
| 175 |
function flashfield_widget_upload_validators($field) {
|
| 176 |
$validators = array();
|
| 177 |
|
| 178 |
// Ensure that only web images are supported.
|
| 179 |
$web_extensions = array('swf', 'flv', 'mp3' ,'jpg', 'jpeg', 'gif', 'png');
|
| 180 |
$extensions = array_filter(explode(' ', $field['widget']['file_extensions']));
|
| 181 |
if (empty($extensions)) {
|
| 182 |
$extensions = $web_extensions;
|
| 183 |
}
|
| 184 |
$validators['filefield_validate_extensions'][0] = implode(' ', array_intersect($extensions, $web_extensions));
|
| 185 |
|
| 186 |
return $validators;
|
| 187 |
}
|
| 188 |
|
| 189 |
|
| 190 |
/**
|
| 191 |
* Implementation of CCK's hook_default_value().
|
| 192 |
*/
|
| 193 |
function flashfield_default_value(&$form, &$form_state, $field, $delta) {
|
| 194 |
return filefield_default_value($form, $form_state, $field, $delta);
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Implementation of hook_form_[form_id]_alter().
|
| 199 |
*
|
| 200 |
* Modify the add new field form to make SWF Tools formatter the default formatter.
|
| 201 |
*/
|
| 202 |
function flashfield_form_content_field_overview_form_alter(&$form, &$form_state) {
|
| 203 |
$form['#submit'][] = 'flashfield_form_content_field_overview_submit';
|
| 204 |
}
|
| 205 |
|
| 206 |
|
| 207 |
/**
|
| 208 |
* Submit handler to set a new field's formatter to SWF Tools basic formatter
|
| 209 |
*/
|
| 210 |
function flashfield_form_content_field_overview_submit(&$form, &$form_state) {
|
| 211 |
if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
|
| 212 |
$new_field = $form_state['fields_added']['_add_new_field'];
|
| 213 |
$node_type = $form['#type_name'];
|
| 214 |
$field = content_fields($new_field, $node_type);
|
| 215 |
if ($field['widget']['module'] == 'flashfield') {
|
| 216 |
foreach ($field['display_settings'] as $display_type => $display_settings) {
|
| 217 |
if ($field['display_settings'][$display_type]['format'] == 'default') {
|
| 218 |
$field['display_settings'][$display_type]['format'] = 'swftools_no_file';
|
| 219 |
}
|
| 220 |
}
|
| 221 |
content_field_instance_update($field);
|
| 222 |
}
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
| 226 |
|
| 227 |
/**
|
| 228 |
* @defgroup "Theme Callbacks"
|
| 229 |
* @{
|
| 230 |
* @see flashfield_theme().
|
| 231 |
*/
|
| 232 |
function theme_flashfield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
|
| 233 |
$file = (array)$file;
|
| 234 |
if (!is_file($file['filepath'])) {
|
| 235 |
return '<!-- File not found: '. $file['filepath'] .' -->';
|
| 236 |
}
|
| 237 |
|
| 238 |
if ($getsize && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath']))) {
|
| 239 |
$attributes['width'] = $width;
|
| 240 |
$attributes['height'] = $height;
|
| 241 |
}
|
| 242 |
|
| 243 |
if (!empty($title)) {
|
| 244 |
$attributes['title'] = $title;
|
| 245 |
}
|
| 246 |
|
| 247 |
// Alt text should be added even if it is an empty string.
|
| 248 |
$attributes['alt'] = $alt;
|
| 249 |
|
| 250 |
// Add a timestamp to the URL to ensure it is immediately updated after editing.
|
| 251 |
$query_string = '';
|
| 252 |
if (isset($file['timestamp'])) {
|
| 253 |
$query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?';
|
| 254 |
$query_string = $query_character . $file['timestamp'];
|
| 255 |
}
|
| 256 |
|
| 257 |
$url = file_create_url($file['filepath']) . $query_string;
|
| 258 |
$attributes['src'] = $url;
|
| 259 |
$attributes = drupal_attributes($attributes);
|
| 260 |
return '<img '. $attributes .' />';
|
| 261 |
}
|
| 262 |
|
| 263 |
function theme_flashfield_item($item) {
|
| 264 |
return theme('flashfield_image', $item, $item['alt'], $item['title']);
|
| 265 |
}
|
| 266 |
|
| 267 |
function theme_flashfield_widget_preview($item = NULL) {
|
| 268 |
return '<div class="flashfield-preview">' . theme('flashfield_admin_thumbnail', $item) . '</div>';
|
| 269 |
}
|
| 270 |
|
| 271 |
function theme_flashfield_widget_item($element) {
|
| 272 |
return theme('filefield_widget_item', $element);
|
| 273 |
}
|
| 274 |
|
| 275 |
function theme_flashfield_admin_thumbnail($item = NULL) {
|
| 276 |
if (is_null($item) || empty($item['filepath'])) {
|
| 277 |
return '<!-- link to default admin thumb -->';
|
| 278 |
}
|
| 279 |
$thumb_path = flashfield_file_admin_thumb_path($item);
|
| 280 |
return '<img src="'. file_create_url($thumb_path) .'" />';
|
| 281 |
}
|
| 282 |
/**
|
| 283 |
* @} End defgroup "Theme Callbacks".
|
| 284 |
*/
|