Source of file TestPage.php
Size: 4,548 Bytes - Last Modified: 2021-12-23T10:31:28+00:00
/var/www/docs.ssmods.com/process/src/code/TestPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | <?php namespace SilverStripe\FrameworkTest\Model; use Page; use PageController; use SilverStripe\Core\Manifest\ModuleResource; use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\ORM\DataObject; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\TextField; use SilverStripe\Control\Email\Email; use SilverStripe\Security\DefaultAdminService; use SilverStripe\Security\Member; use SilverStripe\Security\Security; /** * Parent class of all test pages */ class TestPage extends Page implements \TestPageInterface { private static $table_name = 'FrameworkTestPage'; /** * We can only create subclasses of TestPage */ public function canCreate($member = null, $context = array()) { // Don't allow creation other than through requireDefaultRecords return false; } public function requireDefaultRecords() { if (static::class === self::class) { return; } if (!DataObject::get_one(static::class)) { // Try to create common parent $defaultAdminService = DefaultAdminService::singleton(); Member::actAs($defaultAdminService->findOrCreateDefaultAdmin(), function () { // Create actual page $page = new static(); $page->Title = str_replace(self::class, "", static::class); $page->ShowInMenus = 0; $parent = static::getOrCreateParentPage(); $page->ParentID = $parent->ID; $page->write(); $page->publish('Stage', 'Live'); }); } } 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(); $parent->doPublish(); } }); return SiteTree::get() ->filter('URLSegment', 'feature-test-pages') ->First(); } } /** * Parent class of all test page controllers */ class TestPage_Controller extends PageController { private static $allowed_actions = array( 'Form', 'save', ); /** * 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', ModuleResourceLoader::resourceURL('silverstripe/frameworktest: images/test-button.png') ); $form = new Form($this, "Form", $fields, $actions); $form->loadDataFrom($this->dataRecord); return $form; } public function save($data, $form) { $form->saveInto($this->dataRecord); $this->dataRecord->write(); $this->redirectBack(); } public function gohome() { $this->redirect("./"); } 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") )); } public function email() { return array( 'Content' => '<p>Use this form to send a test email</p>', 'Form' => $this->EmailForm() ); } 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(); echo "<p>email sent to " . $data['Email'] . "</p>"; } } |