You are here

sheetnode_phpexcel.module in Sheetnode 7

Module file for the sheetnode_phpexcel module. This manages handling of files for sheetnode.

File

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

/**
 * @file
 * Module file for the sheetnode_phpexcel module.
 * This manages handling of files for sheetnode.
 */
define('SHEETNODE_PHPEXCEL_LIBRARY', 'phpexcel');
define('SHEETNODE_PHPEXCEL_PDF_RENDERER', 'tcpdf');
define('DEFAULT_SHEETNODE_PHPEXCEL_LIBRARY_PATH', 'sites/all/libraries/PHPExcel');
define('DEFAULT_SHEETNODE_PHPEXCEL_PDF_RENDERER_PATH', 'sites/all/libraries/tcpdf');

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

/**
 * Implements 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)'),
      'format' => '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)'),
      'format' => 'xlsx',
    ),
    'csv' => array(
      'content-type' => 'text/csv',
      'php-excel-type' => 'CSV',
      'input' => TRUE,
      'output' => TRUE,
      'short-name' => t('CSV'),
      'long-name' => t('Comma-separated values (CSV)'),
      'format' => 'csv',
    ),
    '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)'),
      'format' => '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)'),
      'format' => '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;
}

/**
 * API function to batch import several files using Batch API.
 */
function sheetnode_phpexcel_batch_import($files, $destination = NULL, $callback = NULL, $params = array(), $batch = array()) {
  $batch += array(
    'finished' => '_sheetnode_phpexcel_batch_import_finished',
    'title' => t('Importing spreadsheets'),
    'init_message' => t('Import process is starting.'),
    'progress_message' => t('Imported @current out of @total files.'),
    'error_message' => t('Import process has encountered an error.'),
    'file' => drupal_get_path('module', 'sheetnode_phpexcel') . '/sheetnode_phpexcel.import.inc',
  );
  foreach ($files as $filename => $filepath) {
    $batch['operations'][] = array(
      '_sheetnode_phpexcel_batch_import_file',
      array(
        $filename,
        $filepath,
        $callback,
        $params,
      ),
    );
  }
  batch_set($batch);
  batch_process($destination);
}

/**
 * API function to batch export several sheets using Batch API.
 */
function sheetnode_phpexcel_batch_export($sheets, $filename, $format, $destination = NULL, $batch = array()) {
  $batch += array(
    'finished' => '_sheetnode_phpexcel_batch_export_finished',
    'title' => t('Exporting spreadsheets'),
    'init_message' => t('Export process is in progress. This can take a long time for large sheets...'),
    'progress_message' => t('Exported @current out of @total sheets.'),
    'error_message' => t('Export process has encountered an error.'),
    'file' => drupal_get_path('module', 'sheetnode_phpexcel') . '/sheetnode_phpexcel.export.inc',
  );
  foreach ($sheets as $title => $socialcalc) {
    $batch['operations'][] = array(
      '_sheetnode_phpexcel_batch_export_sheet',
      array(
        $title,
        $socialcalc,
        $filename,
        $format,
        $destination ? $destination : $_SERVER['HTTP_REFERER'],
      ),
    );
  }
  batch_set($batch);
  batch_process('sheetnode/export/ready');
}

/**
 * Implements hook_init().
 */
function sheetnode_phpexcel_init() {
  spl_autoload_register('_sheetnode_phpexcel_autoload');
}

/**
 * Autoload callback.
 */
function _sheetnode_phpexcel_autoload($classname) {
  _sheetnode_phpexcel_load_library(SHEETNODE_PHPEXCEL_LIBRARY);
}

/**
 * Helper function to locate libraries.
 */
function _sheetnode_phpexcel_load_library($type) {
  switch ($type) {
    case SHEETNODE_PHPEXCEL_LIBRARY:
      $path = rtrim(variable_get('sheetnode_phpexcel_library_path', DEFAULT_SHEETNODE_PHPEXCEL_LIBRARY_PATH), '/');
      if (!is_dir($path) || !is_file($path . '/Classes/PHPExcel.php')) {
        return FALSE;
      }
      include_once $path . '/Classes/PHPExcel.php';
      break;
    case SHEETNODE_PHPEXCEL_PDF_RENDERER:
      $path = rtrim(variable_get('sheetnode_phpexcel_pdf_renderer_path', DEFAULT_SHEETNODE_PHPEXCEL_PDF_RENDERER_PATH), '/');
      if (!is_dir($path) || !is_file($path . '/tcpdf.php')) {
        return FALSE;
      }
      include_once $path . '/tcpdf.php';
      break;
    default:
      return FALSE;
  }
  return TRUE;
}

/**
 * Implements hook_node_view().
 */
function sheetnode_phpexcel_node_view($node, $view_mode) {
  if ($node->type != 'sheetnode' && !sheetnode_get_sheetfields($node->type) || !variable_get('sheetnode_phpexcel_export_links', TRUE)) {
    return;
  }
  $links = 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}",
    );
  }
  $node->content['links']['sheetnode_phpexcel'] = array(
    '#theme' => 'links',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function sheetnode_phpexcel_menu() {
  $items = array();
  foreach (sheetnode_phpexcel_get_plugins('output') as $format => $plugin) {
    $items["sheetnode/{$format}/%node"] = array(
      'title' => 'Save as !format',
      'title arguments' => array(
        '!format' => $plugin['short-name'],
      ),
      'access arguments' => array(
        'access content',
      ),
      'page callback' => '_sheetnode_phpexcel_export',
      'page arguments' => array(
        $format,
        2,
      ),
      'type' => MENU_CALLBACK,
      'file' => 'sheetnode_phpexcel.export.inc',
    );
  }
  $items['sheetnode/export/ready'] = array(
    'title' => 'Spreadsheet exported',
    'access callback' => '_sheetnode_phpexcel_export_access',
    'page callback' => 'theme',
    'page arguments' => array(
      'sheetnode_phpexcel_export_ready',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'sheetnode_phpexcel.export.inc',
  );
  $items['sheetnode/export/download'] = array(
    'title' => 'Download spreadsheet file',
    'access callback' => '_sheetnode_phpexcel_export_access',
    'page callback' => '_sheetnode_phpexcel_export_download',
    'type' => MENU_CALLBACK,
    'file' => 'sheetnode_phpexcel.export.inc',
  );
  foreach (sheetnode_phpexcel_get_plugins('input') as $format => $plugin) {
    $items["node/add/{$format}"] = array(
      'title' => 'Sheetnode import from !format',
      'title arguments' => array(
        '!format' => $plugin['short-name'],
      ),
      'access arguments' => array(
        'create sheetnode',
      ),
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        '_sheetnode_phpexcel_import_form',
        $format,
      ),
      'description' => 'Create a new sheetnode with content from an existing spreadsheet.',
      'file' => 'sheetnode_phpexcel.import.inc',
    );
  }
  $items['admin/config/content/sheetnode/phpexcel'] = array(
    'title' => 'PHPExcel',
    'access arguments' => array(
      'administer site configuration',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      '_sheetnode_phpexcel_settings',
    ),
    'description' => 'Administer settings for Sheetnode PHPExcel.',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Access function for download ready page.
 */
function _sheetnode_phpexcel_export_access() {
  return !empty($_SESSION['sheetnode_phpexcel_download']);
}

/**
 * Implements hook_theme.
 */
function sheetnode_phpexcel_theme($existing, $type, $theme, $path) {
  return array(
    'sheetnode_phpexcel_export_ready' => array(
      'variables' => array(),
      'file' => 'sheetnode_phpexcel.export.inc',
      'template' => 'sheetnode_phpexcel_export_ready',
    ),
  );
}

/**
 * Form function for admin/settings/sheetnode/phpexcel.
 */
function _sheetnode_phpexcel_settings($form, &$form_state) {
  $form['sheetnode_phpexcel_library_path'] = array(
    '#type' => 'textfield',
    '#title' => t('PHPExcel path'),
    '#description' => t('Enter the path of the extracted PHPExcel package. Full or relative paths should work.'),
    '#default_value' => variable_get('sheetnode_phpexcel_library_path', DEFAULT_SHEETNODE_PHPEXCEL_LIBRARY_PATH),
  );
  $form['sheetnode_phpexcel_export_links'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display export links'),
    '#description' => t('Check this ON to display "Save as ..." links on every sheetnode page.'),
    '#default_value' => variable_get('sheetnode_phpexcel_export_links', TRUE),
  );
  $form['sheetnode_phpexcel_pdf_renderer_path'] = array(
    '#type' => 'textfield',
    '#title' => t('TCPDF path'),
    '#description' => t('Enter the path of the TCPDF rendering library. Full or relative paths should work.'),
    '#default_value' => variable_get('sheetnode_phpexcel_pdf_renderer_path', DEFAULT_SHEETNODE_PHPEXCEL_PDF_RENDERER_PATH),
  );
  $form['#validate'][] = '_sheetnode_phpexcel_settings_validate';
  return system_settings_form($form);
}

/**
 * Validate function for _sheetnode_phpexcel_settings form.
 */
function _sheetnode_phpexcel_settings_validate($form, $form_state) {
  $path = rtrim($form_state['values']['sheetnode_phpexcel_library_path'], '/');
  if (!is_dir($path) || !is_file($path . '/Classes/PHPExcel.php')) {
    form_set_error('sheetnode_phpexcel_library_path', t('The PHPExcel library path you entered does not point to a valid location. Please enter the location of the extracted PHPExcel package.'));
    return;
  }
  include_once $path . '/Classes/PHPExcel.php';
  $pdf = PHPExcel_Settings::PDF_RENDERER_TCPDF;
  $path = rtrim($form_state['values']['sheetnode_phpexcel_pdf_renderer_path'], '/');
  if (!PHPExcel_Settings::setPdfRenderer($pdf, $path)) {
    form_set_error('sheetnode_phpexcel_pdf_renderer_path', t('The PDF renderer library path you entered does not point to a valid location. Please enter the location of the extracted !pdf package.', array(
      '!pdf' => $pdf,
    )));
  }
}

/**
 * Helper function to sanitize a filename.
 */
function _sheetnode_phpexcel_sanitize_filename($string) {
  $string = htmlentities($string, ENT_QUOTES, 'UTF-8');
  $string = preg_replace('/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);/i', '$1', $string);
  $string = preg_replace('/[^0-9a-z-_]/i', '-', $string);
  return trim($string, '-');
}

Functions

Namesort descending Description
sheetnode_phpexcel_batch_export API function to batch export several sheets using Batch API.
sheetnode_phpexcel_batch_import API function to batch import several files using Batch API.
sheetnode_phpexcel_get_plugins API function to fetch existing PHPExcel plugins.
sheetnode_phpexcel_init Implements hook_init().
sheetnode_phpexcel_menu Implements hook_menu().
sheetnode_phpexcel_node_view Implements hook_node_view().
sheetnode_phpexcel_sheetnode_phpexcel_plugins Implements hook_sheetnode_phpexcel_plugins().
sheetnode_phpexcel_theme Implements hook_theme.
sheetnode_phpexcel_views_api Implements hook_views_api().
_sheetnode_phpexcel_autoload Autoload callback.
_sheetnode_phpexcel_export_access Access function for download ready page.
_sheetnode_phpexcel_load_library Helper function to locate libraries.
_sheetnode_phpexcel_sanitize_filename Helper function to sanitize a filename.
_sheetnode_phpexcel_settings Form function for admin/settings/sheetnode/phpexcel.
_sheetnode_phpexcel_settings_validate Validate function for _sheetnode_phpexcel_settings form.

Constants

Namesort descending Description
DEFAULT_SHEETNODE_PHPEXCEL_LIBRARY_PATH
DEFAULT_SHEETNODE_PHPEXCEL_PDF_RENDERER_PATH
SHEETNODE_PHPEXCEL_LIBRARY @file Module file for the sheetnode_phpexcel module. This manages handling of files for sheetnode.
SHEETNODE_PHPEXCEL_PDF_RENDERER