Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Save author in CMS

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

Page: 1
Go to End
Author Topic: 1015 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    Save author in CMS Link to this post

    Hello guys,

    I´m working on an simple form with which users can save information. The form works pretty good, but I also want to save the name of the author (currentMember) from these entry.

    I´ve found some code that work with an onBeforeWrite-function which gets the author id. But I can´t figure out how I can use this to save the author name in cms.

    Here´s my code:

    --------------------Meldung.php--------------------

    class Meldung extends DataObject{
          static $db = array(
             'Datum' => 'Date',
             'Ausrichter' => 'Text',
             'Klasse' => 'Text',
             'gemeldet' => 'Boolean'
          );
          
          static $has_one = array(
             'Meldeliste' => 'Meldeliste',
             'Author' => 'Member'
          );
          
          function getCMSFields(){
             $fields = new FieldSet(
                new DateField('Datum', 'Datum'),
                new TextField('Ausrichter', 'Ausrichter'),
                new TextField('Klasse', 'Klasse'),
                new CheckboxField('gemeldet', 'gemeldet')
             );
             return $fields;
          }
          
          function onBeforeWrite(){
             if(!$this->ID){
                $currentMember = Member::currentMember();
                if($currentMember){
                   $this->AuthorID = $currentMember->ID;
                }
             }
             parent::onBeforeWrite();
          }
       }

    --------------------Meldeliste.php--------------------

    class Meldeliste extends Page{
          static $db = array();
          
          static $has_many = array(
             'Meldeliste' => 'Meldung'
          );
          
          function getCMSFields(){
             $fields = parent::getCMSFields();
             $meldeTabelle = new ComplexTableField(
                $this,
                'Meldeliste',
                'Meldung',
                null,
                null,
                "Meldung.MeldelisteID = {$this->ID}"
             );
             $fields -> addFieldtoTab('Root.Content.Meldeliste', $meldeTabelle);
             return $fields;
          }
       }
       
       class Meldeliste_Controller extends Page_Controller{
          function Form(){
             $fields = new FieldSet(
                new DateField('Datum', 'Datum'),
                new TextField('Ausrichter', 'Ausrichter'),
                new TextField('Klasse', 'Klasse')
             );
             
             $actions = new FieldSet(
                new FormAction('doSubmitEntry', 'Turnier melden')   
             );
             
             $validator = new RequiredFields(
                'Datum',
                'Ausrichter',
                'Klasse'   
             );
             
             $form = new Form(
                $this,
                'Form',
                $fields,
                $actions,
                $validator
             );
             return $form;
          }
          
          function doSubmitEntry($data, $form){
             $Meldung = new Meldung();
             $form->saveInto($Meldung);
             $Meldung->MeldelisteID=$this->dataRecord->ID;
             $Meldung->write();
             $form->sessionMessage('Turnier erfolgreich gemeldet', 'good');
             Director::redirectBack();
             return;
          }
       }

    Some words are German, I hope it doesn´t matter.

    Greetings,
    Sascha

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Save author in CMS Link to this post

    Hi Bereusei,

    Once you've saved the Member, that means you have access to the full member details - how exactly do you want to use it?
    Show it in the CMSFields?

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Save author in CMS Link to this post

    If you click in the CMS (in the Backend) on the tab "Meldeliste", you get an table with all entries from "Meldeliste". I want to see in this table the name of the author (currentMember).
    I also want to get the name of the author in an table at the frontend.

    Here the code from ss:
    <table border="0" cellpadding="0" cellspacing="10" width="100%">
                      <tr>
                         <td class="bold">Datum</td>
                         <td class="bold">Ausrichter</td>
                         <td class="bold">Klasse</td>
                         <td class="bold">Gemeldet</td>
                      </tr>
                      <% control Meldeliste %>
                      <tr id="Meldung{$ID}">
                         <td>$Datum.format(d.m.Y)</td>
                         <td>$Ausrichter</td>
                         <td>$Klasse</td>
                         <td>$gemeldet</td>
                      </tr>   
                      <% end_control %>
                   </table>

    I´ve added an image to show what I mean. Does anybody know why I only see the ID and not the other datas (Datum, Ausrichter, Klasse, etc)? If I click on the magnifying glass everything looks fine and I can see all informations.

    I hope it´s clear what I mean. Thanks for help.

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Save author in CMS Link to this post

    Yep, that is very clear, you need to specficy static $summary_fields, try reading up on this...

    http://doc.silverstripe.org/sapphire/en/reference/dataobject#summary-fields

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Save author in CMS Link to this post

    Thanks for the tip swaiba, you got me on the right way. I can work with it further.

    Just for info, I modify my code to this:
    <?php
       $fields = singleton('Meldung')->summaryFields();
       
       class Meldung extends DataObject{
          static $db = array(
             'Datum' => 'Date',
             'Ausrichter' => 'Text',
             'Klasse' => 'Text',
             'Author' => 'Text',
             'gemeldet' => 'Boolean'
          );
          
          static $has_one = array(
             'Meldeliste' => 'Meldeliste',
             'Author' => 'Member'
          );
          
          static $summary_fields = array(
          'Author.Name',
          'Datum' => 'Date',
             'Ausrichter' => 'Text',
             'Klasse' => 'Text'
          );
          
          static $default_sort = "Datum ASC";
          
          function getCMSFields_forPopup(){
             $fields = new FieldSet(
                new DateField('Datum', 'Datum'),
                new TextField('Ausrichter', 'Ausrichter'),
                new TextField('Klasse', 'Klasse'),
                new CheckboxField('gemeldet', 'gemeldet')
             );
             return $fields;
          }
          
          function onBeforeWrite(){
             if(!$this->ID){
                $currentMember = Member::currentMember();
                if($currentMember){
                   $this->AuthorID = $currentMember->ID;
                }
             }
             parent::onBeforeWrite();
          }
       }
    ?>

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