Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] write a xml file - OnAfterWrite


Go to End


19 Posts   7677 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 March 2011 at 1:17pm

Just because the file is 0 bytes doesn't mean your template isn't working. var_dump() the contents of $data and see if there's anything in it. If there is, then your problem is in the file_put_contents() function.

Avatar
bummzack

Community Member, 904 Posts

16 March 2011 at 8:38pm

It's probably easier to test the rendering using a function in your controller.. like:

public function pageflip(){
	return $this->renderWith('PageFlipXML);
}

Then you should be able to open yoursite.tld/page/pageflip to get the xml output (for testing or direct usage in flash).

Avatar
borriej

Community Member, 267 Posts

16 March 2011 at 8:43pm

Edited: 16/03/2011 8:45pm

When i hit save in the DOM i see this for a short second.

string(0) ""

Redirecting to http://www.url.nl/admin/EditForm/field/FlyerPaginas/item/18/edit?ctf[FlyerPaginas][start]=0&ctf[FlyerPaginas][per_page]=10&ctf[FlyerPaginas][showall]=0&ctf[FlyerPaginas][sort]=&ctf[FlyerPaginas][sort_dir]=&ctf[FlyerPaginas][search]=&ctf[FlyerPaginas][filter]=&ctf[FlyerPaginas][view]=... (output started on /public/sites/www.url.nl/mysite/code/Flyer.php, line 62)

line 62 = var_dump ($data);
So string(0) "" means there's nothing rendered?

Avatar
borriej

Community Member, 267 Posts

16 March 2011 at 8:53pm

Edited: 16/03/2011 9:05pm

Ok it is giving me a nice page WITH xml content

<book plugins="anchors, backgroundsounds, batchpages, bookmarks, console, gallery, help, javascript, keyboardnavigation, links, navigationbar, options, overlays, pdflinks, print, slideshow, swfaddress, titles">
<foreground>
	</foreground>
−
<chapter>
−
<page>
<img src="/assets/Uploads/abcdef.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef1.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef2.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef3.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef4.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef5.jpg"/>
</page>
−
<page>
<img src="/assets/Uploads/abcdef6.jpg"/>
</page>
</chapter>
</book>

How would i get this data to my file?

Avatar
borriej

Community Member, 267 Posts

16 March 2011 at 9:40pm

Edited: 16/03/2011 9:42pm

When i tried this:

	protected function onAfterWrite(){
	   parent::onAfterWrite();

		$data = $this->renderWith('PageFlipXML');	
		$ourFileName = "/public/sites/www.url.nl/pageflip/megazine/megazine.mz3";
		$fh = fopen($ourFileName, 'w+') or die("Can't open file");
		fwrite($fh, $data);
		fclose($fh);    
	}

it does write something.. 452 bytes file with the content:

<book plugins="anchors, backgroundsounds, batchpages, bookmarks, console, gallery, help, javascript, keyboardnavigation, links, navigationbar, options, overlays, pdflinks, print, slideshow, swfaddress, titles">
<foreground>
	</foreground>
−
<chapter>
−
    <page>
        <img src=""/>
    </page>
    
</chapter>
</book>

So i seems it isnt processing the template correctly?

When i go to the public function url:

www.url.nl/Flyer/pageflip

i do get all src's in the img tags..
How do i get the correct template data to the file?

thx!

Avatar
bummzack

Community Member, 904 Posts

17 March 2011 at 8:42pm

Make sure FlyerPaginas is a function of your Page class, not the controller. Because the $this pointer in onAfterWrite isn't the controller, but just the page class (therefore you can't use any functions from the controller).

Avatar
borriej

Community Member, 267 Posts

17 March 2011 at 9:31pm

Edited: 17/03/2011 10:55pm

no change in the file write process.. still get a 0bytes file.

what am i doing wrong?

There must be a way to create a xml file with a onafterwrite() using a template. What code do you use?


<?php

class Flyer extends Page {

	static $has_many = array (
		'FlyerPaginas' => 'FlyerPagina'
	);
	
	public function getCMSFields() {
    $fields = parent::getCMSFields();
    $manager = new FileDataObjectManager(
      $this,
      'FlyerPaginas', // relation name
      'FlyerPagina', // class name of the DataObject
      'Image', // name of the file relation in the DataObject
      array(
			 'Title' => 'Title',
			// 'Comment' => 'Comment',
	 	), // headings
      'getCMSFields_forPopup' // name of the function for the popup fields
    
    );

	$fields->removeFieldFromTab('Root.Content.Main','Content');
	$fields->addFieldToTab('Root.Content.Images', $manager);
    return $fields;
  }
	public function megazine(){
	   return $this->renderWith('PageFlipXML');
	}	

}

class Flyer_Controller extends Page_Controller {

}

class FlyerPagina extends DataObject {

	static $db = array (
		'Title' => 'Text',
	//	'Comment' => 'HTMLText',
	);
	static $has_one = array (
		'Image' => 'Image',
		'Flyer' => 'Flyer'
	);

	public function getCMSFields_forPopup() {
		$fields = new FieldSet();
		//$fields->push(new TextField('Title'));
		//$fields->push(new SimpleTinyMCEField('Comment'));
		$fields->push(new FileUploadField('Image','Upload files'));
		return $fields;
	}
	
	protected function onAfterWrite(){
	   parent::onAfterWrite();
		$data = $this->renderWith('PageFlipXML');
		file_put_contents('/public/sites/www.url.nl/pageflip/megazine/PageFlipXML.mz3', $data);
	}
	


}

template code:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book SYSTEM "http://www.megazine3.de/megazine2.dtd"> 
  
<book plugins="zoom, keyboardnavigation, navigationbar, swfaddress" 
lang="en"	
pagewidth="567" pageheight="567"  
	errorlevel="ERROR" 
	zoomminscale="0.5" zoommaxscale="4.0" 
	zoomsnap="0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 4.0" 
	zoomsteps="2" 
	dragrange="20" 
	maxloaded="32" 
	qualitycontrol="true" 
	thumbscale="0.5" 
	searchmethod="client" 
	cornerhint="true" 
	pagethickness="0.2" 
	shadows="0.3" 
	searchclear="false" 
	startpage="1" 
	centercovers="false" 
	pageoffset="0">     
    
    
<!-- With zoomstep= "0.05" these zoom steps are achieved:  1.0 - 2.0 - 3.0 - 4.0 - ... 19.0 - 20.0 - 21.0 
	reason: the span (zoommaxscale-zoomminscale) is devided into 1/zoomstep (i.e. 20) intervals
	So span*zoomstep=1.0,  which results in a nice step size of 1.0--> 


	<foreground>
	</foreground>
	<chapter>
    
    <% control FlyerPaginas %>
		<page>
               <% control Image %><img src="$setWidth(2362).Link" scale="1" width="567" height="567"><% end_control %>
        </page>
        
    <% end_control %>
    
	</chapter>
</book>

Avatar
bummzack

Community Member, 904 Posts

17 March 2011 at 11:34pm

Edited: 17/03/2011 11:46pm

Obviously, the onAfterWrite method should be in your Flyer page.. Or you leave it in FlyerPagina (if you want to write/update the file whenever a FlyerPagina is saved), but then your onAfterWrite method must look like this:

protected function onAfterWrite(){
	parent::onAfterWrite();
	if($this->FlyerID){
		$data = $this->Flyer->renderWith('PageFlipXML');
		file_put_contents('/public/sites/www.url.nl/pageflip/megazine/PageFlipXML.mz3', $data);
	}
}

and if file_put_contents doesn't work on your system, then use the fwrite function..

Update
You should always check if there's a Flyer.. so I updated the onAfterWrite function above..