/**
  * If this is attached to an object with the hierarchy extension, it returns
  * a set of a schema objects attached to any ancestors (which should be
  * present on this object).
  *
  * @return ArrayList
  */
 public function getInheritedSchemas()
 {
     $result = new ArrayList();
     if (!$this->owner->hasExtension('Hierarchy')) {
         return new ArrayList();
     }
     $ids = array();
     $parents = $this->owner->getAncestors();
     foreach ($parents as $parent) {
         $ids[] = $parent->ID;
     }
     if (count($ids)) {
         $filter = sprintf('"MetadataSchema"."ID" = "MetadataSchemaLink"."SchemaID"' . ' AND "MetadataSchemaLink"."ParentClass" = \'%s\'' . ' AND "MetadataSchemaLink"."ParentID" IN (%s)', ClassInfo::baseDataClass($this->owner->class), implode(', ', $ids));
         $result = MetadataSchema::get()->innerJoin('MetadataSchemaLink', $filter);
         if ($result) {
             $result = new ArrayList($result->toArray());
         } else {
             $result = new ArrayList();
         }
     }
     if ($this->owner instanceof SiteTree) {
         // Check SiteConfig too
         $config = SiteConfig::current_site_config();
         if ($config->hasExtension('MetadataExtension')) {
             $schemas = $config->getAttachedSchemas();
             if ($schemas && $schemas->count()) {
                 $result->merge($schemas);
             }
         }
     }
     return $result;
 }
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // get schemas that need creating
     $schemas = $this->config()->get('default_schemas');
     require_once 'spyc/spyc.php';
     foreach ($schemas as $file) {
         if (file_exists(Director::baseFolder() . '/' . $file)) {
             $parser = new Spyc();
             $factory = new FixtureFactory();
             $fixtureContent = $parser->loadFile(Director::baseFolder() . '/' . $file);
             if (isset($fixtureContent['MetadataSchema'])) {
                 $toBuild = array();
                 // check if it exists or not, if so don't re-create it
                 foreach ($fixtureContent['MetadataSchema'] as $id => $desc) {
                     $name = isset($desc['Name']) ? $desc['Name'] : null;
                     if (!$name) {
                         throw new Exception("Cannot create metadata schema without a name");
                     }
                     $existing = MetadataSchema::get()->filter('Name', $name)->first();
                     if ($existing) {
                         $factory->setId('MetadataSchema', $id, $existing->ID);
                     } else {
                         $factory->createObject('MetadataSchema', $id, $desc);
                         DB::alteration_message('Metadata schema ' . $id . ' created', 'created');
                     }
                 }
                 // don't need this now
                 unset($fixtureContent['MetadataSchema']);
                 // go through and unset any existing fields
                 $toBuild = array();
                 foreach ($fixtureContent as $class => $items) {
                     foreach ($items as $identifier => $data) {
                         $nameField = isset($data['Name']) ? 'Name' : (isset($data['Key']) ? 'Key' : '');
                         if (!strlen($nameField)) {
                             throw new Exception("Metadata fields must have a Name or Key field defined");
                         }
                         if (!isset($data['Title'])) {
                             $data['Title'] = $data[$nameField];
                         }
                         $existing = $class::get()->filter($nameField, $data[$nameField])->first();
                         if ($existing) {
                             $factory->setId($class, $identifier, $existing->ID);
                         } else {
                             $factory->createObject($class, $identifier, $data);
                             DB::alteration_message('Metadata field ' . $data[$nameField] . ' created', 'created');
                         }
                     }
                 }
             }
         }
     }
 }