Jump to:

7937 Posts in 1537 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Upcoming Function ?

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: 444 Views
  • benni91
    Avatar
    Community Member
    72 Posts

    Upcoming Function ? Link to this post

    Hi,

    can someone tell me how to make an upcoming function?
    i created a data object with event entries. i want that only the event which is the next is displayed.

    that's my eventobject.php

    <?php
    class EventObject extends DataObject {

       public static $db = array(
          'NextEvent' => 'Boolean',
          'EventName' => 'Text',
          'EventDate' => 'Date',
          'EventTime' => 'Text',
          'EventDescription' => 'HTMLText',
       );

       public static $has_one = array(
          'EventFlyer' => 'Image',
          'EventPage' => 'EventPage'
       );
       
       static $default_sort = "Created DESC";

    function getCMSFields_forPopup() {
          $fields = new FieldSet();
             $fields->push(new CheckBoxField('NextEvent', 'findet das Event als nächstes Statt?(IMMER NUR BEI EINEM EVENT AUSWÄHLEN!!!)'));
             $fields->push(new TextField('EventName', 'Was findet statt?'));
             $fields->push($Date = new DateField('EventDate', 'An welchem Datum findet es statt?'));
             $Date->setConfig('showcalendar', true);   
             $fields->push(new TextField('EventTime', 'Zu welcher Uhrzeit findet es statt?'));
             $fields->push(new SimpleTINYMCEField('EventDescription', 'Genauere Infos, Auftritte, Preise, etc. zum Event'));
             $fields->push($EventFlyer = new ImageUploadField('EventFlyer', 'Der Flyer des Events'));
             $EventFlyer->setUploadFolder('event');
    return $fields;
    }
    }
    ?>

    i really hope someone could help me.
    Regards
    Benni

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Upcoming Function ? Link to this post

    I'm assuming that your EventPage class has a has_many relation to EventObject called EventObjects.

    The has_many relation function takes the same arguments as a DataObject::get(), so you can filter and sort the resultant set. Something like

    public function UpcomingEvent() {
    return $this->EventObjects('"EventDate" >= CURDATE()', '"EventDate" ASC', '', 1)->First();
    }

    What this does is only returns EventObjects with an EventDate that's either today or in the future, sorts the results by ascending EventDate and then only returns the first one. As the EventObjects() method always returns a ComponentSet, it then goes and specifically gets the first object in the set, which will either be the next event or null.

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: Upcoming Function ? Link to this post

    Hi simon_w,

    EDIT: I found the problem which brought me to another one. See the Edit at the End

    thx for your help. I used your code like this in my EventPage.php

    <?php
    class EventPage extends Page {
       
       static $default_child = 'none';
       static $allowed_children = 'none';
       static $can_be_root = true;
       
       public static $db = array(
       );

       public static $has_one = array(   
       );

       public static $has_many = array(
          'Events' => 'EventObject',
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->removeFieldFromTab('Root.Content.Main', 'Content');
          // Events
          $fields->addFieldToTab('Root.Content.Main', $Event = new DataObjectManager($this, 'Events', 'EventObject', array('NextEvent' => 'Nächstes Event? (1=ja)', 'EventName' => 'Name des Events', 'EventDate' => 'Datum des Events (Jahr/Monat/Tag)', 'EventTime' => 'Uhrzeit des Events'), 'getCMSFields_forPopup'));
          $Event->copyOnImport = false;
          $Event->setAddTitle('ein Event');
          return $fields;
       }
    }
    class EventPage_Controller extends Page_Controller {
       public function UpcomingEvent() {
          return $this->Events('"EventDate" >= CURDATE()', '"EventDate" ASC', '', 1)->First();
       }
    }

    and that's my Upcoming.ss Include

          <% control getPageByClass(EventPage) %>
             <% control UpcomingEvent %>
                <div id="latest_flyer">
                   <div class="event_flyer_hover"><a href="termine#$EventName $EventDate"><img src="{$BaseHref}$ThemeDir/images/blank.gif" alt="$EventName" /></a></div>
                   $EventFlyer.setWidth(155)
                </div>
                <div id="latest_event_details">
                   <ul>
                      <li><h2>$EventName</h2></li>
                      <li></li>
                      <li>$EventDate.NiceGER</li>
                      <li></li>
                      <li>$EventTime</li> <!-- <p>20<i>30</i> Uhr bis 05<i>30</i> Uhr</p> -->
                      <li></li>
                      <li></li>
                   </ul>
                   <div id="latest_event_more">
                      <div class="button"><a href="termine#$EventName $EventDate">Mehr Infos</a></div>
                      <div class="button_arrow"></div>
                   </div>
                </div>
                <div class="clear"></div>
             <% end_control %>
          <% end_control %>

    The Problem is it shows nothing Just a blank area where the content should be. Do you know where's the mistake?

    Regards Benni

    Edit:
    Ok the Problem is the <% control getPageByClass(EventPage) %>
    If i remove this, the upcoming function works at the EventPage but not on the other Pages.
    Why? <% control getPageByClass(EventPage) %> should make the EventPage accessible from all the other Pages.

    Adding the Function to Page.php results an ServerError ;/

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Upcoming Function ? Link to this post

    That's because the method is in your controller, not the actual EventPage class. If you move it from EventPage_Controller into EventPage, it'll work.

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: Upcoming Function ? Link to this post

    thx a lot, now it works.

    could you help me with three other problems too?

    the first one is a function to get the latest 4 entries out of an dataobject - i sadly don't know how to build such a function.

    and the second one is a little bit more complicated i think. Do you know how to get the newsletter subscriber form, from the latest trunkbuild of the newsletter module into a sidebar? And that without using the functions in the page_controller cause this will damage the jquery slider :/

    last but not least the third one: how to allow that only one checkbox is checked?

    Regards Benni

    444 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.