You are here

entity_share_taxonomy.import.inc in Entity Share 7

Class for handling Taxonomy Import.

File

modules/entity_share_taxonomy/includes/entity_share_taxonomy.import.inc
View source
<?php

/**
 * @file
 * Class for handling Taxonomy Import.
 */

/**
 * A class to import the Taxonomy.
 *
 * @property EntityShareEntityImport entityShareEntity
 */
class EntityShareTaxonomyImport extends EntityShareTaxonomyAbstract {
  const WATCHDOG_TYPE = 'entity_share_taxonomy_import';

  /**
   * Manage taxonomy for the import.
   */
  public function importDatas() {

    // Check if the field type is managed by the module.
    if (!$this
      ->isManagedFieldType()) {
      return;
    }
    $entity_info = entity_get_info($this->fieldEntityType);
    $term = (object) $this->fieldData;

    // If there is parents, treat parent first, then continue.
    $this
      ->manageParents($term);

    // It's very important to keep the recursion there cos we have to walk
    // over the tree in reverse order in order to create the term in
    // sub field before the parents terms.
    $this->entityShareEntity
      ->contentFieldWalk($term);
    $this
      ->setLocalIds($term);

    // Create or update the taxonomy term.
    taxonomy_term_save($term);

    // Set the tid (just created if it was not existing or
    // updated if already existing).
    $this->fieldData = NULL;
    $this->fieldData[$entity_info['entity keys']['id']] = $term->{$entity_info['entity keys']['id']};
  }

  /**
   * Set the exported vocabulary to the local vocabulary id.
   *
   * @param object $term
   *   Entity or sub entity to import.
   *
   * @return bool
   *   TRUE if success, FALSE otherwise.
   *
   * @throws EntityShareImportException
   *   Exception thrown in case of unexisting vocabulary locally.
   */
  public static function setLocalVocabularyId($term) {
    $entity_info = entity_get_info($term->entity_type);
    $vocabulary_machine_name = $term->{$entity_info['entity keys']['bundle']};
    $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
    if (!isset($vocabulary->vid)) {
      watchdog(self::WATCHDOG_TYPE, 'The vocabulary %vocabulary doesn\'t exist', array(
        '%vocabulary' => $vocabulary_machine_name,
      ), WATCHDOG_ERROR);
      throw new EntityShareImportException('The vocabulary ' . $vocabulary_machine_name . ' doesn\'t exist');
    }
    $term->vid = $vocabulary->vid;
    return TRUE;
  }

  /**
   * Set the local ids.
   *
   * @param object $term
   *   Term to import.
   */
  protected function setLocalIds($term) {

    // Set the local vid (vocabulary id).
    if ($term->entity_type != $this->fieldEntityType) {
      self::setLocalVocabularyId($term);
    }
    $this->entityShareEntity
      ->setLocalEntityIds($term);
  }

  /**
   * Manage the parents of a term to import.
   *
   * @param object $term
   *   Term to import.
   */
  protected function manageParents($term) {
    if (!empty($term->parent)) {
      foreach ($term->parent as &$parent) {
        if ($parent != 0) {
          $parent = (object) $parent;

          // The parent has parent.
          if (!empty($parent->parent)) {
            $this
              ->manageParents($parent);
          }

          // Import parent term.
          $import = new EntityShareEntityImport($parent);
          $tid = $import
            ->execute();

          // Set the parent tid.
          $parent = $tid;
        }
      }
    }
  }

}

Classes

Namesort descending Description
EntityShareTaxonomyImport A class to import the Taxonomy.