Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » [SOLVED] write a xml file - OnAfterWrite

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3
Go to End
Author Topic: 4148 Views
  • borriej
    Avatar
    Community Member
    267 Posts

    [SOLVED] write a xml file - OnAfterWrite Link to this post

    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>

  • UncleCheese
    Avatar
    4085 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

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

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

    @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);
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

    +1 @banal. You stole my words.

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

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

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: [SOLVED] write a xml file - OnAfterWrite Link to this post

    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>

    4148 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.