You are here

sheetnode_google.module in Sheetnode 7

Module file for the sheetnode_google module. This extends sheetnodes to enable importing of Google sheets.

File

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

/**
 * @file
 * Module file for the sheetnode_google module.
 * This extends sheetnodes to enable importing of Google sheets.
 */

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

/**
 * Implementation of hook_menu().
 */
function sheetnode_google_menu() {
  $items = array();
  $items['node/add/googlesheet'] = array(
    'title' => 'Sheetnode import from Google Spreadsheet',
    'access arguments' => array(
      'create sheetnode',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      '_sheetnode_google_import_form',
    ),
    'description' => 'Create a new sheetnode with content from an existing Google Spreadsheet.',
    'file' => 'sheetnode_google.import.inc',
  );
  $items['admin/config/content/sheetnode/google'] = array(
    'title' => 'Google Spreadsheets',
    'access arguments' => array(
      'administer site configuration',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      '_sheetnode_google_settings',
    ),
    'description' => 'Administer settings for Sheetnode Google Spreadsheets.',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}
function _sheetnode_google_settings() {
  $form['sheetnode_zend_gdata_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Zend Gdata library path'),
    '#description' => t('Enter the full path of the extracted Zend Gdata package (pointing to but not including the Zend/ subfolder).
                         Leave empty if the Zend framework is already included in the PHP include_path setting. On this server, include_path is set to:<br /><pre>!path</pre>', array(
      '!path' => ini_get('include_path'),
    )),
    '#default_value' => variable_get('sheetnode_zend_gdata_path', ''),
    '#size' => 80,
  );
  $form['#validate'][] = '_sheetnode_google_settings_validate';
  return system_settings_form($form);
}
function _sheetnode_google_settings_validate($form, $form_state) {
  $path = rtrim($form_state['values']['sheetnode_zend_gdata_path'], '/');
  if (!empty($path) && (!is_dir($path) || !is_file($path . '/Zend/Gdata.php'))) {
    form_set_error('sheetnode_zend_gdata_path', t('The path you entered does not point to a valid location. Please enter the full path of the extracted Zend Gdata package, including the library subdirectory.'));
    return;
  }
}

/**
 * Implementation of hook_init().
 */
function sheetnode_google_init() {

  // Set PHP include_path to find Zend files.
  $path = variable_get('sheetnode_zend_gdata_path', '');
  if (empty($path)) {
    return;
  }
  $include_path = ini_get('include_path');
  if (FALSE === strpos($include_path, $path)) {
    ini_set('include_path', $include_path . PATH_SEPARATOR . $path);
  }

  // Register the autoload function for Batch API.
  spl_autoload_register('_sheetnode_google_autoload');
}

/**
 * Autoload callback.
 */
function _sheetnode_google_autoload($classname) {
  if (strpos($classname, 'Zend_') === 0) {
    include_once 'Zend/Loader.php';
    try {
      Zend_Loader::loadClass($classname);
    } catch (Zend_Exception $e) {

      // do nothing.
    }
  }
}

Functions

Namesort descending Description
sheetnode_google_batch_import API function to batch import several files using Batch API.
sheetnode_google_init Implementation of hook_init().
sheetnode_google_menu Implementation of hook_menu().
_sheetnode_google_autoload Autoload callback.
_sheetnode_google_settings
_sheetnode_google_settings_validate