Files
aux-bieaux-legumes/local/modules/Recettes/Controller/BackController.php

294 lines
9.5 KiB
PHP
Executable File

<?php
namespace Recettes\Controller;
use Exception;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Recettes\Form\RecetteCreateForm;
use Recettes\Form\StepCreateForm;
use Recettes\Model\Recipe;
use Recettes\Model\RecipeProducts;
use Recettes\Model\RecipeProductsQuery;
use Recettes\Model\RecipeQuery;
use Recettes\Model\RecipeSteps;
use Recettes\Model\RecipeStepsQuery;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Event\Content\ContentUpdateEvent;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Form\BaseForm;
use Thelia\Model\AttributeAvI18n;
use Thelia\Model\AttributeAvI18nQuery;
use Thelia\Model\AttributeCombinationQuery;
use Thelia\Model\AttributeI18n;
use Thelia\Model\AttributeI18nQuery;
use Thelia\Model\AttributeQuery;
use Thelia\Model\ContentI18nQuery;
use Thelia\Model\ContentQuery;
use Thelia\Model\Map\ProductI18nTableMap;
use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\ProductQuery;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Tools\URL;
/**
* Class BackController
* @package Recettes\Controller
*/
class BackController extends BaseAdminController
{
public function saveRecipe(Request $request)
{
$con = Propel::getConnection();
$form = new RecetteCreateForm($request);
try {
$formValidate = $this->validateForm($form);
$data = $formValidate->getData();
$contentId = $data['content_id'];
$title = $data['title'];
$summary = $data['summary'];
$difficulty = $data['difficulty'];
$numberPeople = $data['people'];
$preparationTime = $data['preparation_time'];
$cookingTime = $data['cooking_time'];
$otherIngredients = $data['other_ingredients'];
if (null !== $title && null !== $contentId && null !== $difficulty && null !== $numberPeople && null !== $preparationTime) {
$recipe = RecipeQuery::create()->findOneByContentId($contentId);
if (null === $recipe)
$recipe = new Recipe();
else
$recipeId = $recipe->getId();
$recipe->setContentId($contentId);
$recipe->setSummary($summary);
$recipe->setTitle($title);
$recipe->setDifficulty($difficulty);
$recipe->setPeople($numberPeople);
$recipe->setPreparationTime($preparationTime);
$recipe->setCookingTime($cookingTime);
$recipe->setOtherIngredients($otherIngredients);
$recipe->save($con);
$con->commit();
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/content/update/" . $contentId . "#recipe"));
}
} catch (\Exception $e) {
$error = $e->getMessage();
}
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/content"));
}
public function removeProduct($recipeId, $pseId, $contentId)
{
RecipeProductsQuery::create()->filterByRecipeId($recipeId)->filterByPseId($pseId)->delete();
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/content/update/" . $contentId . "?current_tab=recipe"));
}
public function getData($product_id)
{
$locale = $this->getCurrentEditionLocale();
$combinations = [];
$con = Propel::getConnection();
$pse = ProductSaleElementsQuery::create()
->filterByProductId($product_id)
->find($con);
foreach ($pse as $pseItem)
{
$combinationsArray = AttributeCombinationQuery::create()
->filterByProductSaleElements($pseItem)
->find($con);
foreach ($combinationsArray as $combination) {
$pseId = $pseItem->getId();
$label = AttributeAvI18nQuery::create()
->filterByLocale($locale)
->findOneById($combination->getAttributeAvId())
->getTitle();
array_push($combinations, [$pseId => $label]);
}
}
return $combinations;
}
public function searchProduct()
{
$locale = $this->getCurrentEditionLocale();
$ref = $this->getRequest()->get('query', null);
$result = [];
if (! empty($ref)) {
$data = ProductQuery::create()
->filterByRef("%$ref%", Criteria::LIKE)
->orderByRef()
->useI18nQuery($locale)
->withColumn(ProductI18nTableMap::COL_TITLE, 'title')
->endUse()
->limit(15)
->select([
ProductTableMap::COL_ID,
ProductTableMap::COL_REF,
'title'
])
->find();
foreach ($data as $item) {
$combinations = self::getData($item['product.id']);
$result[] = [
'id' => $item[ProductTableMap::COL_ID],
'ref' => $item[ProductTableMap::COL_REF],
'title' => $item['title'],
'combinations' => $combinations,
];
}
}
return JsonResponse::create($result);
}
public function addProduct($contentId)
{
$pseId = (int) $this->getRequest()->get('pse_id');
$quantityNeeded = (string) $this->getRequest()->get('quantity_needed');
$recipeId = (int) $this->getRequest()->get('recipe_id');
$quantityProposed = (int) $this->getRequest()->get('quantity_proposed');
(new RecipeProducts())
->setRecipeId($recipeId)
->setPseId($pseId)
->setQuantity($quantityNeeded)
->setNbOfProducts($quantityProposed)
->save();
return $this->render('includes/related-products', [ 'content_id' => $contentId ]);
}
public function addStep(Request $request)
{
$form = new StepCreateForm($request);
$errorUrl = "";
try {
$formValidate = $this->validateForm($form);
$data = $formValidate->getData();
$recipeId = $data['recipe_id'];
$step = $data['step'];
$description = $data['description'];
$errorUrl = $data['error_url'];
$successUrl = $data['success_url'];
if ($recipeId && $step && $description) {
(new RecipeSteps())
->setRecipeId($recipeId)
->setStep($step)
->setDescription($description)
->save();
return new RedirectResponse(URL::getInstance()->absoluteUrl($successUrl));
}
else
return new RedirectResponse(URL::getInstance()->absoluteUrl($errorUrl));
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
public function removeStep($step, $recipeId, $contentId)
{
$con = Propel::getConnection();
$foundStep = RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->findOneByStep($step);
$nextSteps = RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->filterByStep($step, Criteria::GREATER_THAN)
->orderByStep()
->find($con);
if ($foundStep) {
$foundStep->delete();
foreach ($nextSteps as $next) {
$nouveauNumero = $next->getStep()-1;
(new RecipeSteps())
->setRecipeId($recipeId)
->setStep($nouveauNumero)
->setDescription($next->getDescription())
->save();
$next->delete();
}
}
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/content/update/" . $contentId . "?current_tab=recipe"));
}
public function updatePosition()
{
$step = $this->getRequest()->get('step');
$mode = $this->getRequest()->get('mode');
if ($mode === "up") $replacedStep = $step - 1;
else $replacedStep = $step + 1;
$recipeId = $this->getRequest()->get('recipe_id');
$contentId = $this->getRequest()->get('content_id');
$current = RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->findOneByStep($step);
$replaced = RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->findOneByStep($replacedStep);
// On supprime les 2 étapes (celle que l'on déplace et celle que l'on remplace) pour les recréer par la suite.
RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->findOneByStep($step)
->delete();
RecipeStepsQuery::create()
->filterByRecipeId($recipeId)
->findOneByStep($replacedStep)
->delete();
(new RecipeSteps())
->setRecipeId($recipeId)
->setStep($step)
->setDescription($replaced->getDescription())
->save();
(new RecipeSteps())
->setRecipeId($recipeId)
->setStep($replacedStep)
->setDescription($current->getDescription())
->save();
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/content/update/" . $contentId . "?current_tab=recipe"));
}
}