2016-12-16 05:34:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests\FormFactoryTest;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
2022-10-17 06:58:20 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-12-16 05:34:21 +01:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Extension;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2022-10-17 06:58:20 +02:00
|
|
|
use SilverStripe\Forms\Form;
|
2016-12-16 05:34:21 +01:00
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
2018-02-19 23:37:27 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-03-21 04:22:23 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides versionable extensions to a controller / scaffolder
|
|
|
|
*/
|
|
|
|
class ControllerExtension extends Extension
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handlers for extra actions added by this extension
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = [
|
|
|
|
'publish',
|
|
|
|
'preview',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds additional form actions
|
|
|
|
*
|
|
|
|
* @param FieldList $actions
|
|
|
|
* @param Controller $controller
|
|
|
|
* @param string $name
|
|
|
|
* @param array $context
|
|
|
|
*/
|
|
|
|
public function updateFormActions(FieldList &$actions, Controller $controller, $name, $context = [])
|
|
|
|
{
|
|
|
|
// Add publish button if record is versioned
|
|
|
|
if (empty($context['Record'])) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-19 23:37:27 +01:00
|
|
|
/** @var Versioned|DataObject $record */
|
2016-12-16 05:34:21 +01:00
|
|
|
$record = $context['Record'];
|
2018-02-19 23:37:27 +01:00
|
|
|
if ($record->hasExtension(Versioned::class) && $record->hasStages()) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$actions->push(new FormAction('publish', 'Publish'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds extra fields to this form
|
|
|
|
*
|
|
|
|
* @param FieldList $fields
|
|
|
|
* @param Controller $controller
|
|
|
|
* @param string $name
|
|
|
|
* @param array $context
|
|
|
|
*/
|
|
|
|
public function updateFormFields(FieldList &$fields, Controller $controller, $name, $context = [])
|
|
|
|
{
|
|
|
|
// Add preview link
|
|
|
|
if (empty($context['Record'])) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-19 23:37:27 +01:00
|
|
|
/** @var Versioned|DataObject $record */
|
2016-12-16 05:34:21 +01:00
|
|
|
$record = $context['Record'];
|
2018-02-19 23:37:27 +01:00
|
|
|
if ($record->hasExtension(Versioned::class) && $record->hasStages()) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$link = $controller->Link('preview');
|
|
|
|
$fields->unshift(
|
|
|
|
new LiteralField(
|
|
|
|
"PreviewLink",
|
|
|
|
sprintf('<a href="%s" rel="external" target="_blank">Preview</a>', Convert::raw2att($link))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 06:58:20 +02:00
|
|
|
public function publish(array $data, Form $form): HTTPResponse
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
// noop
|
2022-10-17 06:58:20 +02:00
|
|
|
return HTTPResponse::create();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2022-10-17 06:58:20 +02:00
|
|
|
public function preview(HTTPRequest $request): HTTPResponse
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
// noop
|
2022-10-17 06:58:20 +02:00
|
|
|
return HTTPResponse::create();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
}
|