You are here

views_data_export.module in Views data export 6

Provides the ability to export to specific

File

views_data_export.module
View source
<?php

/**
 * @file
 * Provides the ability to export to specific
 */
define('VIEWS_DATA_EXPORT_HEADER', 'header');
define('VIEWS_DATA_EXPORT_BODY', 'body');
define('VIEWS_DATA_EXPORT_FOOTER', 'footer');
define('VIEWS_DATA_EXPORT_FINISHED', 'finished');
define('VIEWS_DATA_EXPORT_INDEX_TABLE_PREFIX', 'views_data_export_index_');

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

/**
 * Implementation of hook_theme().
 */
function views_data_export_theme() {

  // Make sure that views picks up the preprocess functions.
  module_load_include('inc', 'views_data_export', 'theme/views_data_export.theme');
  $hooks = array();
  $hooks['views_data_export_feed_icon'] = array(
    'pattern' => 'views_data_export_feed_icon__',
    'arguments' => array(
      'image_path' => NULL,
      'url' => NULL,
      'query' => '',
      'text' => '',
    ),
    'file' => 'theme/views_data_export.theme.inc',
  );
  $hooks['views_data_export_complete_page'] = array(
    'arguments' => array(
      'file' => '',
      'errors' => array(),
      'return_url' => '',
    ),
    'file' => 'theme/views_data_export.theme.inc',
  );
  return $hooks;
}

/**
 * Implementation of hook_cron().
 */
function views_data_export_cron() {
  views_data_export_garbage_collect();
}

/**
 * Removes any temporary index tables that have been left
 * behind. This is caused by batch processes which are
 * started but never finished.
 *
 * Removes all trace of exports from the database that
 * were created more than $expires seconds ago
 *
 * @param $expires
 *   Seconds ago. Defaults to that given in the settings.
 * @param $chunk
 *   The number of tables to test for and delete.
 *   Defaults to that given in the settings. Pass -1
 *   for this setting to remove any restriction and to
 *   garbage collect all exports.
 */
function views_data_export_garbage_collect($expires = NULL, $chunk = NULL) {
  if (!isset($expires)) {
    $expires = variable_get('views_data_export_gc_expires', 604800);

    // one week
  }
  if (!isset($chunk)) {
    $chunk = variable_get('views_data_export_gc_chunk', 30);
  }
  if ($chunk == -1) {
    $qry = db_query("SELECT eid FROM {views_data_export} WHERE time_stamp <= %d ORDER BY time_stamp ASC", time() - $expires);
  }
  else {
    $qry = db_query_range("SELECT eid FROM {views_data_export} WHERE time_stamp <= %d ORDER BY time_stamp ASC", time() - $expires, 0, $chunk);
  }
  $eids_to_clear = array();
  while ($row = db_fetch_array($qry)) {
    $eids_to_clear[] = $row['eid'];
  }

  // We do two things to exports we want to garbage collect
  // 1. Delete the index table for it, if it is still around
  // 2. Delete the row from the exports table
  // 3. Delete the view from the object_cache
  if (count($eids_to_clear)) {
    $ret = array();
    foreach ($eids_to_clear as $eid) {

      // 1. Delete index table, if it is still around for some reason
      $table = VIEWS_DATA_EXPORT_INDEX_TABLE_PREFIX . $eid;
      if (db_table_exists($table)) {
        db_drop_table($ret, $table);
      }
    }

    // 2. Delete the entries in the exports table.
    db_query("DELETE FROM {views_data_export} WHERE eid IN (" . db_placeholders($eids_to_clear) . ")", $eids_to_clear);

    // 3. Clear the cached views
    views_data_export_view_clear($eids_to_clear);
  }
}

/**
 * Batch API callback.
 * Handles all batching operations by executing the appropriate view.
 */
function _views_data_export_batch_process($export_id, $display_id, &$context) {

  // Fetch the view in question from our cache
  $view = views_data_export_view_retrieve($export_id);
  $view
    ->set_display($display_id);

  // Inform the data_export display which export it corresponds to and execute
  $view->display_handler->batched_execution_state->eid = $export_id;
  $view->display_handler->views_data_export_cached_view_loaded = TRUE;
  $view
    ->execute_display($display_id);

  // Update batch api progress information
  $sandbox = $view->display_handler->batched_execution_state->sandbox;
  $context['finished'] = $sandbox['finished'];
  $context['message'] = $sandbox['message'];
  views_data_export_view_store($export_id, $view);
}

/**********/

/** CRUD **/

/**********/

/**
 * Save a new export into the database.
 */
function views_data_export_new($view_name, $view_display_id, $file) {

  // Insert new row into exports table
  $record = (object) array(
    'view_name' => $view_name,
    'view_display_id' => $view_display_id,
    'time_stamp' => time(),
    'fid' => $file,
    'batch_state' => VIEWS_DATA_EXPORT_HEADER,
    'sandbox' => array(),
  );
  drupal_write_record('views_data_export', $record);
  return $record;
}

/**
 * Update an export row in the database
 */
function views_data_export_update($state) {

  // Note, drupal_write_record handles serializing
  // the sandbox field as per our schema definition
  drupal_write_record('views_data_export', $state, 'eid');
}

/**
 * Get the information about a previous export.
 */
function views_data_export_get($export_id) {
  $qry = db_query("SELECT * FROM {views_data_export} WHERE eid = %d", (int) $export_id);
  $object = db_fetch_object($qry);
  if ($object) {
    $object->sandbox = unserialize($object->sandbox);
  }
  return $object;
}

/**
 * Remove the information about an export.
 */
function views_data_export_clear($export_id) {
  db_query("DELETE FROM {views_data_export} WHERE eid = %d", $export_id);
  views_data_export_view_clear($export_id);
}

/**
 * Store a view in the object cache.
 */
function views_data_export_view_store($export_id, $view) {

  // Store a clean copy of the view.
  $_view = $view
    ->clone_view();
  views_data_export_view_clear($export_id);
  $record = array(
    'eid' => $export_id,
    'data' => $_view,
    'updated' => time(),
  );
  drupal_write_record('views_data_export_object_cache', $record);
}

/**
 * Retrieve a view from the object cache.
 */
function views_data_export_view_retrieve($export_id) {
  views_include('view');
  $data = db_fetch_object(db_query("SELECT * FROM {views_data_export_object_cache} WHERE eid = '%s'", $export_id));
  if ($data) {
    $view = unserialize($data->data);
  }
  return $view;
}

/**
 * Clear a view from the object cache.
 *
 * @param $export_id
 *   An export ID or an array of export IDs to clear from the object cache.
 */
function views_data_export_view_clear($export_id) {
  if (is_array($export_id)) {
    db_query("DELETE FROM {views_data_export_object_cache} WHERE eid IN (" . db_placeholders($export_id) . ")", $export_id);
  }
  else {
    db_query("DELETE FROM {views_data_export_object_cache} WHERE eid = '%s'", $export_id);
  }
}

Functions

Namesort descending Description
views_data_export_clear Remove the information about an export.
views_data_export_cron Implementation of hook_cron().
views_data_export_garbage_collect Removes any temporary index tables that have been left behind. This is caused by batch processes which are started but never finished.
views_data_export_get Get the information about a previous export.
views_data_export_new Save a new export into the database.
views_data_export_theme Implementation of hook_theme().
views_data_export_update Update an export row in the database
views_data_export_views_api Implementation of hook_views_api().
views_data_export_view_clear Clear a view from the object cache.
views_data_export_view_retrieve Retrieve a view from the object cache.
views_data_export_view_store Store a view in the object cache.
_views_data_export_batch_process Batch API callback. Handles all batching operations by executing the appropriate view.

Constants