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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[SOLVED] write a xml file - OnAfterWrite


Go to End
Reply


19 Posts   4618 Views

Avatar
borriej

Community Member, 267 Posts

15 March 2011 at 9:32am

Edited: 15/03/2011 9:34am

It it possible to make a onAfterWrite function that writes a xml file?

I want to manage some images with the DOM .. after save and publish it must write the path of these images to a xml file.

In this way i can use the megazine3 pageflip to show my flyer online :) i think it would be very very nice for silverstripe :)

output like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book SYSTEM "http://www.megazine3.de/megazine2.dtd">

<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="http://url.nl/images/name.jpg"/>
      </page>
   </chapter>
</book>

Avatar
UncleCheese

Forum Moderator, 4092 Posts

15 March 2011 at 2:47pm

Yup. You can use onAfterWrite() or onBeforeWrite() to do just about anything. They're just open hooks.

Avatar
borriej

Community Member, 267 Posts

15 March 2011 at 8:57pm

How do i write this in silverstripe code?

in normal php i would do something like this:

$ourFileName = "xml.xml";
$fh = fopen($ourFileName, 'w+') or die("Can't open file");
$stringData = "<Page><img src="..."></Page>";
fwrite($fh, $stringData);
fclose($fh);      

Any idea?

Thx!

Avatar
Willr

Forum Moderator, 5513 Posts

15 March 2011 at 9:41pm

in normal php i would do something like this

That'll still work in SS, you will need to fix the path to the file. By default the file path is sapphire/ so you'll need to use "../mysite/" to get to the mysite folder. Or use can use the absolute path through BASE_PATH

Avatar
banal

Community Member, 901 Posts

16 March 2011 at 5:04am

@borriej Instead of composing your XML string in PHP, I suggest you use the SilverStripe template engine.. its really nice for this kind of things as well.
Instead of BASE_PATH, you could also use ASSETS_PATH (which obviously points to the writable assets directory). Something like:

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

   // render with xml template (MyXMLTemplate.ss)
   $data = $this->renderWith('MyXMLTemplate');

   // write data to file
   file_put_contents(ASSETS_PATH .'/myXmlFile.xml', $data);
}

Avatar
UncleCheese

Forum Moderator, 4092 Posts

16 March 2011 at 5:57am

+1 @banal. You stole my words. :)

Avatar
borriej

Community Member, 267 Posts

16 March 2011 at 6:56am

Edited: 16/03/2011 6:56am

wow that looks very promising. Good that I asked, I'm more of a designer.. so this comes in great :D
Thanks for the help everyone, will try this tonight!

Avatar
borriej

Community Member, 267 Posts

16 March 2011 at 9:46am

Edited: 16/03/2011 9:47am

Ok.. i debugged it line by line and it seems like

    $data = $this->renderWith('PageFlipXML');

is giving me problems, the file is still 0 bytes!
when changing it into $data = 'test' it will write a 4 bytes file, so it is writing.

But the template cant be found i guess?

I placed my template inside mysite/templates & themes/currentTheme/templates
File name is matching offcourse.

template code: PageFlipXML.ss

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book SYSTEM "http://www.megazine3.de/megazine2.dtd">
<book plugins="anchors, backgroundsounds, batchpages, bookmarks, console, gallery, help, javascript, keyboardnavigation, links, navigationbar, options, overlays, pdflinks, print, slideshow, swfaddress, titles">
   <foreground>
   </foreground>
   <chapter>

<% control FlyerPaginas %>
      <page>
<% control Image %><img src="$Link" /><% end_control %>
      </page>
<% end_control %>

   </chapter>
</book>

Go to Top