SlugifyDecorator.php
5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace artbox\core\helpers;
use yii\bootstrap\Html;
use yii\db\ActiveRecord;
use yii\helpers\Url;
use yii\widgets\ActiveField;
/**
* Class SlugifyDecorator
* Decorator to add slugify button to the ActiveField
*/
class SlugifyDecorator
{
/**
* ActiveField input template param
*/
const INPUT_FIELD = '{input}';
/**
* Decorate ActiveField input.
*
* @param \yii\widgets\ActiveField $field ActiveField to decorate
* @param array $url Url to get slug from
* @param \yii\widgets\ActiveField|null $attribute ActiveField to get value to slugify from
* @param bool $enableCancel Whether to generate Cancel button
* @param int|null $languageId
*
* @return \yii\widgets\ActiveField
*/
public static function decorate(
ActiveField $field,
array $url = [ '/alias/slugify' ],
ActiveField $attribute = null,
bool $enableCancel = false,
int $languageId = null
): ActiveField {
if (!$attribute) {
$attribute = $field;
}
$template = $field->template;
$input = self::INPUT_FIELD;
if (( $posBegin = strpos($template, $input) ) !== false) {
$posEnd = $posBegin + strlen($input);
$divBegin = Html::beginTag(
'div',
[
'class' => 'input-group',
]
);
$divEnd = Html::tag(
'span',
self::createButton(
$field,
$url,
$attribute,
$languageId
) . ( $enableCancel ? self::createCancel(
$field
) : '' ),
[
'class' => 'input-group-btn',
]
) . Html::endTag('div');
$template = substr_replace($template, $divEnd, $posEnd, 0);
$template = substr_replace($template, $divBegin, $posBegin, 0);
$field->template = $template;
}
return $field;
}
/**
* Create 'Slugify' button
*
* @param \yii\widgets\ActiveField $field
* @param array $url
* @param \yii\widgets\ActiveField $attribute
* @param int $languageId
*
* @return string
*/
private static function createButton(
ActiveField $field,
array $url,
ActiveField $attribute,
int $languageId
): string {
$options = [
'data' => [
'id' => Html::getInputId($field->model, $field->attribute),
'attribute-id' => Html::getInputId($attribute->model, $attribute->attribute),
'url' => Url::to($url),
'language-id' => $languageId,
],
'class' => 'btn btn-info slugify_button',
];
if ($field !== $attribute) {
$options[ 'data' ] = array_merge(
$options[ 'data' ],
[
'toggle' => 'tooltip',
'placement' => 'top',
'original-title' => \Yii::t(
'core',
'Create slug from {field} field',
[
'field' => $attribute->model->getAttributeLabel(
Html::getAttributeName($attribute->attribute)
),
]
),
]
);
}
return Html::button(
\Yii::t('core', 'Slugify'),
$options
);
}
/**
* Create 'Cancel' button
*
* @param \yii\widgets\ActiveField $field
*
* @return string
*/
private static function createCancel(
ActiveField $field
): string {
/**
* @var ActiveRecord $model
*/
$model = $field->model;
$options = [
'data' => [
'id' => Html::getInputId($model, $field->attribute),
'value' => $model->getAttribute(Html::getAttributeName($field->attribute)),
],
'class' => 'btn btn-default slugify_cancel_button',
];
return Html::button(
\Yii::t('core', 'Cancel'),
$options
);
}
}