/**
  *	Update previous media attributes.
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // Retrieve existing "start time" attributes.
     $attributes = MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => 'Event', 'OriginalTitle = ?' => 'Start Time'));
     foreach ($attributes as $attribute) {
         // These should now be "time" attributes.
         $attribute->OriginalTitle = 'Time';
         $attribute->Title = 'Time';
         $attribute->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.
     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();
                     }
                 }
             }
         }
     }
 }
 /**
  *	Display the respective CMS media type attributes.
  */
 public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     if ($this->Title) {
         // Display the title as read only.
         $fields->replaceField('Title', ReadonlyField::create('Title'));
         $fields->addFieldToTab('Root.Main', LiteralField::create('MediaAttributesTitle', "<div class='field'><label class='left'>Custom Attributes</label></div>"));
         // Allow customisation of media type attributes if a respective media page exists, depending on the current CMS user permissions.
         if (MediaPage::get()->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $this->Title))->exists()) {
             $configuration = $this->checkPermissions() === false ? GridFieldConfig_RecordViewer::create() : GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldDeleteAction');
             $fields->addFieldToTab('Root.Main', GridField::create('MediaAttributes', 'Custom Attributes', MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $this->Title, 'MediaAttribute.LinkID = ?' => -1)), $configuration)->setModelClass('MediaAttribute'));
         } else {
             // Display a notice that respective media pages should first be created.
             Requirements::css(MEDIAWESOME_PATH . '/css/mediawesome.css');
             $fields->addFieldToTab('Root.Main', LiteralField::create('MediaNotice', "<p class='mediawesome notice'><strong>No {$this->Title} Pages Found</strong></p>"));
         }
     }
     // Allow extension customisation.
     $this->extend('updateMediaTypeCMSFields', $fields);
     return $fields;
 }
 /**
  *	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();
                     }
                 }
             }
         }
     }
 }