/** * 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; }
/** * Assign the current attribute to each media page of the respective type. */ public function onBeforeWrite() { parent::onBeforeWrite(); // Set the original title of the current attribute for use in templates. if (is_null($this->OriginalTitle)) { $this->OriginalTitle = $this->Title; } // Retrieve the respective media type for updating all attribute references. $parameters = Controller::curr()->getRequest()->requestVars(); $matches = array(); $result = preg_match('#TypesAttributes/item/[0-9]*/#', $parameters['url'], $matches); if ($result) { $ID = preg_replace('#[^0-9]#', '', $matches[0]); $pages = MediaPage::get()->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where('MediaType.ID = ' . (int) $ID); // Apply this new attribute to existing media pages of the respective type. if ($pages && (is_null($this->MediaPageID) || $this->MediaPageID === 0)) { foreach ($pages as $key => $page) { if ($key === 0) { // Apply the current attribute to the first media page. self::$write_flag = true; $this->LinkID = -1; $this->MediaPageID = $page->ID; $page->MediaAttributes()->add($this); } else { // Create a new attribute for remaining media pages. $new = MediaAttribute::create(); $new->OriginalTitle = $this->OriginalTitle; $new->Title = $this->Title; $new->LinkID = $this->ID; $new->MediaPageID = $page->ID; $page->MediaAttributes()->add($new); $new->write(); } } } else { if ($pages) { // Confirm that a write occurrence doesn't already exist. if (!self::$write_flag) { foreach ($pages as $page) { foreach ($page->MediaAttributes() as $attribute) { // Confirm that each attribute is linked to the original attribute. if ($attribute->LinkID == $this->ID && $attribute->Title !== $this->Title) { // Apply the changes from this attribute. self::$write_flag = true; $attribute->Title = $this->Title; $attribute->write(); } } } self::$write_flag = false; } } } } }
/** * 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(); } } } } } }