i dont use form or userdefinedform from the cms.. i do write them by my own.
when i write a contact form, it will send emails AND save the query into DB as an dataobject. so later i can write a code that shows me all querys or such..
here example for one of my contact forms:
mysite/code/KontaktObjekt.php
<?php
class KontaktObjekt extends DataObject {
static $db = array(
'IhrName' => 'Text',
'Email' => 'Text',
'Message' => 'Text',
'Date' => 'Text',
);
}
?>
mysite/code/KontaktFormular.php
<?php
class KontaktFormular extends Page {
public static $db = array(
);
public static $has_one = array(
);
public static $defaults = array(
);
}
class KontaktFormular_Controller extends Page_Controller {
function init() {
parent::init();
Requirements::themedCSS("pms");
}
function SendenForm() {
$fieldset = new FieldSet(
new TextField(
$name = "IhrName",
$title = "Ihr Name",
$value = ""
),
new EmailField (
$name = "Email",
$title= "Ihre Emailadresse",
$value = "@"
),
new TextareaField(
$name = "Message",
$title = "Ihre Nachricht",
$rows = 10,
$cols = 40,
$value = "Nachricht eingeben"
),
new HiddenField (
$name = "Date",
$title= "Datum",
$value = time()
)
);
$actions = new FieldSet(
new FormAction('doSenden', 'Senden')
);
$validator = new RequiredFields('IhrName', 'Email', 'Message');
return new Form($this, 'SendenForm', $fieldset, $actions);
}
function doSenden($data, $form) {
$submission = new KontaktObjekt();
$form->saveInto($submission);
$submission->write();
$result = DB::query("SELECT * FROM KontaktObjekt ORDER BY ID DESC LIMIT 1");
foreach($result as $value) {
};
$extra = "From: ".$value['IhrName']." <".$value['Email'].">\n";
$extra .= "Content-Type: text/html\n";
$Datum = date("d.m.Y",$value['Date']);
$empfaenger = "YOUR@MAIL.COM";
$betreff = "Nachricht vom Kontaktformular";
$text = '
<br >
<table width="600" align="center" cellpadding="8">
<tr>
<td><h1><center>YOUR COMPANY</center></h1></td>
</tr>
<tr>
<td><strong>Von:</strong> '.$value['IhrName'].'</td>
</tr>
<tr>
<td><strong>Email:</strong> '.$value['Email'].'</td>
</tr>
<tr>
<td><strong>Nachricht:</strong><br >'.nl2br($value['Message']).'</td>
</tr>
<tr>
<td><strong>Datum:</strong> '.$value['Created'].'</td>
</tr>
</table>
';
mail($empfaenger, $betreff, $text, $extra);
// mail($value['Email'], $betreff, $text, $extra);
Director::redirectBack();
}
}
?>
themes/YOURTHEME/templates/Layout/KontaktFormular.ss
here just insert this Line anywhere you want:
$SendenForm
Okay, is not totaly silverstripe compliant, but it works fine. i am learning all the new cool stuff from 2.3.1, so soon i will make the code much more elegant.
here is short excurs:
When you define the DataObjekt, you can create a field in DB with some options to choose..
<?php
class Bewerberdata extends DataObject {
static $db = array(
'CanYourSpeakItaliano' => "Enum('No,Just a little bit,Yes,Mother-Tongue')"
);
}
?>
0==================================================0
<?php
class Bewerbermaske extends Page {
static $db = array(
);
static $has_one = array(
);
}
class Bewerbermaske_Controller extends Page_Controller {
function SendenForm() {
$fieldset = new FieldSet(
new DropdownField(
'CanYourSpeakItaliano',
'Can Your Speak Italiano?',
singleton('Bewerberdata')->dbObject('CanYourSpeakItaliano')->enumValues()
)
);
}
...
...
...
0==================================================0