You are here

ocupload.module in One Click Upload 7

Same filename and directory in other branches
  1. 7.2 ocupload.module

Hooks and general functions

File

ocupload.module
View source
<?php

/**
 * @file
 * Hooks and general functions
 */

/**
 * Implements hook_menu().
 */
function ocupload_menu() {
  $items = array();
  $items['admin/config/content/ocupload'] = array(
    'title' => 'One click upload',
    'description' => 'Configure module',
    'page callback' => 'ocupload_config_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/content/ocupload/add'] = array(
    'title' => 'Add template',
    'description' => 'Add template form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_form_template',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/content/ocupload/edit/%ocupload_template'] = array(
    'title' => 'Edit template',
    'description' => 'Edit template form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_form_template',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/content/ocupload/delete/%ocupload_template'] = array(
    'title' => 'Delete template',
    'description' => 'Delete template confirm form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_delete_confirm',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['ocupload/upload'] = array(
    'title' => 'Upload file',
    'page callback' => 'ocupload_upload',
    'access callback' => TRUE,
    'file' => 'ocupload.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function ocupload_permission() {
  module_load_include('inc', 'ocupload');
  $permissions = array();
  foreach (ocupload_templates() as $template) {
    $permissions['upload files use template ' . $template->tid] = array(
      'title' => t('Upload !extensions files', array(
        '!extensions' => $template->mask,
      )),
      'restrict access' => TRUE,
    );
  }
  return $permissions;
}

/**
 * Implements hook_preprocess_html().
 */
function ocupload_preprocess_html(&$vars) {
  global $base_path;
  $scripts = drupal_add_js();
  $item = menu_get_item();
  $module_path = _ocupload_get_path();
  $module_static_path = $module_path . '/static/';
  $ocupload_use = FALSE;
  if (module_exists('bueditor') && (isset($scripts[drupal_get_path('module', 'bueditor') . '/bueditor.js']) || $item['path'] == 'admin/config/content/bueditor/%')) {
    drupal_add_js($module_static_path . 'bueditor.js', array(
      'weight' => 100,
    ));
    $ocupload_use = TRUE;
  }
  elseif (module_exists('ckeditor') && isset($scripts[ckeditor_path('url') . '/ckeditor.js'])) {
    $ocupload_use = TRUE;
  }
  elseif (module_exists('wysiwyg')) {
    foreach ($scripts['settings']['data'] as $data) {
      if (isset($data['wysiwyg']['configs']['ckeditor'])) {
        $ocupload_use = TRUE;
        break;
      }
    }
  }
  if ($ocupload_use) {
    $allowed_ext = array();
    foreach (ocupload_templates() as $template) {
      if (user_access('upload files use template ' . $template->tid)) {
        $allowed_ext = array_merge($allowed_ext, explode(',', $template->mask));
      }
    }
    drupal_add_js(array(
      'ocupload' => array(
        'allowedExt' => '*.' . implode('; *.', $allowed_ext) . ';',
      ),
    ), 'setting');
    if ($allowed_ext) {
      drupal_add_js('sites/all/libraries/swfupload/swfupload.js');
      drupal_add_js('sites/all/libraries/swfupload/plugins/swfupload.queue.js');
      drupal_add_js(array(
        'ocupload' => array(
          'phpsessid' => session_id(),
          'sizeLimit' => file_upload_max_size(),
          'modulePath' => $base_path . $module_path,
          'uploadPath' => url('ocupload/upload'),
        ),
      ), 'setting');
      drupal_add_css($module_static_path . 'styles.css');
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function ocupload_form_alter(&$form, &$form_state, $form_id) {
  if (variable_get('ocupload_delete_unused_files', 1) && isset($form['#submit'])) {
    array_unshift($form['#submit'], 'ocupload_change_files_status');
  }
}

/**
 * Custom form submit. Change status uploaded files to "permanent".
 */
function ocupload_change_files_status($form, &$form_state) {
  $cid = 'ocupload:' . $form['#form_id'] . ':' . $GLOBALS['user']->uid;
  if ($cache = cache_get($cid)) {
    $permanent_files = array();
    foreach ($cache->data as $field_name => $files) {
      $field_value = _ocupload_field_value_by_html_name($form_state, $field_name);
      foreach ($files as $fid => $file_name) {
        if (strpos($field_value, $file_name) !== FALSE) {
          $file = file_load($fid);
          $file->status = FILE_STATUS_PERMANENT;
          file_save($file);
          unset($cache->data[$field_name][$fid]);
          $permanent_files[] = $file;
        }
      }
      if (count($cache->data[$field_name]) == 0) {
        unset($cache->data[$field_name]);
      }
    }
    if ($permanent_files) {
      drupal_alter('ocupload_saved_data', $permanent_files, $form, $form_state);
    }
    cache_set($cid, $cache->data, 'cache', count($cache->data) > 0 ? $cache->expire : CACHE_TEMPORARY);
  }
}

/**
 * Return template object by tid.
 */
function ocupload_template_load($tid) {
  return db_select('ocupload_templates', 'bc')
    ->fields('bc')
    ->condition('bc.tid', $tid)
    ->execute()
    ->fetch();
}

/**
 * Return all templates.
 */
function ocupload_templates() {
  return db_select('ocupload_templates', 'bc')
    ->fields('bc')
    ->execute()
    ->fetchAllAssoc('tid');
}

/**
 * Return value by input name.
 */
function _ocupload_field_value_by_html_name($form_state, $field_name) {
  $parts = explode('[', $field_name);
  $value =& $form_state['values'];
  foreach ($parts as $part) {
    $part = rtrim($part, ']');
    $value =& $value[$part];
  }
  return $value;
}

/**
 * Implements hook_wysiwyg_plugin().
 */
function ocupload_wysiwyg_plugin($editor, $version) {
  if ($editor == 'ckeditor') {
    return array(
      'OCUpload' => array(
        'url' => 'http://drupal.org/project/ocupload',
        'path' => _ocupload_get_path() . '/static',
        'load' => TRUE,
        'buttons' => array(
          'OCUpload' => t('One Click Upload'),
        ),
      ),
    );
  }
}

/**
 * Implements hook_ckeditor_plugin().
 */
function ocupload_ckeditor_plugin() {
  return array(
    'ocupload' => array(
      'name' => 'OCUpload',
      'desc' => 'One Click Upload',
      'path' => _ocupload_get_path() . '/static/',
      'buttons' => array(
        'OCUpload' => array(
          'icon' => 'button.png',
          'label' => 'One Click Upload',
        ),
      ),
    ),
  );
}

/**
 * Return module path.
 */
function _ocupload_get_path() {
  return drupal_get_path('module', 'ocupload');
}

/**
 * Implements hook_features_api().
 */
function ocupload_features_api() {
  return array(
    'ocupload_templates' => array(
      'name' => t('One Click Upload'),
      'file' => drupal_get_path('module', 'ocupload') . '/ocupload.features.inc',
      'default_hook' => 'ocupload_templates',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
    ),
  );
}

Functions

Namesort descending Description
ocupload_change_files_status Custom form submit. Change status uploaded files to "permanent".
ocupload_ckeditor_plugin Implements hook_ckeditor_plugin().
ocupload_features_api Implements hook_features_api().
ocupload_form_alter Implements hook_form_alter().
ocupload_menu Implements hook_menu().
ocupload_permission Implements hook_permission().
ocupload_preprocess_html Implements hook_preprocess_html().
ocupload_templates Return all templates.
ocupload_template_load Return template object by tid.
ocupload_wysiwyg_plugin Implements hook_wysiwyg_plugin().
_ocupload_field_value_by_html_name Return value by input name.
_ocupload_get_path Return module path.