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.

Template Questions /

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

Call functions/methods from template


Go to End


3 Posts   5375 Views

Avatar
Mackodlak

Community Member, 95 Posts

18 May 2011 at 2:04am

Edited: 18/05/2011 2:05am

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

Avatar
Mackodlak

Community Member, 95 Posts

18 May 2011 at 6:25pm

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

Avatar
Mackodlak

Community Member, 95 Posts

18 May 2011 at 10:52pm

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) {
...
}