public function onBeforeWrite() {
		parent::onBeforeWrite();
		$slug = singleton('SiteTree')->generateURLSegment($this->Identifier);
		$original_slug = $slug;
		$i = 0;
		while($t = PermamailTemplate::get()
			->filter(array("Identifier" => "$slug"))
			->exclude(array("ID" => $this->ID))
			->first()
		) {	
			$i++;
			$slug = $original_slug."-{$i}";
		}
		$this->Identifier = $slug;

		$reflector = EmailReflectionTemplate::create();
		$reflector->process($this->Content);

		$vars = array ();
		foreach($reflector->getTopLevelVars() as $var => $type) {
			$vars[$var] = false;
		}
		foreach($reflector->getTopLevelBlocks() as $block) {
			$vars[$block->getName()] = $block->isLoop();
		}
		// Remove any variables that are no longer in the template
		if($this->TestVariables()->exists()) {
			$this->TestVariables()->exclude(array('Variable' => array_keys($vars)))->removeAll();
		}
		
		$currentVars = $this->TestVariables()->column('Variable');

		foreach($vars as $var => $isList) {
			if(!in_array($var, $currentVars)) {
				$v = PermamailTemplateVariable::create(array(
					'Variable' => $var,
					'PermamailTemplateID' => $this->ID,
					'List' => $isList
				));
				$v->write();
			}
		}
	}
public function onBeforeWrite() {
		parent::onBeforeWrite();
		$slug = singleton('SiteTree')->generateURLSegment($this->Identifier);
		$original_slug = $slug;
		$i = 0;
		while($t = PermamailTemplate::get()
			->filter(array("Identifier" => "$slug"))
			->exclude(array("ID" => $this->ID))
			->first()
		) {	
			$i++;
			$slug = $original_slug."-{$i}";
		}
		$this->Identifier = $slug;

		$reflector = EmailReflectionTemplate::create();
		$reflector->process($this->Content);

		$vars = array ();
		foreach($reflector->getTopLevelVars() as $var => $type) {
			$vars[$var] = false;
		}
		foreach($reflector->getTopLevelBlocks() as $block) {
			$vars[$block->getName()] = $block->isLoop();
		}
		// Remove any variables that are no longer in the template
		if($this->TestVariables()->exists()) {
			$this->TestVariables()->exclude(array('Variable' => array_keys($vars)))->removeAll();
		}
		
		$currentVars = $this->TestVariables()->column('Variable');

		foreach($vars as $var => $isList) {
			if(!in_array($var, $currentVars)) {
				$v = PermamailTemplateVariable::create(array(
					'Variable' => $var,
					'PermamailTemplateID' => $this->ID,
					'List' => $isList
				));
				$v->write();
			}
		}
	}
public function onBeforeWrite ( ) { parent :: onBeforeWrite ( ) ; $ slug = singleton ( 'SiteTree' ) -> generateURLSegment ( $ this -> Identifier ) ; $ original_slug = $ slug ; $ i = 0 ; while ( $ t = PermamailTemplate :: get ( ) -> filter ( array ( "Identifier" => "$slug" ) ) -> exclude ( array ( "ID" => $ this -> ID ) ) -> first ( ) ) { $ i ++ ; $ slug = $ original_slug . "-{$i}" ; } $ this -> Identifier = $ slug ; $ reflector = EmailReflectionTemplate :: create ( ) ; $ reflector -> process ( $ this -> Content ) ; $ vars = array ( ) ; foreach ( $ reflector -> getTopLevelVars ( ) as $ var => $ type ) { $ vars [ $ var ] = false ; } foreach ( $ reflector -> getTopLevelBlocks ( ) as $ block ) { $ vars [ $ block -> getName ( ) ] = $ block -> isLoop ( ) ; } // Remove any variables that are no longer in the template if ( $ this -> TestVariables ( ) -> exists ( ) ) { $ this -> TestVariables ( ) -> exclude ( array ( 'Variable' => array_keys ( $ vars ) ) ) -> removeAll ( ) ; } $ currentVars = $ this -> TestVariables ( ) -> column ( 'Variable' ) ; foreach ( $ vars as $ var => $ isList ) { if ( ! in_array ( $ var , $ currentVars ) ) { $ v = PermamailTemplateVariable :: create ( array ( 'Variable' => $ var , 'PermamailTemplateID' => $ this -> ID , 'List' => $ isList ) ) ; $ v -> write ( ) ; } } }
Sanitise the identifier and populate the variables list
Sanitise the identifier and populate the variables list