You are here

shrinktheweb.module in ShrinkTheWeb 7

Same filename and directory in other branches
  1. 8 shrinktheweb.module
  2. 6 shrinktheweb.module

File

shrinktheweb.module
View source
<?php

/**
 * Implementation of hook_help()
 */
function shrinktheweb_help($path, $arg) {
  switch ($path) {
    case 'admin/help#shrinktheweb':
      return '<p>' . t('You can find the latest help information for the ShrinkTheWeb module at !url .', array(
        '!url' => '<a href="http://drupal.org/node/1067900" target="_blank">http://drupal.org/node/1067900</a>',
      )) . '</p>';
  }
}

/**
 * Implementation of hook_permission().
 */
function shrinktheweb_permission() {
  return array(
    'administer ShrinkTheWeb settings' => array(
      'title' => t('Administer ShrinkTheWeb settings'),
      'description' => t('Set API keys and other configuration options for the ShrinkTheWeb module.'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function shrinktheweb_menu() {
  $items = array();
  $items['admin/config/media/shrinktheweb'] = array(
    'title' => 'ShrinkTheWeb',
    'description' => 'Configure API settings such as public, private keys, thumbnails cache and other',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'shrinktheweb_admin_settings',
    ),
    'access arguments' => array(
      'administer ShrinkTheWeb settings',
    ),
    'file' => 'shrinktheweb.admin.inc',
  );
  $items['admin/config/media/shrinktheweb/settings'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/media/shrinktheweb/log'] = array(
    'title' => 'Log',
    'description' => 'Browse the request records',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'shrinktheweb_admin_log',
    ),
    'access arguments' => array(
      'administer ShrinkTheWeb settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
    'file' => 'shrinktheweb.admin.inc',
  );
  return $items;
}

/**
 * Generates or returns the path/source of the thumbnail
 *
 * @param string $url The URL of the web page
 * @param array $options Overwrite the options set in admin config
 * @access public
 *
 * @return mixed Thumbnail path/source or FALSE
 */
function shrinktheweb_get_thumbnail($url, $options = array()) {
  module_load_include('inc', 'shrinktheweb', 'shrinktheweb.api');
  $sImageURL = shrinktheweb_getThumbnailURL($url, $options);
  return $sImageURL;
}

/**
 * Implementation of hook_theme()
 */
function shrinktheweb_theme($existing, $type, $theme, $path) {
  return array(
    'shrinktheweb_image' => array(
      'variables' => array(
        'url' => NULL,
        'options' => array(),
      ),
    ),
    'shrinktheweb_image_link' => array(
      'variables' => array(
        'url' => NULL,
        'options' => array(),
      ),
    ),
    // -- Link module ----------------------------
    'shrinktheweb_formatter_shrinktheweb_link_no_text' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_default' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_plain' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_url' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_short' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_label' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_separate' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_image' => array(
      'variables' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Displays the thumbnail.
 *
 * @param mixed $url
 * @param array $options.
 * @access public
 *
 * @return mixed link with the image tag or FALSE on error
 */
function theme_shrinktheweb_image($variables) {
  $url = $variables['url'];
  $options = $variables['options'];
  if ($src = shrinktheweb_get_thumbnail($url)) {
    $options['path'] = $src;
    $options['attributes'] = !empty($options['attributes']) ? $options['attributes'] : array();
    return theme_image($options);
  }
  return FALSE;
}

/**
 * Displays the thumbnail as a link.
 *
 * @param mixed $url
 * @param array $options.
 * @access public
 *
 * @return mixed link with the image tag or FALSE on error
 */
function theme_shrinktheweb_image_link($variables) {
  $url = $variables['url'];
  $options = $variables['options'];
  if ($img = theme('shrinktheweb_image', $variables)) {
    $options['html'] = TRUE;
    $options['attributes'] = !empty($options['attributes']) ? $options['attributes'] : array();
    return l($img, $url, $options);
  }
  return FALSE;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function shrinktheweb_field_formatter_info() {
  return array(
    // -- Link module ----------------------------
    'shrinktheweb_link_no_text' => array(
      'label' => t('[ShrinkTheWeb] No link text'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_default' => array(
      'label' => t('[ShrinkTheWeb] Title, as link'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_url' => array(
      'label' => t('[ShrinkTheWeb] URL, as link'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_plain' => array(
      'label' => t('[ShrinkTheWeb] URL, as plain text'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_short' => array(
      'label' => t('[ShrinkTheWeb] Short, as link with title "Link"'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_label' => array(
      'label' => t('[ShrinkTheWeb] Label, as link with label as title'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_separate' => array(
      'label' => t('[ShrinkTheWeb] Separate title and URL'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
    'shrinktheweb_link_image' => array(
      'label' => t('[ShrinkTheWeb] Image, as link'),
      'field types' => array(
        'link_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function shrinktheweb_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  foreach ($items as $delta => $item) {
    $elements[$delta] = array(
      '#markup' => theme('shrinktheweb_formatter_' . $display['type'], array(
        'element' => $item,
        'field' => $instance,
      )),
    );
  }
  return $elements;
}

/**
 * Theme function for 'no text' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_no_text($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return $preview;
}

/**
 * Theme function for 'default' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_default($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));

  // Display a normal link if both title and URL are available.
  if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) {
    return $preview . l($vars['element']['title'], $vars['element']['url'], array(
      'attributes' => $vars['element']['attributes'],
    ));
  }
  elseif (!empty($vars['element']['title'])) {
    return check_plain($vars['element']['title']);
  }
  elseif (!empty($vars['element']['url'])) {
    return $preview . l($vars['element']['title'], $vars['element']['url'], array(
      'attributes' => $vars['element']['attributes'],
    ));
  }
}

/**
 * Theme function for 'plain' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_plain($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return empty($vars['element']['url']) ? check_plain($vars['element']['title']) : $preview . url($vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
  ));
}

/**
 * Theme function for 'url' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_url($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return $vars['element']['url'] ? $preview . l($vars['element']['display_url'], $vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
  )) : '';
}

/**
 * Theme function for 'short' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_short($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return $vars['element']['url'] ? $preview . l(t('Link'), $vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
  )) : '';
}

/**
 * Theme function for 'label' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_label($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return $vars['element']['url'] ? $preview . l($vars['field']['label'], $vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
  )) : '';
}

/**
 * Theme function for 'separate' text field formatter.
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_separate($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  $class = empty($vars['element']['attributes']['class']) ? '' : ' ' . $vars['element']['attributes']['class'];
  unset($vars['element']['attributes']['class']);
  $title = empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
  $output = '';
  $output .= '<div class="link-item ' . $class . '">';
  if (!empty($title)) {
    $output .= '<div class="link-title">' . $title . '</div>';
  }
  if (!empty($preview)) {
    $output .= '<div class="link-preview">' . $preview . '</div>';
  }
  $output .= '<div class="link-url">' . l($vars['element']['url'], $vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
  )) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Theme Screenshot image as link
 */
function theme_shrinktheweb_formatter_shrinktheweb_link_image($vars) {
  $preview = theme('shrinktheweb_image', array(
    'url' => $vars['element']['url'],
    'options' => array(
      'attributes' => array(
        'class' => array(
          'shrinktheweb',
        ),
      ),
    ),
  ));
  return $vars['element']['url'] ? l($preview, $vars['element']['url'], array(
    'attributes' => $vars['element']['attributes'],
    'html' => TRUE,
  )) : '';
}

/**
 * Implementation of hook_permission().
 */
function shrinktheweb_file_download($uri) {
  if (preg_match('/^' . file_default_scheme() . ':\\/\\/' . variable_get('shrinktheweb_thumbs_folder', '') . '(.*?)\\.jpg$/si', $uri)) {
    $info = image_get_info($uri);
    return array(
      'Content-Type' => $info['mime_type'],
    );
  }
}

Functions

Namesort descending Description
shrinktheweb_field_formatter_info Implementation of hook_field_formatter_info().
shrinktheweb_field_formatter_view Implements hook_field_formatter_view().
shrinktheweb_file_download Implementation of hook_permission().
shrinktheweb_get_thumbnail Generates or returns the path/source of the thumbnail
shrinktheweb_help Implementation of hook_help()
shrinktheweb_menu Implementation of hook_menu().
shrinktheweb_permission Implementation of hook_permission().
shrinktheweb_theme Implementation of hook_theme()
theme_shrinktheweb_formatter_shrinktheweb_link_default Theme function for 'default' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_image Theme Screenshot image as link
theme_shrinktheweb_formatter_shrinktheweb_link_label Theme function for 'label' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_no_text Theme function for 'no text' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_plain Theme function for 'plain' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_separate Theme function for 'separate' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_short Theme function for 'short' text field formatter.
theme_shrinktheweb_formatter_shrinktheweb_link_url Theme function for 'url' text field formatter.
theme_shrinktheweb_image Displays the thumbnail.
theme_shrinktheweb_image_link Displays the thumbnail as a link.