Переглянути джерело

[Administration] Produits : problème upload images webp #1260

feature/souke
Guillaume Bourgeois 1 рік тому
джерело
коміт
ea726157c8
4 змінених файлів з 55 додано та 35 видалено
  1. +42
    -28
      backend/controllers/ProductController.php
  2. +1
    -1
      backend/views/product/_form.php
  3. +4
    -2
      common/helpers/Upload.php
  4. +8
    -4
      common/logic/Product/Product/Model/Product.php

+ 42
- 28
backend/controllers/ProductController.php Переглянути файл

@@ -125,27 +125,34 @@ class ProductController extends BackendController
$model->sunday = 1;
$model->available_on_points_sale = 1;

if ($model->load(\Yii::$app->request->post()) && $productManager->saveCreate($model)) {
if ($model->load(\Yii::$app->request->post())) {

$lastProductOrder = Product::find()->where('id_producer = :id_producer')->params([':id_producer' => GlobalParam::getCurrentProducerId()])->orderBy('order DESC')->one();
if ($lastProductOrder) {
$model->order = ++$lastProductOrder->order;
}
$model->photoFile = UploadedFile::getInstance($model, 'photoFile');
if($model->validate()) {

Upload::uploadFile($model, 'photo');
$productManager->saveUpdate($model);
$lastProductOrder = Product::find()->where('id_producer = :id_producer')->params([':id_producer' => GlobalParam::getCurrentProducerId()])->orderBy('order DESC')->one();
if ($lastProductOrder) {
$model->order = ++$lastProductOrder->order;
}

$this->processAvailabilityPointsSale($model);
$distributionManager->addProductIncomingDistributions($model);
$productManager->create($model);

$this->setFlash('success', 'Produit <strong>' . Html::encode($model->name) . '</strong> ajouté');
if($model->photoFile) {
Upload::uploadFile($model, 'photoFile', 'photo');
}

return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
]);
$this->processAvailabilityPointsSale($model);
$distributionManager->addProductIncomingDistributions($model);

$this->setFlash('success', 'Produit <strong>' . Html::encode($model->name) . '</strong> ajouté');

return $this->redirect(['index']);
}
}

return $this->render('create', [
'model' => $model,
]);
}

/**
@@ -165,24 +172,31 @@ class ProductController extends BackendController

$photoFilenameOld = $model->photo;

if ($model->load(\Yii::$app->request->post()) && $productManager->saveUpdate($model)) {
if (Yii::$app->request->isPost && $model->load(\Yii::$app->request->post())) {

Upload::uploadFile($model, 'photo', $photoFilenameOld);
$model->photoFile = UploadedFile::getInstance($model, 'photoFile');

$deletePhoto = $request->post('delete_photo', 0);
if ($deletePhoto) {
$model->photo = '';
$model->save();
}
if($model->validate()) {
if($model->photoFile) {
Upload::uploadFile($model, 'photoFile', 'photo', $photoFilenameOld);
}

$deletePhoto = $request->post('delete_photo', 0);
if ($deletePhoto) {
$model->photo = '';
$model->save();
}

$this->processAvailabilityPointsSale($model);
$this->processAvailabilityPointsSale($model);
if ($model->apply_distributions) {
$distributionManager->addProductIncomingDistributions($model);
}
$productManager->update($model);

if ($model->apply_distributions) {
$distributionManager->addProductIncomingDistributions($model);
}
$this->setFlash('success', 'Produit <strong>' . Html::encode($model->name) . '</strong> modifié');
return $this->redirect(['index']);

$this->setFlash('success', 'Produit <strong>' . Html::encode($model->name) . '</strong> modifié');
return $this->redirect(['index']);
}
}

return $this->render('update/update', [

+ 1
- 1
backend/views/product/_form.php Переглянути файл

@@ -115,7 +115,7 @@ $taxRateManager = $this->getTaxRateManager();
?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'photo')->fileInput() ?>
<?= $form->field($model, 'photoFile')->fileInput() ?>
<?php
if (strlen($model->photo)) {
echo '<img class="photo-product" src="' . Image::getThumbnailSmall($model->photo, true). '" width="200px" /><br />';

+ 4
- 2
common/helpers/Upload.php Переглянути файл

@@ -43,9 +43,9 @@ use Yii;

class Upload
{
public static function uploadFile($model, $champs, $filename_old = '')
public static function uploadFile($model, $champsFile, $champs, $filename_old = '')
{
$file = UploadedFile::getInstance($model, $champs);
$file = $model->$champsFile;
if ($file) {
$file_name = $file->baseName . '-' . uniqid();
$file_name_extension = $file_name . '.' . $file->extension;
@@ -79,6 +79,8 @@ class Upload
$model->$champs = $filename_old;
}

$model->$champsFile = null;

$model->save();
}
}

+ 8
- 4
common/logic/Product/Product/Model/Product.php Переглянути файл

@@ -48,6 +48,7 @@ use common\logic\Product\ProductPointSale\Model\ProductPointSale;
use common\logic\Product\ProductPrice\Model\ProductPrice;
use common\components\ActiveRecordCommon;
use common\logic\Subscription\ProductSubscription\Model\ProductSubscription;
use yii\web\UploadedFile;

/**
* This is the model class for table "product".
@@ -57,12 +58,15 @@ class Product extends ActiveRecordCommon
{
public $total = 0;
public $apply_distributions = true;

public $price_with_tax = 0;
public $wording_unit = '';

public $pointsSale;

/**
* @var UploadedFile
*/
public $photoFile;

public static $unitsArray = [
'piece' => [
'unit' => 'piece',
@@ -121,7 +125,7 @@ class Product extends ActiveRecordCommon
[['active', 'order', 'id_producer', 'id_tax_rate', 'id_product_category'], 'integer'],
[['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable', 'apply_distributions', 'available_on_points_sale'], 'boolean'],
[['price', 'weight', 'step', 'quantity_max', 'quantity_max_monday', 'quantity_max_tuesday', 'quantity_max_wednesday', 'quantity_max_thursday', 'quantity_max_friday', 'quantity_max_saturday', 'quantity_max_sunday'], 'number'],
[['photo'], 'file'],
[['photoFile'], 'file', 'extensions' => 'png, jpg, jpeg', 'mimeTypes' => 'image/png, image/jpeg'],
[['name', 'reference', 'description', 'photo', 'unit'], 'string', 'max' => 255],
[['recipe'], 'string', 'max' => 1000],
['step', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
@@ -145,7 +149,7 @@ class Product extends ActiveRecordCommon
'reference' => 'Référence',
'description' => 'Description',
'active' => 'Actif',
'photo' => 'Photo',
'photoFile' => 'Photo',
'price' => 'Prix (€) TTC',
'weight' => 'Poids',
'recipe' => 'Recette',

Завантаження…
Відмінити
Зберегти