View source
<?php
function flashnode_help($section) {
switch ($section) {
case 'admin/settings/flashnode':
return t('Flash node lets you create nodes that store a piece of Flash animation. You can use it in a basic mode, where the Flash item is always displayed at the start of the node, or you can enable the flash filter to incorporate Flash content in to other nodes.');
case 'admin/help#flashnode':
return t('
<p>Flash node lets you create nodes that store a piece of <a href="!flashurl">Flash</a> animation. You can use it in a basic mode, where the Flash item is always displayed at the start of the node, but you can choose whether to have the Flash element displayed in both the teaser and the body, just the teaser, or just the body. You can define the height and width of the Flash element, or you can let the node use the Flash content\'s original settings.</p>
<p>The module also defines a new input filter called <strong>flash</strong> that you can use. This lets you re-use Flash content in other nodes by using the format <strong>[flash|nid=<nid>]</strong> in the body of a node. You can pass optional parameters to manipulate the display of the Flash content by including them in the macro. Allowable parameters are:
<ul><li>
<strong>width</strong> - set a specific width, in pixels
</li><li>
<strong>height</strong> - set a specific height, in pixels
</li><li>
<strong>scale</strong> - scale both width and height to a multiple of the original size
</li><li>
<strong>xscale</strong> - scale just the width to a multiple of the original size
</li><li>
<strong>yscale</strong> - scale just the height to a multiple of the original size
</li><li>
<strong>scalewidth</strong> - set a specific width, in pixels, and automatically adjust the height to maintain the aspect ratio
</li><li>
<strong>scaleheight</strong> - set a specific height, in pixels, and automatically adjust the width to maintain the aspect ratio
</li><li>
<strong>class</strong> - create the container div with a specific CSS class (the default class is <em>flash</em>)
</li></ul>
For example, to use Flash content from node 10, scaled to 50% of its original size, and with CSS class flash-left you would use <strong>[flash|nid=10|scale=0.5|class=flash-left]</strong></p>
<p>Flash content is inserted using the method that you specify in SWFTools.');
}
}
function flashnode_node_info() {
return array(
'flashnode' => array(
'name' => t('Flash'),
'module' => 'flashnode',
'description' => t('Allows you to easily upload and display a Flash file. You can choose whether the animation appears in the teaser, the body, or both.'),
),
);
}
function flashnode_perm() {
return array(
'create flash nodes',
'edit own flash nodes',
'administer flash node',
);
}
function flashnode_access($op, $node) {
global $user;
if ($op == 'create' && user_access('create flash nodes')) {
return TRUE;
}
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own flash nodes') && $user->uid == $node->uid) {
return TRUE;
}
}
}
function flashnode_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'node/add/flashnode',
'title' => t('Flash'),
'access' => user_access('create flash nodes'),
);
$items[] = array(
'path' => 'admin/media/swf/flashnode',
'title' => 'Flash node',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'flashnode_admin_settings',
),
'access' => user_access('administer flash node'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Set the directory where flash files are uploaded to.'),
);
}
return $items;
}
function flashnode_cron() {
$path = variable_get('flashnode_default_path', 'flash') . '/temp';
$files = file_scan_directory(file_create_path($path), '.*');
foreach ($files as $file => $info) {
if (time() - filemtime($file) > 60 * 60 * 6) {
file_delete($file);
}
}
}
function flashnode_prepare(&$node) {
$field_name = 'flashfile';
if ($file = file_check_upload($field_name)) {
$file = file_save_upload($field_name, _flashnode_filename($file->filename, TRUE));
if ($file) {
if (strtolower($file->filemime) != 'application/x-shockwave-flash') {
watchdog($field_name, t('Flash node was given %type for upload.', array(
'%type' => $node->file->filemime,
)));
form_set_error($field_name, t('The specified file is not a valid Flash format.'));
file_delete($file->filepath);
return;
}
}
else {
return;
}
$node->flashnode['_flashnode'] = $file->filepath;
$node->flashnode['filemime'] = $file->filemime;
$node->new_file = TRUE;
}
$info = image_get_info(file_create_path($node->flashnode['_flashnode']));
$node->flashnode['_height'] = $info['height'];
$node->flashnode['_width'] = $info['width'];
}
function flashnode_validate(&$node, $form) {
if (empty($form['flashfile']['#value']) && empty($form['flashnode']['_flashnode']['#value'])) {
form_set_error('flashfile', t('You must specify a Flash file to upload.'));
}
if (!is_numeric($form['flashnode']['options']['height']['#value']) && !empty($form['flashnode']['options']['height']['#value'])) {
form_set_error('flashnode][height', t('You must enter a valid height.'));
}
if (!is_numeric($form['flashnode']['options']['width']['#value']) && !empty($form['flashnode']['options']['width']['#value'])) {
form_set_error('flashnode][width', t('You must enter a valid width.'));
}
}
function flashnode_form(&$node, &$param) {
_flashnode_check_settings();
$form['#attributes'] = array(
"enctype" => "multipart/form-data",
);
$form['flashnode']['#tree'] = TRUE;
if ($node->new_file) {
$form['new_file'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
if ($node->new_file) {
$form['flashnode']['_flashnode'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_flashnode'],
);
$form['flashnode']['filemime'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['filemime'],
);
$form['flashnode']['_height'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_height'],
);
$form['flashnode']['_width'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_width'],
);
}
else {
$form['flashnode']['_flashnode'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_flashnode'],
);
$form['flashnode']['filemime'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['filemime'],
);
$form['flashnode']['_height'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_height'],
);
$form['flashnode']['_width'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_width'],
);
}
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5,
);
}
if ($type->has_body) {
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#rows' => 20,
'#required' => $type->min_word_count > 0,
);
$form['body_filter']['format'] = filter_form($node->format);
}
$form['flashnode']['flashfile'] = array(
'#type' => 'file',
'#title' => t('Flash file'),
'#description' => t('Click "Browse..." to select a Flash file to upload.'),
'#tree' => FALSE,
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options1'] = array(
'#type' => 'fieldset',
'#title' => t('Basic flash node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['flashnode']['options1']['display'] = array(
'#type' => 'radios',
'#title' => t('Display in'),
'#default_value' => $node->flashnode['display'] ? $node->flashnode['display'] : 0,
'#options' => array(
0 => t('Teaser and body'),
1 => t('Teaser only'),
2 => t('Body only'),
),
'#parents' => array(
'flashnode',
'display',
),
);
$form['flashnode']['options1']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $node->flashnode['width'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The width of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#parents' => array(
'flashnode',
'width',
),
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options1']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $node->flashnode['height'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The height of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#parents' => array(
'flashnode',
'height',
),
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options2'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced flash node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['flashnode']['options2']['substitution'] = array(
'#type' => 'textarea',
'#title' => t('Substitution content'),
'#rows' => 5,
'#default_value' => $node->flashnode['substitution'],
'#parents' => array(
'flashnode',
'substitution',
),
'#description' => t('If a javascript method is used to embed flash then this is the content that users will see if they are unable to, or choose not to, display the flash content. This content uses the same input format as the body. The default content may be used by entering @default.', array(
'@default' => '!default',
)),
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options2']['flashvars'] = array(
'#type' => 'textarea',
'#title' => t('Flashvars'),
'#rows' => 5,
'#default_value' => $node->flashnode['flashvars'],
'#parents' => array(
'flashnode',
'flashvars',
),
'#description' => t('Specify any flashvars that need to be passed to the movie. If the input format allows PHP code you may use PHP to create a dynamic flashvars string.'),
);
$form['flashnode']['options2']['base'] = array(
'#type' => 'textfield',
'#title' => t('Base'),
'#default_value' => $node->flashnode['base'] ? $node->flashnode['base'] : variable_get('flashnode_default_base', './' . file_directory_path()),
'#parents' => array(
'flashnode',
'base',
),
'#description' => t('Over-ride the default setting with a different base path here if necessary, e.g. if migrating existing content. This setting is only needed for movies that use the %loadmovie command with relative paths.', array(
'%loadmovie' => 'loadMovie()',
)),
);
return $form;
}
function flashnode_load(&$node) {
$result = db_query("SELECT filepath FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, '_flashnode');
$node->flashnode['_flashnode'] = db_result($result);
$result = db_query("SELECT height, width, display, substitution, flashvars, base FROM {flashnode} WHERE nid=%d", $node->nid);
$settings = db_fetch_object($result);
foreach ($settings as $parameter => $value) {
$node->flashnode[$parameter] = $value;
}
}
function flashnode_insert($node) {
$fid = _flashnode_insert($node->nid, file_create_path($node->flashnode['_flashnode']), $node->flashnode['filemime']);
if ($fid) {
db_query("INSERT INTO {flashnode} (nid, height, width, display, substitution, flashvars, base) VALUES (%d, %d, %d, %d, '%s', '%s', '%s')", $node->nid, $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base']);
}
}
function flashnode_update($node) {
$old_path = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid));
if ($old_path != $node->flashnode['_flashnode']) {
file_delete(file_create_path($old_path));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid);
db_query("DELETE FROM {flashnode} WHERE nid=%d", $node->nid);
flashnode_insert($node);
}
else {
db_query("UPDATE {flashnode} SET height=%d, width=%d, display=%d, substitution='%s', flashvars='%s', base='%s' WHERE nid=%d", $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base'], $node->nid);
}
cache_clear_all('*', 'cache_filter', true);
}
function flashnode_delete($node) {
file_delete(file_create_path($node->flashnode[_flash]));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid);
db_query("DELETE FROM {flashnode} WHERE nid=%d", $node->nid);
}
function flashnode_view($node, $teaser, $page = 0) {
$node = node_prepare($node, $teaser);
$node->flashnode['substitution'] = check_markup($node->flashnode['substitution'], $node->format, FALSE);
$node->flashnode['flashvars'] = _flashnode_php_flashvars($node->flashnode['flashvars'], $node->format);
$flash = flashnode_display($node->flashnode);
if ($node->flashnode['display'] != 2 && $teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
if ($node->flashnode['display'] != 1 && !$teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
return $node;
}
function flashnode_display($file, $options = NULL) {
$max_width = variable_get('flashnode_max_width', 0);
$max_height = variable_get('flashnode_max_height', 0);
if ($max_width) {
if ($file['width'] > $max_width) {
$scale = $max_width / $file['width'];
$file['width'] = $file['width'] * $scale;
$file['height'] = $file['height'] * $scale;
}
}
if ($max_height) {
if ($file['height'] > $max_height) {
$scale = $max_height / $file['height'];
$file['width'] = $file['width'] * $scale;
$file['height'] = $file['height'] * $scale;
}
}
$params = array(
'width' => round($file['width']),
'height' => round($file['height']),
'base' => $file['base'],
);
$preview = t($file['substitution'], array(
'!default' => variable_get('flashnode_default_html_alt', 'You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.'),
));
$filepath = $file['_flashnode'];
$output .= swf($filepath, $params, $file['flashvars'], array(
'html_alt' => $preview,
), SWFDEFAULT, 0);
return $output;
}
function flashnode_content($args = array(), $format = FILTER_FORMAT_DEFAULT) {
if (!is_numeric($args['nid'])) {
return;
}
$flash['_flashnode'] = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $args['nid']));
if (!$flash['_flashnode']) {
return;
}
$result = db_query("SELECT height, width, display, substitution, flashvars, base FROM {flashnode} WHERE nid=%d", $args['nid']);
$result = db_fetch_object($result);
foreach ($result as $parameter => $value) {
$flash[$parameter] = $value;
}
$parent_format = db_result(db_query('SELECT r.format FROM {node_revisions} r INNER JOIN {node} n ON n.vid = r.vid WHERE n.nid=%d', $args['nid']));
$flash['flashvars'] = _flashnode_php_flashvars($flash['flashvars'], $parent_format);
unset($args['nid']);
if ($args) {
foreach ($args as $parameter => $value) {
$xscale = $yscale = 1;
switch ($parameter) {
case 'scalewidth':
if (is_numeric($value)) {
$xscale = $yscale = $value / $flash['width'];
}
break;
case 'scaleheight':
if (is_numeric($value)) {
$xscale = $yscale = $value / $flash['height'];
}
break;
case 'xscale':
if (is_numeric($value)) {
$xscale = $value;
}
break;
case 'yscale':
if (is_numeric($value)) {
$yscale = $value;
}
break;
case 'scale':
if (is_numeric($value)) {
$xscale = $yscale = $value;
}
break;
case 'height':
case 'width':
if (is_numeric($value)) {
$flash[$parameter] = $value;
}
break;
case 'class':
$flash['class'] = $value;
break;
case 'flashvars':
$value = str_replace('&', '&', $value);
$flash['flashvars'] = $value;
$flash['flashvars'] = _flashnode_php_flashvars($flash['flashvars'], $format);
break;
case 'substitution':
$flash['substitution'] = $value;
break;
default:
$options[$parameter] = $value;
}
}
}
$flash['substitution'] = check_markup($flash['substitution'], $format, FALSE);
return flashnode_display($flash, $options);
}
function flashnode_get_macros($text) {
$m = array();
preg_match_all('/ \\[ ([^\\[\\]]+)* \\] /x', $text, $matches);
$tag_match = (array) array_unique($matches[1]);
foreach ($tag_match as $macro) {
$current_macro = '[' . $macro . ']';
$param = array_map('trim', explode('|', $macro));
$func_name = array_shift($param);
if ($func_name == 'flashnode') {
$vars = array();
foreach ($param as $p) {
$pos = strpos($p, '=');
$varname = substr($p, 0, $pos);
$varvalue = substr($p, $pos + 1);
$vars[$varname] = $varvalue;
}
$m[$current_macro] = $vars;
}
}
return $m;
}
function flashnode_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Flash node filter'),
);
case 'description':
return t('Add Flash from a flash node to your posts using a flash node macro.');
case 'process':
foreach (flashnode_get_macros($text) as $unexpanded_macro => $macro) {
$expanded_macro = flashnode_content($macro, $format);
$text = str_replace($unexpanded_macro, $expanded_macro, $text);
}
return $text;
default:
return $text;
}
}
function flashnode_filter_tips($delta, $format, $long = false) {
return t('Flash node macros can be added to this post.');
}
function flashnode_admin_settings() {
_flashnode_check_settings();
cache_clear_all('*', 'cache_filter', true);
$form['flashnode_updated'] = array(
'#type' => 'hidden',
'#value' => time(),
);
$form['flashnode_default_path'] = array(
'#type' => 'textfield',
'#title' => t('Default flash path'),
'#default_value' => variable_get('flashnode_default_path', 'flash'),
'#description' => t('Subdirectory in the directory "%dir" where Flash files will be stored. Do not include a leading or trailing slash.', array(
'%dir' => variable_get('file_directory_path', 'files'),
)),
);
$form['flashnode_default_html_alt'] = array(
'#type' => 'textarea',
'#rows' => 5,
'#title' => t('Default substitution content'),
'#default_value' => variable_get('flashnode_default_html_alt', 'You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.'),
'#description' => t('If you are using a javascript method to embed flash then this is the content that users will see if they are unable to, or choose not to, display the flash content. Use this content in a node by entering %default in the substitution field when creating a flash node. Note that this content is NOT filtered when it is displayed in a node so you may use mark-up that would not otherwise be allowed.', array(
'%default' => '!default',
)),
);
$form['flashnode_default_base'] = array(
'#type' => 'textfield',
'#title' => t('Default base parameter'),
'#default_value' => variable_get('flashnode_default_base', './' . file_directory_path()),
'#description' => t('If you use the ActionScript command %loadmovie or similar then you may need to use this setting to tell the Flash player where to find supporting movies. The base path is used for any relative paths that occur in the movie file. If you upload files using Drupal\'s upload module then the default setting of %base should be used. Only change it if you are uploading files to a different directory using something like FTP. This setting can be over-ridden when a node is created.', array(
'%loadmovie' => 'loadMovie()',
'%base' => './' . file_directory_path(),
)),
);
$form['flashnode_max_width'] = array(
'#type' => 'textfield',
'#title' => t('Maximum displayed width'),
'#default_value' => variable_get('flashnode_max_width', 0),
'#description' => t('The maximum displayed width of a flash movie can be limited by entering a non-zero value here. If the movie width is greater than this width then the movie will be scaled down when it is displayed. A value of zero means that no scaling will occur. This setting can be useful to ensure that the page layout is not disrupted by a large flash movie.'),
);
$form['flashnode_max_height'] = array(
'#type' => 'textfield',
'#title' => t('Maximum displayed height'),
'#default_value' => variable_get('flashnode_max_height', 0),
'#description' => t('The maximum displayed height of a flash movie can be limited by entering a non-zero value here. If the movie height is greater than this height then the movie will be scaled down when it is displayed. A value of zero means that no scaling will occur. This setting can be useful to ensure that the page layout is not disrupted by a large flash movie.'),
);
return system_settings_form($form);
}
function flashnode_admin_settings_validate($form_id, $form_values) {
if (!is_numeric($form_values['flashnode_max_width']) || $form_values['flashnode_max_width'] < 0) {
form_set_error('flashnode_max_width', t('The maximum displayed width must be zero or a positive number.'));
}
if (!is_numeric($form_values['flashnode_max_height']) || $form_values['flashnode_max_height'] < 0) {
form_set_error('flashnode_max_height', t('The maximum displayed height must be zero or a positive number.'));
}
}
function _flashnode_check_settings() {
$flashnode_path = file_create_path(variable_get('flashnode_default_path', 'flash'));
$temp_path = $flashnode_path . '/temp';
file_check_directory($flashnode_path, FILE_CREATE_DIRECTORY, 'flashnode_default_path');
file_check_directory($temp_path, FILE_CREATE_DIRECTORY, 'flashnode_default_path');
}
function _flashnode_filename($filename, $temp = FALSE) {
$result = variable_get('flashnode_default_path', 'flash') . '/';
if ($temp) {
$result .= 'temp/';
}
$result .= $filename;
return $result;
}
function _flashnode_insert($nid, $flash, $mime) {
$dest = _flashnode_filename(basename($flash));
if (file_move($flash, $dest)) {
$file->filename = '_flashnode';
$file->filepath = _flashnode_filename(basename($flash));
$file->filemime = $mime;
$file->filesize = filesize(file_create_path($dest));
$fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
return $fid;
}
}
function _flashnode_form_after_build($form, $form_values) {
if (!empty($form_values['flashnode']['_flashnode']) && $form['#parents'][0] == 'flashfile') {
$form['#description'] = t('Current file is %filename. Click "Browse..." to upload a different file.', array(
'%filename' => basename($form_values['flashnode']['_flashnode']),
));
}
if (empty($form_values['flashnode']['width']) && $form['#parents'][1] == 'width') {
$form['#value'] = $form_values['flashnode']['_width'];
form_set_value($form, $form_values['flashnode']['_width']);
}
if (empty($form_values['flashnode']['height']) && $form['#parents'][1] == 'height') {
$form['#value'] = $form_values['flashnode']['_height'];
form_set_value($form, $form_values['flashnode']['_height']);
}
if (empty($form_values['flashnode']['_flashnode']) && $form['#parents'][1] == 'substitution' && empty($form_values['flashnode']['substitution'])) {
$form['#value'] = '!default';
form_set_value($form, '!default');
}
return $form;
}
function _flashnode_php_flashvars($flashvars, $format = -1) {
$filters = filter_list_format($format);
foreach ($filters as $filter) {
if ($filter->module == 'filter' && $filter->delta == 1) {
$flashvars = module_invoke($filter->module, 'filter', 'process', $filter->delta, $format, $flashvars);
}
}
$flashvars = check_plain($flashvars);
$flashvars = str_replace('&', '&', $flashvars);
return $flashvars;
}