index.php 1.54 KB
<?php
    
    use kartik\file\FileInput;
    use yii\web\View;
    use yii\widgets\ActiveForm;
    use yiister\gentelella\widgets\Panel;
    
    /**
     * @var View $this
     */
    
    $js = <<< JS
$(document).on('submit', '#my-form', function(e) {
  var formData = new FormData(this);
  var form = $(this);
  
  $(document.body).append('<div class="animated yt-loader"></div>');
  
  $.ajax({
      url: "/admin/price-import/upload",
      data: formData,
      type: "POST",
      success: function(data) {
        console.log(data);
        $('.yt-loader').remove();
        new PNotify({
                        title: "Success",
                        text: "File updated",
                        type: "success",
                        styling: "bootstrap3",
                        icon: "glyphicon glyphicon-exclamation-sign"
                      });
      },
      cache: false,
      contentType: false,
      processData: false
  });
  
  e.preventDefault();
});

JS;
    
    $this->registerJs($js, View::POS_READY);
    
    $this->params[ 'breadcrumbs' ][] = \Yii::t('catalog', 'Import');
    
    $panel = Panel::begin();
    
    $form = ActiveForm::begin(
    [
        'options' => [
            'enctype' => 'multipart/form-data',
        ],
        'id'      => 'my-form',
        'action'  => 'price-import',
    ]
);
    echo '<label class="control-label">' . \Yii::t('catalog', 'Upload Document') . '</label>';
    echo FileInput::widget(
        [
            'name' => 'attachment_3',
        ]
    );
    
 $form::end();
 
 $panel::end();