You are here

dnd_test.module in Scald: Media Management made easy 6

File

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

/**
 * Implementation of hook_menu().
 */
function dnd_test_menu() {
  $items = array();
  $items['dnd-test/library'] = array(
    'title' => 'Drag and drop test library',
    'page callback' => 'dnd_test_dnd_library',
    'access arguments' => array(
      'access dnd test library',
    ),
  );
  return $items;
}

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

/**
 * Implementation of hook_form_alter().
 *
 * This demonstrates how to attach Drag and Drop to a given textarea.
 */
function dnd_test_form_alter(&$form, &$form_state) {
  if ($form['#id'] == 'node-form' && $form['type']['#value'] == 'page') {
    $form['body_field']['body']['#dnd-enabled'] = TRUE;
    $form['body_field']['body']['#dnd-settings'] = array(
      'drop_selector' => '#edit-body-dnd-library .drop',
      'url' => 'dnd-test/library/?json',
    );
    $form['body_field']['body']['#rows'] = 28;
  }
}

/**
 * Implementation of hook_theme().
 */
function dnd_test_theme() {
  return array(
    'dnd_editor_item' => array(
      'arguments' => array(
        'i' => NULL,
        'size' => NULL,
      ),
      'template' => 'dnd-editor-item',
    ),
    'dnd_library_item' => array(
      'arguments' => array(
        'i' => NULL,
      ),
      'template' => 'dnd-library-item',
    ),
    'dnd_library_preview' => array(
      'arguments' => array(
        'i' => NULL,
      ),
      'template' => 'dnd-library-preview',
    ),
    'dnd_library_header' => array(
      'arguments' => array(
        'page' => NULL,
      ),
      'template' => 'dnd-library-header',
    ),
    'dnd_library_footer' => array(
      'arguments' => array(
        'page' => NULL,
      ),
      'template' => 'dnd-library-footer',
    ),
  );
}

/**
 * Overloaded page callback.
 */
function dnd_test_dnd_library($element = NULL) {
  $page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
  drupal_add_css(drupal_get_path('module', 'dnd_test') . '/dnd_test.css');
  drupal_json(dnd_test_generate_library($page));
}

/**
 * Create contrived output
 */
function dnd_test_generate_library($page = 1, $limit = 8) {
  $start = $page * $limit - $limit;
  $end = $page * $limit;
  $library = theme('dnd_library_header', $page);
  $editor_representations = array();
  $library_previews = array();
  for ($i = $start + 1; $i < $end + 1; $i++) {
    $library .= theme('dnd_library_item', $i);
    $editor_representations += dnd_editor_items($i);
    $library_previews['dnd-test-' . $i] = theme('dnd_library_preview', $i);
  }
  $library .= theme('dnd_library_footer', $page);
  return array(
    'library' => $library,
    'editor_representations' => $editor_representations,
    'library_previews' => $library_previews,
  );
}

/**
 * Theme wrapper that spins out multiple library representations for a given
 * editor representation.  This is because we want to demonstrate how to allow
 * for multiple versions (i.e. different sizes) of a single item
 */
function dnd_editor_items($i) {
  $item = array();
  foreach (array(
    t('S'),
    t('M'),
    t('L'),
  ) as $size) {
    $item[$i . '-' . $size] = array(
      'body' => theme('dnd_editor_item', $i, $size),
      'title' => 'Item ' . $i . '-S',
    );
  }
  return $item;
}

/**
 * Completely contrived library preview theme function
 */
function template_preprocess_dnd_library_preview(&$variables) {
  template_preprocess_dnd_library_item($variables);
  $variables['id'] = 'dnd-preview-' . $variables['i'];
  $variables['image'] = '<img src="http://' . $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') . '/img/item-' . $variables['img_num'] . '-S.jpg" class="drop" width="125" height="94" />';
  $variables['description'] = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
}

/**
 * Completely contrived edit item theme function
 */
function template_preprocess_dnd_library_item(&$variables) {
  global $_SERVER;
  $i = $variables['i'];
  if ($i % 3 == 0) {
    $img = 3;
  }
  else {
    if ($i % 2 == 0) {
      $img = 2;
    }
    else {
      $img = 1;
    }
  }
  $variables['img_num'] = $img;
  $variables['id'] = 'dnd-test-' . $i;
  $variables['image'] = '<img src="http://' . $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') . '/img/item-' . $img . '-thumb.jpg?dnd_id=' . $i . '-M" class="drop" />';
  $variables['title'] = t('Lorem Ipsum @count', array(
    '@count' => $i,
  ));
  $variables['date'] = 'Feb 18 2009';
  $variables['author'] = 'David Eads';
  foreach (array(
    t('S'),
    t('M'),
    t('L'),
  ) as $size) {
    $sizes[] = '<img src="http://' . $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') . '/img/icon/' . $size . '.png?dnd_id=' . $i . '-' . $size . '" class="drop" />';
  }
  $variables['sizes'] = '<ul><li>' . implode('</li><li>', $sizes) . '</li></ul>';
}
function template_preprocess_dnd_editor_item(&$variables) {
  list($i, $size) = array(
    $variables['i'],
    $variables['size'],
  );
  if ($i % 3 == 0) {
    $img = 3;
  }
  else {
    if ($i % 2 == 0) {
      $img = 2;
    }
    else {
      $img = 1;
    }
  }
  $variables['image'] = theme('image', drupal_get_path('module', 'dnd_test') . '/img/item-' . $img . '-' . $size . '.jpg', '', '', array(
    'class' => 'dnd-dropped',
    'id' => 'dnd-id-' . $i,
  ));
}
function template_preprocess_dnd_library_header(&$variables) {
}
function template_preprocess_dnd_library_footer(&$variables) {
}

Functions

Namesort descending Description
dnd_editor_items Theme wrapper that spins out multiple library representations for a given editor representation. This is because we want to demonstrate how to allow for multiple versions (i.e. different sizes) of a single item
dnd_test_dnd_library Overloaded page callback.
dnd_test_form_alter Implementation of hook_form_alter().
dnd_test_generate_library Create contrived output
dnd_test_menu Implementation of hook_menu().
dnd_test_perm Implementation of hook_perm().
dnd_test_theme Implementation of hook_theme().
template_preprocess_dnd_editor_item
template_preprocess_dnd_library_footer
template_preprocess_dnd_library_header
template_preprocess_dnd_library_item Completely contrived edit item theme function
template_preprocess_dnd_library_preview Completely contrived library preview theme function