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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

User/Member logging


Go to End


5 Posts   1443 Views

Avatar
david_nash

Community Member, 55 Posts

7 September 2010 at 6:18pm

I'm building a site for a client with a file download section. I've created a directory under assets/ to upload the files, and then a custom page type (FileViewerPage) that lists the files to logged in Members.

What I'd like to do is have a tab for the file download section in the CMS that shows a log of what time which member is downloading which file.

The way I'm trying to do this is with a FileDownloadLogger:

class FileDownloadLogger extends DataObject
{
	static $db = array (
		'Filename' => 'Text'
	);
	
	static $has_one = array (
		'Member' => 'Member'
	);
}

To link to the file I append to http://example.com/file-home/download?file=test.pdf

Where test.pdf is in /assets/Client/

Then in my FileViewerPage_Controller I have a download() function:

	function download() {

		$file = $_SERVER['DOCUMENT_ROOT'].'/assets/Client/'.$this->requestParams['file'];
		
		if (file_exists($file)) {	

			//make a new log entry
			$log = new FileDownloadLogger;
			$log->Filename = $this->requestParams['file'];
			$log->Member = Member::currentUser();
			$log->write();
			
			//set headers, send the file with readfile()
		}
	}

It's about this point I get stuck... when I look at the database table FileDownloadLogger, MemberID is always 0.

I think what I want from here is to maybe use DataObjectManager or complexfieldtable to show a list of: time, member (name), file.

I couldn't find anything on using a Member in other DataObject classes... is it even "right" to do that?

Any advice on how I should do this would be most appreciated, thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

7 September 2010 at 7:45pm

Try

$log->MemberID = Member::currentUserID();

If that still doesn't do it then check that Member::currentUserID(); is actually set by doing something like

Debug::show(Member::currentUserID());
die();

Avatar
david_nash

Community Member, 55 Posts

8 September 2010 at 1:24pm

Thanks Will, that's working well.

Now how would I list them in the CMS?

This is what I'm working at:

class FileViewerPage extends Page {

	public function getCMSFields() {
		$f = parent::getCMSFields();
		$mgr = new DataObjectManager(
			$this,
			'FileDownloadLoggers',
			'FileDownloadLogger',
			array('Member'=>'Member', 'Filename'=>'Filename') //not sure what to do here

		);
		$f->addFieldToTab('Root.Content.Log', $mgr);
		return $f;
	}

}

When I do this I get "[User Error] Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'Member'" - which makes sense, because it doesn't know how to output a member... unfortunately I don't either.

How would I display each member's name? It would also be nice to make the name link to the security section.

Avatar
Willr

Forum Moderator, 5523 Posts

8 September 2010 at 2:57pm

You want to display just fields such as

array(
'Member.FirstName' => 'First Name'
..
);

It will then look up the has_one on member for the field 'FirstName'

Avatar
david_nash

Community Member, 55 Posts

9 September 2010 at 12:25pm

Perfect! Thanks Will!

For the record, here's the code I'm using for the "Log" tab:

class FileViewerPage extends Page {

	public function getCMSFields() {
		$f = parent::getCMSFields();
		$mgr = new DataObjectManager(
			$this,
			'FileDownloadLoggers',
			'FileDownloadLogger',
			array('Created'=>'Time', 'Member.Name'=>'Member', 'Filename'=>'Filename')

		);
		$f->addFieldToTab('Root.Content.Log', $mgr);
		return $f;
	}

}