ldc-legaulois/app/Http/Controllers/ClubController.php

309 lines
10 KiB
PHP
Raw Normal View History

2021-03-01 11:51:00 +01:00
<?php
namespace App\Http\Controllers;
2021-03-10 18:58:40 +01:00
use App\Http\Requests\BlockCustomerRequest;
2021-03-10 17:16:31 +01:00
use App\Http\Requests\DeleteMessagesRequest;
use App\Http\Requests\SendMessageRequest;
2021-03-05 08:17:35 +01:00
use App\Http\Requests\SurveyAnswersRequest;
use App\Http\Requests\UpdateInfosRequest;
use App\Models\Articles;
2021-03-10 18:58:40 +01:00
use App\Models\BlockList;
use App\Models\Customers;
2021-03-10 17:16:31 +01:00
use App\Models\Message;
use App\Models\Notifications;
use App\Models\Reductions;
2021-03-01 11:51:00 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2021-03-10 17:16:31 +01:00
use Illuminate\Support\Facades\View;
use RuntimeException;
use Snipe\BanBuilder\CensorWords;
2021-03-01 11:51:00 +01:00
class ClubController extends Controller
{
2021-04-14 16:10:49 +02:00
public function index(Request $request)
2021-03-08 12:00:46 +01:00
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
$reductions = Reductions::where([
['end_date_broadcast', '>', now()],
['status', 1]
])->get();
2021-04-26 10:29:34 +02:00
$articles = Articles::where('status', 1)->orderBy('updated_at', 'DESC')->get();
$categories = Articles::select('type')->where('status', true)->groupBy('type')->get()->pluck('type');
$montantReductions = 0.0;
2021-03-05 08:17:35 +01:00
foreach ($reductions as $reduc) {
$montantReductions += $reduc->face_value;
2021-03-05 08:17:35 +01:00
}
$nbCustomers = Customers::count();
2021-03-03 15:26:59 +01:00
// Cas de catégorie d'article
2021-04-14 16:10:49 +02:00
$category = $request->category;
2021-03-03 15:26:59 +01:00
2021-03-19 21:40:10 +01:00
return view('front.club.index', compact(
'montantReductions',
'nbCustomers',
'articles',
'categories',
'category'
));
}
2021-03-08 12:00:46 +01:00
public function infos()
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
$customer = Auth::guard('customer')->user();
2021-03-19 21:40:10 +01:00
if (!$customer) {
abort(404);
2021-03-19 21:40:10 +01:00
}
/** @var \App\Lib\LDCCRM\ApiV1 */
2021-03-05 15:55:22 +01:00
$crm = app('crmApi');
$crmCustomer = $crm->getCustomer($customer->crm_id);
2021-03-19 21:40:10 +01:00
if ($crmCustomer->status === "KO") {
2021-03-08 12:00:46 +01:00
abort(503);
2021-03-19 21:40:10 +01:00
}
$crmInfos = $crmCustomer->user;
$birthDate = explode('-', $crmInfos->birth_date);
2021-03-10 17:16:31 +01:00
return view('front.club.informations', compact('customer', 'crmInfos', 'birthDate'));
}
/**
* Send a request to the CRM to change the given customer's informations
* @param Request $request
*/
2021-03-08 12:00:46 +01:00
public function updateInfos(UpdateInfosRequest $request)
{
// TODO : Activer le changement d'email quand ce sera possible dans la CRM
/** @var \App\Lib\LDCCRM\ApiV1 */
2021-03-05 15:55:22 +01:00
$crm = app('crmApi');
$crmCustomer = $crm->updateCustomer(
Auth::guard('customer')->user()->crm_id,
$request->firstname,
$request->lastname,
$request->optin !== null ? 1 : 0,
$request->year . '-' . $request->month . '-' . $request->day,
$request->phone,
$request->address,
null,
$request->city,
$request->postcode,
null
);
2021-03-19 21:40:10 +01:00
if ($crmCustomer->status === "KO") {
return redirect()->route('account.infos')
->withErrors(__("Une erreur est survenue, merci de réessayer ultérieurement."));
}
return redirect()->route('account.infos')
->with('success', __("Vos informations ont bien été enregistrées."));
}
/**
* Call the notifications page
*/
2021-03-08 12:00:46 +01:00
public function notifications()
{
$notificationsIds = Notifications::pluck('id')->toArray();
2021-03-10 17:16:31 +01:00
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
$customer->readNotifications()->sync($notificationsIds);
2021-03-21 14:14:41 +01:00
self::getNotifications();
2021-03-10 17:16:31 +01:00
return view('front.club.notifications');
}
2021-02-24 16:04:53 +01:00
/**
* Call the cart page
*/
2021-03-08 12:00:46 +01:00
public function cart()
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
2021-02-24 16:04:53 +01:00
$products = Auth::guard('customer')->user()->products;
2021-03-10 17:16:31 +01:00
return view('front.club.cart', compact('products'));
2021-02-24 16:04:53 +01:00
}
/**
* Call the favorites recipes page
*/
2021-03-08 12:00:46 +01:00
public function favoritesRecipes()
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
2021-02-24 16:04:53 +01:00
$recipes = Auth::guard('customer')->user()->recipes;
2021-03-10 17:16:31 +01:00
return view('front.club.favorites-recipes', compact('recipes'));
2021-02-24 16:04:53 +01:00
}
2021-03-03 15:26:59 +01:00
/**
* Call the reductions page
*/
2021-03-08 12:00:46 +01:00
public function reductions()
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
// TODO: get url from MesBr class
/** @var \App\Lib\MesBR */
$mesBR = app('mesBr');
2021-04-13 10:09:01 +02:00
$urlReductions = "https://www.mesbr.eu/?refsite=" . config('app.MESBR_REF_SITE') . "&refcamp=" .
config('app.MESBR_REF_CAMP') . "&hash=" . Auth::guard('customer')->user()->hashcode . "&now=" . time();
2021-03-10 17:16:31 +01:00
return view('front.club.reductions', compact('urlReductions'));
2021-03-03 15:26:59 +01:00
}
/**
* Call the article page with a slug for the article to print
* @param $slug Slug of the article
* @return View The view that the article have to print
*/
2021-03-05 08:17:35 +01:00
public function article(Articles $publishedArticle)
2021-03-08 12:00:46 +01:00
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
2021-03-03 15:26:59 +01:00
// Check si le customer connecté a déjà répondu
$alreadyAnswered = false;
if (Auth::guard('customer')->check()) {
2021-03-05 08:17:35 +01:00
$answersIds = $publishedArticle->answers()->pluck('id')->toArray();
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
2021-03-21 22:51:19 +01:00
$alreadyAnswered = $customer->surveysAnswers()
2021-03-05 08:17:35 +01:00
->whereIn('surveys_answer_id', $answersIds)->count() > 0 ? true : false;
2021-03-03 15:26:59 +01:00
}
2021-04-27 17:50:57 +02:00
$linkedArticles = Articles::where([
['status', true], ['type', $publishedArticle->type], ['id', '<>', $publishedArticle->id]
2021-03-05 08:17:35 +01:00
])->get();
2021-03-03 15:26:59 +01:00
2021-04-26 10:29:34 +02:00
$categories = Articles::select('type')->where('status', true)->groupBy('type')->get()->pluck('type');
2021-03-03 15:26:59 +01:00
2021-03-05 08:17:35 +01:00
return view('front.club.article', compact(
'publishedArticle',
'categories',
'linkedArticles',
'alreadyAnswered'
));
2021-03-03 15:26:59 +01:00
}
/**
* Handle the request of a customer that answered a survey
* @param $request Request
* @return View View to go to
*/
2021-03-05 08:17:35 +01:00
public function answerSurvey(SurveyAnswersRequest $request)
2021-03-08 12:00:46 +01:00
{
2021-03-03 15:26:59 +01:00
$answers = $request->answers;
2021-03-05 08:17:35 +01:00
if (empty($answers)) {
2021-03-03 15:26:59 +01:00
return back()->withErrors(__("Votre réponse est incomplète. Merci de corriger"));
2021-03-05 08:17:35 +01:00
}
2021-03-03 15:26:59 +01:00
2021-03-19 21:40:10 +01:00
/** @var \App\Models\Customers $customer */
2021-03-03 15:26:59 +01:00
$customer = Auth::guard('customer')->user();
2021-03-05 08:17:35 +01:00
if (is_array($answers)) {
2021-03-19 21:40:10 +01:00
$customer->surveysAnswers()->syncWithoutDetaching($answers);
2021-03-05 08:17:35 +01:00
} else {
2021-03-19 21:40:10 +01:00
$customer->surveysAnswers()->syncWithoutDetaching($answers);
2021-03-05 08:17:35 +01:00
}
2021-03-03 15:26:59 +01:00
return back();
}
2021-03-10 17:16:31 +01:00
public function messages()
{
2021-03-21 14:14:41 +01:00
self::getNotifications();
2021-03-10 17:16:31 +01:00
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
$conversations = $customer->conversations();
2021-03-10 18:58:40 +01:00
$ids = collect($conversations)->pluck('to_id');
$blockList = BlockList::where('customer_id', $customer->id)
->whereIn('blocked_id', $ids)->get();
return view('front.club.messages', compact('conversations', 'blockList'));
2021-03-10 17:16:31 +01:00
}
public function sendMessage(SendMessageRequest $request)
{
$customer = Customers::where('username', $request->to)->firstOrFail();
2021-03-11 08:22:31 +01:00
// phpcs:ignore PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace
if (
BlockList::where([
'customer_id' => Auth::guard('customer')->user()->id,
'blocked_id' => $customer->id
])->exists()
) {
2021-03-10 18:58:40 +01:00
return response()->json(false);
}
2021-03-10 17:16:31 +01:00
$message = new Message($request->all());
$message->to = $customer->id;
$message->from = Auth::guard('customer')->user()->id;
$censor = new CensorWords();
try {
$censor->setDictionary(['en-us', 'en-uk', config('app.locale')]);
} catch (RuntimeException $e) {
$censor->setDictionary(['en-us', 'en-uk', 'fr']);
}
$message->message = $censor->censorString($message->message)['clean'];
$message->saveOrFail();
return response()->json($message->load(['toCustomer' => function ($q) {
$q->select(['id', 'username', 'created_at']);
}, 'fromCustomer' => function ($q) {
$q->select(['id', 'username', 'created_at']);
}]));
}
public function deleteMessage(DeleteMessagesRequest $request)
{
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
$message = Message::findOrFail($request->message);
$conversations = (object)$customer->conversations();
foreach ($conversations as $conversation) {
$conversation = (object)$conversation;
if ($conversation->id != $message->id) {
continue;
}
/** @var \App\Models\Message $message */
foreach ($conversation->messages as $message) {
if ($message->from == $customer->id) {
$message->hide_from = true;
}
if ($message->to == $customer->id) {
$message->hide_to = true;
}
if ($message->hide_to && $message->hide_from) {
$message->delete();
} else {
$message->saveOrFail();
}
}
}
return response()->json();
}
2021-03-10 18:58:40 +01:00
public function blockCustomer(BlockCustomerRequest $request, bool $status)
{
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
$customerToBlock = Customers::where('username', $request->username)->firstOrFail();
$blockCustomer = BlockList::firstOrNew([
'customer_id' => $customer->id,
'blocked_id' => $customerToBlock->id,
]);
if ($status) {
$blockCustomer->saveOrFail();
} else {
$blockCustomer->delete();
}
return response()->json($blockCustomer);
}
2021-03-21 14:14:41 +01:00
public static function getNotifications()
2021-03-10 17:16:31 +01:00
{
if (!Auth::guard('customer')->check()) {
return;
}
/** @var \App\Models\Customers */
$customer = Auth::guard('customer')->user();
$notifications = $customer->getAllNotifications();
View::share('notifications', count($notifications) - count($customer->readNotifications()->get()));
2021-03-10 17:16:31 +01:00
View::share('notificationsList', $notifications);
}
2021-03-01 11:51:00 +01:00
}