/**
  * Do stuff before the decorated object is written.
  * In this case create the associated folder if it does not already exist.
  *
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $folder = null;
     // Check whether the folder id is 0 or the Folder object does not exist
     if ($this->owner->AssociatedFolderID != 0) {
         $folder = DataObject::get_by_id('Folder', $this->owner->AssociatedFolderID);
     }
     if (!$folder) {
         // A valid folder does not exist
         $this->createAssociatedFolder();
     } else {
         if (!file_exists($folder->getFullPath())) {
             // The folder object may exist but the folder on the file system does not.
             mkdir($folder->getFullPath(), Filesystem::$folder_create_mask);
         }
     }
 }
	/**
	 * Update Metadata fields function
	 */
	public function onBeforeWrite () {
		
		$id = $this->owner->ID;		
		if($id){
			
			// if GenerateMetaData checkbox is checked, generate metadata based on content and title
			if(isset($_REQUEST['GenerateMetaData']) && $_REQUEST['GenerateMetaData']){
				
				if(self::$update_meta_title == 1){
					// Empty MetaTitle
					$this->owner->MetaTitle = '';
					// Check for Content, to prevent errors
					if($this->owner->Title){
						$this->owner->MetaTitle = strip_tags($this->owner->Title);
					}
				}
				if(self::$update_meta_desc == 1){
					// Empty MetaDescription
					$this->owner->MetaDescription = '';
					// Check for Content, to prevent errors
					if($this->owner->Content){
						$this->owner->MetaDescription = html_entity_decode(strip_tags($this->owner->Content), ENT_COMPAT , 'UTF-8');
						if(self::$meta_desc_length > 0 && strlen($this->owner->MetaDescription) > self::$meta_desc_length) {
							$this->owner->MetaDescription = substr($this->owner->MetaDescription, 0, self::$meta_desc_length) . "...";
						}
					}
				}
				
				if(self::$update_meta_keys == 1){
					// Empty MetaKeywords
					$this->owner->MetaKeywords = '';
					// Check for Content, to prevent errors
					if($this->owner->Content){
						// calculateKeywords
						$keystring = self::calculateKeywords($this->owner->Content, self::$min_word_char, self::$keyword_amount, self::$exclude_words);
						if($keystring){	
							$this->owner->MetaKeywords = $keystring;
						}	
					}
				}
			}
		}

    parent::onBeforeWrite ();
  }
 function onBeforeWrite()
 {
     if (!$this->owner->ID && !$this->owner->SubsiteID) {
         $this->owner->SubsiteID = Subsite::currentSubsiteID();
     }
     // If the content has been changed, then the page should be marked as 'custom content'
     if (!$this->nextWriteDoesntCustomise && $this->owner->ID && $this->owner->MasterPageID && !$this->owner->CustomContent) {
         $changed = $this->owner->getChangedFields();
         foreach (self::$template_fields as $field) {
             if (isset($changed[$field]) && $changed[$field]) {
                 $this->owner->CustomContent = true;
                 FormResponse::add("if(\$('Form_EditForm_CustomContent')) \$('Form_EditForm_CustomContent').checked = true;");
                 break;
             }
         }
     }
     $this->nextWriteDoesntCustomise = false;
     parent::onBeforeWrite();
 }