Browse Source

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

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
ea726157c8
4 changed files with 55 additions and 35 deletions
  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 View File

$model->sunday = 1; $model->sunday = 1;
$model->available_on_points_sale = 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,
]);
} }


/** /**


$photoFilenameOld = $model->photo; $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', [ return $this->render('update/update', [

+ 1
- 1
backend/views/product/_form.php View File

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

+ 4
- 2
common/helpers/Upload.php View File



class Upload 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) { if ($file) {
$file_name = $file->baseName . '-' . uniqid(); $file_name = $file->baseName . '-' . uniqid();
$file_name_extension = $file_name . '.' . $file->extension; $file_name_extension = $file_name . '.' . $file->extension;
$model->$champs = $filename_old; $model->$champs = $filename_old;
} }


$model->$champsFile = null;

$model->save(); $model->save();
} }
} }

+ 8
- 4
common/logic/Product/Product/Model/Product.php View File

use common\logic\Product\ProductPrice\Model\ProductPrice; use common\logic\Product\ProductPrice\Model\ProductPrice;
use common\components\ActiveRecordCommon; use common\components\ActiveRecordCommon;
use common\logic\Subscription\ProductSubscription\Model\ProductSubscription; use common\logic\Subscription\ProductSubscription\Model\ProductSubscription;
use yii\web\UploadedFile;


/** /**
* This is the model class for table "product". * This is the model class for table "product".
{ {
public $total = 0; public $total = 0;
public $apply_distributions = true; public $apply_distributions = true;

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

public $pointsSale; public $pointsSale;


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

public static $unitsArray = [ public static $unitsArray = [
'piece' => [ 'piece' => [
'unit' => 'piece', 'unit' => 'piece',
[['active', 'order', 'id_producer', 'id_tax_rate', 'id_product_category'], 'integer'], [['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'], [['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'], [['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], [['name', 'reference', 'description', 'photo', 'unit'], 'string', 'max' => 255],
[['recipe'], 'string', 'max' => 1000], [['recipe'], 'string', 'max' => 1000],
['step', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { ['step', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
'reference' => 'Référence', 'reference' => 'Référence',
'description' => 'Description', 'description' => 'Description',
'active' => 'Actif', 'active' => 'Actif',
'photo' => 'Photo',
'photoFile' => 'Photo',
'price' => 'Prix (€) TTC', 'price' => 'Prix (€) TTC',
'weight' => 'Poids', 'weight' => 'Poids',
'recipe' => 'Recette', 'recipe' => 'Recette',

Loading…
Cancel
Save