You are here

function merci_import_1 in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 import/merci_import.php \merci_import_1()

Add issue files.

File

import/merci_import.php, line 25
Administrative page for adding MERCI bucket/resource content types and items.

Code

function merci_import_1() {

  // This determines how many content types will be created per page load.
  // A resonable default has been chosen, but feel free to tweak to suit your needs.
  $limit = 10;

  // No file uploaded, so skip the import.
  if (!$_SESSION['content_types_file']) {
    return array();
  }

  // Multi-part import.
  if (!isset($_SESSION['merci_import_1'])) {
    $csv_file_array = file($_SESSION['content_types_file']);
    $_SESSION['merci_import_file_position'] = 0;
    $_SESSION['merci_import_1'] = 0;
    $_SESSION['merci_import_1_max'] = count($csv_file_array) - 1;
  }

  // Open import file.
  $handle = fopen($_SESSION['content_types_file'], 'r');
  fseek($handle, $_SESSION['merci_import_file_position']);

  // Have to manually load the content types inc file.
  module_load_include('inc', 'node', 'content_types');
  $ret = array();
  $count = 0;
  while (($data = fgetcsv($handle)) !== FALSE) {
    $count++;
    $_SESSION['merci_import_1']++;

    // Only save if the row count for the data is right.
    if (count($data) == 13) {

      // Node type settings.
      $form_state['values']['orig_type'] = '';
      $form_state['values']['type'] = $data[0];
      $form_state['values']['name'] = $data[1];
      $form_state['values']['description'] = $data[2];
      $form_state['values']['help'] = $data[3];
      $form_state['values']['min_word_count'] = 0;
      $form_state['values']['title_label'] = t('Title');
      $form_state['values']['body_label'] = t('Body');
      $form_state['values']['custom'] = TRUE;
      $form_state['values']['locked'] = FALSE;
      $form_state['values']['node_options'] = array(
        'promote' => 0,
        'revision' => 1,
        'status' => 1,
        'sticky' => 0,
      );

      // MERCI settings.
      $form_state['values']['merci_type_setting'] = $data[4];
      $form_state['values']['merci_max_hours_per_reservation'] = $data[5];
      $form_state['values']['merci_allow_overnight'] = $data[6];
      $form_state['values']['merci_allow_weekends'] = $data[7];
      $form_state['values']['merci_late_fee_per_hour'] = $data[8];
      $form_state['values']['merci_rate_per_hour'] = $data[9];
      $form_state['values']['merci_fee_free_hours'] = $data[10];
      $form_state['values']['merci_status'] = $data[11];
      $form_state['values']['merci_spare_items'] = $data[0] == 'bucket' ? $data[12] : 0;
      $form_state['clicked_button']['#value'] = t('Save content type');
      drupal_execute('node_type_form', $form_state);
    }

    // Jump out of the loop here and do another cycle.  Save
    // where we're at in the CSV file.
    if ($count >= $limit) {
      $_SESSION['merci_import_file_position'] = ftell($handle);
      break;
    }
  }

  // No more lines in the file, import is done.  Clean up.
  if (!fgets($handle)) {
    fclose($handle);
    unset($_SESSION['merci_import_file_position']);
    unset($_SESSION['merci_import_1']);
    unset($_SESSION['merci_import_1_max']);
    file_delete($_SESSION['content_types_file']);
    unset($_SESSION['content_types_file']);
    return $ret;
  }
  fclose($handle);
  $ret['#finished'] = $_SESSION['merci_import_1'] / $_SESSION['merci_import_1_max'];
  return $ret;
}