'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'); $templates = ocupload_templates(); $permissions = array(); foreach ($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 = drupal_get_path('module', 'ocupload'); $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' => 1)); $ocupload_use = TRUE; } elseif (module_exists('ckeditor') && isset($scripts[ckeditor_path() . '/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) { $templates = ocupload_templates(); $allowed_ext = array(); foreach ($templates as $template) { if (user_access('upload files use template ' . $template->tid)) { $allowed_ext = array_merge($allowed_ext, explode(',', $template->mask)); } } 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(), 'allowedExt' => '*.' . implode('; *.', $allowed_ext) . ';', 'modulePath' => $base_path . $module_path, )), '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_state['values']['form_id'] . ':' . $GLOBALS['user']->uid; if ($cache = cache_get($cid)) { 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]); } } if (count($cache->data[$field_name]) == 0) { unset($cache->data[$field_name]); } } 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' => drupal_get_path('module', 'ocupload') . '/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' => drupal_get_path('module', 'ocupload') . '/static/', 'buttons' => array( 'OCUpload' => array( 'icon' => 'button.png', 'label' => 'One Click Upload', ), ), ), ); }