You are here

rotor.module in Rotor Banner 5.7

Same filename and directory in other branches
  1. 5 rotor.module
  2. 6.2 rotor.module
  3. 6 rotor.module
  4. 7 rotor.module

A rotor banner consists in a set of images that will be changing. This module is made using jquery.

@author Nestor Mata Cuthbert, nestor@achieveinternet.com. http://www.achieveinternet.com

File

rotor.module
View source
<?php

/**
 * @file
 * A rotor banner consists in a set of images that will be changing.
 * This module is made using jquery.
 * 
 * @author Nestor Mata Cuthbert, nestor@achieveinternet.com. http://www.achieveinternet.com
 */

// CONSTANTS
define('ROTOR_GROUP_TABS', 0);
define('ROTOR_DONT_GROUP_TABS', 1);

/**
 * Node Info Hook
 */
function rotor_node_info() {
  return array(
    'rotor_item' => array(
      'name' => t('Rotor Item'),
      'module' => 'rotor',
      'description' => t("A node to be presented in the rotor block."),
    ),
  );
}

/**
 * Perm hook
 */
function rotor_perm() {
  return array(
    'administer rotor',
  );
}

/**
 * Access hook
 */
function rotor_access($op, $node) {
  return user_access('administer rotor');
}

/**
 * Help hook
 */
function rotor_help($section) {
  switch ($section) {
    case 'node/add/rotor-item':
      $text = '<p>' . t('A rotor item is a banner that will appear in the rotor block for advertising' . ' or display important information or images.<br />' . 'The rotor item will have a tab text that can be configured to be shown or not' . ' in the administration page for the rotor.<br />' . 'The item will show the image if this one is chosen otherwise will show the' . ' content.') . '</p>';
      if (user_access('administer rotor')) {
        $text .= t('You can go to the rotor administration page <a href="@link">here</a>', array(
          '@link' => url('admin/settings/rotor'),
        ));
      }
      return $text;
    case 'admin/settings/rotor':
      return t('');
  }
}

/**
 * Menu Hook
 */
function rotor_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/rotor',
      'title' => t('Administer the Rotor Banner'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'rotor_admin_form',
      ),
      'access' => user_access('administer rotor'),
    );
  }
  return $items;
}

/**
 * Block hook
 */
function rotor_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('The rotor banner block'),
        'weight' => 0,
        'enabled' => 0,
        'region' => 'content',
      );
      return $blocks;
    case 'view':

      // Only one delta so far so we are not checking the delta
      $block = array(
        'subject' => t('Information'),
        'content' => rotor_block_content(),
      );
      return $block;
  }
}

/**
 * Admin settings form page.
 */
function rotor_admin_form() {
  $form['max_items'] = array(
    '#type' => 'textfield',
    '#title' => t('Max nodes'),
    '#default_value' => variable_get('rotor_max_items', 3),
    '#rows' => 1,
    '#size' => 2,
    '#description' => t('Define the maximun number of nodes to present in the rotor block.'),
  );
  $form['time'] = array(
    '#type' => 'textfield',
    '#title' => t('Time'),
    '#default_value' => variable_get('rotor_seconds', 10),
    '#rows' => 1,
    '#size' => 2,
    '#description' => t('The time in seconds that will be shown every rotor item before change to the next one.'),
  );
  $form['show_tab'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable tabs'),
    '#default_value' => variable_get('rotor_show_tabs', TRUE),
    '#description' => t('Shows/Hide the item tabs in the block'),
  );
  $form['group_tabs'] = array(
    '#type' => 'radios',
    '#title' => t('Group tabs'),
    '#default_value' => variable_get('rotor_group_tabs', ROTOR_GROUP_TABS),
    '#options' => array(
      ROTOR_GROUP_TABS => t('Group tabs'),
      ROTOR_DONT_GROUP_TABS => t('Each tab with item'),
    ),
    '#description' => t('If tabs are grups this will be presented all the tabs toguether otherwise each tab will be with each item.'),
  );
  $form['#suffix'] = theme('rotor_admin_list', rotor_get_all_items());
  return system_settings_form($form);
}

/**
 * Admin settings form submit function
 */
function rotor_admin_form_submit($formid, $values, $extra = null) {
  if ($formid == 'rotor_admin_form') {
    if ($values['op'] == $values['submit']) {
      variable_set('rotor_max_items', $values['max_items']);
      variable_set('rotor_seconds', $values['time']);
      variable_set('rotor_show_tabs', $values['show_tab']);
      variable_set('rotor_group_tabs', $values['group_tabs']);
      drupal_set_message(t('Settings saved'));
    }
    else {
      variable_set('rotor_max_items', 3);
      variable_set('rotor_seconds', 10);
      variable_set('rotor_show_tabs', TRUE);
      variable_set('rotor_group_tabs', ROTOR_GROUP_TABS);
      drupal_set_message(t('Settings back to defaults'));
    }
  }
}

/**
 * Node form hook
 */
function rotor_form($node) {

  // Get metadata
  $type = node_get_types('type', $node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Tab Text'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
    '#description' => t('The text that will be shown in the tab for this item.'),
  );
  $image = $node->rotor_image ? theme('image', file_create_url($node->rotor_image), '', '', NULL, FALSE) : '';
  $form['rotor_image'] = array(
    '#type' => 'file',
    '#title' => t('Attach an image'),
    '#default_value' => $node->rotor_image,
    '#weight' => -4,
    '#prefix' => $image,
    '#description' => t('The image that will be shown in the rotor content.' . ' If an image is uploaded only the image will be shown, otherwise only the content.'),
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Content'),
    '#default_value' => $node->body,
    '#rows' => 10,
    '#weight' => -3,
    '#description' => t('The content that will be shown in case no image is uploaded.'),
  );
  $form['body_filter']['filter'] = filter_form($node->format);

  // Change the enctype of the form to handle the upload file.
  $form['#attributes']['enctype'] = 'multipart/form-data';
  return $form;
}

/**
 * Node form submit function.
 * We handle the image submition.
 */
function rotor_submit(&$node) {
  if (!($file = file_save_upload('rotor_image', file_directory_path(), FILE_EXISTS_RENAME))) {
    watchdog('rotor', 'Imaged not saved');
    form_set_error('rotor_image', t('File upload failed.'));
  }
  else {
    watchdog('rotor', 'Imaged saved:' . $file->file_path);
    $node->rotor_image = $file->filepath;
  }
}

/**
 * Hook Insert
 */
function rotor_insert($node) {
  db_query("INSERT INTO {rotor_item} (nid, file_path) VALUES (%d, '%s')", $node->nid, $node->rotor_image);
}

/**
 * Hook Update
 */
function rotor_update($node) {
  db_query("UPDATE {rotor_item} SET file_path = '%s' WHERE nid = %d", $node->rotor_image, $node->nid);
}

/**
 * Hook Delete
 */
function rotor_delete($node) {
  db_query("DELETE FROM {rotor_item} WHERE nid = %d", $node->nid);
}

/**
 * Node Load hook
 */
function rotor_load($node) {
  $additions = db_fetch_object(db_query('SELECT file_path AS rotor_image FROM {rotor_item} WHERE nid = %d', $node->nid));
  return $additions;
}

/**
 * Node view hook
 */
function rotor_view($node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node, $teaser);
  if ($node->rotor_image) {
    $node->content['body']['#value'] = theme('image', file_create_url($node->rotor_image), '', '', NULL, FALSE);
  }
  return $node;
}

/**
 * Returns the block content.
 */
function rotor_block_content() {
  $limit = variable_get('rotor_max_items', 3);
  $items = array();
  $result = db_query("SELECT nid FROM {node} WHERE type = 'rotor_item' LIMIT %d", $limit);
  if ($result) {
    while ($node = db_fetch_array($result)) {
      $items[] = node_load($node);
    }
  }
  return theme('rotor_block', $items);
}

/**
 * Return a list of nodes of all the rotor_item nodes.
 */
function rotor_get_all_items() {
  $items = array();
  $result = db_query("SELECT nid FROM {node} WHERE type = 'rotor_item'");
  if ($result) {
    while ($node = db_fetch_array($result)) {
      $items[] = node_load($node);
    }
  }
  return $items;
}

/**
 * Theme function for the block
 *
 * @param array $items The list of nodes to present in the block.
 */
function theme_rotor_block($items = array()) {
  if (count($items) == 0) {
    return '';
  }

  // Prints the script variables and includes the rotor javascript file.
  $time = variable_get('rotor_seconds', 10);
  drupal_add_js(drupal_get_path('module', 'rotor') . '/rotor.js');
  drupal_add_js('var rotor_enabled = true;', 'inline');
  drupal_add_js('var rotor_time = ' . $time . ';', 'inline');

  // Print the rotor items.
  $output = '<div id="rotor">';
  if (variable_get('rotor_group_tabs', ROTOR_GROUP_TABS) == ROTOR_GROUP_TABS) {
    $output .= theme('rotor_tabs', $items);
    $output .= theme('rotor_items', $items);
  }
  else {
    $output .= theme('rotor_items', $items);
  }
  $output .= '</div>';
  return $output;
}

/**
 * Theme for the rotor tabs.
 *
 * @param array $items The array of items from where to get the tabs.
 */
function theme_rotor_tabs($items = array()) {
  $show_tabs = variable_get('rotor_show_tabs', TRUE);
  $group_tabs = variable_get('rotor_group_tabs', ROTOR_GROUP_TABS);
  $output = '';
  if ($show_tabs && $group_tabs == ROTOR_GROUP_TABS) {
    $output = '<div class="rotor_tabs">';
    foreach ($items as $item) {
      $output .= '<div class="rotor_tab">' . $item->title . '</div>';
    }
    $output .= '</div>';
  }
  return $output;
}

/**
 * Theme for the rotor items list.
 *
 * @param array $items The array of items.
 */
function theme_rotor_items($items = array()) {
  $output = '';
  foreach ($items as $item) {
    $output .= theme('rotor_item', $item);
  }
  return $output;
}

/**
 * Theme for each rotor item.
 *
 * @param node $item The rotor_item node to theme.
 */
function theme_rotor_item($item) {
  $show_tabs = variable_get('rotor_show_tabs', TRUE);
  $group_tabs = variable_get('rotor_group_tabs', ROTOR_GROUP_TABS);
  $output = '<div class="rotor_content">';
  if ($show_tabs && $group_tabs == ROTOR_DONT_GROUP_TABS) {
    $output .= '<div class="rotor_tab">' . $item->title . '</div>';
  }
  $output .= '<div class="rotor_content_detail">' . ($item->rotor_image ? theme('image', file_create_url($item->rotor_image), '', '', NULL, FALSE) : $item->body) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Theme the admin list to include in the rotor administration page.
 *
 * @param array $list The list of rotor_item nodes to display in the list.
 */
function theme_rotor_admin_list($list = array()) {
  $headers = array(
    t('Tab'),
    t('Content'),
    t('Actions'),
  );
  $rows = array();
  $count = 0;
  foreach ($list as $item) {
    if ($item->rotor_image) {
    }
    $rows[] = array(
      l($item->title, 'node/' . $item->nid . '/edit'),
      $item->rotor_image ? theme('image', file_create_url($item->rotor_image), '', '', NULL, FALSE) : $item->body,
      l(t('Edit'), 'node/' . $item->nid . '/edit') . ' | ' . l(t('Remove'), 'node/' . $item->nid . '/delete'),
    );
  }
  $output = l(t('Add new item'), 'node/add/rotor-item');
  $output .= theme('table', $headers, $rows, array(
    'class' => 'rotor_admin_list',
  ));
  return $output;
}

Functions

Namesort descending Description
rotor_access Access hook
rotor_admin_form Admin settings form page.
rotor_admin_form_submit Admin settings form submit function
rotor_block Block hook
rotor_block_content Returns the block content.
rotor_delete Hook Delete
rotor_form Node form hook
rotor_get_all_items Return a list of nodes of all the rotor_item nodes.
rotor_help Help hook
rotor_insert Hook Insert
rotor_load Node Load hook
rotor_menu Menu Hook
rotor_node_info Node Info Hook
rotor_perm Perm hook
rotor_submit Node form submit function. We handle the image submition.
rotor_update Hook Update
rotor_view Node view hook
theme_rotor_admin_list Theme the admin list to include in the rotor administration page.
theme_rotor_block Theme function for the block
theme_rotor_item Theme for each rotor item.
theme_rotor_items Theme for the rotor items list.
theme_rotor_tabs Theme for the rotor tabs.

Constants