134 lines
4.1 KiB
PHP
134 lines
4.1 KiB
PHP
|
<?php
|
||
|
namespace App\Product\Actions;
|
||
|
|
||
|
|
||
|
use App\Auth\UserTable;
|
||
|
use App\Product\Entity\Product;
|
||
|
use App\Product\ProductUpload;
|
||
|
use App\Product\Table\ProductTable;
|
||
|
use Framework\Actions\CrudAction;
|
||
|
use Framework\Renderer\RendererInterface;
|
||
|
use Framework\Router;
|
||
|
use Framework\Session\FlashService;
|
||
|
use Framework\Session\SessionInterface;
|
||
|
use Psr\Http\Message\ServerRequestInterface;
|
||
|
|
||
|
class ProductCrudAction extends CrudAction
|
||
|
{
|
||
|
protected $viewPath = "@product/admin/products";
|
||
|
|
||
|
//protected $routePrefix = "product.admin";
|
||
|
protected $routePrefix = "product.admin";
|
||
|
|
||
|
/**
|
||
|
* @var UserTable
|
||
|
*/
|
||
|
private $userTable;
|
||
|
/**
|
||
|
* @var ProductUpload
|
||
|
*/
|
||
|
private $productUpload;
|
||
|
/**
|
||
|
* @var SessionInterface
|
||
|
*/
|
||
|
private $session;
|
||
|
|
||
|
|
||
|
public function __construct(
|
||
|
RendererInterface $renderer,
|
||
|
Router $router,
|
||
|
ProductTable $table,
|
||
|
FlashService $flash,
|
||
|
UserTable $userTable,
|
||
|
ProductUpload $productUpload,
|
||
|
SessionInterface $session
|
||
|
) {
|
||
|
|
||
|
parent::__construct($renderer, $router, $table, $flash);
|
||
|
$this->userTable = $userTable;
|
||
|
$this->productUpload = $productUpload;
|
||
|
$this->session = $session;
|
||
|
}
|
||
|
|
||
|
public function delete(ServerRequestInterface $request)
|
||
|
{
|
||
|
$product = $this->table->find($request->getAttribute('id'));
|
||
|
$this->productUpload->delete($product->image);
|
||
|
return parent::delete($request);
|
||
|
}
|
||
|
|
||
|
protected function formParams(array $params): array
|
||
|
{
|
||
|
//$params['categories'] = $this->userTable->findList();
|
||
|
$params['users'] = $this->userTable->findList();
|
||
|
//$params['categories']['1231232'] = 'User fake';
|
||
|
$params['users']['1231232'] = 'User fake';
|
||
|
return $params;
|
||
|
}
|
||
|
|
||
|
protected function getNewEntity()
|
||
|
{
|
||
|
/*$product = new Product();
|
||
|
$product->created_at = new \DateTime();
|
||
|
return $product;*/
|
||
|
$entity = parent::getNewEntity();
|
||
|
$entity->setCreatedAt(new \DateTime());
|
||
|
//$entity->setAntenneId('');
|
||
|
return $entity;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param ServerRequestInterface $request
|
||
|
* @param Product $product
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function prePersist(ServerRequestInterface $request, $product): array
|
||
|
{
|
||
|
$params = array_merge($request->getParsedBody(), $request->getUploadedFiles());
|
||
|
// Uploader le fichier
|
||
|
$image = $this->productUpload->upload($params['image'], $product->image);
|
||
|
if ($image) {
|
||
|
$params['image'] = $image;
|
||
|
} else {
|
||
|
unset($params['image']);
|
||
|
}
|
||
|
$date_actu = date('d-m-Y');
|
||
|
if ($request->getAttribute('id')) { // Lors d'une modif
|
||
|
$ajout_params = [];
|
||
|
} else { // Lors d'une création
|
||
|
// $params = $request->getQueryParams();
|
||
|
$str_created_at = $date_actu;
|
||
|
$ajout_params = ['created_at' => date('Y-m-d H:i:s'),
|
||
|
'str_created_at' => $str_created_at];
|
||
|
}
|
||
|
$str_updated_at = $date_actu;
|
||
|
|
||
|
$userId = $this->session->get('auth.user');
|
||
|
|
||
|
$params = array_filter($params, function ($key) {
|
||
|
return in_array($key, ['name', 'quantite', 'prix', 'slug', 'content', 'created_at', 'image','antenne_id']);
|
||
|
}, ARRAY_FILTER_USE_KEY);
|
||
|
return array_merge($params, $ajout_params,['user_id' => $userId,
|
||
|
// 'updated_at' => date('Y-m-d H:i:s'),
|
||
|
'str_updated_at' =>$str_updated_at ]);
|
||
|
}
|
||
|
|
||
|
protected function getValidator(ServerRequestInterface $request)
|
||
|
{
|
||
|
$validator = parent::getValidator($request)
|
||
|
->required('content', 'name', 'slug', 'quantite', 'prix')
|
||
|
// ->length('content', 10)
|
||
|
->length('name', 2, 80)
|
||
|
->length('slug', 2, 50);
|
||
|
//->exists('user_id', $this->userTable->getTable(), $this->userTable->getPdo())
|
||
|
//->dateTime('created_at')
|
||
|
//->extension('image', ['jpg', 'png'])
|
||
|
//->slug('slug')
|
||
|
|
||
|
if (is_null($request->getAttribute('id'))) {
|
||
|
$validator->uploaded('image');
|
||
|
}
|
||
|
return $validator;
|
||
|
}
|
||
|
}
|