You are here

sheetnode_phpexcel.module in Sheetnode 5

File

modules/sheetnode_phpexcel/sheetnode_phpexcel.module
View source
<?php

/**
 * Implementation of hook_views_api().
 */
function sheetnode_phpexcel_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implementation of hook_sheetnode_phpexcel_plugins().
 */
function sheetnode_phpexcel_sheetnode_phpexcel_plugins() {
  return array(
    'xls' => array(
      'content-type' => 'application/vnd.ms-excel',
      'php-excel-type' => 'Excel5',
      'input' => TRUE,
      'output' => TRUE,
      'short-name' => t('XLS'),
      'long-name' => t('Microsoft Excel 5 (XLS)'),
    ),
    'xlsx' => array(
      'content-type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'php-excel-type' => 'Excel2007',
      'input' => TRUE,
      'output' => TRUE,
      'short-name' => t('XLSX'),
      'long-name' => t('Microsoft Excel 2007 (XLSX)'),
    ),
    'ods' => array(
      'content-type' => 'application/vnd.oasis.opendocument.spreadsheet',
      'php-excel-type' => 'OOCalc',
      'input' => TRUE,
      'output' => FALSE,
      'short-name' => t('ODS'),
      'long-name' => t('OpenOffice.org Calc (ODS)'),
    ),
    'pdf' => array(
      'content-type' => 'application/pdf',
      'php-excel-type' => 'PDF',
      'input' => FALSE,
      'output' => TRUE,
      'short-name' => t('PDF'),
      'long-name' => t('Adobe Acrobat (PDF)'),
    ),
  );
}

/**
 * API function to fetch existing PHPExcel plugins.
 */
function sheetnode_phpexcel_get_plugins($dir = 'all') {
  $plugins = module_invoke_all('sheetnode_phpexcel_plugins');

  //drupal_alter('sheetnode_phpexcel_plugins', $plugins);
  if ($dir != 'all') {
    foreach ($plugins as $format => $plugin) {
      if (empty($plugin[$dir])) {
        unset($plugins[$format]);
      }
    }
  }
  return $plugins;
}

/**
 * Implementation of hook_link().
 */
function sheetnode_phpexcel_link($type, $node = NULL, $teaser = FALSE) {
  if ($type != 'node' || $node->type != 'sheetnode') {
    return array();
  }
  foreach (sheetnode_phpexcel_get_plugins('output') as $format => $plugin) {
    $links[$format] = array(
      'title' => t('Save as !format', array(
        '!format' => $plugin['short-name'],
      )),
      'href' => "sheetnode/{$format}/{$node->nid}",
    );
  }
  return $links;
}
require_once drupal_get_path('module', 'sheetnode_phpexcel') . '/sheetnode_phpexcel.export.inc';
require_once drupal_get_path('module', 'sheetnode_phpexcel') . '/sheetnode_phpexcel.import.inc';

/**
 * Implementation of hook_menu().
 */
function sheetnode_phpexcel_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    foreach (sheetnode_phpexcel_get_plugins('output') as $format => $plugin) {
      $items[] = array(
        'path' => "sheetnode/{$format}",
        'title' => t('Save as !format', array(
          '!format' => $plugin['short-name'],
        )),
        'access' => user_access('access content'),
        'callback' => '_sheetnode_phpexcel_export',
        'callback arguments' => array(
          $format,
        ),
        'type' => MENU_CALLBACK,
      );
    }
    foreach (sheetnode_phpexcel_get_plugins('input') as $format => $plugin) {
      $items[] = array(
        'path' => "node/add/{$format}",
        'title' => t('Sheetnode import from !format', array(
          '!format' => $plugin['short-name'],
        )),
        'access' => user_access('create sheetnode'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array(
          '_sheetnode_phpexcel_import_form',
          $format,
        ),
        'description' => t('Create a new sheetnode with content from an existing spreadsheet.'),
      );
    }
    $items[] = array(
      'path' => 'admin/settings/sheetnode/phpexcel',
      'title' => t('PHPExcel'),
      'access' => user_access('administer site configuration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        '_sheetnode_phpexcel_settings',
      ),
      'description' => t('Administer settings for Sheetnode PHPExcel.'),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}
function _sheetnode_phpexcel_settings() {
  $form['sheetnode_phpexcel_library_path'] = array(
    '#type' => 'textfield',
    '#title' => t('PHPExcel path'),
    '#description' => t('Enter the location of the extracted PHPExcel package.'),
    '#default_value' => variable_get('sheetnode_phpexcel_library_path', ''),
  );
  return system_settings_form($form);
}
function _sheetnode_phpexcel_settings_validate($form, $form_values) {
  $path = rtrim($form_values['sheetnode_phpexcel_library_path'], '/');
  if (!is_dir($path) || !is_file($path . '/Classes/PHPExcel.php')) {
    form_set_error('sheetnode_phpexcel_library_path', t('The path you entered does not point to a valid location. Please enter the location of the extracted PHPExcel package.'));
    return;
  }
}

Functions

Namesort descending Description
sheetnode_phpexcel_get_plugins API function to fetch existing PHPExcel plugins.
sheetnode_phpexcel_link Implementation of hook_link().
sheetnode_phpexcel_menu Implementation of hook_menu().
sheetnode_phpexcel_sheetnode_phpexcel_plugins Implementation of hook_sheetnode_phpexcel_plugins().
sheetnode_phpexcel_views_api Implementation of hook_views_api().
_sheetnode_phpexcel_settings
_sheetnode_phpexcel_settings_validate