_form.php 4.04 KB
<?php
use mihaildev\ckeditor\CKEditor;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\TypesOfProducts;
/* @var $this yii\web\View */
/* @var $model backend\models\Articles */
/* @var $form yii\widgets\ActiveForm */
$this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/vendor/jquery.ui.widget.js');
$this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.iframe-transport.js');
$this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupload.js');
?>

<div class="articles-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => 250]) ?>

    <?=  $form->field($model, 'body')->widget(CKEditor::className(),[
        'editorOptions' => [
            'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать
            'inline' => false, //по умолчанию false
        ],
    ]); ?>


    <?= $form->field($model, 'type')->hiddenInput() ?>

    <fieldset>
        <legend>Миниатюра</legend>
        <?= $form->field(new \backend\models\UploadForm(), 'file')->fileInput(['id'=>'fileupload', 'data-url'=>"index.php?r=site/download-photo"]); ?>

        <?= $form->field($model, 'image')->hiddenInput(['id' => 'picture_link']) ?>

        <div id="img_block">
            <?= $model->image ? '<img src='.$model->image.'>': '' ?>
        </div>
    </fieldset>

    <fieldset>
        <legend>Галерея</legend>


        <?= $form->field(new \backend\models\UploadForm(), 'file_three')->fileInput(['id'=>'fileupload_3', 'data-url'=>"index.php?r=site/download-gallery", 'multiple'=> "multiple"]); ?>

        <?= $form->field($model, 'gallery')->hiddenInput(['id' => 'picture_link_3']) ?>

        <div id="img_block_3">
            <?php

            foreach($model->getGallery() as $image){
                echo $this->render('@app/views/site/_gallery_item', [ 'item' => ['image'=>$image]]);
            }
            ?>
        </div>


    </fieldset>


    <fieldset>
        <legend>Video</legend>
        <?= \backend\components\FieldEditor::widget(['template'=>'video','item_id'=> $model->id, 'model'=>'Articles']); ?>
    </fieldset>

    <?= $form->field($model, 'meta_title')->textInput(['maxlength' => 250]) ?>

    <?= $form->field($model, 'description')->textInput(['maxlength' => 250]) ?>

    <?= $form->field($model, 'h1')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'seo_text')->textarea(['rows' => 6]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
    $(function(){

        $('#fileupload').fileupload({
            dataType: 'json',
            done: function (e, data) {
                var host = window.location.host.toString();
                var img = '<img src="http://'+host+data.result.link+'">';
                var block = $('#img_block');
                block.find('img').remove();
                block.append(img);
                $('#picture_link').val(data.result.link);
            }
        });


        $('#fileupload_3').fileupload({
            dataType: 'json',
            done: function (e, data) {

                var img = data.result.view;
                var block = $('#img_block_3');
                block.append(img);
                var gallery = $('#picture_link_3');
                gallery.val(gallery.val()+data.result.link+',');
            }
        });
        $('body').on('click','.delete-gallery-item', function(){
            var url = $(this).data('url');
            $(this).parent('.gallery_image').remove();
            var gallery = $('#picture_link_3');
            var urls = gallery.val();
            gallery.val(urls.replace(url+',', ""));
        })

    })
</script>