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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Export CSV from EditForm


Go to End


3 Posts   825 Views

Avatar
_Vince

Community Member, 165 Posts

12 April 2016 at 11:43am

Edited: 12/04/2016 11:44am

I have a project where people can book places on events. There are dozens of events. I have been requested to generate a report listing attendees so they can be checked in at the door. Because there are so many events, I would like to add an "Attendees Report" button to the editform for each event. I have the button, and it works.

BUT... instead of outputting the data in a CSV, I get the (correct) data displaying in the CMS as if it was being echoed.

Here is my function

//***********************************************
	public function getCustomersForEvent($data, $form){
//***********************************************

		$attendees = CustomerOrderLine::get()->filter(array("EventID" => $data["ID"]));

		if(!$attendees){
			return false;
		}

		$fileName = "report.csv";

		$separator = ",";
		$csvColumns = array("ID", "Description", "Customer");

		$fileData = "";

		foreach($attendees as $row){
			$fileData .= $row->Event()->EventName . $separator;
			$fileData .= $row->CustomerOrder()->Customer()->FirstName . " " . $row->CustomerOrder()->Customer()->Surname . "\n";
		}

		return SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
	}

I have had a look at this http://www.silverstripe.org/community/forums/general-questions/show/15325 but the data echoed to the CMS anyway.

Can someone please tell me what I am doing wrong? As I said, the data is fine, but I'm not getting the CSV Open or Save dialog.

Avatar
_Vince

Community Member, 165 Posts

14 April 2016 at 10:33pm

In the end, I wrote everything to a file in the assets folder and returned a sprintf with a link to the file. But really, it looks kind of yuk, so if anyone has a better suggestion, I'd be really happy to hear it.

Avatar
martimiz

Forum Moderator, 1391 Posts

18 April 2016 at 4:34am

I've sometimes made it easy for myself by adding a link to the editform, in this case probably someting like

<a href="/showresults/csv?event=4" target="blank">Export csv</a> 

Then create a CSVEventController, with a function csv() that returns the details, set the proper routing in yml, and you're on your way :)

A lot easier would be to create a modeladmin for the events, make it filterable on event title, and use the default export to CSV button. If you don't want people to edit events from there, make sure it's readonly...