You are here

scald_dnd_library.module in Scald: Media Management made easy 6

Scald DnD Library

File

scald_dnd_library/scald_dnd_library.module
View source
<?php

/**
 * @file
 * Scald DnD Library
 */

/**
 * Implementation of hook_perm().
 */
function scald_dnd_library_perm() {
  return array(
    'access scald dnd library',
  );
}

/**
 * Implements hook_theme().
 */
function scald_dnd_library_theme() {
  return array(
    'sdl_header' => array(
      'arguments' => array(
        'page' => NULL,
        'count' => 0,
        'form' => NULL,
      ),
      'template' => 'sdl-header',
    ),
    'sdl_library' => array(
      'arguments' => array(
        'page' => NULL,
        'library_items' => NULL,
      ),
      'template' => 'sdl-library',
    ),
    'sdl_footer' => array(
      'arguments' => array(
        'page' => NULL,
        'count' => 0,
        'pager' => NULL,
      ),
      'template' => 'sdl-footer',
    ),
    'sdl_library_item' => array(
      'arguments' => array(
        'informations' => array(),
        'image' => NULL,
      ),
    ),
    'sdl_editor_item' => array(
      'arguments' => array(
        'informations' => array(),
        'image' => NULL,
      ),
    ),
    'sdl_editor_legend' => array(
      'arguments' => array(
        'atom' => array(),
      ),
    ),
    'sdl_preview_item' => array(
      'arguments' => array(
        'atom' => array(),
        'image' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_dnd_libraries_info.
 */
function scald_dnd_library_dnd_libraries_info() {
  $return = array();
  if (module_exists('views')) {
    $return['scald/library_dnd'] = t('Scald Library');
  }
  return $return;
}

/**
 * Adds an item in the library array.
 */
function scald_dnd_library_add_item(&$library, $sid) {
  $atom = scald_fetch($sid);
  $library['atoms'][$sid] = array(
    'meta' => array(
      'title' => $atom->title,
      'type' => $atom->type,
      'legend' => theme('sdl_editor_legend', $atom),
      'action' => $atom->base_entity->license->action_fin,
    ),
    'editor' => scald_render($atom, 'sdl_editor_representation'),
    'preview' => scald_render($atom, 'sdl_preview'),
    'library' => scald_render($atom, 'sdl_library_item'),
  );
}

/**
 * Implements hook_preprocess_sdl_library.
 */
function template_preprocess_sdl_library(&$variables) {
  if (is_object($variables['view'])) {
    $variables['library_items'] = array();
    $results = $variables['view']->result;
    foreach ($results as $result) {
      $sid = $result->sid;
      $variables['library_items'][$sid] = scald_render($sid, 'sdl_library_item');
    }
  }
  elseif (empty($variables['library_items']) && isset($variables['options'])) {
    $variables['library_items'] = $variables['options'];
  }
}

/**
 * Implements hook_scald_provider.
 */
function scald_dnd_library_scald_provider() {
  return array(
    'contexts' => array(
      'sdl_editor_representation' => array(
        'title' => t('Editor Representation'),
        'description' => t('The Editor Rep'),
        'render_language' => 'XHTML',
        'parseable' => TRUE,
        'formats' => array(
          'image' => array(
            'jpeg',
            'png',
            'passthrough',
          ),
          'audio' => array(
            'wav',
            'ogg',
            'mp3',
            'passthrough',
          ),
        ),
      ),
      'sdl_preview' => array(
        'title' => t('Preview Representation'),
        'description' => t('The Preview Rep'),
        'render_language' => 'XHTML',
        'parseable' => FALSE,
        'formats' => array(
          'image' => array(
            'jpeg',
            'png',
            'passthrough',
          ),
          'audio' => array(
            'wav',
            'ogg',
            'mp3',
            'passthrough',
          ),
        ),
      ),
      'sdl_library_item' => array(
        'title' => t('Library item'),
        'description' => t('The Library Rep'),
        'render_language' => 'XHTML',
        'parseable' => FALSE,
        'formats' => array(
          'image' => array(
            'jpeg',
            'png',
            'passthrough',
          ),
          'audio' => array(
            'wav',
            'ogg',
            'mp3',
            'passthrough',
          ),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_scald_render.
 */
function scald_dnd_library_scald_render($atom, $context, $options = array()) {
  if ($atom->rendered->file_transcoded_url) {
    $path = $atom->rendered->file_transcoded_url;
  }
  else {
    $path = $atom->rendered->thumbnail_source_url;
  }
  $attributes = array();
  if ($context == 'sdl_library_item') {
    $path .= strpos($path, '?') !== FALSE ? '&' : '?';
    $path .= 'dnd_id=' . $atom->sid;
    $attributes += array(
      'class' => 'drop',
      'draggable' => 'TRUE',
    );
  }
  elseif ($context == 'sdl_preview') {
    $path .= strpos($path, '?') !== FALSE ? '&' : '?';
    $path .= 'dnd_id=' . $atom->sid;
    $attributes += array(
      'class' => 'drop',
    );
  }
  else {
    $attributes += array(
      'class' => 'dnd-dropped',
    );
  }

  // Try to get the image size.
  if ($atom->rendered->file_transcoded_url && $atom->file_transcoded) {
    list($width, $height, $type, $attr) = @getimagesize($atom->file_transcoded);
  }
  elseif ($atom->thumbnail_source) {
    list($width, $height, $type, $attr) = @getimagesize($atom->thumbnail_source);
  }
  if ($width && $height) {
    $attributes += array(
      'width' => $width,
      'height' => $height,
    );
  }
  $image = "<img src='{$path}' alt='' " . drupal_attributes($attributes) . ' />';
  switch ($context) {
    case 'sdl_preview':
      $render = theme('sdl_preview_item', $atom, $image);
      break;
    case 'sdl_library_item':
      $render = theme('sdl_library_item', $atom, $image);
      break;
    default:
      $render = theme('sdl_editor_item', $atom->rendered, $image);
  }
  return $render;
}

/**
 * Returns HTML for an atom rendered in the "Library Item" context.
 */
function theme_sdl_library_item($atom, $image) {
  $informations = $atom->rendered;
  if (is_object($atom->base_entity) && is_numeric($atom->base_entity->nid)) {
    $view = l(t('View'), 'node/' . $atom->base_entity->nid, array(
      'attributes' => array(
        'target' => '_blank',
      ),
    ));
    $edit = l(t('Edit'), 'node/' . $atom->base_entity->nid . '/edit', array(
      'attributes' => array(
        'target' => '_blank',
      ),
    ));
    $links = "<div class='links'>{$view} / {$edit}</div>";
  }
  else {
    $links = '';
  }
  $return = "\n  <div class='image'>\n    {$image}\n  </div>";
  if (!empty($informations->player)) {
    $return .= "\n  <div class='player'>\n    {$informations->player}\n  </div>";
  }
  $return .= "\n  <div class='meta'>\n    <div class='type type-" . strtolower($informations->type) . "'></div>\n    <div class='title'>{$informations->title}</div>\n    <div class='author'>{$informations->authors[0]['link']}</div>\n    {$links}\n  </div>\n  <span class='clear' />\n  ";
  return $return;
}

/**
 * Returns HTML for an atom rendered in the "Editor Representation" context.
 */
function theme_sdl_editor_item($informations, $image) {
  $data = !empty($informations->player) ? $informations->player : $image;
  return "\n  <div class='image'>\n    {$data}\n  </div>\n  ";
}

/**
 * Returns HTML for the legend of an atom, used in the Editor Representation context.
 */
function theme_sdl_editor_legend($atom) {
  $informations = $atom->rendered;
  $by = isset($informations->authors[0]['link']) ? $informations->authors[0]['link'] : $informations->publisher['link'];
  $by = t('by %name', array(
    '%name' => $by,
  ));
  return "\n  <div class='meta'>\n    <!--copyright={$sid}-->{$informations->title}, {$by}<!--END copyright={$sid}-->\n  </div>\n  ";
}

/**
 * Returns HTML for an atom rendered in the "Preview" context.
 */
function theme_sdl_preview_item($atom, $image) {
  $resource_label = t('Resource');
  $informations_label = t('Informations');
  $resource = theme('sdl_editor_item', $atom->rendered, $image);
  $title_label = t('Title');
  $title_value = $atom->rendered->title;
  $author_label = t('Author');
  $author_name = $atom->rendered->authors[0]['link'];
  return "\n<div class='sdl-preview-item'>\n  <h3>{$resource_label}</h3>\n    {$resource}\n  <h3>{$informations_label}</h3>\n  <dl>\n    <dt>{$title_label}</dt>\n    <dd>{$title_value}</dd>\n    <dt>{$author_label}</dt>\n    <dd>{$author_name}</dt>\n  </dl>\n</div>\n  ";
}

/**
 * Implements hook_form_alter().
 */
function scald_dnd_library_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {

    // Check if the style plugin is scald_library.
    $view = $form['#parameters'][1]['view'];
    if ($view->display_handler
      ->get_option('style_plugin') == 'scald_library') {

      // Put the filters in a fieldset... Hackish.
      $filter = t('Filter');
      drupal_add_js('misc/collapse.js');
      $form['#prefix'] = "<fieldset class='collapsible collapsed'><legend>{$filter}</legend>";
      $form['#suffix'] = "</fieldset>";

      // Try to expose a sort based on the fields defined for this display.
      // That's hackish too, but won't be needed when Views 3 hits a stable
      // release, so let's say it's a temporary workaround.
      $fields = $view->field;
      $options = array();
      foreach ($fields as $name => $field) {
        if ($field->definition['click sortable']) {
          $options[$name] = $field->options['label'];
        }
      }
      if (count($options)) {
        $filter = t('Filter');
        $form['order'] = array(
          '#type' => 'select',
          '#title' => t('Sort'),
          '#options' => $options,
          '#default_value' => $_GET['order'],
        );
        $form['sort'] = array(
          '#type' => 'select',
          '#title' => '',
          '#options' => array(
            'desc' => t('Descending'),
            'asc' => t('Ascending'),
          ),
          '#default_value' => $_GET['sort'],
        );
        $form['submit']['#weight'] = 1;
        $form['reset'] = array(
          '#type' => 'markup',
          '#value' => "<input type='reset' value='" . t('Reset') . "' />",
          '#weight' => 2,
        );
      }
    }
  }
}

/**
 * Implements hook_views_api().
 */
function scald_dnd_library_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'scald_dnd_library') . '/includes',
  );
}

/**
 * Provides default ImageCache presets that can be overridden by site
 * administrators.
 *
 * @return array
 *   An array of imagecache preset definitions. Each definition can be
 *   generated by exporting a preset from the database. Each preset
 *   definition should be keyed on its presetname (for easier interaction
 *   with drupal_alter) and have the following attributes:
 *     "presetname": the imagecache preset name. Required.
 *     "actions": an array of action defintions for this preset. Required.
 */
function scald_dnd_library_imagecache_default_presets() {
  $presets = array();
  $presets['Library'] = array(
    'presetname' => 'Library',
    'actions' => array(
      0 => array(
        'weight' => '0',
        'module' => 'imagecache',
        'action' => 'imagecache_scale',
        'data' => array(
          'width' => '48',
          'height' => '',
          'upscale' => 0,
        ),
      ),
    ),
  );
  return $presets;
}

Functions

Namesort descending Description
scald_dnd_library_add_item Adds an item in the library array.
scald_dnd_library_dnd_libraries_info Implements hook_dnd_libraries_info.
scald_dnd_library_form_alter Implements hook_form_alter().
scald_dnd_library_imagecache_default_presets Provides default ImageCache presets that can be overridden by site administrators.
scald_dnd_library_perm Implementation of hook_perm().
scald_dnd_library_scald_provider Implements hook_scald_provider.
scald_dnd_library_scald_render Implements hook_scald_render.
scald_dnd_library_theme Implements hook_theme().
scald_dnd_library_views_api Implements hook_views_api().
template_preprocess_sdl_library Implements hook_preprocess_sdl_library.
theme_sdl_editor_item Returns HTML for an atom rendered in the "Editor Representation" context.
theme_sdl_editor_legend Returns HTML for the legend of an atom, used in the Editor Representation context.
theme_sdl_library_item Returns HTML for an atom rendered in the "Library Item" context.
theme_sdl_preview_item Returns HTML for an atom rendered in the "Preview" context.