Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » User/Member logging

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

Page: 1
Go to End
Author Topic: 1033 Views
  • david_nash
    Avatar
    Community Member
    55 Posts

    User/Member logging Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: User/Member logging Link to this post

    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();

  • david_nash
    Avatar
    Community Member
    55 Posts

    Re: User/Member logging Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: User/Member logging Link to this post

    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'

  • david_nash
    Avatar
    Community Member
    55 Posts

    Re: User/Member logging Link to this post

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

    }

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