You are here

function merci_import_2 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_2()

Add followup files.

File

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

Code

function merci_import_2() {
  global $user;

  // 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['items_file']) {
    return array();
  }

  // Multi-part import
  if (!isset($_SESSION['merci_import_2'])) {
    $csv_file_array = file($_SESSION['items_file']);
    $_SESSION['merci_import_file_position'] = 0;
    $_SESSION['merci_import_2'] = 0;
    $_SESSION['merci_import_2_max'] = count($csv_file_array) - 1;
  }

  // Open import file.
  $handle = fopen($_SESSION['items_file'], 'r');
  fseek($handle, $_SESSION['merci_import_file_position']);
  $ret = array();
  $count = 0;
  while (($data = fgetcsv($handle)) !== FALSE) {
    $count++;
    $_SESSION['merci_import_2']++;

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

      // Node data.
      $item = new stdClass();
      $item->type = $data[0];
      $item->name = $user->name;
      $item->uid = $user->uid;
      $item->title = $data[1];
      $item->body = $data[2];
      $item->status = 1;
      $item->promote = 0;
      $item->sticky = 0;

      // MERCI specific data.
      $merci_settings = merci_load_content_type_settings($item->type);
      $is_resource = $merci_settings->type_setting == 'resource' ? TRUE : FALSE;
      $item->merci_default_availability = $data[3];
      $item->merci_sub_type = MERCI_SUB_TYPE_ITEM;

      // Only resources get per item accounting data.
      $item->merci_late_fee_per_hour = $is_resource ? $data[4] : 0;
      $item->merci_rate_per_hour = $is_resource ? $data[5] : 0;
      $item->merci_fee_free_hours = $is_resource ? $data[6] : 0;
      $item = node_submit($item);
      node_save($item);
      if (isset($item->nid)) {
        drupal_set_message(t("The %type item %title has been added.", array(
          '%type' => $item->type,
          '%title' => $item->title,
        )));
      }
    }

    // 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_2']);
    unset($_SESSION['merci_import_2_max']);
    file_delete($_SESSION['items_file']);
    unset($_SESSION['items_file']);
    return $ret;
  }
  fclose($handle);
  $ret['#finished'] = $_SESSION['merci_import_2'] / $_SESSION['merci_import_2_max'];
  return $ret;
}