array( 'title' => t('Block upload'), 'description' => t('Upload files through the block.'), ), 'block remove' => array( 'title' => t('Allow to remove files via block'), 'description' => t('Allow user to remove files through the block.'), ), ); } /** * Implements hook_menu(). */ function block_upload_menu() { $items['admin/config/content/block_upload'] = array( 'page callback' => 'drupal_get_form', 'page arguments' => array('block_upload_settings_form'), 'title' => 'Block Upload settings', 'access arguments' => array('administer site'), 'file' => 'block_upload.admin.inc', ); return $items; } /** * Implements hook_block_info(). */ function block_upload_block_info() { $blocks_count = variable_get('block_upload_blocks_count', 1); for ($i = 1; $i <= $blocks_count; $i++) { $blocks['block_upload_' . $i] = array( 'info' => t('Block Upload') . ' ' . $i, ); } return $blocks; } /** * Implements hook_block_configure(). */ function block_upload_block_configure($delta = '') { $form = array(); $blocks_count = variable_get('block_upload_blocks_count', 1); for ($i = 1; $i <= $blocks_count; $i++) { if ($delta == 'block_upload_' . $i) { $fields = block_upload_get_field_list(); $form['block_upload_' . $i . '_field'] = array( '#type' => 'select', '#title' => t('Field'), '#description' => t('Select field you wish to upload file.'), '#options' => $fields, '#default_value' => array(variable_get('block_upload_' . $i . '_field', '')), '#ajax' => array( 'callback' => 'block_upload_ajax_callback', 'wrapper' => 'config', 'effect' => 'fade', ), ); $form['block_upload_id'] = array( '#type' => 'textfield', '#default_value' => $i, '#access' => FALSE, ); // Add field additional display options. $field_name = variable_get('block_upload_' . $i . '_field', ''); $field = field_info_field($field_name); _block_upload_field_options_form_elements($form, $i, $field['type']); // Check if plupload module exists and displau enable option. if (module_exists('plupload')) { $settings = variable_get('block_upload_' . $i . '_settings', array()); $form['block_upload_' . $i . '_plupload_status'] = array( '#type' => 'checkbox', '#title' => t('Use Plupoad for file uploads'), '#default_value' => isset($settings['plupload']) ? $settings['plupload'] : 0, ); } else { $link = l(t('Plupload integration'), 'https://drupal.org/project/plupload', array('external' => TRUE, 'attributes' => array('target' => '_blank'))); $description = t('To enable multiuploads and drag&drop upload features, download and install !link module', array('!link' => $link)); $form['block_upload_plupload_status'] = array( '#type' => 'checkbox', '#title' => t('Use Plupoad for file uploads'), '#disabled' => TRUE, '#description' => $description, ); } } } return $form; } /** * Implements hook_block_save(). */ function block_upload_block_save($delta = '', $edit = array()) { $id = $edit['block_upload_id']; $block_id = 'block_upload_' . $id . '_'; variable_set($block_id . 'field', $edit[$block_id . 'field']); $settings = array(); $settings['plupload'] = (isset($edit[$block_id . 'plupload_status'])) ? $edit[$block_id . 'plupload_status'] : FALSE; $settings['alt'] = (isset($edit[$block_id . 'alt'])) ? $edit[$block_id . 'alt'] : FALSE; $settings['title'] = (isset($edit[$block_id . 'title'])) ? $edit[$block_id . 'title'] : FALSE; $settings['desc'] = (isset($edit[$block_id . 'desc'])) ? $edit[$block_id . 'desc'] : FALSE; variable_set($block_id . 'settings', $settings); } /** * Implements hook_block_view(). */ function block_upload_block_view($delta = '') { $block = array(); $blocks_count = variable_get('block_upload_blocks_count', 1); for ($i = 1; $i <= $blocks_count; $i++) { if ($delta == 'block_upload_' . $i && user_access('block upload') && $node = menu_get_object()) { $field = field_info_field(variable_get('block_upload_' . $i . '_field', '')); if (!empty($field['bundles']['node'])) { foreach ($field['bundles']['node'] as $bundle) { if ($bundle == $node->type) { $settings = variable_get('block_upload_' . $i . '_settings', array()); if (isset($settings['plupload']) && $settings['plupload']) { // Plupload integration form. $block['content'] = drupal_get_form('block_upload_plupload_form', $node, $i); } else { // Simple file upload form. $block['content'] = drupal_get_form('block_upload_form', $node, $i); } } } } } } return $block; } /** * Builds form for removing the files with block. */ function block_upload_remove_form($field_limit, $node, $field) { foreach ($node->{$field}[LANGUAGE_NONE] as $key => $val) { $uploader = user_load($val['uid']); $uploader = l($uploader->name, 'user/' . $uploader->uid); $options[$val['fid']] = array( array( 'data' => array( '#type' => 'item', '#title' => filter_xss($uploader), '#description' => check_plain(format_date($val['timestamp'])), ), ), array( 'data' => array( '#theme' => 'file_link', '#file' => (object) $val, ), 'field_type' => $field_limit['type'], ), ); } $header = array(t('Uploader'), t('File')); $form = array( '#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => t('No content available.'), '#attributes' => array('class' => array('delete-files')), ); return $form; } /** * Block upload form. * * @see block_upload_form_validate() * @see block_upload_form_submit() * * @return array * Form. */ function block_upload_form($form, &$form_state, $node, $buid) { $submit = FALSE; $field = variable_get('block_upload_' . $buid . '_field', ''); $field_limit = field_info_field($field); $field_info = field_info_instance('node', $field, $node->type); if (isset($node->{$field}[LANGUAGE_NONE])) { $field_files_exists = count($node->{$field}[LANGUAGE_NONE]); } else { $field_files_exists = 0; } if (user_access('block remove') && $field_files_exists > 0) { $title_remove_form = t('Remove files'); $form['remove_files_title'] = array('#markup' => '

' . $title_remove_form . '

'); $form['remove_files'] = block_upload_remove_form($field_limit, $node, $field); $submit = TRUE; } if (($field_limit['cardinality'] > $field_files_exists || $field_limit['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) { $title_upload_form = t('Upload file'); $form['upload_files_title'] = array('#markup' => '

' . $title_upload_form . '

'); $form['block_upload_file'] = array( '#type' => 'managed_file', '#upload_location' => block_upload_get_upload_destination($field_info), '#upload_validators' => block_upload_get_validators($field, $field_info), ); $submit = TRUE; $settings = variable_get('block_upload_' . $buid . '_settings', array()); if (isset($settings['alt']) && $settings['alt']) { $form['block_upload_' . $buid . '_alt'] = array( '#type' => 'textfield', '#title' => t('Alt'), ); } if (isset($settings['title']) && $settings['title']) { $form['block_upload_' . $buid . '_title'] = array( '#type' => 'textfield', '#title' => t('Title'), ); } if (isset($settings['desc']) && $settings['desc']) { $form['block_upload_' . $buid . '_desc'] = array( '#type' => 'textfield', '#title' => t('Description'), ); } } else { $form[] = array( '#type' => 'item', '#description' => t('Exceeded limit of files'), ); } if ($submit) { $module_path = drupal_get_path('module', 'block_upload'); $form['#attached']['js'] = array( $module_path . '/theme/block_upload.js', ); $form['#attached']['css'] = array( $module_path . '/theme/block_upload.css', ); $form['block_upload_nid'] = array( '#type' => 'textfield', '#access' => FALSE, '#value' => $node->nid, ); $form['block_upload_node_type'] = array( '#type' => 'textfield', '#access' => FALSE, '#value' => $node->type, ); $form['buid'] = array( '#type' => 'value', '#value' => $buid, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); } return $form; } /** * Validate and save uploaded file. */ function block_upload_form_validate($form, &$form_state) { $values = $form_state['values']; if (!empty($values['block_upload_file'])) { $buid = $values['buid']; $field_name = variable_get('block_upload_' . $buid . '_field', ''); $field = field_info_instance('node', $field_name, $values['block_upload_node_type']); $file = file_load($form_state['values']['block_upload_file']); $file->status = FILE_STATUS_PERMANENT; if ($field['widget']['module'] == 'file') { $file->display = 1; } $file = file_save($file); $form_state['storage']['file'] = $file; } } /** * Save uploaded file to node. */ function block_upload_form_submit($form, &$form_state) { $values = $form_state['values']; $nid = $values['block_upload_nid']; $buid = $values['buid']; $field_name = variable_get('block_upload_' . $buid . '_field', ''); $node = node_load($nid); if (isset($form_state['input']['remove_files'])) { if (array_filter($form_state['input']['remove_files'])) { block_upload_deletefiles($node, $field_name, $form_state); } } if (isset($form_state['storage']['file'])) { $file = $form_state['storage']['file']; if (isset($values['block_upload_' . $buid . '_alt'])) { $file->alt = check_plain($values['block_upload_' . $buid . '_alt']); } if (isset($values['block_upload_' . $buid . '_title'])) { $file->title = check_plain($values['block_upload_' . $buid . '_title']); } if (isset($values['block_upload_' . $buid . '_desc'])) { $file->description = check_plain($values['block_upload_' . $buid . '_desc']); } $node->{$field_name}[LANGUAGE_NONE][] = (array) $file; unset($form_state['storage']['file']); drupal_set_message(t('File was successfully uploaded!')); } // Do not change existing path alias if pathauto module installed. if (module_exists('pathauto')) { $node->path['pathauto'] = FALSE; } node_save($node); } /** * Returns avaliable field list of filefield type. * * @return array * Field list. */ function block_upload_get_field_list() { $fields = array(); foreach (field_info_fields() as $key => $item) { // Display only file type fields. if ($item['type'] == 'image' || $item['type'] == 'file') { $fields[$key] = $item['field_name']; } } return $fields; } /** * Returns destinaton for file upload. * * @return string * Destination path. */ function block_upload_get_upload_destination($field) { if ($destination = $field['settings']['file_directory']) { if (module_exists('token')) { $node = menu_get_object(); $destination = token_replace($destination, array('node' => $node)); } } $field_info = field_info_field($field['field_name']); $uri_scheme = $field_info['settings']['uri_scheme']; if (!$uri_scheme) { $uri_scheme = 'public'; } $destination = $uri_scheme . '://' . $destination; file_prepare_directory($destination, FILE_CREATE_DIRECTORY); return $destination; } /** * Returns validators array. * * @return array * List of validators. */ function block_upload_get_validators($field_name, $field) { $validators = file_field_widget_upload_validators($field_name, $field); if ($field['widget']['module'] == 'image') { $validators['file_validate_is_image'] = array(); $validators['file_validate_image_resolution'] = array( $field['settings']['max_resolution'], $field['settings']['min_resolution'], ); } return $validators; } /** * Deletes files marked by checkbox in deletion form. */ function block_upload_deletefiles($node, $field_name, &$form_state) { $delete_files = array_values($form_state['input']['remove_files']); foreach ($node->{$field_name}[LANGUAGE_NONE] as $key => $file_field) { if (in_array($file_field['fid'], $delete_files)) { $old_file = file_load($file_field['fid']); file_delete($old_file, TRUE); unset($node->{$field_name}[LANGUAGE_NONE][$key]); } } drupal_set_message(t('File(s) was successfully deleted!')); } /** * Returns plupload form for the form builder. * * @see block_upload_plupload_form_submit() */ function block_upload_plupload_form($form, &$form_state, $node, $buid) { $field_name = variable_get('block_upload_' . $buid . '_field', ''); $field = field_info_instance('node', $field_name, $node->type); $validators = block_upload_get_validators($field_name, $field); $submit = FALSE; $field_limit = field_info_field($field_name); if (isset($node->{$field_name}[LANGUAGE_NONE])) { $field_files_exists = count($node->{$field_name}[LANGUAGE_NONE]); } else { $field_files_exists = 0; } if (user_access('block remove') && $field_files_exists > 0) { $title_remove_form = t('Remove files'); $form['remove_files_title'] = array('#markup' => '

' . $title_remove_form . '

'); $form['remove_files'] = block_upload_remove_form($field_limit, $node, $field_name); $submit = TRUE; } if (($field_limit['cardinality'] > $field_files_exists || $field_limit['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) { if ($field_limit['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { $avaliable = $field_limit['cardinality'] - $field_files_exists; $attention = t('Attention: Thit field is limited to uploading files. Availability %num%', array('%num%' => $avaliable)); $form[] = array( '#type' => 'item', '#title' => check_plain($attention), ); } $submit = TRUE; unset($validators['file_validate_is_image']); $title_upload_form = t('Upload files'); $form['upload_files_title'] = array('#markup' => '

' . $title_upload_form . '

'); $form['block_upload_file'] = array( '#type' => 'plupload', '#upload_validators' => $validators, ); } else { $form[] = array( '#type' => 'item', '#description' => t('Exceeded limit of files'), ); } $form_state['node'] = $node; $form_state['field'] = $field; $form_state['buid'] = $buid; if ($submit) { $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); } return $form; } /** * Saves files uploaded via plupload form. * * Example taken from plupload_test_submit(); */ function block_upload_plupload_form_submit($form, &$form_state) { $field = $form_state['field']; $path = block_upload_get_upload_destination($field); // We can't use file_save_upload() because of // http://www.jacobsingh.name/content/tight-coupling-no-not // file_uri_to_object(); $buid = $form_state['buid']; $field_name = variable_get('block_upload_' . $buid . '_field', ''); $node = node_load($form_state['node']->nid); if (isset($form_state['input']['remove_files'])) { if (array_filter($form_state['input']['remove_files'])) { block_upload_deletefiles($node, $field_name, $form_state); } } if (isset($form_state['values']['block_upload_file'])) { foreach ($form_state['values']['block_upload_file'] as $uploaded_file) { if ($uploaded_file['status'] == 'done') { $source = $uploaded_file['tmppath']; $destination = file_stream_wrapper_uri_normalize($path . '/' . $uploaded_file['name']); // Rename it to its original name, and put it in its final home. // Note - not using file_move here because if we call file_get_mime // (in file_uri_to_object) while it has a .tmp extension, it horks. $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME); $file = plupload_file_uri_to_object($destination); if ($field['widget']['module'] == 'file') { $file->display = 1; } file_save($file); $node->{$field_name}[LANGUAGE_NONE][] = (array) $file; drupal_set_message(t('File @name was successfully uploaded!', array('@name' => $uploaded_file['name']))); } else { // @todo move this to element validate or something and clean up t(). form_set_error('block_upload_file', t('Upload of') . $uploaded_file['name'] . t('failed')); } } } // Do not change existing path alias if pathauto module installed. if (module_exists('pathauto')) { $node->path['pathauto'] = FALSE; } node_save($node); } /** * Display fields checkboxes depends on selected field. */ function block_upload_ajax_callback($form, $form_state) { $block_upload_id = $form_state['values']['block_upload_id']; if (empty($form_state['values']['block_upload_' . $block_upload_id . '_field'])) { return; } $field_name = $form_state['values']['block_upload_' . $block_upload_id . '_field']; $field = field_info_field($field_name); _block_upload_field_options_form_elements($form, $block_upload_id, $field['type']); return $form['config']; } /** * Dinamic form elements for image/file field types. * * @param int $buid * Block upload variable ID. * @param string $type * Field type. */ function _block_upload_field_options_form_elements(&$form, $buid, $type) { $form['config'] = array( '#title' => t("Additional display"), '#description' => t('Alt and title fields display for single form display. Will not apply for plupload widget.'), '#prefix' => '
', '#suffix' => '
', '#type' => 'fieldset', ); $settings = variable_get('block_upload_' . $buid . '_settings', array()); // Alt and title enable options form elements for image field type. if ($type == 'image') { $form['config']['block_upload_' . $buid . '_alt'] = array( '#type' => 'checkbox', '#title' => t('Show alt field'), '#default_value' => isset($settings['alt']) ? $settings['alt'] : 0, ); $form['config']['block_upload_' . $buid . '_title'] = array( '#type' => 'checkbox', '#title' => t('Show title field'), '#default_value' => isset($settings['title']) ? $settings['title'] : 0, ); } // Description enable option form element for file field type. elseif ($type == 'file') { $form['config']['block_upload_' . $buid . '_desc'] = array( '#type' => 'checkbox', '#title' => t('Show description field'), '#default_value' => isset($settings['desc']) ? $settings['desc'] : 0, ); } }