/**
  * Finds or creates a taxonomy.
  * @param array $find params to find a taxonomy
  * @param array $create used if taxonomy could not be found with above params
  */
 public static function getOrCreate($find, $create)
 {
     $taxonomy = TaxonomyTerm::get()->filter($find)->first();
     if (!$taxonomy || !$taxonomy->exists()) {
         $taxonomy = new TaxonomyTerm($create);
         $taxonomy->write();
     }
     return $taxonomy;
 }
 public function transform($item, $parent, $strategy)
 {
     echo get_class($parent);
     if (is_a($parent, 'TaxonomyTerm')) {
         // If a child term of $parent doesn't already exists with this name, create it.
         $taxonomyTerms = TaxonomyTerm::get()->filter('Name', $item->Name)->filter('ParentID', $parent->ID);
         if ($taxonomyTerms->exists()) {
             $taxonomyTerm = $taxonomyTerms->first();
         } else {
             $taxonomyTerm = new TaxonomyTerm();
             $taxonomyTerm->Name = $item->Name;
             $taxonomyTerm->ParentID = $parent->ID;
             $taxonomyTerm->write();
             $parent->Children()->Add($taxonomyTerm);
         }
         return new TransformResult($taxonomyTerm, $item->stageChildren()->filter('ClassName', 'DrupalTaxonomyTermContentItem'), $item);
     } else {
         $page = new DrupalTaxonomyTerm();
         $params = $this->importer->getParams();
         $existingPage = DataObject::get_one('DrupalTaxonomyTerm', sprintf('"DrupalID" = %d AND "ParentID" = %d', $item->DrupalID, $parent->ID));
         if ($existingPage) {
             switch ($strategy) {
                 case ExternalContentTransformer::DS_OVERWRITE:
                     $page = $existingPage;
                     break;
                 case ExternalContentTransformer::DS_DUPLICATE:
                     break;
                 case ExternalContentTransformer::DS_SKIP:
                     return;
             }
         }
         $page->Title = $item->Title;
         $page->MenuTitle = $item->Title;
         $page->ParentID = $parent->ID;
         $page->DrupalID = $item->DrupalID;
         $page->OriginalData = serialize($item->getRemoteProperties());
         $page->write();
         $this->importMedia($item, $page);
         return new TransformResult($page, $item->stageChildren(), $item);
     }
 }
 public function updateCMSFields(FieldList $fields)
 {
     $taxonomySourceFunction = function () {
         $source = TaxonomyTerm::get()->exclude('ParentID', 0);
         $result = array();
         if ($source->count()) {
             foreach ($source as $term) {
                 $result[$term->ID] = $term->getTaxonomyName() . ": {$term->Title}";
             }
         }
         asort($result);
         return $result;
     };
     $taxonomySource = $taxonomySourceFunction();
     $fields->addFieldToTab('Root.Main', ListBoxField::create('Terms', 'Terms', $taxonomySource, null, null, true)->useAddNew('TaxonomyTerm', $taxonomySourceFunction, FieldList::create(TextField::create('Name', 'Title'), DropdownField::create('ParentID', 'Parent', TaxonomyTerm::get()->filter('ParentID', 0)->map()->toArray())->setEmptyString(''))), 'Content');
 }
 protected function importTags($item, $page)
 {
     $params = $this->importer->getParams();
     $relation = $params['TaxonomyRelation'];
     // Check that we should import the tags and import them into the specified relation.
     if (!$relation || $page->getRelationClass($relation) != 'TaxonomyTerm') {
         return;
     }
     if (class_exists('TaxonomyTerm')) {
         $relationList = $page->{$relation}();
         foreach ($item->Tags as $tag) {
             $terms = TaxonomyTerm::get('TaxonomyTerm')->Filter('Name', $tag['name']);
             if ($terms->exists()) {
                 $relationList->add($terms->first());
             }
         }
     }
 }
 /**
  * Get the TaxonomyTerm related to the current tag GET parameter.
  *
  * @return TaxonomyTerm
  */
 public function CurrentTag()
 {
     $tagID = $this->request->getVar('tag');
     if (isset($tagID)) {
         return TaxonomyTerm::get_by_id('TaxonomyTerm', (int) $tagID);
     }
 }
 public function testRecursiveDeleteFromTopLevel()
 {
     $this->objFromFixture('TaxonomyTerm', 'Plant')->delete();
     $this->assertEquals(TaxonomyTerm::get()->filter(array('Name' => 'Carrot'))->Count(), 0, "Removing top level term removes all children");
 }