2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-07-12 08:51:08 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-06-26 15:03:11 +02:00
|
|
|
* The action buttons are <input type="submit"> as well as <button> tags.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* Upon clicking the button below will redirect the user to doAction under the current controller.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new FormAction (
|
|
|
|
* // doAction has to be a defined controller member
|
|
|
|
* $action = "doAction",
|
|
|
|
* $title = "Submit button"
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage actions
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class FormAction extends FormField {
|
2008-08-11 02:14:48 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $casting = [
|
|
|
|
'ButtonContent' => 'HTMLFragment',
|
|
|
|
];
|
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
/**
|
|
|
|
* Action name, normally prefixed with 'action_'
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-08-06 08:54:59 +02:00
|
|
|
protected $action;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-07 00:39:44 +02:00
|
|
|
/**
|
|
|
|
* Identifier of icon, if supported on the frontend
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $icon = null;
|
|
|
|
|
|
|
|
protected $schemaComponent = 'FormAction';
|
|
|
|
|
2008-08-11 02:14:48 +02:00
|
|
|
/**
|
|
|
|
* Enables the use of <button> instead of <input>
|
|
|
|
* in {@link Field()} - for more customizeable styling.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-02-10 06:01:19 +01:00
|
|
|
* @var boolean
|
2008-08-11 02:14:48 +02:00
|
|
|
*/
|
|
|
|
public $useButtonTag = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
/**
|
|
|
|
* Literal button content, used when useButtonTag is true.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-02-16 19:54:09 +01:00
|
|
|
protected $buttonContent = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-22 04:21:43 +02:00
|
|
|
/**
|
|
|
|
* Should validation be skipped when performing this action?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $validationExempt = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new action button.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2015-02-10 06:01:19 +01:00
|
|
|
* @param string $action The method to call when the button is clicked
|
|
|
|
* @param string $title The label on the button. This should be plain text, not escaped as HTML.
|
|
|
|
* @param Form form The parent form, auto-set when the field is placed inside a form
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($action, $title = "", $form = null) {
|
2008-08-06 08:54:59 +02:00
|
|
|
$this->action = "action_$action";
|
2015-02-10 06:01:19 +01:00
|
|
|
$this->setForm($form);
|
2015-06-20 12:11:08 +02:00
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
parent::__construct($this->action, $title);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-07-07 00:39:44 +02:00
|
|
|
/**
|
|
|
|
* Add extra options to data
|
|
|
|
*/
|
|
|
|
public function getSchemaDataDefaults() {
|
|
|
|
$defaults = parent::getSchemaDataDefaults();
|
|
|
|
$defaults['attributes']['type'] = $this->getUseButtonTag() ? 'button' : 'submit';
|
|
|
|
$defaults['data']['icon'] = $this->getIcon();
|
|
|
|
return $defaults;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get button icon, if supported
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIcon() {
|
|
|
|
return $this->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets button icon
|
|
|
|
*
|
|
|
|
* @param string $icon Icon identifier (not path)
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setIcon($icon) {
|
|
|
|
$this->icon = $icon;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
/**
|
|
|
|
* Get the action name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function actionName() {
|
2011-03-23 05:12:25 +01:00
|
|
|
return substr($this->name, 7);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 08:54:59 +02:00
|
|
|
/**
|
|
|
|
* Set the full action name, including action_
|
|
|
|
* This provides an opportunity to replace it with something else
|
2015-02-10 06:01:19 +01:00
|
|
|
*
|
|
|
|
* @param string $fullAction
|
|
|
|
* @return $this
|
2008-08-06 08:54:59 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFullAction($fullAction) {
|
2008-08-06 08:54:59 +02:00
|
|
|
$this->action = $fullAction;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-08-06 08:54:59 +02:00
|
|
|
}
|
2007-10-18 03:44:37 +02:00
|
|
|
|
2015-06-20 12:11:08 +02:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2015-06-20 12:11:08 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2011-03-23 05:12:25 +01:00
|
|
|
$properties = array_merge(
|
|
|
|
$properties,
|
|
|
|
array(
|
|
|
|
'Name' => $this->action,
|
2012-02-16 19:54:09 +01:00
|
|
|
'Title' => ($this->description && !$this->useButtonTag) ? $this->description : $this->Title(),
|
2011-03-23 05:12:25 +01:00
|
|
|
'UseButtonTag' => $this->useButtonTag
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return parent::Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 12:11:08 +02:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
2016-07-12 08:51:08 +02:00
|
|
|
* @return DBHTMLText
|
2015-06-20 12:11:08 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2012-04-11 08:07:55 +02:00
|
|
|
return $this->Field($properties);
|
2012-03-07 22:02:57 +01:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'action';
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttributes() {
|
2012-06-13 09:57:54 +02:00
|
|
|
$type = (isset($this->attributes['src'])) ? 'image' : 'submit';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
|
|
|
'disabled' => ($this->isReadonly() || $this->isDisabled()),
|
|
|
|
'value' => $this->Title(),
|
2012-06-13 09:57:54 +02:00
|
|
|
'type' => $type,
|
2012-02-16 19:54:09 +01:00
|
|
|
'title' => ($this->useButtonTag) ? $this->description : null,
|
2011-12-22 13:10:57 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-16 19:54:09 +01:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Add content inside a button field. This should be pre-escaped raw HTML and should be used sparingly.
|
2015-02-10 06:01:19 +01:00
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @return $this
|
2012-02-16 19:54:09 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setButtonContent($content) {
|
2012-02-16 19:54:09 +01:00
|
|
|
$this->buttonContent = (string) $content;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Gets the content inside the button field. This is raw HTML, and should be used sparingly.
|
2015-02-10 06:01:19 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2012-02-16 19:54:09 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getButtonContent() {
|
2012-02-16 19:54:09 +01:00
|
|
|
return $this->buttonContent;
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:24:35 +01:00
|
|
|
/**
|
2015-02-10 06:01:19 +01:00
|
|
|
* Enable or disable the rendering of this action as a <button />
|
|
|
|
*
|
|
|
|
* @param boolean
|
|
|
|
* @return $this
|
2012-02-17 00:24:35 +01:00
|
|
|
*/
|
|
|
|
public function setUseButtonTag($bool) {
|
|
|
|
$this->useButtonTag = $bool;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-10 06:01:19 +01:00
|
|
|
* Determine if this action is rendered as a <button />
|
|
|
|
*
|
|
|
|
* @return boolean
|
2012-02-17 00:24:35 +01:00
|
|
|
*/
|
|
|
|
public function getUseButtonTag() {
|
|
|
|
return $this->useButtonTag;
|
|
|
|
}
|
|
|
|
|
2016-04-22 04:21:43 +02:00
|
|
|
/**
|
|
|
|
* Set whether this action can be performed without validating the data
|
|
|
|
*
|
|
|
|
* @param bool $exempt
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setValidationExempt($exempt = true) {
|
|
|
|
$this->validationExempt = $exempt;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get whether this action can be performed without vaidating the data
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getValidationExempt() {
|
|
|
|
return $this->validationExempt;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Does not transform to readonly by purpose.
|
|
|
|
* Globally disabled buttons would break the CMS.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|