You are here

dnd.module in Scald: Media Management made easy 7

File

modules/library/dnd/dnd.module
View source
<?php

// A suffix for auto generated IDs
define('DND_ID_SUFFIX', '-dnd-library');

/**
 * Implementation of hook_menu().
 */
function dnd_menu() {
  $items = array();
  $items['admin/config/content/dnd'] = array(
    'title' => 'Drag and Drop Library',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dnd_admin_form',
    ),
    'description' => 'Configure the resource library used when creating content.',
    'access arguments' => array(
      'administer dnd',
    ),
    'file' => 'dnd.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function dnd_permission() {
  return array(
    'administer dnd' => array(
      'title' => t('Administer DnD'),
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function dnd_theme() {
  return array(
    'dnd_library_wrapper' => array(
      'variables' => array(
        'settings' => NULL,
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Get the list of Scald contexts that can be used in a WYSIWYG, keyed by type.
 *
 * @return array
 *   Returns an associative array, keyed by the atom type machine name and whose
 *   values are associative arrays, each keyed by the context machine name and
 *   whose values are the user facing name of this context for this atom type.
 */
function dnd_scald_wysiwyg_context_list() {
  $contexts =& drupal_static(__FUNCTION__, NULL);
  if (!isset($contexts)) {
    $types = scald_types();
    foreach (scald_contexts_public() as $name => $definition) {
      if (empty($definition['parseable'])) {
        continue;
      }

      // There "formats" is actually used nowhere in Scald. Every context is
      // available to all atom types.
      $definition['formats'] = $types;
      foreach ($definition['formats'] as $type => $data) {
        $contexts[$type][$name] = $definition['title'];
      }
    }
    drupal_alter('scald_wysiwyg_context_list', $contexts);
  }
  return $contexts;
}

/**
 * Get the list of Scald contexts machine names that are allowed in WYSIWYG.
 */
function dnd_scald_wysiwyg_context_slugs() {
  $contexts_type = dnd_scald_wysiwyg_context_list();
  $slugs = array();
  foreach ($contexts_type as $contexts) {
    foreach ($contexts as $slug => $label) {
      $slugs[$slug] = $slug;
    }
  }
  return array_values($slugs);
}

/**
 * Implements hook_library().
 */
function dnd_library() {
  $path = drupal_get_path('module', 'dnd');
  $contexts = dnd_scald_wysiwyg_context_list();
  $config = array();
  foreach (dnd_scald_wysiwyg_context_slugs() as $slug) {
    $config[$slug] = scald_context_config_load($slug);
  }
  $qtip_settings = '';
  if (function_exists('qtip_fetch_instances_field')) {
    $instance = variable_get('dnd_qtip_instance', '');
    if (!empty($instance)) {
      $qtip_settings = qtip_clean_settings(qtip_load($instance));
    }
  }
  $libraries['library'] = array(
    'title' => 'DnD Library',
    'website' => 'http://drupal.org/project/scald',
    'version' => '1.x',
    'dependencies' => array(
      array(
        'system',
        'jquery.form',
      ),
    ),
    'js' => array(
      // Drag and drop
      $path . '/js/dnd-library.js' => array(),
      // Javascript workaround for the continue button.
      $path . '/js/dnd-modal.js' => array(),
      // Settings for the library url.
      array(
        'type' => 'setting',
        'data' => array(
          'dnd' => array(
            'url' => url(dnd_get_library()),
            'contexts' => $contexts,
            'contextDefault' => variable_get('dnd_context_default', 'sdl_editor_representation'),
            'usesCaptionDefault' => variable_get('dnd_uses_caption_default', TRUE),
            'contexts_config' => $config,
            'qTipSettings' => $qtip_settings,
          ),
        ),
      ),
    ),
    'css' => array(
      // Contains the library theming.
      $path . '/css/dnd-library.css' => array(
        'type' => 'file',
        'media' => 'screen',
      ),
    ),
  );

  // Add the qTip library as a dependency if it exists.
  if (function_exists('qtip_library')) {
    $libraries['library']['dependencies'][] = array(
      'qtip',
      'qtip',
    );
  }

  // Libraries might provide atom quick add links. We add CTools Modal JS so
  // that libraries can take use of it if they want.
  dnd_library_add_ctools_modal($libraries);
  $libraries['library']['dependencies'][] = array(
    'dnd',
    'ctools.modal',
  );
  return $libraries;
}

/**
 * Handle adding CTools Modal JavaScript files.
 *
 * It would be *really* nice if CTools implemented hook_library
 * and allowed us to simply list ctools.modal as a dependency.
 *
 * @see ctools_modal_add_js.
 */
function dnd_library_add_ctools_modal(&$libraries) {
  $ctools_path = drupal_get_path('module', 'ctools');
  $libraries['ctools.modal'] = array(
    'title' => 'CTools modal',
    'version' => '1.x',
  );
  $libraries['ctools.modal']['js'][$ctools_path . '/js/modal.js'] = array();
  $settings = array(
    'CToolsModal' => array(
      'loadingText' => t('Loading...'),
      'closeText' => t('Close Window'),
      'closeImage' => theme('image', array(
        'path' => ctools_image_path('icon-close-window.png'),
        'title' => t('Close window'),
        'alt' => t('Close window'),
      )),
      'throbber' => theme('image', array(
        'path' => ctools_image_path('throbber.gif'),
        'title' => t('Loading...'),
        'alt' => t('Loading'),
      )),
    ),
  );
  $libraries['ctools.modal']['js'][] = array(
    'type' => 'setting',
    'data' => $settings,
  );
  $modal_width = (double) variable_get('dnd_modal_width', 500);
  $modal_height = (double) variable_get('dnd_modal_height', 300);

  // Create our own javascript that will be used to theme a modal.
  $sample_style = array(
    'custom-style' => array(
      'modalSize' => array(
        'type' => $modal_width <= 1 ? 'scale' : 'fixed',
        'width' => $modal_width,
        'height' => $modal_height,
        'addWidth' => 20,
        'addHeight' => 15,
      ),
      'modalOptions' => array(
        'opacity' => 0.5,
        'background-color' => '#000',
      ),
      'animation' => 'fadeIn',
      'modalTheme' => 'CToolsSampleModal',
      'throbber' => theme('image', array(
        'path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'),
        'alt' => t('Loading...'),
        'title' => t('Loading'),
      )),
    ),
  );
  $libraries['ctools.modal']['js'][] = array(
    'type' => 'setting',
    'data' => $sample_style,
  );
  $libraries['ctools.modal']['css'][$ctools_path . '/css/modal.css'] = array(
    'type' => 'file',
  );
  $libraries['ctools.modal']['dependencies'][] = array(
    'system',
    'drupal.progress',
  );
  $libraries['ctools.modal']['dependencies'][] = array(
    'system',
    'drupal.ajax',
  );
  $libraries['ctools.modal']['css'][$ctools_path . '/ctools_ajax_sample/css/ctools-ajax-sample.css'] = array(
    'type' => 'file',
  );
  $libraries['ctools.modal']['js'][$ctools_path . '/ctools_ajax_sample/js/ctools-ajax-sample.js'] = array();
}

/**
 * Implements hook_library_alter().
 */
function dnd_library_alter(&$libraries, $module) {
  if ($module == 'mee' || $module == 'atom_reference') {
    $libraries['library']['dependencies'][] = array(
      'dnd',
      'library',
    );
  }
}

/**
 * Implements hook_entity_view_alter().
 *
 * Adds the dnd library in case we have quickedit enabled and dnd enabled on the entity.
 */
function dnd_entity_view_alter(&$build, $type) {
  if (!module_exists('quickedit')) {
    return;
  }
  if (!user_access('access in-place editing')) {
    return;
  }

  // In-place editing is only supported on the front-end.
  if (path_is_admin(current_path())) {
    return;
  }
  $dnd_enabled = FALSE;
  foreach ($build as $item) {
    if (!is_array($item)) {
      continue;
    }
    if (isset($item['#field_name'])) {
      $instance_info = field_info_instance($type, $item['#field_name'], $build['#bundle']);
      if (!empty($instance_info['settings']['dnd_enabled'])) {
        $dnd_enabled = TRUE;
      }
      if (isset($item['#field_type']) && $item['#field_type'] === 'atom_reference') {
        $dnd_enabled = TRUE;
      }
    }
  }
  if ($dnd_enabled) {
    $build['#attached']['library'][] = array(
      'dnd',
      'library',
    );
  }
  return;
}

/**
 * Tells DnD that the library shouldn't be displayed on this page.
 *
 * This function should be called whenever the library shouldn't be
 * displayed.
 *
 * @param boolean $set
 *   If FALSE, no change to the suppression status will be done, allowing
 *   other functions to query the suppression state. Defaults to TRUE.
 *
 * @return boolean
 *   TRUE if the library output has been suppressed, FALSE otherwise.
 */
function dnd_suppress_library($set = TRUE) {
  static $suppress = FALSE;
  if ($set && !$suppress) {
    $suppress = TRUE;
    drupal_add_js(array(
      'dnd' => array(
        'suppress' => 1,
      ),
    ), 'setting');
  }
  return $suppress;
}

/**
 * Theme the markup that will surround a library loaded via JSON.
 */
function theme_dnd_library_wrapper($variables) {
  return '<div id="' . $variables['settings']['library_id'] . '" class="dnd-library-wrapper"></div>';
}

/**
 * Return the list of all the available libraries.
 * @return array
 *   An associative array, where the keys are the library paths, and
 *   the value is an associated label.
 */
function dnd_get_libraries() {
  static $libraries = NULL;
  if (is_null($libraries)) {
    $libraries = module_invoke_all('dnd_libraries_info');
    drupal_alter('dnd_libraries_info', $libraries);
  }
  return $libraries;
}

/**
 * Return the default library.
 */
function dnd_get_library() {
  $libraries = dnd_get_libraries();
  $default = variable_get('dnd_callback_url', '');
  if (isset($libraries[$default])) {
    $library = $default;
  }
  else {
    reset($libraries);
    $library = key($libraries);
  }
  return $library;
}

Functions

Namesort descending Description
dnd_entity_view_alter Implements hook_entity_view_alter().
dnd_get_libraries Return the list of all the available libraries.
dnd_get_library Return the default library.
dnd_library Implements hook_library().
dnd_library_add_ctools_modal Handle adding CTools Modal JavaScript files.
dnd_library_alter Implements hook_library_alter().
dnd_menu Implementation of hook_menu().
dnd_permission Implementation of hook_perm().
dnd_scald_wysiwyg_context_list Get the list of Scald contexts that can be used in a WYSIWYG, keyed by type.
dnd_scald_wysiwyg_context_slugs Get the list of Scald contexts machine names that are allowed in WYSIWYG.
dnd_suppress_library Tells DnD that the library shouldn't be displayed on this page.
dnd_theme Implementation of hook_theme().
theme_dnd_library_wrapper Theme the markup that will surround a library loaded via JSON.

Constants

Namesort descending Description
DND_ID_SUFFIX