', now()], ['status', 1] ])->get(); $articles = Articles::where('status', 1)->orderBy('updated_at', 'DESC')->get(); $categories = ArticlesCategories::whereHas('articles', function ($q) { $q->where('status', true); })->get(); $montantReductions = 0.0; foreach ($reductions as $reduc) { $montantReductions += $reduc->face_value; } $nbCustomers = Customers::count(); // Cas de catégorie d'article $category = $category->id ?? null; return view('front.club.index', compact( 'montantReductions', 'nbCustomers', 'articles', 'categories', 'category' )); } public function infos() { self::getNotifications(); $customer = Auth::guard('customer')->user(); if (!$customer) { abort(404); } /** @var \App\Lib\LDCCRM\ApiV1 */ $crm = app('crmApi'); $crmCustomer = $crm->getCustomer($customer->crm_id); if ($crmCustomer->status === "KO") { abort(503); } $crmInfos = $crmCustomer->user; $birthDate = explode('-', $crmInfos->birth_date); 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 */ public function updateInfos(UpdateInfosRequest $request) { // TODO : Activer le changement d'email quand ce sera possible dans la CRM /** @var \App\Lib\LDCCRM\ApiV1 */ $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 ); 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 */ public function notifications() { $notificationsIds = Notifications::pluck('id')->toArray(); /** @var \App\Models\Customers */ $customer = Auth::guard('customer')->user(); $customer->readNotifications()->sync($notificationsIds); self::getNotifications(); return view('front.club.notifications'); } /** * Call the cart page */ public function cart() { self::getNotifications(); $products = Auth::guard('customer')->user()->products; return view('front.club.cart', compact('products')); } /** * Call the favorites recipes page */ public function favoritesRecipes() { self::getNotifications(); $recipes = Auth::guard('customer')->user()->recipes; return view('front.club.favorites-recipes', compact('recipes')); } /** * Call the reductions page */ public function reductions() { self::getNotifications(); // TODO: get url from MesBr class /** @var \App\Lib\MesBR */ $mesBR = app('mesBr'); $urlReductions = "https://www.mesbr.eu/?refsite=" . config('app.REF_SITE') . "&refcamp=" . config('app.REF_CAMP') . "&hash=" . Auth::guard('customer')->user()->hashcode . "&now=" . time(); return view('front.club.reductions', compact('urlReductions')); } /** * 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 */ public function article(Articles $publishedArticle) { self::getNotifications(); // Check si le customer connecté a déjà répondu $alreadyAnswered = false; if (Auth::guard('customer')->check()) { $answersIds = $publishedArticle->answers()->pluck('id')->toArray(); /** @var \App\Models\Customers */ $customer = Auth::guard('customer')->user(); $alreadyAnswered = $customer->surveysAnswers() ->whereIn('surveys_answer_id', $answersIds)->count() > 0 ? true : false; } $linkedArticles = Articles::where([ ['status', true], ['category_id', $publishedArticle->category_id], ['id', '<>', $publishedArticle->id] ])->get(); $categories = ArticlesCategories::whereHas('articles', function ($q) { $q->where('status', 1); })->get(); return view('front.club.article', compact( 'publishedArticle', 'categories', 'linkedArticles', 'alreadyAnswered' )); } /** * Handle the request of a customer that answered a survey * @param $request Request * @return View View to go to */ public function answerSurvey(SurveyAnswersRequest $request) { $answers = $request->answers; if (empty($answers)) { return back()->withErrors(__("Votre réponse est incomplète. Merci de corriger")); } /** @var \App\Models\Customers $customer */ $customer = Auth::guard('customer')->user(); if (is_array($answers)) { $customer->surveysAnswers()->syncWithoutDetaching($answers); } else { $customer->surveysAnswers()->syncWithoutDetaching($answers); } return back(); } public function messages() { self::getNotifications(); /** @var \App\Models\Customers */ $customer = Auth::guard('customer')->user(); $conversations = $customer->conversations(); $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')); } public function sendMessage(SendMessageRequest $request) { $customer = Customers::where('username', $request->to)->firstOrFail(); // phpcs:ignore PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace if ( BlockList::where([ 'customer_id' => Auth::guard('customer')->user()->id, 'blocked_id' => $customer->id ])->exists() ) { return response()->json(false); } $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(); } 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); } public static function getNotifications() { 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())); View::share('notificationsList', $notifications); } }