silverstripe-frameworktest/code/TestPage.php

136 lines
3.4 KiB
PHP
Raw Normal View History

2008-10-03 00:05:45 +02:00
<?php
namespace SilverStripe\FrameworkTest\Model;
use Page;
use SiteTree;
use Page_Controller;
use FieldList;
use FormAction;
use Form;
use TextField;
use Email;
use SilverStripe\ORM\DataObject;
2008-10-03 00:05:45 +02:00
/**
* Parent class of all test pages
*/
2015-12-17 21:20:49 +01:00
class TestPage extends Page
{
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
/**
* We can only create subclasses of TestPage
*/
2016-04-18 23:27:29 +02:00
public function canCreate($member = null, $context = array())
2015-12-17 21:20:49 +01:00
{
// Don't allow creation other than through requireDefaultRecords
return false;
}
2008-10-03 00:05:45 +02:00
2015-12-17 21:20:49 +01:00
public function requireDefaultRecords()
{
if ($this->class == 'SilverStripe\\FrameworkTest\\Model\\TestPage') {
2015-12-17 21:20:49 +01:00
return;
}
2008-10-03 00:05:45 +02:00
2015-12-17 21:20:49 +01:00
$class = $this->class;
if (!DataObject::get_one($class)) {
// Try to create common parent
$parent = SiteTree::get()
->filter('URLSegment', 'feature-test-pages')
->First();
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
if (!$parent) {
$parent = new Page(array(
'Title' => 'Feature Test Pages',
'Content' => 'A collection of pages for testing various features in the SilverStripe CMS',
'ShowInMenus' => 0
));
$parent->write();
$parent->doPublish();
}
2015-12-17 21:20:49 +01:00
// Create actual page
$page = new $class();
$page->Title = str_replace("SilverStripe\\FrameworkTest\\Model\\TestPage", "", $class);
2015-12-17 21:20:49 +01:00
$page->ShowInMenus = 0;
if ($parent) {
$page->ParentID = $parent->ID;
}
$page->write();
$page->publish('Stage', 'Live');
}
}
2008-10-03 00:05:45 +02:00
}
/**
* Parent class of all test page controllers
*/
2015-12-17 21:20:49 +01:00
class TestPage_Controller extends Page_Controller
{
private static $allowed_actions = array(
'Form',
'save',
);
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
/**
* This form is exactly like the CMS form. It gives us an opportunity to test the fields outside of the CMS context
*/
public function Form()
{
$fields = $this->getCMSFields();
$actions = new FieldList(
new FormAction("save", "Save"),
$gohome = new FormAction("gohome", "Go home")
);
$gohome->setAttribute('src', 'frameworktest/images/test-button.png');
$form = new Form($this, "Form", $fields, $actions);
$form->loadDataFrom($this->dataRecord);
return $form;
}
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
public function save($data, $form)
{
$form->saveInto($this->dataRecord);
$this->dataRecord->write();
$this->redirectBack();
}
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
public function gohome()
{
$this->redirect("./");
}
2015-12-17 21:20:49 +01:00
public function EmailForm()
{
return new Form($this, "EmailForm", new FieldList(
new TextField("Email", "Email address")
), new FieldList(
new FormAction("sendEmail", "Send test email to this address")
));
}
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
public function email()
{
return array(
'Content' => '<p>Use this form to send a test email</p>',
'Form' => $this->EmailForm()
);
}
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
public function sendEmail($data, $form)
{
$email = new Email();
$email->setTo($data['Email']);
$email->setFrom($data['Email']);
$email->setSubject('A subject with some umlauts: öäüß');
$email->setBody('A body with some umlauts: öäüß');
$email->send();
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
echo "<p>email sent to " . $data['Email'] . "</p>";
}
2008-10-03 00:05:45 +02:00
}