You are here

function merci_core_import_items in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

File

merci_core/merci_core.module, line 233

Code

function merci_core_import_items() {
  $row = 0;
  $items = array();
  $entity_info = entity_get_info('node');
  $language_key = $entity_info['entity keys']['language'];
  $manf_vid = taxonomy_vocabulary_machine_name_load('manufacturer');
  $ass_vid = taxonomy_vocabulary_machine_name_load('reservable_product_accessories');
  $resource_vid = taxonomy_vocabulary_machine_name_load('resource_type');
  if (($handle = fopen(DRUPAL_ROOT . '/' . drupal_get_path('module', 'merci_core') . "/items.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle)) !== FALSE) {
      $items[] = $data;
      $row++;
      if ($row == 1) {
        continue;
      }
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'reservable_product_display')
        ->propertyCondition('title', $data[20]);
      $result = $query
        ->execute();
      if (isset($result['node'])) {
        $nids = array_keys($result['node']);
        $items = entity_load('node', $nids);
        $display = reset($items);
      }
      else {

        // Gepty($display)) {
        $display = entity_create('node', array(
          $language_key => 'und',
          'type' => 'reservable_product_display',
          'title' => $data[20],
        ));
      }
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'merci_core_product')
        ->entityCondition('bundle', 'reservable_product')
        ->propertyCondition('sku', $data[0]);
      $result = $query
        ->execute();
      if (isset($result['merci_core_product'])) {
        $nids = array_keys($result['merci_core_product']);
        $items = entity_load('merci_core_product', $nids);
        $product = reset($items);
      }
      else {
        $product = entity_create('merci_core_product', array(
          'type' => 'reservable_product',
          'sku' => $data[0],
          'title' => $data[20],
        ));
      }
      $display_wrapper = entity_metadata_wrapper('node', $display);
      $product_wrapper = entity_metadata_wrapper('merci_core_product', $product);
      $product_wrapper->merci_core_price = merci_core_price_field_data_auto_creation();
      $product_wrapper
        ->save();
      $display_wrapper->field_crp_product_reference[] = $product_wrapper->product_id
        ->value();
      $display_wrapper->field_model = $data[3];
      $term = taxonomy_get_term_by_name($data[2]);
      $tids = array();
      if (empty($term) and !empty($data[2])) {
        $term = array(
          'vid' => $manf_vid->vid,
          'name' => $data[2],
        );
        taxonomy_term_save((object) $term);
        $term = taxonomy_get_term_by_name($data[2]);
      }
      if (!empty($term)) {
        $tids = array_keys($term);
        $display_wrapper->field_manufacturer
          ->set(end($tids));
      }
      $ass = explode(',', $data[6]);
      $tids = array();
      foreach ($ass as $name) {
        $terms = taxonomy_get_term_by_name($name);
        if (empty($terms)) {
          $term = array(
            'vid' => $ass_vid->vid,
            'name' => $name,
          );
          taxonomy_term_save((object) $term);
          $terms = taxonomy_get_term_by_name($name);
        }
        $tid = array_keys($terms);
        if (end($tid)) {
          $tids[] = end($tid);
        }
      }
      if (!empty($tids)) {
        $display_wrapper->field_associated_components
          ->set($tids);
      }
      $ass = explode(' -- ', $data[33]);
      $tids = array();
      foreach ($ass as $name) {
        $terms = taxonomy_get_term_by_name($name);
        if (empty($terms)) {
          $term = array(
            'vid' => $resource_vid->vid,
            'name' => $name,
          );
          if (end($tids)) {
            $term['parent'] = end($tids);
          }
          taxonomy_term_save((object) $term);
          $terms = taxonomy_get_term_by_name($name);
        }
        $tid = array_keys($terms);
        if (end($tid)) {
          $tids[] = end($tid);
        }
      }
      if (!empty($tids)) {
        $display_wrapper->field_resource_type
          ->set(end($tids));
      }
      $display_wrapper
        ->save();
    }
    fclose($handle);
  }
}