function onBeforeWrite()
 {
     if ($this->EmailBodyTemplate) {
         $this->EmailBodyTemplate = self::stripSSFromFilename($this->EmailBodyTemplate);
     }
     return parent::onBeforeWrite();
 }
Example #2
0
	function onBeforeWrite() {
		if($this->LegacyURL) {
			$SQL_legacyURL = Convert::raw2sql($this->LegacyURL);
			$existingPages = DataObject::get_one(
				'Page',
				"URLSegment = '{$SQL_legacyURL}'"
			);
			if($existingPages) {
				$this->LegacyURL = null;
			}
		}
		
		parent::onBeforeWrite();
	}
 /**
  *	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();
                     }
                 }
             }
         }
     }
 }
Example #4
0
 /**
  * Handles the UseAsRootForMainNavigation property on before write.
  * 
  * @return void
  *
  * @author Sebastian Diel <*****@*****.**>
  * @since 07.10.2014
  */
 protected function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $request = Controller::curr()->getRequest();
     /* @var $request SS_HTTPRequest */
     if ($request->postVar('ID') == $this->ID && $request->postVar('UseAsRootForMainNavigation') == '1') {
         $this->UseAsRootForMainNavigation = true;
     }
     if ($this->isChanged('UseAsRootForMainNavigation')) {
         $changed = $this->getChangedFields(false, 1);
         $ch = $changed['UseAsRootForMainNavigation'];
         if ($this->UseAsRootForMainNavigation) {
             DB::query('UPDATE SilvercartPage SET UseAsRootForMainNavigation = 0 WHERE ID != ' . $this->ID);
         } elseif ($ch['before'] != $ch['after']) {
             $this->UseAsRootForMainNavigation = true;
         }
     }
 }