[
'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()
{
$dataProviderPointsSale = new ActiveDataProvider([
'query' => $this->getPointSaleManager()->queryPointSalesPublic($this->getProducerCurrent()),
'pagination' => [
'pageSize' => 50,
],
'sort' => false,
]);
$productCategoryArray = $this->getProductCategoryManager()->findProductCategories();
$dataProviderProductsByCategories = [];
foreach ($productCategoryArray as $productCategory) {
$dataProviderProductsByCategories[$productCategory->id] = new ActiveDataProvider([
'query' => $this->getProductManager()->queryProductsByProductCategory($productCategory),
'pagination' => [
'pageSize' => 500,
],
'sort' => false,
]);
}
$queryProducts = Product::find()
->andWhere([
'id_producer' => $this->getProducerCurrent()->id,
'active' => true,
'id_product_category' => null,
])
->orderBy('order ASC');
$dataProviderProducts = new ActiveDataProvider([
'query' => $queryProducts,
'pagination' => [
'pageSize' => 500,
],
'sort' => false,
]);
$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,
]);
}
/**
* Affiche et traite le formulaire de contact dédié aux producteurs.
*/
public function actionContact()
{
$model = new ContactForm();
$producer = $this->getProducerCurrent();
if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
$isSent = false;
if (is_array($producer->contact)) {
$email = $this->getProducerManager()->getMainContactEmail($producer);
if (strlen($email) && $model->sendEmail($email)) {
$isSent = true;
}
else {
die('email : '.$email);
}
}
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->getUserProducerManager()
->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 = GlobalParam::getCurrentProducer();
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 = GlobalParam::getCurrentProducer();
if (!strlen($producer->gcs)) {
throw new \yii\base\UserException('Conditions générales de vente introuvables.');
}
return $this->render('gcs', [
'producer' => $producer
]);
}
}
?>