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

Upcoming Function ?


Go to End


5 Posts   1206 Views

Avatar
benni91

Community Member, 72 Posts

12 February 2012 at 12:25pm

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

Avatar
(deleted)

Community Member, 473 Posts

12 February 2012 at 1:04pm

Edited: 12/02/2012 1:05pm

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.

Avatar
benni91

Community Member, 72 Posts

13 February 2012 at 12:47am

Edited: 13/02/2012 1:08am

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 ;/

Avatar
(deleted)

Community Member, 473 Posts

13 February 2012 at 7:10am

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.

Avatar
benni91

Community Member, 72 Posts

13 February 2012 at 9:46am

Edited: 13/02/2012 10:07am

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