View source
<?php
include 'attachment_links.field-formatters.inc';
function attachment_links_menu() {
$items['node/%node/attachment'] = array(
'title' => 'Authoritative Attachment',
'description' => 'The canonical or "lightest" attached file.',
'type' => MENU_CALLBACK,
'page callback' => 'attachment_links_retrieve',
'page arguments' => array(
1,
'authoritative',
),
'access callback' => 'node_access',
'access arguments' => array(
'view',
1,
),
);
$items['node/%node/attachment/newest'] = array(
'title' => 'Latest Attachment',
'description' => 'The most recently attached file.',
'type' => MENU_CALLBACK,
'page callback' => 'attachment_links_retrieve',
'page arguments' => array(
1,
'newest',
),
'access callback' => 'node_access',
'access arguments' => array(
'view',
1,
),
);
return $items;
}
function attachment_links_node_view($node, $view_mode, $langcode) {
$file_field_name = variable_get('attachment_links_selection_' . $node->type, 0);
if ($file_field_name) {
$files = field_get_items('node', $node, $file_field_name);
if ($files) {
$node->content['attachment_links'] = array(
'#markup' => theme('attachment_links', array(
'node' => $node,
)),
'#weight' => 3,
);
}
}
}
function attachment_links_node_insert($node) {
if (variable_get('attachment_links_create_alias_' . $node->type, FALSE)) {
path_delete(array(
'source' => 'node/' . $node->nid . '/attachment',
));
attachment_links_save_alias($node);
}
}
function attachment_links_node_update($node) {
path_delete(array(
'source' => 'node/' . $node->nid . '/attachment',
));
attachment_links_save_alias($node);
}
function attachment_links_node_delete($node) {
path_delete(array(
'source' => 'node/' . $node->nid . '/attachment',
));
}
function attachment_links_save_alias($node) {
$create_alias = variable_get('attachment_links_create_alias_' . $node->type, FALSE);
if ($create_alias) {
$file_field_name = variable_get('attachment_links_selection_' . $node->type, 0);
if ($file_field_name) {
$files = field_get_items('node', $node, $file_field_name);
if (!empty($files)) {
$file = reset($files);
$file = file_load($file['fid']);
if (!empty($node->path['alias'])) {
$node_alias = $node->path['alias'];
$alias_parts = explode('/', $node_alias);
if (count($alias_parts) > 1) {
array_pop($alias_parts);
$node_alias = implode('/', $alias_parts);
}
$alias = $node_alias . '/' . $file->filename;
$source = 'node/' . $node->nid . '/attachment';
$new_alias = array(
'source' => $source,
'alias' => $alias,
'language' => $node->language,
);
path_save($new_alias);
return TRUE;
}
}
}
}
return FALSE;
}
function attachment_links_theme($existing, $type, $theme, $path) {
$hooks['attachment_links'] = array(
'template' => 'attachment-links',
'variables' => array(
'node' => NULL,
),
);
$hooks['attachment_links_formatter_attachment_links_preferred'] = array(
'arguments' => array(
'element' => NULL,
),
);
$hooks['attachment_links_formatter_attachment_links_newest'] = array(
'arguments' => array(
'element' => NULL,
),
);
return $hooks;
}
function attachment_links_form_node_type_form_alter(&$form, &$form_state) {
if (isset($form['type'])) {
$node_type = $form['#node_type']->type;
if (isset($node_type)) {
$options = array(
0 => 'Disabled',
);
$instances = field_info_instances('node', $node_type);
foreach ($instances as $field_name => $instance) {
$field_info = field_info_field($field_name);
if ($field_info['type'] == 'file') {
$options[$field_name] = $instances[$field_name]['label'] . " ({$field_name})";
}
}
$form['attachment_links'] = array(
'#type' => 'fieldset',
'#title' => t('Attachment links'),
'#group' => 'additional_settings',
);
$form['attachment_links']['attachment_links_selection'] = array(
'#type' => 'select',
'#title' => t('Select file field'),
'#description' => t('When hitting attachment links URLs, it will deliver files from this file field.'),
'#default_value' => variable_get('attachment_links_selection_' . $node_type, 0),
'#options' => $options,
'#weight' => 5,
);
$form['attachment_links']['attachment_links_create_alias'] = array(
'#type' => 'checkbox',
'#title' => t('Generate convenience aliases'),
'#description' => t('Uses the file extension and node alias to create a file alias, see README.txt for more info.'),
'#default_value' => variable_get('attachment_links_create_alias_' . $node_type, FALSE),
'#weight' => 6,
);
}
$form['#submit'][] = 'attachment_links_node_type_form_submit';
}
}
function attachment_links_node_type_form_submit($form, &$form_state) {
field_info_cache_clear();
}
function attachment_links_form_node_form_alter(&$form, &$form_state) {
$node = $form['#node'];
if (isset($form['type']) && isset($node)) {
$file_field_name = variable_get('attachment_links_selection_' . $node->type, 0);
if ($file_field_name) {
$instances = field_info_instances('node', $node->type);
$label = $instances[$file_field_name]['label'];
$form['attachment_links'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Attachment links'),
'#group' => 'additional_settings',
'#weight' => 50,
);
$help_blurb = t('Attachment links provides permanent links for files in the field %field. The "preferred" version is the first file listed on the field %field above. Click and drag the handles to the left of each file to reorder them. You can choose a different field in the content type settings.', array(
'%field' => $label,
));
if ($form['nid']['#value']) {
$help_blurb .= theme('attachment_links', array(
'node' => $node,
));
}
else {
$help_blurb .= '<div><strong>' . t('Attachment links will be shown here after you save the node.') . '</strong></div>';
}
$form['attachment_links']['current'] = array(
'#type' => 'item',
'#title' => t('Attachment links on %field', array(
'%field' => $label . ' (' . $file_field_name . ')',
)),
'#markup' => $help_blurb,
);
}
}
}
function attachment_links_retrieve($node, $type) {
$file_field_name = variable_get('attachment_links_selection_' . $node->type, 0);
if (!$file_field_name) {
return MENU_NOT_FOUND;
}
$files = field_get_items('node', $node, $file_field_name);
if (!empty($files)) {
switch ($type) {
case 'authoritative':
$file = reset($files);
$uri = $file['uri'];
break;
case 'newest':
$max_timestamp = 0;
foreach ($files as $key => $file) {
if ($file['timestamp'] > $max_timestamp) {
$max_timestamp = $file['timestamp'];
$max_key = $key;
}
}
$uri = $files[$max_key]['uri'];
break;
}
$scheme = file_uri_scheme($uri);
if ($scheme == 'public') {
$file_url = file_create_url($uri);
drupal_goto($file_url);
}
else {
if ($scheme == 'private') {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
$file_url = $wrapper
->getExternalUrl();
$file_url = str_replace(url('system/files/', array(
'absolute' => TRUE,
)), '', $file_url);
$file_url = urldecode($file_url);
$args = explode('/', $file_url);
array_unshift($args, 'private');
header('Content-Disposition: attachment; filename="' . end($args) . '"');
call_user_func_array('file_download', $args);
}
}
else {
return MENU_NOT_FOUND;
}
}
}
else {
return MENU_NOT_FOUND;
}
}
function template_preprocess_attachment_links(&$vars) {
$node = $vars['node'];
$options = array(
'absolute' => TRUE,
);
$vars['items'] = array(
t('Preferred version: !link', array(
'!link' => l(url("node/{$node->nid}/attachment", $options), "node/{$node->nid}/attachment"),
)),
t('Newest version: !link', array(
'!link' => l(url("node/{$node->nid}/attachment/newest", $options), "node/{$node->nid}/attachment/newest"),
)),
);
}
function attachment_links_field_extra_fields() {
$types = node_type_get_types();
foreach ($types as $key => $type) {
if (variable_get('attachment_links_selection_' . $key, 0)) {
$extra['node'][$key] = array(
'display' => array(
'attachment_links' => array(
'label' => t('Attachment links'),
'description' => t('Links to the canonical or preferred version of the attached files.'),
'weight' => 3,
),
),
);
}
}
if (isset($extra)) {
return $extra;
}
}