You are here

class EntityShareTaxonomyImport in Entity Share 7

A class to import the Taxonomy.

@property EntityShareEntityImport entityShareEntity

Hierarchy

Expanded class hierarchy of EntityShareTaxonomyImport

File

modules/entity_share_taxonomy/includes/entity_share_taxonomy.import.inc, line 13
Class for handling Taxonomy Import.

View source
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;
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareTaxonomyAbstract::$delta protected property Delta of the field.
EntityShareTaxonomyAbstract::$entity protected property Entity object.
EntityShareTaxonomyAbstract::$entityShareEntity protected property Entity share object.
EntityShareTaxonomyAbstract::$fieldData protected property Field datas.
EntityShareTaxonomyAbstract::$fieldEntityType protected property Entity type of the current field.
EntityShareTaxonomyAbstract::$fieldInfo protected property Metadatas of the field.
EntityShareTaxonomyAbstract::$fieldLang protected property Field language.
EntityShareTaxonomyAbstract::$fieldName protected property Field name.
EntityShareTaxonomyAbstract::$fieldType protected property Field type.
EntityShareTaxonomyAbstract::$managedFieldTypes protected property Type of field managed by the class.
EntityShareTaxonomyAbstract::isManagedFieldType public function Check if the field type is managed.
EntityShareTaxonomyAbstract::__construct public function Constructor. Initialize properties.
EntityShareTaxonomyImport::importDatas public function Manage taxonomy for the import.
EntityShareTaxonomyImport::manageParents protected function Manage the parents of a term to import.
EntityShareTaxonomyImport::setLocalIds protected function Set the local ids.
EntityShareTaxonomyImport::setLocalVocabularyId public static function Set the exported vocabulary to the local vocabulary id.
EntityShareTaxonomyImport::WATCHDOG_TYPE constant