index.php 2.67 KB
<?php
    
    /**
     * @var $sheet PHPExcel_Worksheet
     * @var $this  View
     */
    
    use kartik\file\FileInput;
    use yii\helpers\Html;
    use yii\web\View;
    use yii\widgets\ActiveForm;
    use yiister\gentelella\widgets\Panel;
    
    $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/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();
});

$(document).on('click', '#generate-button', function() {
    
    $('#progress-container').html('<h3>Process: </h3>' 
    + '<div class="progress">' 
    + '<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">' 
    + '0%' 
    + '</div>' 
    + '</div>');
    
    (function updateInfo(i, finish) {
    if (finish) {
        return;
    }
    $.ajax({
        type: "GET",
        url: "/admin/import/import?id=" + i
     
    }).done(function(data) {
        $('#progress-container .progress-bar').css('width', data.percent + '%');
        $('#progress-container .progress-bar').html(data.percent + '%');
        updateInfo(i + 1, data.finish);
    });
})(0, false);
  
});
JS;
    
    $this->registerJs($js, View::POS_READY);
    
    $this->params[ 'breadcrumbs' ][] = \Yii::t('catalog', 'Import');
?>

<?php $panel = Panel::begin() ?>

<?php $form = ActiveForm::begin(
    [
        'options' => [
            'enctype' => 'multipart/form-data',
        ],
        'id'      => 'my-form',
        'action'  => 'import/upload',
    ]
) ?>

<?php
    echo '<label class="control-label">' . \Yii::t('catalog', 'Upload Document') . '</label>';
    echo FileInput::widget(
        [
            'name' => 'attachment_3',
        ]
    );
?>

<?= Html::submitButton(
    \Yii::t('catalog', 'Send'),
    [
        'class' => 'btn btn-success',
    ]
) ?>

<?php
    echo Html::button(
        \Yii::t('catalog', 'Generate'),
        [
            'class' => 'btn btn-warning',
            'id'    => 'generate-button',
        ]
    );
?>

<div id="progress-container">

</div>

<?php $form::end() ?>

<?php $panel::end() ?>