[
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Affiche et gère les erreurs.
*/
public function actionError()
{
$exception = \Yii::$app->errorHandler->exception;
if ($exception !== null) {
if ($exception->getMessage() == 'Producteur introuvable') {
\Yii::$app->getResponse()->redirect(
$this->getUrlManagerFrontend()->createAbsoluteUrl(['site/error', 'producer_not_found' => true])
)->send();
} else {
return $this->render('error', ['exception' => $exception]);
}
}
}
/**
* Affiche la page d'accueil des producteurs comprenant une image, une
* description, la liste des points de vente et les produits.
*/
public function actionIndex()
{
$productModule = $this->getProductModule();
$pointSaleModule = $this->getPointSaleModule();
$dataProviderPointsSale = new ActiveDataProvider([
'query' => $pointSaleModule->queryPointSalesPublic($this->getProducerCurrent()),
'pagination' => [
'pageSize' => 50,
],
'sort' => false,
]);
$productCategoryArray = $this->getProductCategoryModule()->findProductCategories();
$dataProviderProductsByCategories = [];
foreach ($productCategoryArray as $productCategory) {
$dataProviderProductsByCategories[$productCategory->id] = new ActiveDataProvider([
'query' => $productModule->queryProductsByProductCategory($productCategory)->query(),
'pagination' => [
'pageSize' => 500,
],
'sort' => false,
]);
$this->filterProductsPublic($dataProviderProductsByCategories[$productCategory->id]);
}
$queryProducts = Product::find()
->andWhere([
'id_producer' => $this->getProducerCurrent()->id,
'status' => Product::STATUS_ONLINE,
'id_product_category' => null,
])
->with('productPointSale')
->orderBy('order ASC');
$dataProviderProducts = new ActiveDataProvider([
'query' => $queryProducts,
'pagination' => [
'pageSize' => 500,
],
'sort' => false,
]);
$this->filterProductsPublic($dataProviderProducts);
$products = $queryProducts->all();
foreach ($dataProviderProductsByCategories as $dataProvider) {
$products = array_merge(
$products,
$dataProvider->query->all()
);
}
$hasProductPhoto = false;
$hasProductWeight = false;
foreach ($products as $product) {
if (strlen($product->photo) > 0) {
$hasProductPhoto = true;
}
if ($product->weight && $product->weight > 0) {
$hasProductWeight = true;
}
}
return $this->render('index', [
'dataProviderProductsByCategories' => $dataProviderProductsByCategories,
'dataProviderPointsSale' => $dataProviderPointsSale,
'dataProviderProducts' => $dataProviderProducts,
'hasProductPhoto' => $hasProductPhoto,
'hasProductWeight' => $hasProductWeight,
'categories' => $productCategoryArray,
]);
}
public function filterProductsPublic($dataProviderProducts)
{
$productModule = $this->getProductModule();
$models = $dataProviderProducts->getModels();
foreach($models as $index => $product) {
if(!$productModule->isPublic($product)) {
unset($models[$index]);
}
}
$dataProviderProducts->setModels($models);
}
/**
* Affiche et traite le formulaire de contact dédié aux producteurs.
*/
public function actionContact()
{
$featureModule = $this->getFeatureModule();
if($featureModule->getChecker()->isDisabled(Feature::ALIAS_CONTACT)) {
return $this->redirect(['site/index']);
}
$model = new ContactForm();
$producer = $this->getProducerCurrent();
if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
$isSent = false;
if ($producer->contact_email && strlen($producer->contact_email) > 0 && $model->sendEmail($producer)) {
$isSent = true;
}
if ($isSent) {
$this->setFlash('success', 'Votre message a bien été envoyé.');
} else {
$this->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
}
$model = new ContactForm();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Ajoute ou supprime un producteur des favoris de l'utilisateur.
* Redirige vers la page d'accueil du producteur.
*/
public function actionBookmarks(string $action)
{
$producer = $this->getProducerCurrent();
$userProducer = $this->getUserProducerModule()
->createUserProducerIfNotExist(GlobalParam::getCurrentUser(), $producer);
if ($action == 'add') {
$userProducer->bookmark = 1;
$this->setFlash(
'success',
'Le producteur ' . Html::encode($producer->name) . ' vient d\'être ajouté à vos favoris.'
);
} else {
$userProducer->bookmark = 0;
$this->setFlash(
'success',
'Le producteur ' . Html::encode($producer->name) . ' vient d\'être supprimé de vos favoris.'
);
}
$userProducer->save();
return $this->redirect(['site/index']);
}
/**
* Affiche les mentions légales du producteur.
*/
public function actionMentions()
{
$producer = $this->getProducerCurrent();
if (!strlen($producer->mentions)) {
throw new \yii\base\UserException('Mentions légales introuvables.');
}
return $this->render('mentions', [
'producer' => $producer
]);
}
/**
* Affiche les conditions générales de vente du producteur.
*/
public function actionGcs()
{
$producer = $this->getProducerCurrent();
if (!strlen($producer->gcs)) {
throw new \yii\base\UserException('Conditions générales de vente introuvables.');
}
return $this->render('gcs', [
'producer' => $producer
]);
}
}
?>