2008-10-03 00:05:45 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-01 04:37:50 +02:00
|
|
|
namespace SilverStripe\FrameworkTest\Model;
|
|
|
|
|
|
|
|
use Page;
|
2017-01-15 09:19:56 +01:00
|
|
|
use PageController;
|
2018-10-18 03:11:38 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleResource;
|
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader;
|
2016-07-01 04:37:50 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-16 06:38:31 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-09-07 07:10:55 +02:00
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
2022-10-19 01:00:04 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2019-01-18 01:56:58 +01:00
|
|
|
use SilverStripe\Security\DefaultAdminService;
|
2017-01-15 23:11:40 +01:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2016-07-01 04:37:50 +02:00
|
|
|
|
2008-10-03 00:05:45 +02:00
|
|
|
/**
|
|
|
|
* Parent class of all test pages
|
|
|
|
*/
|
2020-11-03 05:48:30 +01:00
|
|
|
class TestPage extends Page implements \TestPageInterface
|
2015-12-17 21:20:49 +01:00
|
|
|
{
|
2018-09-24 01:23:42 +02:00
|
|
|
private static $table_name = 'FrameworkTestPage';
|
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()
|
|
|
|
{
|
2024-06-05 06:30:08 +02:00
|
|
|
if (static::class === TestPage::class) {
|
2015-12-17 21:20:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-03 00:05:45 +02:00
|
|
|
|
2017-05-18 07:11:01 +02:00
|
|
|
if (!DataObject::get_one(static::class)) {
|
2015-12-17 21:20:49 +01:00
|
|
|
// Try to create common parent
|
2019-01-18 01:56:58 +01:00
|
|
|
$defaultAdminService = DefaultAdminService::singleton();
|
|
|
|
Member::actAs($defaultAdminService->findOrCreateDefaultAdmin(), function () {
|
2017-01-15 23:11:40 +01:00
|
|
|
// Create actual page
|
2017-05-18 07:11:01 +02:00
|
|
|
$page = new static();
|
2024-06-05 06:30:08 +02:00
|
|
|
$page->Title = str_replace(TestPage::class, "", static::class);
|
2017-01-15 23:11:40 +01:00
|
|
|
$page->ShowInMenus = 0;
|
2019-01-18 01:56:58 +01:00
|
|
|
$parent = static::getOrCreateParentPage();
|
|
|
|
$page->ParentID = $parent->ID;
|
2017-01-15 23:11:40 +01:00
|
|
|
$page->write();
|
2022-08-30 03:18:41 +02:00
|
|
|
$page->copyVersionToStage('Stage', 'Live');
|
2017-01-15 23:11:40 +01:00
|
|
|
});
|
2015-12-17 21:20:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 01:56:58 +01:00
|
|
|
|
|
|
|
public static function getOrCreateParentPage()
|
|
|
|
{
|
|
|
|
$defaultAdminService = DefaultAdminService::singleton();
|
|
|
|
Member::actAs($defaultAdminService->findOrCreateDefaultAdmin(), function () {
|
|
|
|
$parent = SiteTree::get()
|
|
|
|
->filter('URLSegment', 'feature-test-pages')
|
|
|
|
->First();
|
|
|
|
|
|
|
|
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();
|
2022-08-30 03:18:41 +02:00
|
|
|
$parent->publishRecursive();
|
2019-01-18 01:56:58 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return SiteTree::get()
|
|
|
|
->filter('URLSegment', 'feature-test-pages')
|
|
|
|
->First();
|
|
|
|
}
|
2008-10-03 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parent class of all test page controllers
|
|
|
|
*/
|
2017-01-15 09:19:56 +01:00
|
|
|
class TestPage_Controller extends PageController
|
2015-12-17 21:20:49 +01:00
|
|
|
{
|
|
|
|
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")
|
|
|
|
);
|
2018-10-18 03:11:38 +02:00
|
|
|
$gohome->setAttribute(
|
|
|
|
'src',
|
|
|
|
ModuleResourceLoader::resourceURL('silverstripe/frameworktest: images/test-button.png')
|
|
|
|
);
|
2015-12-17 21:20:49 +01:00
|
|
|
$form = new Form($this, "Form", $fields, $actions);
|
|
|
|
$form->loadDataFrom($this->dataRecord);
|
|
|
|
return $form;
|
|
|
|
}
|
2016-04-18 23:27:29 +02:00
|
|
|
|
2022-10-19 01:00:04 +02:00
|
|
|
public function save(array $data, Form $form): HTTPResponse
|
2015-12-17 21:20:49 +01:00
|
|
|
{
|
|
|
|
$form->saveInto($this->dataRecord);
|
|
|
|
$this->dataRecord->write();
|
2022-10-19 01:00:04 +02:00
|
|
|
return $this->redirectBack();
|
2015-12-17 21:20:49 +01:00
|
|
|
}
|
2016-04-18 23:27:29 +02:00
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
public function gohome()
|
|
|
|
{
|
|
|
|
$this->redirect("./");
|
|
|
|
}
|
2009-08-25 07:49:13 +02:00
|
|
|
|
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
|
|
|
|
2022-10-19 01:00:04 +02:00
|
|
|
public function sendEmail(array $data, Form $form): HTTPResponse
|
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2022-10-19 01:00:04 +02:00
|
|
|
return HTTPResponse::create()->setBody("<p>email sent to " . $data['Email'] . "</p>");
|
2015-12-17 21:20:49 +01:00
|
|
|
}
|
2008-10-03 00:05:45 +02:00
|
|
|
}
|