/**
  *	Apply the parent holder media type and update any respective media type attributes.
  */
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // Set the default media page date.
     if (is_null($this->Date)) {
         $this->Date = date('Y-m-d');
     }
     // Confirm that the external link exists.
     if ($this->ExternalLink) {
         if (stripos($this->ExternalLink, 'http') === false) {
             $this->ExternalLink = 'http://' . $this->ExternalLink;
         }
         $file_headers = @get_headers($this->ExternalLink);
         if (!$file_headers || strripos($file_headers[0], '404 Not Found')) {
             $this->ExternalLink = null;
         }
     }
     // Apply the changes from each media type attribute.
     foreach ($this->record as $name => $value) {
         if (strrpos($name, 'MediaAttribute')) {
             $ID = substr($name, 0, strpos($name, '_'));
             $attribute = MediaAttribute::get_by_id('MediaAttribute', $ID);
             $attribute->Content = $value;
             $attribute->write();
         }
     }
     // Apply the parent holder media type.
     $parent = $this->getParent();
     if ($parent) {
         $type = $parent->MediaType();
         if ($type->exists()) {
             $this->MediaTypeID = $type->ID;
             $type = $type->Title;
         } else {
             $existing = MediaType::get_one('MediaType');
             $parent->MediaTypeID = $existing->ID;
             $parent->write();
             $this->MediaTypeID = $existing->ID;
             $type = $existing->Title;
         }
         // Merge the default and custom default media types and their respective attributes.
         $temporary = array();
         foreach (self::$custom_defaults as $default => $attributes) {
             if (isset(self::$page_defaults[$default])) {
                 self::$page_defaults[$default] = array_unique(array_merge(self::$page_defaults[$default], $attributes));
             } else {
                 $temporary[$default] = $attributes;
             }
         }
         $defaults = array_merge(self::$page_defaults, $temporary);
         // Retrieve existing attributes for the respective media type.
         $attributes = MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $type, 'MediaAttribute.LinkID = ?' => -1));
         // Apply existing attributes to a new media page.
         if (!$this->MediaAttributes()->exists()) {
             if ($attributes->exists()) {
                 foreach ($attributes as $attribute) {
                     // Create a new attribute for each one found.
                     $new = MediaAttribute::create();
                     $new->OriginalTitle = $attribute->OriginalTitle;
                     $new->Title = $attribute->Title;
                     $new->LinkID = $attribute->ID;
                     $new->MediaPageID = $this->ID;
                     $this->MediaAttributes()->add($new);
                     $new->write();
                 }
             } else {
                 if (isset($defaults[$type])) {
                     foreach ($defaults[$type] as $attribute) {
                         $new = MediaAttribute::create();
                         $new->Title = $attribute;
                         $new->LinkID = -1;
                         $new->MediaPageID = $this->ID;
                         $this->MediaAttributes()->add($new);
                         $new->write();
                     }
                 }
             }
         } else {
             // Determine whether there are new attributes for this media page.
             if ($attributes->exists() && isset($defaults[$type])) {
                 $defaults = $defaults[$type];
                 foreach ($attributes as $attribute) {
                     $title = $attribute->OriginalTitle;
                     foreach ($defaults as $index => $default) {
                         if ($title === $default) {
                             // This attribute already exists.
                             unset($defaults[$index]);
                             // Determine whether this media page requires the attribute.
                             if (!$this->MediaAttributes()->filter('OriginalTitle', $title)->exists()) {
                                 // Create a new attribute.
                                 $new = MediaAttribute::create();
                                 $new->OriginalTitle = $title;
                                 $new->Title = $attribute->Title;
                                 $new->LinkID = $attribute->ID;
                                 $new->MediaPageID = $this->ID;
                                 $this->MediaAttributes()->add($new);
                                 $new->write();
                             }
                             break;
                         }
                     }
                 }
                 if (count($defaults)) {
                     // Create a new attribute for the remaining defaults.
                     foreach ($defaults as $attribute) {
                         $new = MediaAttribute::create();
                         $new->Title = $attribute;
                         $new->LinkID = -1;
                         $new->MediaPageID = $this->ID;
                         $this->MediaAttributes()->add($new);
                         $new->write();
                     }
                 }
             }
         }
     }
 }
 /**
  *	Apply the parent holder media type and update any respective media type attributes.
  */
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // Set the default media page date to the current time.
     if (is_null($this->Date)) {
         $this->Date = SS_Datetime::now()->Format('Y-m-d H:i:s');
     }
     // Confirm that the external link exists.
     if ($this->ExternalLink) {
         if (stripos($this->ExternalLink, 'http') === false) {
             $this->ExternalLink = 'http://' . $this->ExternalLink;
         }
         $file_headers = @get_headers($this->ExternalLink);
         if (!$file_headers || strripos($file_headers[0], '404 Not Found')) {
             $this->ExternalLink = null;
         }
     }
     // Apply the changes from each media type attribute.
     foreach ($this->record as $name => $value) {
         if (strrpos($name, 'MediaAttribute')) {
             $ID = substr($name, 0, strpos($name, '_'));
             $attribute = MediaAttribute::get_by_id('MediaAttribute', $ID);
             $attribute->Content = $value;
             $attribute->write();
         }
     }
     // Apply the parent holder media type.
     $parent = $this->getParent();
     if ($parent) {
         $type = $parent->MediaType();
         if ($type->exists()) {
             $this->MediaTypeID = $type->ID;
             $type = $type->Title;
         } else {
             $existing = MediaType::get_one('MediaType');
             $parent->MediaTypeID = $existing->ID;
             $parent->write();
             $this->MediaTypeID = $existing->ID;
             $type = $existing->Title;
         }
         // Merge the default and custom default media types and their respective attributes.
         $temporary = array();
         foreach (self::$custom_defaults as $default => $attributes) {
             if (isset(self::$page_defaults[$default])) {
                 self::$page_defaults[$default] = array_unique(array_merge(self::$page_defaults[$default], $attributes));
             } else {
                 $temporary[$default] = $attributes;
             }
         }
         $defaults = array_merge(self::$page_defaults, $temporary);
         // Apply existing attributes to a new media page.
         if (!$this->MediaAttributes()->exists()) {
             // Retrieve existing attributes for the respective media type.
             $attributes = MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where("MediaType.Title = '" . Convert::raw2sql($type) . "' AND MediaAttribute.LinkID = -1");
             if ($attributes->exists()) {
                 foreach ($attributes as $attribute) {
                     // Create a new attribute for each one found.
                     $new = MediaAttribute::create();
                     $new->Title = $attribute->Title;
                     $new->LinkID = $attribute->ID;
                     $new->MediaPageID = $this->ID;
                     $this->MediaAttributes()->add($new);
                     $new->write();
                 }
             } else {
                 if (isset($defaults[$type])) {
                     foreach ($defaults[$type] as $attribute) {
                         $new = MediaAttribute::create();
                         $new->Title = $attribute;
                         $new->LinkID = -1;
                         $new->MediaPageID = $this->ID;
                         $this->MediaAttributes()->add($new);
                         $new->write();
                     }
                 }
             }
         }
     }
 }
 /**
  *	Confirm that the current type is valid.
  */
 public function validate()
 {
     $result = parent::validate();
     // Confirm that the current type has been given a title and doesn't already exist.
     if ($result->valid() && !$this->Title) {
         $result->error('"Title" required!');
     } else {
         if ($result->valid() && MediaType::get_one('MediaType', array('ID != ?' => $this->ID, 'Title = ?' => $this->Title))) {
             $result->error('Type already exists!');
         }
     }
     // Allow extension customisation.
     $this->extend('validateMediaType', $result);
     return $result;
 }
 /**
  *	Confirm that the current type is valid.
  */
 public function validate()
 {
     $result = parent::validate();
     // Confirm that the current type has been given a title and doesn't already exist.
     if ($result->valid() && !$this->Title) {
         $result->error('"Title" required!');
     } else {
         if ($result->valid() && MediaType::get_one('MediaType', "ID != " . (int) $this->ID . " AND Title = '" . Convert::raw2sql($this->Title) . "'")) {
             $result->error('Type already exists!');
         }
     }
     // Allow extension customisation.
     $this->extend('validateMediaType', $result);
     return $result;
 }