View source
<?php
function flash_help($section) {
switch ($section) {
case 'admin/settings/flash':
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#flash':
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 a technique called <a href="!ufourl">unobtrusive flash object</a>. This uses a small piece of javascript to put the Flash content on the page in a way that leaves the page compliant with XHTML standards. Older browsers, or devices that don\'t understand javascript or Flash, will see a piece of text explaining that the Flash content is missing.', array(
'!flashurl' => 'http://en.wikipedia.org/wiki/Macromedia_Flash',
'!ufourl' => 'http://www.bobbyvandersluis.com/ufo',
));
}
}
function flash_node_info() {
return array(
'flash' => array(
'name' => t('Flash'),
'module' => 'flash',
'description' => t('Allows you to upload a Flash file. You can choose whether the animation appears in the teaser, the body, or both.'),
),
);
}
function flash_perm() {
return array(
'create flash',
'edit own flash',
'administer flash',
);
}
function flash_access($op, $node) {
global $user;
if ($op == 'create' && user_access('create flash')) {
return TRUE;
}
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own flash') && $user->uid == $node->uid) {
return TRUE;
}
}
}
function flash_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'node/add/flash',
'title' => t('Flash'),
'access' => user_access('create flash'),
);
$items[] = array(
'path' => 'admin/settings/flash',
'title' => 'Flash node settings',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'flash_admin_settings',
),
'access' => user_access('administer flash'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Tell Drupal where to store Flash files, and specify the default minimum Flash player version.'),
);
}
return $items;
}
function flash_nodeapi(&$node, $op, $arg) {
switch ($op) {
case 'view':
static $ufo_added = FALSE;
if (!$ufo_added) {
$flash_script = drupal_get_path('module', 'flash') . '/ufo.js';
drupal_add_js($flash_script);
$ufo_added = TRUE;
}
}
}
function flash_cron() {
$path = variable_get('flash_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 flash_prepare(&$node, $field_name) {
if (is_null($field_name)) {
$field_name = 'flashfile';
}
if ($file = file_check_upload($field_name)) {
$file = file_save_upload($field_name, _flash_filename($file->filename, TRUE));
if ($file) {
if (strtolower($file->filemime) != 'application/x-shockwave-flash') {
watchdog($field_name, t('flash 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->flash['_flash'] = $file->filepath;
$node->flash['filemime'] = $file->filemime;
$node->new_file = TRUE;
}
$info = image_get_info(file_create_path($node->flash['_flash']));
$node->flash['_height'] = $info['height'];
$node->flash['_width'] = $info['width'];
}
function flash_validate(&$node, $form) {
if (empty($form['flashfile']['#value']) && empty($form['flash']['_flash']['#value'])) {
form_set_error('flashfile', t('You must specify a Flash file to upload.'));
}
$settings = array(
'height',
'width',
'version',
'build',
);
foreach ($settings as $label) {
if (!is_numeric($form['flash']['options'][$label]['#value']) && !empty($form['flash']['options'][$label]['#value'])) {
form_set_error('flash][' . $label, t('You must enter a valid @label.', array(
'@label' => $label,
)));
}
}
}
function flash_form(&$node, &$param) {
_flash_check_settings();
$form['#attributes'] = array(
"enctype" => "multipart/form-data",
);
$form['flash']['#tree'] = TRUE;
if ($node->new_file) {
$form['new_file'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
if ($node->new_file) {
$form['flash']['_flash'] = array(
'#type' => 'hidden',
'#value' => $node->flash['_flash'],
);
$form['flash']['filemime'] = array(
'#type' => 'hidden',
'#value' => $node->flash['filemime'],
);
$form['flash']['_height'] = array(
'#type' => 'hidden',
'#value' => $node->flash['_height'],
);
$form['flash']['_width'] = array(
'#type' => 'hidden',
'#value' => $node->flash['_width'],
);
}
else {
$form['flash']['_flash'] = array(
'#type' => 'hidden',
'#default_value' => $node->flash['_flash'],
);
$form['flash']['filemime'] = array(
'#type' => 'hidden',
'#default_value' => $node->flash['filemime'],
);
$form['flash']['_height'] = array(
'#type' => 'hidden',
'#default_value' => $node->flash['_height'],
);
$form['flash']['_width'] = array(
'#type' => 'hidden',
'#default_value' => $node->flash['_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['flash']['flashfile'] = array(
'#type' => 'file',
'#title' => t('Flash file'),
'#description' => t('Click "Browse..." to select a Flash file to upload.'),
'#tree' => FALSE,
'#after_build' => array(
'_flash_form_after_build',
),
);
$form['flash']['options'] = array(
'#type' => 'fieldset',
'#title' => t('Flash node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['flash']['options']['display'] = array(
'#type' => 'radios',
'#title' => t('Display in'),
'#default_value' => $node->flash['display'] ? $node->flash['display'] : 0,
'#options' => array(
0 => t('Teaser and body'),
1 => t('Teaser only'),
2 => t('Body only'),
),
'#parents' => array(
'flash',
'display',
),
);
$form['flash']['options']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $node->flash['width'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The width of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#after_build' => array(
'_flash_form_after_build',
),
'#parents' => array(
'flash',
'width',
),
);
$form['flash']['options']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $node->flash['height'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The height of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#after_build' => array(
'_flash_form_after_build',
),
'#parents' => array(
'flash',
'height',
),
);
$form['flash']['options']['version'] = array(
'#type' => 'textfield',
'#title' => t('Flash version required'),
'#default_value' => $node->flash['version'] ? $node->flash['version'] : variable_get('flash_version', 6),
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The minimum Flash version required to play this movie. Leave blank to reset to the default.'),
'#after_build' => array(
'_flash_form_after_build',
),
'#parents' => array(
'flash',
'version',
),
);
$form['flash']['options']['build'] = array(
'#type' => 'textfield',
'#title' => t('Flash build required'),
'#default_value' => $node->flash['build'] != '' ? $node->flash['build'] : variable_get('flash_build', 0),
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The minimum Flash build required to play this movie. Leave blank to reset to the default.'),
'#after_build' => array(
'_flash_form_after_build',
),
'#parents' => array(
'flash',
'build',
),
);
return $form;
}
function _flash_form_after_build($form, $form_values) {
if (!empty($form_values['flash']['_flash']) && $form['#parents'][0] == 'flashfile') {
$form['#description'] = t('Current file is %filename. Click "Browse..." to upload a different file.', array(
'%filename' => basename($form_values['flash']['_flash']),
));
}
if (empty($form_values['flash']['width']) && $form['#parents'][1] == 'width') {
$form['#value'] = $form_values['flash']['_width'];
form_set_value($form, $form_values['flash']['_width']);
}
if (empty($form_values['flash']['height']) && $form['#parents'][1] == 'height') {
$form['#value'] = $form_values['flash']['_height'];
form_set_value($form, $form_values['flash']['_height']);
}
if (empty($form_values['flash']['version']) && $form['#parents'][1] == 'version') {
$form['#value'] = variable_get('flash_version', 6);
form_set_value($form, variable_get('flash_version', 6));
}
if (empty($form_values['flash']['build']) && $form['#parents'][1] == 'build') {
$form['#value'] = variable_get('flash_build', 0);
form_set_value($form, variable_get('flash_build', 0));
}
return $form;
}
function flash_load(&$node) {
$result = db_query("SELECT filepath FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, '_flash');
$node->flash['_flash'] = db_result($result);
$result = db_query("SELECT height, width, version, build, display FROM {flash} WHERE nid=%d", $node->nid);
$settings = db_fetch_object($result);
foreach ($settings as $parameter => $value) {
$node->flash[$parameter] = $value;
}
}
function flash_insert($node) {
$fid = _flash_insert($node->nid, file_create_path($node->flash['_flash']), $node->flash['filemime']);
if ($fid) {
db_query("INSERT INTO {flash} (nid, height, width, version, build, display) VALUES (%d, %d, %d, %d, %d, %d)", $node->nid, $node->flash['height'], $node->flash['width'], $node->flash['version'], $node->flash['build'], $node->flash['display']);
}
}
function flash_update($node) {
$old_path = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flash', $node->nid));
if ($old_path != $node->flash['_flash']) {
file_delete(file_create_path($old_path));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flash', $node->nid);
db_query("DELETE FROM {flash} WHERE nid=%d", $node->nid);
flash_insert($node);
}
else {
db_query("UPDATE {flash} SET height=%d, width=%d, version=%d, build=%d, display=%d WHERE nid=%d", $node->flash['height'], $node->flash['width'], $node->flash['version'], $node->flash['build'], $node->flash['display'], $node->nid);
}
cache_clear_all('*', 'cache_filter', true);
}
function flash_delete($node) {
file_delete(file_create_path($node->flash[_flash]));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flash', $node->nid);
db_query("DELETE FROM {flash} WHERE nid=%d", $node->nid);
}
function flash_view($node, $teaser, $page = 0) {
$node = node_prepare($node, $teaser);
$flash = flash_display($node->flash);
if ($node->flash['display'] != 2 && $teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
if ($node->flash['display'] != 1 && !$teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
return $node;
}
function flash_display($file, $options = NULL) {
static $div_count = 1;
$div_id = time() . '-' . $div_count;
$flash_dir = base_path() . drupal_get_path('module', 'flash');
$class = $file['class'] ? $file['class'] : 'flash';
if ($options) {
$options = ', ' . implode(', ', $options);
}
$output .= t('<div id="ufo-!id" class="!class">
<p>You are missing some Flash content!</p>
<p>Maybe your browser does not have javascript enabled, perhaps you don\'t have the version of Flash this content needs (it needs at least version !version), or perhaps the content hasn\'t initialised correctly. Please check your settings and then try again. Thank you!</p>
</div>', array(
'!class' => $class,
'!id' => $div_id,
'!version' => $file['version'],
));
$output .= t('<script type="text/javascript"> var FO = { movie:"!movie", width:"!width", height:"!height", majorversion:"!version", build:"!build", xi:"true", ximovie:"!flash_dir/ufo.swf"!options }; UFO.create(FO, "ufo-!id"); </script>', array(
'!movie' => base_path() . file_create_path($file['_flash']),
'!flash_dir' => $flash_dir,
'!height' => $file['height'],
'!width' => $file['width'],
'!version' => $file['version'],
'!build' => $file['build'],
'!id' => $div_id,
'!options' => $options,
));
$div_count += 1;
return $output;
}
function _flash_check_settings() {
if (!file_exists(drupal_get_path('module', 'flash') . '/ufo.js')) {
drupal_set_message(t('The required files ufo.js and ufo.swf are not available. You must !ufo and copy them to the flashnode module directory in order for flashnode to work. Refer to the flashnode README.txt file for more details.', array(
'!ufo' => '<a href="http://www.bobbyvandersluis.com/ufo/ufo.zip" title="Download required files">download them</a>',
)));
}
$flash_path = file_create_path(variable_get('flash_default_path', 'flash'));
$temp_path = $flash_path . '/temp';
file_check_directory($flash_path, FILE_CREATE_DIRECTORY, 'flash_default_path');
file_check_directory($temp_path, FILE_CREATE_DIRECTORY, 'flash_default_path');
}
function _flash_filename($filename, $temp = FALSE) {
$result = variable_get('flash_default_path', 'flash') . '/';
if ($temp) {
$result .= 'temp/';
}
$result .= $filename;
return $result;
}
function _flash_insert($nid, $flash, $mime) {
$dest = _flash_filename(basename($flash));
if (file_copy($flash, $dest)) {
$file->filename = '_flash';
$file->filepath = _flash_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 flash_content($args = array()) {
if (!is_numeric($args['nid'])) {
return;
}
$flash['_flash'] = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flash', $args['nid']));
$result = db_query("SELECT height, width, version, build, display FROM {flash} WHERE nid=%d", $args['nid']);
$result = db_fetch_object($result);
foreach ($result as $parameter => $value) {
$flash[$parameter] = $value;
}
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] = round($value);
}
break;
case 'class':
$flash['class'] = $value;
break;
default:
$options[] = $parameter . ':"' . $value . '"';
}
$flash['width'] = round($flash['width'] * $xscale);
$flash['height'] = round($flash['height'] * $yscale);
}
}
return flash_display($flash, $options);
}
function flash_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 == 'flash') {
$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 flash_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Flash'),
);
case 'description':
return t('Add flash to your posts using a flash macro.');
case 'process':
foreach (flash_get_macros($text) as $unexpanded_macro => $macro) {
$expanded_macro = flash_content($macro);
$text = str_replace($unexpanded_macro, $expanded_macro, $text);
}
return $text;
default:
return $text;
}
}
function flash_filter_tips($delta, $format, $long = false) {
return t('Flash can be added to this post.');
}
function flash_admin_settings() {
_flash_check_settings();
$form['flash_updated'] = array(
'#type' => 'hidden',
'#value' => time(),
);
$settings = array(
'version' => 6,
'build' => 0,
);
foreach ($settings as $parameter => $value) {
$cur_value = variable_get('flash_' . $parameter, $value);
if (!is_numeric($cur_value)) {
variable_set('flash_' . $parameter, $value);
form_set_error('flash_' . $parameter, t('The @parameter was invalid. The default value has been saved.', array(
'@parameter' => $parameter,
)));
}
}
$form['paths'] = array(
'#type' => 'fieldset',
'#title' => t('File path'),
);
$form['paths']['flash_default_path'] = array(
'#type' => 'textfield',
'#title' => t('Default flash path'),
'#default_value' => variable_get('flash_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['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
);
foreach ($settings as $parameter => $value) {
$form['settings']['flash_' . $parameter] = array(
'#type' => 'textfield',
'#title' => t('Default ' . $parameter),
'#default_value' => variable_get('flash_' . $parameter, $value),
'#size' => 5,
'#maxlength' => 5,
);
}
return system_settings_form($form);
}