Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions Api/Resource/RestockingAlert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace StockAlert\Api\Resource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use Propel\Runtime\Map\TableMap;
use StockAlert\Model\Map\RestockingAlertTableMap;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
use Thelia\Api\Bridge\Propel\Attribute\Relation;
use Thelia\Api\Resource\ProductSaleElements;
use Thelia\Api\Resource\PropelResourceInterface;
use Thelia\Api\Resource\PropelResourceTrait;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/admin/stock-alert/{id}',
name: 'api_stock_alert_get_id'
),
new GetCollection(
uriTemplate: '/admin/stock-alerts',
name: 'api_stock_alert_get_collection'
),
new Post(
uriTemplate: '/admin/stock-alert',
name: 'api_stock_alert_post_id'
),
new Delete(
uriTemplate: '/admin/stock-alert/{id}',
name: 'api_stock_alert_delete_id'
),
new Patch(
uriTemplate: '/admin/stock-alert/{id}',
name: 'api_stock_alert_patch_id'
)
],
normalizationContext: ['groups' => [self::GROUP_ADMIN_READ]],
denormalizationContext: ['groups' => [self::GROUP_ADMIN_WRITE]]
)]
#[ApiResource(
operations: [
new Get(
uriTemplate: '/front/stock-alert/{id}',
name: 'api_stock_alert_get_id_front'
),
new GetCollection(
uriTemplate: '/front/stock-alerts',
name: 'api_stock_alert_get_collection_front'
),
new Post(
uriTemplate: '/front/stock-alert',
name: 'api_stock_alert_post_id_front'
),
new Delete(
uriTemplate: '/front/stock-alert/{id}',
name: 'api_stock_alert_delete_id_front'
),
new Patch(
uriTemplate: '/front/stock-alert/{id}',
name: 'api_stock_alert_patch_id_front'
)
],
normalizationContext: ['groups' => [self::GROUP_FRONT_READ]],
denormalizationContext: ['groups' => [self::GROUP_FRONT_WRITE]]
)]
class RestockingAlert implements PropelResourceInterface
{
use PropelResourceTrait;

public const GROUP_ADMIN_READ = 'admin:stock_alert:read';
public const GROUP_ADMIN_WRITE = 'admin:stock_alert:write';
public const GROUP_FRONT_READ = 'front:stock_alert:read';
public const GROUP_FRONT_WRITE = 'front:stock_alert:write';

/**
* @var int|null
*/
#[Groups([self::GROUP_ADMIN_READ, self::GROUP_FRONT_READ])]
public ?int $id = null;

/**
* @var ProductSaleElements|null
*/
#[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])]
#[Relation(targetResource: ProductSaleElements::class)]
public ?ProductSaleElements $productSaleElements = null;

/**
* @var string|null
*/
#[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])]
public ?string $email = null;

/**
* @var string|null
*/
#[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])]
public ?string $locale = null;

/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}

/**
* @param int|null $id
* @return void
*/
public function setId(?int $id): void
{
$this->id = $id;
}

/**
* @return ProductSaleElements|null
*/
public function getProductSaleElements(): ?ProductSaleElements
{
return $this->productSaleElements;
}

/**
* @param ProductSaleElements|null $productSaleElements
* @return void
*/
public function setProductSaleElements(?ProductSaleElements $productSaleElements): void
{
$this->productSaleElements = $productSaleElements;
}

/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}

/**
* @param string|null $email
* @return void
*/
public function setEmail(?string $email): void
{
$this->email = $email;
}

/**
* @return string|null
*/
public function getLocale(): ?string
{
return $this->locale;
}

/**
* @param string|null $locale
* @return void
*/
public function setLocale(?string $locale): void
{
$this->locale = $locale;
}

/**
* @return TableMap|null
*/
#[Ignore] public static function getPropelRelatedTableMap(): ?TableMap
{
return new RestockingAlertTableMap();
}
}
6 changes: 5 additions & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.0.2</version>
<version>2.0.4</version>
<author>
<name>Julien Chanséaume</name>
<email>julien@thelia.net</email>
</author>
<author>
<name>Loïc MO</name>
<email>lmo@openstudio.fr</email>
</author>
<type>classic</type>
<thelia>2.5.0</thelia>
<stability>rc</stability>
Expand Down
9 changes: 0 additions & 9 deletions Config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<!--<route id="stockalert.front.subscribe" path="/module/stockalert/subscribe" methods="post">
<default key="_controller">StockAlert\Controller\StockAlertFrontOfficeController::subscribe</default>
</route>

<route id="stockalert.back.subscribe" path="/admin/module/stockalert/configuration" methods="post">
<default key="_controller">StockAlert\Controller\StockAlertBackOfficeController::configuration</default>
</route>-->

</routes>
54 changes: 31 additions & 23 deletions Controller/StockAlertBackOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,42 @@

namespace StockAlert\Controller;


use Propel\Runtime\Exception\PropelException;
use StockAlert\Form\StockAlertConfig;
use StockAlert\Model\RestockingAlertQuery;
use StockAlert\StockAlert;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\ParserContext;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\ConfigQuery;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Tools\URL;

/**
* @Route("/admin/module/stockalert", name="stockalert_back")
* Class StockAlertBackOfficeController
* @package StockAlert\Controller
* @author Baixas Alban <abaixas@openstudio.fr>
* @author Julien Chanséaume <julien@thelia.net>
*/
#[Route(path: '/admin/module/stockalert', name: 'stockalert_back')]
class StockAlertBackOfficeController extends BaseAdminController
{

/**
* @Route("/configuration", name="_configuration", methods="POST")
* @param ParserContext $parserContext
* @return Response|RedirectResponse|string|\Symfony\Component\HttpFoundation\Response|null
*/
public function configuration(ParserContext $parserContext)
#[Route(path: '/configuration', name: '_configuration', methods: ['POST'])]
public function configuration(ParserContext $parserContext): Response|RedirectResponse|string|\Symfony\Component\HttpFoundation\Response|null
{
$errorMessage = null;

$form = $this->createForm(StockAlertConfig::getName());

try {
$configForm = $this->validateForm($form)->getData();

ConfigQuery::write(StockAlert::CONFIG_ENABLED, $configForm['enabled']);
ConfigQuery::write(StockAlert::CONFIG_ENABLED, $configForm['enabled']);
ConfigQuery::write(StockAlert::CONFIG_THRESHOLD, $configForm['threshold']);
$emails = str_replace(' ', '', $configForm['emails']);
ConfigQuery::write(StockAlert::CONFIG_EMAILS, $emails);
ConfigQuery::write(StockAlert::CONFIG_NOTIFY, $configForm['notify']);
ConfigQuery::write(StockAlert::CONFIG_EMAILS, $emails);
ConfigQuery::write(StockAlert::CONFIG_NOTIFY, $configForm['notify']);

return $this->generateSuccessRedirect($form);
} catch (FormValidationException $e) {
Expand All @@ -68,23 +63,36 @@ public function configuration(ParserContext $parserContext)
->setGeneralError($errorMessage);

return $this->render(
"module-configure",
'module-configure',
[
"module_code" => StockAlert::getModuleCode()
'module_code' => StockAlert::getModuleCode(),
]
);
}

/**
* @Route("/delete", name="_delete", methods="GET")
* @param RequestStack $requestStack
* @param Session $session
* @return RedirectResponse
* @throws PropelException
*/
public function deleteEmail(RequestStack $requestStack, Session $session)
#[Route(path: '/delete', name: '_delete', methods: ['GET'])]
public function deleteEmail(RequestStack $requestStack, Session $session): RedirectResponse
{
$restockingAlertId = $requestStack->getCurrentRequest()->get("id");
$restockingAlertId = $requestStack->getCurrentRequest()->get('id');

if ($restockingAlertId) {
$restockingAlert = RestockingAlertQuery::create()->filterById($restockingAlertId)->findOne();
$restockingAlert->delete();
$restockingAlert = RestockingAlertQuery::create()
->filterById($restockingAlertId)
->findOne();

if (null !== $restockingAlert) {
$restockingAlert->delete();
}
}
return new RedirectResponse(URL::getInstance()->absoluteUrl($session->getReturnToUrl()));

return new RedirectResponse(
URL::getInstance()->absoluteUrl($session->getReturnToUrl())
);
}
}
Loading