silverstripe-frameworktest/code/FrameworktestRegressSessionAdmin.php

110 lines
3.2 KiB
PHP
Raw Normal View History

2010-05-03 22:43:40 +02:00
<?php
2016-07-20 00:34:52 +02:00
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Session;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\Form;
2016-07-20 00:34:52 +02:00
2010-05-03 22:43:40 +02:00
/**
* Starts a test session with various configurations set in session.
* These configurations are assumed to be evaluated in mysite/_config.php,
* with custom switches for the different options.
*/
2015-12-17 21:20:49 +01:00
class FrameworktestRegressSessionAdmin extends Controller
{
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
protected $template = 'BlankPage';
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
public function init()
{
parent::init();
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
if (!Permission::check('ADMIN')) {
return Security::permissionFailure($this);
}
}
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
public function Link($action = null)
{
return Controller::join_links('dev', 'regress', $action);
}
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
public function Form()
{
$isRunning = (Session::get('db'));
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
if ($isRunning) {
$actions = new FieldList(
new FormAction('endsession', 'End Session')
);
} else {
$actions = new FieldList(
new FormAction('startsession', 'Start Session')
);
}
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
$form = new Form(
$this,
'SilverStripe\\Forms\\Form',
2015-12-17 21:20:49 +01:00
new FieldList(
new HeaderField('Header1', ($isRunning) ? 'Session is already running' : 'Start new regress session'),
new LiteralField('Lit1',
'<p>Use this form to set configuration prior to starting a <a href="http://regress.silverstripe.com">regress.silverstripe.com</a> test session (manual testing).</p>'
),
$dbField = new DropdownField(
'db',
'Database',
array(
'mysql' => 'MySQL',
'postgresql' => 'Postgres',
'mssql' => 'MSSQL',
'sqlite3' => 'SQLite3',
),
Session::get('db')
),
$chkField = new CheckboxField(
'enabletranslatable',
'Translatable?',
Session::get('enabletranslatable')
)
),
$actions
);
$dbField->setHasEmptyDefault(false);
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
if ($isRunning) {
foreach ($form->Fields() as $field) {
$form->Fields()->replaceField($field->Name(),
$field->performReadonlyTransformation()
);
}
}
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
return $form;
}
2010-05-03 22:43:40 +02:00
2015-12-17 21:20:49 +01:00
public function startsession($data, $form)
{
Session::set('enabletranslatable', (isset($data['enabletranslatable'])) ? $data['enabletranslatable'] : null);
Session::set('db', $data['db']);
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
return $this->redirect('dev/build/?BackURL=admin');
}
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
public function endsession()
{
Session::set('enabletranslatable', null);
Session::set('db', null);
2016-07-20 00:34:52 +02:00
2015-12-17 21:20:49 +01:00
return $this->redirectBack();
}
}