2016-10-14 03:30:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests\GridField\GridField_URLHandlerTest;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2022-10-17 06:58:20 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
2017-03-02 03:24:38 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test URLHandler with a nested request handler
|
|
|
|
*/
|
|
|
|
class TestComponent extends RequestHandler implements GridField_URLHandler
|
|
|
|
{
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $allowed_actions = ['Form', 'showform', 'testpage', 'handleItem'];
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
/**
|
|
|
|
* @var GridField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $gridField;
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function getURLHandlers($gridField)
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
return [
|
2016-12-16 05:34:21 +01:00
|
|
|
'showform' => 'showform',
|
|
|
|
'testpage' => 'testpage',
|
|
|
|
'Form' => 'Form',
|
|
|
|
'item/$ID' => 'handleItem',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public function handleItem(GridField $gridField, HTTPRequest $request)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->setRequest($request);
|
2016-12-16 05:34:21 +01:00
|
|
|
$id = $request->param("ID");
|
|
|
|
return new TestComponent_ItemRequest(
|
|
|
|
$gridField,
|
|
|
|
$id,
|
|
|
|
Controller::join_links($gridField->Link(), 'item/' . $id)
|
|
|
|
);
|
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
public function Link($action = null)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
return $this->gridField->Link();
|
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public function showform(GridField $gridField, HTTPRequest $request)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->setRequest($request);
|
2016-12-16 05:34:21 +01:00
|
|
|
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form($gridField, $request)->forTemplate();
|
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return Form
|
2017-03-02 03:24:38 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public function Form(GridField $gridField, HTTPRequest $request)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->setRequest($request);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->gridField = $gridField;
|
|
|
|
return new Form(
|
|
|
|
$this,
|
|
|
|
'Form',
|
|
|
|
new FieldList(
|
|
|
|
new TextField("Test")
|
|
|
|
),
|
|
|
|
new FieldList(
|
|
|
|
new FormAction('doAction', 'Go')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2022-10-17 06:58:20 +02:00
|
|
|
public function doAction(array $data, Form $form): HTTPResponse
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-10-17 06:58:20 +02:00
|
|
|
return HTTPResponse::create()->setBody("Submitted " . $data['Test'] . " to component");
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public function testpage(GridField $gridField, HTTPRequest $request)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->setRequest($request);
|
2016-12-16 05:34:21 +01:00
|
|
|
return "Test page for component";
|
|
|
|
}
|
2016-10-14 03:30:05 +02:00
|
|
|
}
|