Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Call functions/methods from template

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

Page: 1
Go to End
Author Topic: 1770 Views
  • Mackodlak
    Avatar
    Community Member
    95 Posts

    Call functions/methods from template Link to this post

    Hello, I need some help.

    The thing is, I need to be able to call functions (methods) delete and edit from template.
    I have made a new Dataobject called 'LOTD' (still being tested):

    <?php
    class LOTD extends DataObject implements PermissionProvider{
    static $db = array(
    'LName' => 'Text',
    'LOTD' => 'Text'
    );

    function providePermissions() {
    return array(
          "IMAOVLASTI" => "Unos, brisanje i editiranje LOTD",
    "ADDLOTD" => array(
    'name' => _t($this->class . '.ADDLOTD', ' Korisnik moze dodati LOTD'),
    'category' => _t($this->class . '.ADDLOTD_CATEGORY', 'Ovlasti za LOTD'),
    'help' => _t($this->class . 'ADDLOTD_HELP', 'Korisnik moze dodati Link Of The Day'),
    'sort' => 1
        )
          );

    }

       function canAddLOTD($member = null){
          if (($member == null)||($member == 0)) return false;
          return (Permission::checkMember($member, 'ADDLOTD'));
       }

    function deleteLOTD($SQL_id) {

          $lotd = DataObject::get_one('LOTD', "LName=$SQL_id",false,"");
          $lotd->delete();

          Director::redirectBack();

          return null;
    }

    }

    The function deleteLOTD (in this form) doesn't work... ignore most of the other things inside Dataobject LOTD, since they are under development. The idea is to have functions deleteLOTD and editLOTD inside 'LOTD' so I can use them with a push of a button from LOTDPage.ss template. This is part of LOTDPage.ss

    <div id="newLOTDForm">
       <% if canAddLOTD %>
          <h2>Unos novog LOTD-a</h2>
          $NewLOTDForm
       <% end_if %>
          
       <ul>
       <% control getLOTDs %>   
          <li>
             <a href="$LOTD">$LName</a>
             </br>
    <a href="$deleteLOTD('$LName')">Brisanje</a>
             
          </li>
       <% end_control %>
       </ul>
       </div>

    the <a href="$deleteLOTD($LName)"> ... </a> doesn't work, else I wouldn't be asking how to do this.

    getLOTDs function is inside LOTDPage.php and it looks like this:

    function getLOTDs(){
          return DataObject::get('LOTD');
       }

    also there is a form to enter new LOTD in database and for display that looks like this:

    function NewLOTDForm() {

    // Create fields
    $fields = new FieldSet(
    new TextField('LName'),
        new TextField('LOTD')
    );
    // Create actions
    $actions = new FieldSet(
    new FormAction('addNewLOTD', 'Unesi')
    );

       $validator = new RequiredFields('LName', 'LOTD');
    return new Form($this, 'NewLOTDForm', $fields, $actions, $validator);
    }

    function addNewLOTD($data, $form){
    $submission = new LOTD();
    $form->saveInto($submission);
    $submission->write();
    Director::redirectBack();
    }

    OK, so the idea is that when you click on "Brisanje" on the page that displays LOTDs (LOTD is actually Link Of The Day) it should call function deleteLOTD and delete it from database so it stops being displayed on the page and at the same time stops existing in LOTD table in database. Also if there is a kind heart out there that would like to help me with edit function aswell as with delete uinction

  • Mackodlak
    Avatar
    Community Member
    95 Posts

    Re: Call functions/methods from template Link to this post

    I tried some other approaches like:

    function deleteLOTD() {
          $SQL_id = $this->ID;

          $lotd = DataObject::get_by_id('LOTD', $SQL_id);
          $lotd->delete();

    }

    and so on, but I just can't seem to figure it out... this approach usually deletes all of my LOTDs, not only the 1 I wanna delete

  • Mackodlak
    Avatar
    Community Member
    95 Posts

    Re: Call functions/methods from template Link to this post

    OK, solved.
    I have found a way to call a function and pass parametars via URL like this:

    <% control getLOTDs %>   
          <li>
             <a href="$LOTD">$LName</a>
             </br>
             <% if Top.canDelLOTD %>
       <a href="lotd/dellotd/$ID">Brisanje</a>
             <% end_if %>
             
          </li>
    <% end_control %>

    so basically <a href="lotd/dellotd/$ID"> is calling a function dellotd and is passing $ID to it as parametar

    function dellotd ($ID) {
    ...
    }

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