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

160 lines
4.9 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\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\AttributeCombinationQuery;
use Thelia\Model\ContentI18nQuery;
use Thelia\Model\ContentQuery;
use Thelia\Model\Map\ProductI18nTableMap;
use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\ProductQuery;
use Thelia\Tools\URL;
/**
* Class BackController
* @package Recettes\Controller
*/
class BackController extends BaseAdminController
{
public function saveRecipe(Request $request)
{
$error = "";
$con = Propel::getConnection();
$nouvelleRecette = false;
$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 . "#recipe"));
}
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 = AttributeCombinationQuery::create()
->filterByProductSaleElementsId($item->getId())
$item
$result[] = [
'id' => $item[ProductTableMap::COL_ID],
'ref' => $item[ProductTableMap::COL_REF],
'title' => $item['title'],
'combinations' => []
];
}
}
return JsonResponse::create($result);
}
public function searchCombination()
{
$locale = $this->getCurrentEditionLocale();
$ref = $this->getRequest()->get('query', null);
$result = [];
return JsonResponse::create($result);
}
/*
public function addProduct($contentId)
{
$productId = (int) $this->getRequest()->get('product_id');
(new AgendaRelatedProduct())
->setProductId($productId)
->setContentId($contentId)
->save();
return $this->render('ajax/related-products', [ 'content_id' => $contentId ]);
}
*/
}