You are here

flipping_book.module in Flipping Book 7

Same filename and directory in other branches
  1. 8 flipping_book.module

Manage Flipping Book packages into Drupal

File

flipping_book.module
View source
<?php

/**
 * @file
 * Manage Flipping Book packages into Drupal
 */

/**
 * Defining constants.
 */
define('FLIPPING_BOOK_MANAGEMENT_PATH', 'admin/config/media/flipping_book');

/**
 * Implments hook_permission().
 */
function flipping_book_permission() {
  return array(
    'administer flipping_book' => array(
      'title' => t('Administer Flipping Book'),
    ),
    'upload flipping_book' => array(
      'title' => t('Upload Flipping Books'),
    ),
    'access private flipping_books' => array(
      'title' => t('Access private Flipping Books'),
      'description' => t('Allow access to flipping books imported into the private folder.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function flipping_book_menu() {
  $module_path = drupal_get_path('module', 'flipping_book');
  $items = array();
  $items[FLIPPING_BOOK_MANAGEMENT_PATH] = array(
    'title' => 'Flipping Book',
    'description' => 'Import, delete your Flipping Books',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'flipping_book_management_form',
    ),
    'access arguments' => array(
      'upload flipping_book',
    ),
    'file' => 'flipping_book.pages.inc',
    'file path' => $module_path . '/includes',
  );
  $items[FLIPPING_BOOK_MANAGEMENT_PATH . '/import'] = array(
    'title' => 'Import',
    'access arguments' => array(
      'upload flipping_book',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items[FLIPPING_BOOK_MANAGEMENT_PATH . '/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'flipping_book_settings_form',
    ),
    'access arguments' => array(
      'administer flipping_book',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'flipping_book.admin.inc',
    'file path' => $module_path . '/includes',
  );
  $items[FLIPPING_BOOK_MANAGEMENT_PATH . '/%flipping_book/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'flipping_book_delete_confirm',
      4,
    ),
    'access arguments' => array(
      'administer flipping_book',
    ),
    'type' => MENU_CALLBACK,
    'context' => MENU_CONTEXT_INLINE,
    'file' => 'flipping_book.admin.inc',
    'file path' => $module_path . '/includes',
  );
  return $items;
}

/**
 * Loads flipping books from the database.
 *
 * This function should be used whenever you need to load more than one
 * flipping_book from the database. Flipping books are loaded into memory
 * and will not require database access if loaded again during the same page
 * request.
 *
 * @param array $fbids
 *   An array of flipping books id.
 *
 * @return array
 *   An array of flipping books objects indexed by fbid.
 */
function flipping_book_load_multiple(array $fbids) {
  $flipping_books = array();
  foreach ($fbids as $fbid) {
    $flipping_books[$fbid] = flipping_book_load($fbid);
  }
  return $flipping_books;
}

/**
 * Load a flipping_book from the database.
 *
 * @param int $fbid
 *   The Flipping Book ID.
 *
 * @return object
 *   A fully-populated flipping_book object, or FALSE if the flipping_book
 *   is not found.
 */
function flipping_book_load($fbid) {
  $flipping_books =& drupal_static(__FUNCTION__);
  if (!isset($flipping_books[$fbid])) {
    $flipping_book = db_select('flipping_book', 'f')
      ->fields('f')
      ->condition('f.fbid', $fbid, '=')
      ->execute()
      ->fetchObject();
    if (empty($flipping_book)) {
      return FALSE;
    }
    $flipping_book->uri = flipping_book_dir() . '/' . $flipping_book->dir . '/index.html';
    $flipping_book->url = file_create_url($flipping_book->uri);
    $flipping_book->path = preg_replace('/^\\/?/', '', parse_url($flipping_book->url, PHP_URL_PATH));
    $flipping_books[$fbid] = $flipping_book;
  }
  return $flipping_books[$fbid];
}

/**
 * Delete a flipping_book.
 *
 * @param int $fbid
 *   The Flipping Book ID.
 *
 * @return bool
 */
function flipping_book_delete($fbid) {
  $flipping_book = flipping_book_load($fbid);
  $path = pathinfo(drupal_realpath($flipping_book->uri), PATHINFO_DIRNAME);
  if (file_unmanaged_delete_recursive($path)) {
    $result = db_delete('flipping_book')
      ->condition('fbid', $fbid, '=')
      ->execute();
  }
  else {
    $result = FALSE;
  }
  return $result;
}

/**
 * Implements hook_views_api().
 */
function flipping_book_views_api() {
  return array(
    "api" => "3.0",
  );
}

/**
 * Helper function: get flipping book import directory.
 */
function flipping_book_dir() {
  $dir =& drupal_static(__FUNCTION__);
  if (empty($dir)) {
    $dir = variable_get('flipping_book_schema', 'public') . '://flipping_book';
  }
  return $dir;
}

/**
 * Helper function: creates flipping book import directory.
 */
function flipping_book_prepare_directory() {
  $directory = flipping_book_dir();
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
}

/**
 * Implements hook_file_download().
 */
function flipping_book_file_download($uri) {
  if (arg(2) != 'flipping_book') {
    return NULL;
  }
  $file_path = drupal_realpath($uri);
  $denied = !file_exists($file_path) || !is_file($file_path) || !user_access('access private flipping_books');

  // Access specifically denied.
  if ($denied) {
    return -1;
  }
  $file = new stdClass();
  $uri_chunks = explode('/', $uri);
  $file->filename = end($uri_chunks);
  $file->filemime = file_get_mimetype($uri);
  $file->filesize = filesize($file_path);

  // Access is granted.
  $headers = file_get_content_headers($file);
  return $headers;
}

Functions

Namesort descending Description
flipping_book_delete Delete a flipping_book.
flipping_book_dir Helper function: get flipping book import directory.
flipping_book_file_download Implements hook_file_download().
flipping_book_load Load a flipping_book from the database.
flipping_book_load_multiple Loads flipping books from the database.
flipping_book_menu Implements hook_menu().
flipping_book_permission Implments hook_permission().
flipping_book_prepare_directory Helper function: creates flipping book import directory.
flipping_book_views_api Implements hook_views_api().

Constants

Namesort descending Description
FLIPPING_BOOK_MANAGEMENT_PATH Defining constants.