silverstripe-framework/forms/InlineFormAction.php

102 lines
2.4 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\FieldType\DBField;
/**
* Render a button that will submit the form its contained in through ajax.
2015-02-16 21:19:53 +01:00
* If you want to add custom behaviour, please set {@link includeDefaultJS()} to FALSE
2014-08-15 08:53:05 +02:00
*
* @see framework/client/dist/js/InlineFormAction.js
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage actions
*/
class InlineFormAction extends FormField {
2014-08-15 08:53:05 +02:00
protected $includeDefaultJS = true;
/**
* Create a new action button.
* @param action The method to call when the button is clicked
* @param title The label on the button
* @param extraClass A CSS class to apply to the button in addition to 'action'
*/
public function __construct($action, $title = "", $extraClass = '') {
$this->extraClass = ' '.$extraClass;
2015-10-28 04:44:14 +01:00
parent::__construct($action, $title);
}
2014-08-15 08:53:05 +02:00
public function performReadonlyTransformation() {
return $this->castedCopy('InlineFormAction_ReadOnly');
}
2014-08-15 08:53:05 +02:00
2015-06-20 12:11:08 +02:00
/**
* @param array $properties
* @return HTMLText
*/
public function Field($properties = array()) {
if($this->includeDefaultJS) {
Requirements::javascriptTemplate(FRAMEWORK_DIR . '/client/dist/js/InlineFormAction.js',
array('ID'=>$this->id()));
}
2014-08-15 08:53:05 +02:00
2015-06-20 12:11:08 +02:00
return DBField::create_field(
'HTMLText',
2015-10-28 04:44:14 +01:00
FormField::create_tag('input', array(
'type' => 'submit',
2015-06-20 12:11:08 +02:00
'name' => sprintf('action_%s', $this->getName()),
'value' => $this->title,
'id' => $this->ID(),
'class' => sprintf('action%s', $this->extraClass),
))
);
2014-08-15 08:53:05 +02:00
}
public function Title() {
2014-08-15 08:53:05 +02:00
return false;
}
2014-08-15 08:53:05 +02:00
/**
* Optionally disable the default javascript include (framework/client/dist/js/InlineFormAction.js),
* which routes to an "admin-custom"-URL.
2014-08-15 08:53:05 +02:00
*
* @param $bool boolean
*/
public function includeDefaultJS($bool) {
$this->includeDefaultJS = (bool)$bool;
}
}
/**
* Readonly version of {@link InlineFormAction}.
* @package forms
* @subpackage actions
*/
class InlineFormAction_ReadOnly extends FormField {
2014-08-15 08:53:05 +02:00
protected $readonly = true;
2014-08-15 08:53:05 +02:00
2015-06-20 12:11:08 +02:00
/**
* @param array $properties
* @return HTMLText
*/
public function Field($properties = array()) {
2015-06-20 12:11:08 +02:00
return DBField::create_field('HTMLText',
FormField::create_tag('input', array(
'type' => 'submit',
'name' => sprintf('action_%s', $this->name),
'value' => $this->title,
'id' => $this->id(),
'disabled' => 'disabled',
'class' => 'action disabled ' . $this->extraClass,
))
);
2014-08-15 08:53:05 +02:00
}
public function Title() {
2014-08-15 08:53:05 +02:00
return false;
}
}