2021-02-10 00:04:59 +03:00
|
|
|
<?php
|
2021-08-07 00:38:53 +03:00
|
|
|
/**
|
|
|
|
* @link http://www.yiiframework.com/
|
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
|
|
* @license http://www.yiiframework.com/license/
|
|
|
|
*/
|
2021-02-10 00:04:59 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace yii\bootstrap5;
|
|
|
|
|
2022-01-05 11:17:55 +01:00
|
|
|
use Yii;
|
2021-08-04 08:03:41 +02:00
|
|
|
use yii\base\InvalidConfigException;
|
2021-02-10 00:04:59 +03:00
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modal renders a modal window that can be toggled by clicking on a button.
|
|
|
|
*
|
|
|
|
* The following example will show the content enclosed between the [[begin()]]
|
|
|
|
* and [[end()]] calls within the modal window:
|
|
|
|
*
|
|
|
|
* ~~~php
|
|
|
|
* Modal::begin([
|
|
|
|
* 'title' => 'Hello world',
|
|
|
|
* 'toggleButton' => ['label' => 'click me'],
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* echo 'Say hello...';
|
|
|
|
*
|
|
|
|
* Modal::end();
|
|
|
|
* ~~~
|
|
|
|
*
|
2021-08-05 09:21:05 +02:00
|
|
|
* @see https://getbootstrap.com/docs/5.1/components/modal/
|
2021-02-10 00:04:59 +03:00
|
|
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
|
* @author Simon Karlen <simi.albi@outlook.com>
|
|
|
|
*/
|
|
|
|
class Modal extends Widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The additional css class of extra large modal
|
|
|
|
* @since 2.0.3
|
|
|
|
*/
|
|
|
|
const SIZE_EXTRA_LARGE = 'modal-xl';
|
|
|
|
/**
|
|
|
|
* The additional css class of large modal
|
|
|
|
*/
|
|
|
|
const SIZE_LARGE = 'modal-lg';
|
|
|
|
/**
|
|
|
|
* The additional css class of small modal
|
|
|
|
*/
|
|
|
|
const SIZE_SMALL = 'modal-sm';
|
|
|
|
/**
|
|
|
|
* The additional css class of default modal
|
|
|
|
*/
|
|
|
|
const SIZE_DEFAULT = '';
|
|
|
|
|
|
|
|
/**
|
2021-08-04 09:57:30 +02:00
|
|
|
* @var string the title content in the modal window.
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $title;
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array additional title options
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $titleOptions = [];
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array additional header options
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $headerOptions = [];
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array body options
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $bodyOptions = [];
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
2021-07-12 17:22:29 +02:00
|
|
|
* @var string|null the footer content in the modal window.
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $footer;
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array additional footer options
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $footerOptions = [];
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
2021-07-12 17:22:29 +02:00
|
|
|
* @var string|null the modal size. Can be [[SIZE_LARGE]] or [[SIZE_SMALL]], or empty for default.
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $size;
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array|false the options for rendering the close button tag.
|
|
|
|
* The close button is displayed in the header of the modal window. Clicking
|
|
|
|
* on the button will hide the modal window. If this is false, no close button will be rendered.
|
|
|
|
*
|
|
|
|
* The following special options are supported:
|
|
|
|
*
|
|
|
|
* - tag: string, the tag name of the button. Defaults to 'button'.
|
|
|
|
*
|
|
|
|
* The rest of the options will be rendered as the HTML attributes of the button tag.
|
|
|
|
* Please refer to the [Modal plugin help](http://getbootstrap.com/javascript/#modals)
|
|
|
|
* for the supported HTML attributes.
|
|
|
|
*/
|
|
|
|
public $closeButton = [];
|
|
|
|
/**
|
|
|
|
* @var array|false the options for rendering the toggle button tag.
|
|
|
|
* The toggle button is used to toggle the visibility of the modal window.
|
|
|
|
* If this property is false, no toggle button will be rendered.
|
|
|
|
*
|
|
|
|
* The following special options are supported:
|
|
|
|
*
|
|
|
|
* - tag: string, the tag name of the button. Defaults to 'button'.
|
|
|
|
* - label: string, the label of the button. Defaults to 'Show'.
|
|
|
|
*
|
|
|
|
* The rest of the options will be rendered as the HTML attributes of the button tag.
|
|
|
|
* Please refer to the [Modal plugin help](http://getbootstrap.com/javascript/#modals)
|
|
|
|
* for the supported HTML attributes.
|
|
|
|
*/
|
|
|
|
public $toggleButton = false;
|
|
|
|
/**
|
|
|
|
* @var boolean whether to center the modal vertically
|
|
|
|
*
|
|
|
|
* When true the modal-dialog-centered class will be added to the modal-dialog
|
|
|
|
* @since 2.0.9
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $centerVertical = false;
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var boolean whether to make the modal body scrollable
|
|
|
|
*
|
|
|
|
* When true the modal-dialog-scrollable class will be added to the modal-dialog
|
|
|
|
* @since 2.0.9
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $scrollable = false;
|
2021-02-10 00:04:59 +03:00
|
|
|
/**
|
|
|
|
* @var array modal dialog options
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
|
* @since 2.0.9
|
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
public $dialogOptions = [];
|
2021-02-10 00:04:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-12 17:22:29 +02:00
|
|
|
* {@inheritDoc}
|
2021-08-04 08:03:41 +02:00
|
|
|
* @throws InvalidConfigException
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
$this->initOptions();
|
|
|
|
|
|
|
|
echo $this->renderToggleButton() . "\n";
|
|
|
|
echo Html::beginTag('div', $this->options) . "\n";
|
|
|
|
echo Html::beginTag('div', $this->dialogOptions) . "\n";
|
|
|
|
echo Html::beginTag('div', ['class' => 'modal-content']) . "\n";
|
|
|
|
echo $this->renderHeader() . "\n";
|
|
|
|
echo $this->renderBodyBegin() . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the widget.
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
echo "\n" . $this->renderBodyEnd();
|
|
|
|
echo "\n" . $this->renderFooter();
|
|
|
|
echo "\n" . Html::endTag('div'); // modal-content
|
|
|
|
echo "\n" . Html::endTag('div'); // modal-dialog
|
|
|
|
echo "\n" . Html::endTag('div');
|
|
|
|
|
|
|
|
$this->registerPlugin('modal');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the header HTML markup of the modal
|
|
|
|
* @return string the rendering result
|
|
|
|
*/
|
2021-07-12 17:22:29 +02:00
|
|
|
protected function renderHeader(): string
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
|
|
|
$button = $this->renderCloseButton();
|
2021-07-12 17:22:29 +02:00
|
|
|
if (isset($this->title)) {
|
2021-02-10 00:04:59 +03:00
|
|
|
Html::addCssClass($this->titleOptions, ['widget' => 'modal-title']);
|
|
|
|
$header = Html::tag('h5', $this->title, $this->titleOptions);
|
|
|
|
} else {
|
|
|
|
$header = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($button !== null) {
|
|
|
|
$header .= "\n" . $button;
|
|
|
|
} elseif ($header === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
Html::addCssClass($this->headerOptions, ['widget' => 'modal-header']);
|
2021-08-04 08:03:41 +02:00
|
|
|
|
2021-02-10 00:04:59 +03:00
|
|
|
return Html::tag('div', "\n" . $header . "\n", $this->headerOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the opening tag of the modal body.
|
|
|
|
* @return string the rendering result
|
|
|
|
*/
|
2021-07-12 17:22:29 +02:00
|
|
|
protected function renderBodyBegin(): string
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
|
|
|
Html::addCssClass($this->bodyOptions, ['widget' => 'modal-body']);
|
2021-08-04 08:03:41 +02:00
|
|
|
|
2021-02-10 00:04:59 +03:00
|
|
|
return Html::beginTag('div', $this->bodyOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the closing tag of the modal body.
|
|
|
|
* @return string the rendering result
|
|
|
|
*/
|
2021-07-12 17:22:29 +02:00
|
|
|
protected function renderBodyEnd(): string
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
|
|
|
return Html::endTag('div');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the HTML markup for the footer of the modal
|
2021-08-04 08:03:41 +02:00
|
|
|
* @return string|null the rendering result
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
protected function renderFooter()
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
2021-07-12 17:22:29 +02:00
|
|
|
if (isset($this->footer)) {
|
2021-02-10 00:04:59 +03:00
|
|
|
Html::addCssClass($this->footerOptions, ['widget' => 'modal-footer']);
|
2021-08-04 08:03:41 +02:00
|
|
|
|
2021-02-10 00:04:59 +03:00
|
|
|
return Html::tag('div', "\n" . $this->footer . "\n", $this->footerOptions);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the toggle button.
|
2021-08-04 08:03:41 +02:00
|
|
|
* @return string|null the rendering result
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
protected function renderToggleButton()
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
|
|
|
if (($toggleButton = $this->toggleButton) !== false) {
|
|
|
|
$tag = ArrayHelper::remove($toggleButton, 'tag', 'button');
|
|
|
|
$label = ArrayHelper::remove($toggleButton, 'label', 'Show');
|
|
|
|
|
|
|
|
return Html::tag($tag, $label, $toggleButton);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the close button.
|
2021-08-04 08:03:41 +02:00
|
|
|
* @return string|null the rendering result
|
2021-02-10 00:04:59 +03:00
|
|
|
*/
|
2021-08-04 08:03:41 +02:00
|
|
|
protected function renderCloseButton()
|
2021-02-10 00:04:59 +03:00
|
|
|
{
|
|
|
|
if (($closeButton = $this->closeButton) !== false) {
|
|
|
|
$tag = ArrayHelper::remove($closeButton, 'tag', 'button');
|
2021-08-18 15:41:26 +02:00
|
|
|
$label = ArrayHelper::remove($closeButton, 'label', '');
|
|
|
|
if ($tag === 'button' && !isset($closeButton['type'])) {
|
|
|
|
$closeButton['type'] = 'button';
|
|
|
|
}
|
2021-02-10 00:04:59 +03:00
|
|
|
|
2021-08-18 15:41:26 +02:00
|
|
|
return Html::tag($tag, $label, $closeButton);
|
2021-02-10 00:04:59 +03:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the widget options.
|
|
|
|
* This method sets the default values for various options.
|
|
|
|
*/
|
|
|
|
protected function initOptions()
|
|
|
|
{
|
|
|
|
$this->options = array_merge([
|
|
|
|
'tabindex' => -1,
|
|
|
|
'aria-hidden' => 'true',
|
|
|
|
], $this->options);
|
2021-07-12 17:22:29 +02:00
|
|
|
Html::addCssClass($this->options, ['widget' => 'modal fade']);
|
2021-02-10 00:04:59 +03:00
|
|
|
|
2021-07-12 17:22:29 +02:00
|
|
|
if (!empty($this->clientOptions)) {
|
2021-02-10 00:04:59 +03:00
|
|
|
$this->clientOptions = array_merge(['show' => false], $this->clientOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->titleOptions = array_merge([
|
|
|
|
'id' => $this->options['id'] . '-label',
|
|
|
|
], $this->titleOptions);
|
2022-01-03 09:40:20 +01:00
|
|
|
if (!isset($this->options['aria']['label'], $this->options['aria']['labelledby']) && isset($this->title)) {
|
|
|
|
$this->options['aria']['labelledby'] = $this->titleOptions['id'];
|
2021-02-10 00:04:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->closeButton !== false) {
|
|
|
|
$this->closeButton = array_merge([
|
2021-08-18 15:41:26 +02:00
|
|
|
'class' => ['widget' => 'btn-close'],
|
|
|
|
'data' => ['bs-dismiss' => 'modal'],
|
2022-01-05 11:17:55 +01:00
|
|
|
'aria' => ['label' => Yii::t('yii/bootstrap5', 'Close')]
|
2021-02-10 00:04:59 +03:00
|
|
|
], $this->closeButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->toggleButton !== false) {
|
|
|
|
$this->toggleButton = array_merge([
|
2022-01-03 09:40:20 +01:00
|
|
|
'data' => ['bs-toggle' => 'modal'],
|
2021-02-10 00:04:59 +03:00
|
|
|
'type' => 'button',
|
|
|
|
], $this->toggleButton);
|
2022-01-03 09:40:20 +01:00
|
|
|
if (!isset($this->toggleButton['data']['bs-target']) && !isset($this->toggleButton['href'])) {
|
|
|
|
$this->toggleButton['data']['bs-target'] = '#' . $this->options['id'];
|
2021-02-10 00:04:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Html::addCssClass($this->dialogOptions, ['widget' => 'modal-dialog']);
|
2021-07-12 17:22:29 +02:00
|
|
|
if (isset($this->size)) {
|
2021-02-10 00:04:59 +03:00
|
|
|
Html::addCssClass($this->dialogOptions, ['size' => $this->size]);
|
|
|
|
}
|
|
|
|
if ($this->centerVertical) {
|
|
|
|
Html::addCssClass($this->dialogOptions, ['align' => 'modal-dialog-centered']);
|
|
|
|
}
|
|
|
|
if ($this->scrollable) {
|
|
|
|
Html::addCssClass($this->dialogOptions, ['scroll' => 'modal-dialog-scrollable']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|