public function rules()
{
return [
[['iid', 'itype', 'name'], 'required'],
[['iid'], 'integer'],
[['itype'], 'string'],
[['file'], 'file'],
[['name'], 'string', 'max' => 150],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'iid' => 'Счет',
'itype' => 'Вид файла',
'file' => 'Ссылка на файл',
'name' => 'Заголовок',
];
}
public function beforeSave($insert) {
if ($insert) {
$this->itype='invoice';
}
return parent::beforeSave($insert);
}
<?php
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\select2\Select2;
use kartik\file\FileInput;
/* @var $this yii\web\View */
/* @var $model common\models\InvoicesFiles */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="box box-primary">
<div class="box-body">
<?php $form = ActiveForm::begin(['id'=>'form_invoice_file','type' => ActiveForm::TYPE_INLINE ,'options' => ['enctype' => 'multipart/form-data']]);?>
<?=$form->errorSummary($model);?>
<div class='row'>
<div class='col-md-6'>
<?=$form->field($model, 'iid')->widget(Select2::classname(), [
'data' => Yii::$app->MyFunctions->InvoicesArray(),
'options' => ['placeholder' => 'Выберите счет','id'=>'invoices-selector'],
'pluginOptions' => [
'allowClear' => true,
]
]);?>
</div>
<div class='col-md-6'>
<?= $form->field($model, 'name')->textInput(['maxlength' => true,'style'=>'width:100%']) ?>
</div>
</div>
<div class='row'>
<div class='col-md-6'>
<?=$form->field($model, 'itype')->widget(Select2::classname(), [
'data' => $model->typesArray,
'language' => 'ru',
'options' => ['placeholder' => 'Вид файла','id'=>'filetype-selector'],
'pluginOptions' => [
'allowClear' => true,
'width' => '100%'
]
]);?>
</div><?=$model->file;?>
<div class='col-md-6'>
<?php echo $form->field($model, 'file')->fileInput(); ?>
<?php $form->field($model, 'file')->widget(FileInput::classname(), [
'showMessage' => true,
'pluginOptions' => [
'showPreview' => false,
'showCaption' => true,
'showRemove' => true,
'showUpload' => false,
'maxFileSize'=>30720,
]]
);?>
</div>
</div>
<div class="box-footer">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary btn-flat']) ?>
</div>
<!-- /.box-footer-->
<?php ActiveForm::end(); ?>
</div>
namespace backend\controllers;
use Yii;
use common\models\InvoicesFiles;
use common\models\InvoicesFilesSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
public function actionCreate()
{
$model = new InvoicesFiles();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Загрузка файла
if($model->file = UploadedFile::getInstance($model,'file')){
//Сохранение файла
$uploadPath = Yii::getAlias('@frontend/web/uploads/invoices/'.$model->iid.'/');
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
$filename = $model->file->baseName.'.'.$model->file->extension;
if(file_exists($uploadPath.$filename)){unlink($uploadPath.$filename);}
$model->file->saveAs($uploadPath.$filename);
$model->file =$filename;
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Загрузка файла
if($model->file = UploadedFile::getInstance($model,'file')){
//Сохранение файла
$uploadPath = Yii::getAlias('@frontend/web/uploads/invoices/'.$model->iid.'/');
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
$filename = $model->file->baseName.'.'.$model->file->extension;
if(file_exists($uploadPath.$filename)){unlink($uploadPath.$filename);}
$model->file->saveAs($uploadPath.$filename);
$model->file =$filename;
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
public function getImagesLinks()
{
$path = ArrayHelper::getColumn(self::find()->all(), 'pathImg');
return $path;
}
public function getImagesLinksData()
{
$files = UploadsFiles::find()->all();
return ArrayHelper::toArray($files,[
UploadsFiles::class => [
'caption' => 'file',
'key' => 'id'
]
]);
}
echo $form->field($model, 'imagesFile')->widget(FileInput::class, [
'options' => ['accept' => 'image/*', 'multiple' => true],
'pluginOptions' => [
'showUpload' => false,
'showRemove' => false,
'overwriteInitial'=>false,
'initialPreviewAsData' => true,
'initialPreview' => $model->imagesLinks,
'initialPreviewConfig' => $model->imagesLinksData,
'uploadUrl' => Url::to(['upload-img']),
'deleteUrl' => Url::to(['delete-img']),
]
])->label('<h3>Test Img</h3>');