Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Output DataObject to XML

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1117 Views
  • bunheng
    Avatar
    Community Member
    69 Posts

    Output DataObject to XML Link to this post

    Hi,

    I would like to know is there any possible way to output DataObject into XML format, I want to use with my flash slideshow, is there any tutorial or trick, I have to complete my project today.

    Thanks
    Bunheng

  • UncleCheese
    Avatar
    4085 Posts

    Re: Output DataObject to XML Link to this post

    Sure.. you can just do:

    return $yourObject->renderWith('YourXMLTemplate');

    YourXMLTemplate.ss

    <?xml ... .?>
    <SomeField>$SomeField</SomeField>
    <AnotherField>$AnotherField</AnotherField>
    <RelatedStuff>
    <% control MyHasManyRelation %>
    <Something>$Something</Something>
    <% end_control %>
    </RelatedStuff>

    etc..

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • bunheng
    Avatar
    Community Member
    69 Posts

    Re: Output DataObject to XML Link to this post

    Hi UncleCheese,

    First of all thanks for your kindness advice. Assuming with your comment I have DataObject call NewsItem with the following code:

    --------------------------------------------
    NewsItem.php
    --------------------------------------------

    class NewsItem extends DataObject {

    static $db = array(
    'Title' => 'Varchar(255)',
    'Details' => 'HTMLText',
    'Source' => 'Varchar(255)',
    'Date' => 'Date'
    );
    static $has_one = array(
    'NewsPage' => 'NewsPage',
    'NewsPhoto' => 'Image'
    );
    static $summary_fields = array(
    'Title' => 'Title',
    'Source' => 'Source',
    'Date' => 'Date'
    );

    public function getCMSFields() {
    return new FieldSet(
    new TextField('Title'),
    new SimpleTinyMCEField('Details', 'News Details'),
    new TextField('Source', 'News Source'),
    new DatePickerField('Date', 'Release Date'),
    new ImageField('NewsPhoto', 'Photo', Null, Null, Null, 'Uploads/latest-news/photos/')
    );
    }

    function canView() {
    return true;
    }

    //Return the Name as a menu title
    public function MenuTitle() {
    return $this->Title;
    }

    public function Link() {
    if ($NewsPage = $this->NewsPage()) {
    return $NewsPage->Link('show/') . $this->ID;
    }
    }

    public function LinkingMode() {
    if ($Controller = Controller::CurrentPage() && Controller::CurrentPage()->ClassName == 'NewsPage') {
    if (Controller::CurrentPage()->getAction() == 'show' && $NewsItem = Controller::CurrentPage()->getNewsItem()) {
    return ($NewsItem->ID == $this->ID) ? 'current' : 'link';
    }
    }
    }

    }

    --------------------------------------------
    NewsPage.php
    --------------------------------------------
    class NewsPage extends Page {

    static $has_many = array(
    'NewsItems' => 'NewsItem'
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
    $manager = new DataObjectManager(
    $this,
    'NewsItems',
    'NewsItem'
    );
    $fields->addFieldToTab("Root.Content.News", $manager);
    return $fields;
    }

    }

    class NewsPage_Controller extends Page_Controller {

    static $allowed_actions = array(
    'show','xml'
    );

    function show() {
    if ($NewsItem = $this->getNewsItem()) {
    $Data = array(
    'NewsItem' => $NewsItem
    );
    return $this->Customise($Data);
    } else {
    return $this->httpError(404, 'Not found');
    }
    }

    public function getNewsItem() {
    $Params = $this->getURLParams();

    if (is_numeric($Params['ID']) && $NewsItem = DataObject::get_by_id('NewsItem', $Params['ID'])) {
    return $NewsItem;
    }
    }

    public function Breadcrumbs() {

    $Breadcrumbs = parent::Breadcrumbs();

    if ($NewsItem = $this->getNewsItem()) {
    $Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);

    //Count the parts
    $NumOfParts = count($Parts);
    $Parts[$NumOfParts - 2] = ("<li><a href=\"" . $this->Link() . "\">" . $this->MenuTitle . "</a></li>");

    $Parts[$NumOfParts] = ("<li class=\"current\">" . $NewsItem->Title . "</li>");
    $Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
    }

    return $Breadcrumbs;
    }

    public function init() {
    parent::init();
    Requirements::css('news/css/news.css');
    }

    }

    ---------------------------------------------------------------------------------
    Could you please kindly just show me where I can place function to render XML within template file it is in PageController, or I have to create new page controller.

    Best Regards,
    Bunheng

  • UncleCheese
    Avatar
    4085 Posts

    Re: Output DataObject to XML Link to this post

    You can really put it anywhere. You could put it in your NewsPage_Controller class, in which case you'd just have to pass the ID of the news item you want in the url.

    /your-news-page/xml/123

    public function xml(SS_HTTPRequest $r) {
    if($news = DataObject::get_by_id("NewsItem", (int) $r->param('ID'))) {
    return $news->renderWith('MyXMLTemplate');
    }
    return $this->httpError(404);
    }

    And of course, don't forget to update your $allowed_actions array in the controller.

    To tidy things up, I'd probably add a new template accessor method to your NewsItem class, XMLLink(), for instance, that would generate its link to the NewsPage controller.

    Good luck!

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • bunheng
    Avatar
    Community Member
    69 Posts

    Re: Output DataObject to XML Link to this post

    Hi,

    Now I can get my multimedia work with xml generate from DataObject.
    Thanks UncleCheese for your comment and support.

    Regards,
    Bunheng

    1117 Views
Page: 1
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.