* @copyright (c), Thread
*/
class ActiveField extends \yii\bootstrap\ActiveField
{
/**
* @param $sign
* @return $this
*/
public function sign($sign)
{
$this->inputTemplate = '
' . $sign . '{input}
';
return $this;
}
/**
* @return $this|ActiveField
*/
public function sign_lang()
{
return $this->sign(\Yii::$app->language);
}
/**
* @param $placeholder
* @return $this
*/
public function placeholder($placeholder)
{
$this->inputOptions['placeholder'] = $placeholder;
return $this->textInput($this->inputOptions);
}
/**
* @param $value
* @return $this
*/
public function value($value)
{
$this->inputOptions['value'] = $value;
return $this->textInput($this->inputOptions);
}
/**
* @param string $thema
* @return $this
* @throws \Exception
*/
public function editor($thema = 'full')
{
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$this->parts['{input}'] = Editor::widget([
'model' => $this->model,
'attribute' => $this->attribute,
'thema' => $thema
]);
return $this;
}
/**
* @param $value
* @param string $format
* @return $this
* @throws \Exception
*/
public function datePicker($value, $format = 'dd.mm.yyyy')
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$this->parts['{input}'] = DatePicker::widget([
'model' => $this->model,
'attribute' => $this->attribute,
'options' => [
'placeholder' => $format,
'value' => $value,
],
'pluginOptions' => [
'autoclose' => true,
'format' => $format
]
]);
return $this;
}
/**
* @param $value
* @param string $format
* @return $this
* @throws \Exception
*/
public function dateTimePicker($value, $format = 'dd.mm.yyyy H:i')
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$this->parts['{input}'] = DateTimePicker::widget([
'model' => $this->model,
'attribute' => $this->attribute,
'options' => [
'placeholder' => $format,
'value' => $value,
],
'pluginOptions' => [
'autoclose' => true,
'format' => $format,
'todayHighlight' => true
]
]);
return $this;
}
/**
* @param string $preview
* @return $this
* @throws \Exception
*/
public function imageOne($preview = '')
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$name = uniqid();
$imagePreview = '';
if (!empty($preview)) {
$imagePreview = Html::img($preview, [
'class' => 'file-preview-image',
'style' => ['max-height' => '200px', 'max-width' => '200px']
]);
}
$inputName = Html::getInputName($this->model, $this->attribute);
$this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute);
$this->parts['{input}'] .= FileInput::widget([
'language' => Yii::$app->params['themes']['language'],
'name' => $name,
'options' => [
'class' => 'file-loading',
'accept' => 'image/*'
],
'pluginOptions' => [
'uploadUrl' => Url::toRoute(['fileupload', 'input_file_name' => $name, 'model_id' => $this->model->id]),
'uploadExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'browseClass' => 'btn btn-success',
'uploadClass' => 'btn btn-info',
'removeClass' => 'btn btn-danger',
'uploadAsync' => true,
'showUpload' => true,
'showRemove' => true,
'overwriteInitial' => true,
'initialPreview' => $imagePreview,
'browseOnZoneClick' => true,
'maxFileSize' => 2048
],
'pluginEvents' => [
'filebatchselected' => 'function(event, files) {
}',
'filebatchuploadsuccess' => 'function(event, data, previewId, index){
var response = data.response;
$("input[name=\'' . $inputName . '\']").val(response.name);
}',
'fileuploaded' => 'function(event, data, previewId, index){
var response = data.response;
$("input[name=\'' . $inputName . '\']").val(response.name);
}',
'fileclear' => 'function(event){
$("input[name=\'' . $inputName . '\']").val("");
}',
]
]);
return $this;
}
/**
* @return $this
* @throws \Exception
*/
public function colorPicker()
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$this->parts['{input}'] = ColorInput::widget([
'model' => $this->model,
'attribute' => $this->attribute,
'options' => [
'placeholder' => Yii::t('app', 'Choose your color'),
],
]);
return $this;
}
/**
* @param array $data
* @return $this
* @throws \Exception
*/
public function selectOne(array $data)
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$this->parts['{input}'] = Select2::widget([
'model' => $this->model,
'attribute' => $this->attribute,
'data' => $data,
'options' => [
'placeholder' => Yii::t('app', 'Choose'),
],
]);
return $this;
}
/**
* @param array $options
* @param array $pluginOptions
* @return $this
* @throws \Exception
*/
public function imageSeveral($options = [], $pluginOptions = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$name = uniqid();
$initImageConfig = [];
$initImage = [];
if (isset($options['initialPreview']) && !empty($options['initialPreview'])) {
foreach ($options['initialPreview'] as $image) {
$initImage[] = Html::img($image, [
'class' => 'file-preview-image',
'style' => ['max-height' => '150px']
]);
$initImageConfig[] = "{
key: '" . basename($image) . "',
url: '" . Url::toRoute(['filedelete', 'id' => $this->model->id]) . "'
}";
}
}
$inputName = Html::getInputName($this->model, $this->attribute);
$this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute);
$this->parts['{input}'] .= FileInput::widget([
'name' => $name,
'options' => [
'multiple' => true,
'class' => 'file-loading',
'accept' => 'image/*'
],
'pluginOptions' => ArrayHelper::merge([
'uploadUrl' => Url::toRoute(['fileupload', 'input_file_name' => $name]),
'uploadExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'deleteExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'uploadAsync' => true,
'showUpload' => true,
'showRemove' => false,
'overwriteInitial' => false,
'initialPreview' => $initImage,
'initialPreviewConfig' => new JsExpression('[' . implode(',', $initImageConfig) . ']'),
'minFileCount' => $options['minFileCount'] ?? 1,
'maxFileCount' => $options['maxFileCount'] ?? 10,
// 'maxFileSize' => '134217728999'
], $pluginOptions),
'pluginEvents' => [
'filebatchselected' => 'function(event, files) {
}',
'filebatchuploadsuccess' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'fileuploaded' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'filedeleted' => 'function(event, key){
console.log(key);
}',
'fileclear' => 'function(event){
$("input[name=\'' . $inputName . '\']").val("");
}',
]
]);
return $this;
}
/**
* Image upload widget field
*
* @param array $options
* @param array $pluginOptions
* @return $this
* @internal param string $preview image url
*/
public function fileSeveral($options = [], $pluginOptions = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$name = uniqid();
$initImageConfig = [];
$initImage = [];
if (isset($options['initialPreview']) && !empty($options['initialPreview'])) {
foreach ($options['initialPreview'] as $image) {
$initImage[] = Html::img($image, [
'class' => 'file-preview-image',
'style' => ['max-height' => '150px']
]);
$initImageConfig[] = "{
key: '" . basename($image) . "',
url: '" . Url::toRoute(['filedelete', 'id' => $this->model->id]) . "'
}";
}
}
$inputName = Html::getInputName($this->model, $this->attribute);
$this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute);
$this->parts['{input}'] .= FileInput::widget([
'name' => $name,
'options' => [
'multiple' => true,
'class' => 'file-loading',
// 'accept' => 'image/*'
],
'pluginOptions' => ArrayHelper::merge([
'uploadUrl' => Url::toRoute(['fileupload', 'input_file_name' => $name]),
'uploadExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'deleteExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'uploadAsync' => true,
'showUpload' => true,
'showRemove' => false,
'overwriteInitial' => false,
'initialPreview' => $initImage,
'initialPreviewConfig' => new JsExpression('[' . implode(',', $initImageConfig) . ']'),
'minFileCount' => $options['minFileCount'] ?? 1,
'maxFileCount' => $options['maxFileCount'] ?? 10,
// 'maxFileSize' => '134217728999'
], $pluginOptions),
'pluginEvents' => [
'filebatchselected' => 'function(event, files) {
}',
'filebatchuploadsuccess' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'fileuploaded' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'filedeleted' => 'function(event, key){
console.log(key);
}',
'fileclear' => 'function(event){
$("input[name=\'' . $inputName . '\']").val("");
}',
]
]);
return $this;
}
/**
* Image upload widget field
*
* @param array $options
* @param array $pluginOptions
* @return $this
* @internal param string $preview image url
*/
public function imageGallery($options = [], $pluginOptions = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
$name = uniqid();
$initImageConfig = [];
$initImage = [];
if (isset($options['initialPreview']) && !empty($options['initialPreview'])) {
foreach ($options['initialPreview'] as $image) {
$initImage[] = Html::img($image, [
'class' => 'file-preview-image',
'style' => ['max-height' => '150px']
]);
$initImageConfig[] = "{
key: '" . basename($image) . "',
url: '" . Url::toRoute(['filedelete', 'id' => $this->model->id]) . "'
}";
}
}
$inputName = Html::getInputName($this->model, $this->attribute);
$this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute);
$this->parts['{input}'] .= FileInput::widget([
'name' => $name,
'options' => [
'multiple' => true,
'class' => 'file-loading',
'accept' => 'image/*'
],
'pluginOptions' => ArrayHelper::merge([
'uploadUrl' => Url::toRoute(['fileupload', 'input_file_name' => $name]),
'uploadExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'deleteExtraData' => [
'_csrf' => Yii::$app->getRequest()->getCsrfToken(),
],
'uploadAsync' => true,
'showUpload' => true,
'showRemove' => false,
'overwriteInitial' => false,
'initialPreview' => $initImage,
'minFileCount' => $options['minFileCount'] ?? 1,
'maxFileCount' => $options['maxFileCount'] ?? 10,
// 'maxFileSize' => '134217728999'
], $pluginOptions),
'pluginEvents' => [
'filebatchselected' => 'function(event, files) {
}',
'filebatchuploadsuccess' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'fileuploaded' => 'function(event, data, previewId, index){
var response = data.response;
var val = $("input[name=\'' . $inputName . '\']").val();
var aval = val.split(\',\');
aval.push(response.name);
console.log(val, aval);
$("input[name=\'' . $inputName . '\']").val(aval.join(\',\'));
}',
'filedeleted' => 'function(event, key){
console.log(key);
}',
'fileclear' => 'function(event){
$("input[name=\'' . $inputName . '\']").val("");
}',
]
]);
return $this;
}
}