[
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Affiche et gère les erreurs.
*
* @return mixed
*/
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
if ($exception->getMessage() == 'Producteur introuvable') {
Yii::$app->getResponse()->redirect(
Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/error', 'producer_not_found' => true])
)->send();
return;
} 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
*
* @return ProducerView
*/
public function actionIndex()
{
// points de vente
$dataProviderPointsSale = new ActiveDataProvider([
'query' => PointSale::find()
->where([
'id_producer' => $this->getProducer(
)->id,
'restricted_access' => 0,
'status' => 1
])->orderBy(
'default DESC, is_bread_box ASC, name ASC'
),
'pagination' => [
'pageSize' => 50,
],
'sort' => false,
]);
// produits
$categoriesArray = ProductCategory::searchAll([], ['orderby' => 'product_category.position ASC']);
$dataProviderProductsByCategories = [];
foreach ($categoriesArray as $category) {
$dataProviderProductsByCategories[$category->id] = new ActiveDataProvider([
'query' => Product::find()
->andWhere([
'id_producer' => $this->getProducer(
)->id,
'active' => true,
])
->andWhere(
'product.id_product_category = :id_product_category'
)
->params(
[':id_product_category' => $category->id]
)
->orderBy(
'order ASC'
),
'pagination' => [
'pageSize' => 500,
],
'sort' => false,
]);
}
$queryProducts = Product::find()
->andWhere([
'id_producer' => $this->getProducer()->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' => $categoriesArray,
]);
}
/**
* Affiche et traite le formulaire de contact dédié aux producteurs
*
* @return ProducerView
*/
public function actionContact()
{
$model = new ContactForm();
$producer = $this->getProducer();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$isSent = false;
if (is_array($producer->contact)) {
$email = '';
$contact = $producer->getMainContact();
if ($contact) {
$email = $contact->email;
}
if (strlen($email) && $model->sendEmail($email)) {
$isSent = true;
}
}
if ($isSent) {
Yii::$app->session->setFlash('success', 'Votre message a bien été envoyé.');
} else {
Yii::$app->session->setFlash('error', 'Il y a eu une erreur lors de l\'envoi de votre message.');
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
/**
* Ajoute ou supprime un producteur des favoris de l'utilisateur.
* Redirige vers la page d'accueil du producteur.
*
* @param $action 'add' ou 'delete'
*/
public function actionBookmarks($action)
{
$producer = $this->getProducer();
$userProducer = UserProducer::find()
->where([
'id_user' => User::getCurrentId(),
'id_producer' => $producer->id
])
->one();
if (!$userProducer) {
$userProducer = Producer::addUser(User::getCurrentId(), $producer->id);
}
if ($userProducer) {
if ($action == 'add') {
$userProducer->bookmark = 1;
Yii::$app->session->setFlash(
'success',
'Le producteur ' . Html::encode(
$producer->name
) . ' vient d\'être ajouté à vos favoris.'
);
} else {
$userProducer->bookmark = 0;
Yii::$app->session->setFlash(
'success',
'Le producteur ' . Html::encode(
$producer->name
) . ' vient d\'être supprimé de vos favoris.'
);
}
$userProducer->save();
}
$this->redirect(['site/index']);
}
/**
* Affiche les mentions légales du producteur.
*
* @return ProducerView
*/
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.
*
* @return ProducerView
*/
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
]);
}
}
?>