View source
<?php
define('VSF_DEFAULT_UPLOAD_EXTENSIONS', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm');
define('VSF_DEFAULT_UPLOAD_FILE_SIZE', '2M');
define('VSF_DEFAULT_UPLOAD_LOCATION', 'visual_select_file');
define('VSF_DEFAULT_UPLOAD_SCHEME', 'public');
require __DIR__ . '/vsf_upload.admin.inc';
function vsf_upload_init() {
if ('file/add' == $_GET['q'] && isset($_GET['vsf_destination'])) {
module_invoke('admin_menu', 'suppress');
$path = drupal_get_path('module', 'visual_select_file');
drupal_add_css($path . '/visual_select_file.modal.css');
}
}
function vsf_upload_menu() {
$items = array();
if (!module_exists('file_entity')) {
$items['file/add'] = array(
'title' => 'Add file',
'description' => 'Upload a file',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vsf_upload_form',
),
'access arguments' => array(
'upload vsf files',
),
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'navigation',
);
}
return $items;
}
function vsf_upload_preprocess_views_view(&$vars) {
$view = $vars['view'];
if ($view->tag == 'visual_select_file' && isset($_GET['ckeditor'])) {
if (module_exists('file_entity') && variable_get('vsf_file_entity', TRUE)) {
if (file_entity_access('create')) {
$link = l(t('Add file'), 'file/add', array(
'query' => array(
'vsf_destination' => request_uri(),
),
));
$vars['header'] = theme('item_list', array(
'items' => array(
$link,
),
'attributes' => array(
'class' => array(
'action-links',
),
),
));
}
}
else {
if (user_access('upload vsf files')) {
$form = drupal_get_form('vsf_upload_form');
$query = array_intersect_key($_GET, array_flip(array(
'vsf_formatter',
'ckeditor',
'iframe',
'vsf_field',
)));
$form['#action'] = url('admin/visual_select_file', array(
'query' => $query,
));
$vars['header'] = drupal_render($form);
}
}
}
}
function vsf_upload_form($form, &$form_state) {
$form['upload'] = array(
'#type' => 'fieldset',
'#title' => t('Upload a file'),
'#collapsed' => current_path() != 'file/add',
'#collapsible' => current_path() != 'file/add',
);
$form['upload']['file'] = array(
'#title' => t('File'),
'#type' => 'managed_file',
'#required' => TRUE,
'#upload_location' => variable_get('vsf_upload_scheme', VSF_DEFAULT_UPLOAD_SCHEME) . '://' . variable_get('vsf_upload_location', VSF_DEFAULT_UPLOAD_LOCATION),
'#upload_validators' => array(
'file_validate_extensions' => array(
variable_get('vsf_upload_extensions', VSF_DEFAULT_UPLOAD_EXTENSIONS),
),
'file_validate_size' => array(
parse_size(variable_get('vsf_upload_size', VSF_DEFAULT_UPLOAD_FILE_SIZE)),
),
),
'#process' => array(
'vsf_upload_file_element_process',
),
'#description' => t('The maximum file size is: %file_size, the allowed file extensions are: %file_extensions.', array(
'%file_extensions' => variable_get('vsf_upload_extensions', VSF_DEFAULT_UPLOAD_EXTENSIONS),
'%file_size' => format_size(parse_size(variable_get('vsf_upload_size', VSF_DEFAULT_UPLOAD_FILE_SIZE))),
)),
);
$form['upload']['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#submit' => array(
'vsf_upload_form_submit',
),
);
return $form;
}
function vsf_upload_file_element_process($element, &$form_state, $form) {
$element = file_managed_file_process($element, $form_state, $form);
$element['upload_button']['#access'] = FALSE;
return $element;
}
function vsf_upload_form_submit($form, &$form_state) {
$file = file_load($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
drupal_set_message(t('File uploaded succesfully'));
$form_state['redirect'] = array(
current_path(),
array(
'fragment' => 'modal',
'query' => drupal_get_query_parameters(),
),
);
}
function vsf_upload_admin_menu_output_build(&$content) {
$link = db_query("SELECT * FROM {menu_links} WHERE router_path = 'file/add' AND module = 'system'")
->fetchAssoc();
if ($link) {
$conditions = array();
for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
if (!empty($link["p{$i}"])) {
$conditions["p{$i}"] = $link["p{$i}"];
}
}
$tree = menu_build_tree($link['menu_name'], array(
'conditions' => $conditions,
'min_depth' => $link['depth'],
));
$links = admin_menu_links_menu($tree);
if (!empty($links)) {
if (isset($content['menu']['admin/content'])) {
$content['menu']['admin/content'] += $links;
}
else {
$key = key($links);
$links[$key]['#weight'] = -100;
$content['menu'] += $links;
}
}
}
}
function vsf_upload_admin_paths() {
$paths = array(
'file/add' => TRUE,
);
return $paths;
}
function vsf_upload_permission() {
return array(
'upload vsf files' => array(
'title' => t('Upload files'),
'description' => t('Allows the user to upload files via the vsf upload page.'),
),
);
}
function vsf_upload_form_file_entity_add_upload_alter(&$form, $form_state) {
$form['#submit'][] = 'vsf_upload_form_file_entity_add_upload_submit';
}
function vsf_upload_form_file_entity_add_upload_submit($form, &$form_state) {
if (isset($_GET['vsf_destination'])) {
$url = parse_url($_GET['vsf_destination']);
$query = array();
isset($url['query']) && parse_str($url['query'], $query);
if (isset($url['path'])) {
$form_state['redirect'] = array(
$url['path'],
array(
'query' => $query,
'fragment' => 'modal',
),
);
}
}
}