106 lines
3.8 KiB
PHP
106 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo\Klub;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\Customers\CrmQuestionsAnswersRequest;
|
|
use App\Models\Customer;
|
|
use App\Models\Game;
|
|
use App\Models\StaticPage;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
|
|
use LDC\LDCCRM\Models\Answer;
|
|
use LDC\LDCCRM\Models\Question;
|
|
|
|
class KlubController extends Controller
|
|
{
|
|
/**
|
|
* Display Klub Customer Homepage
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function home(): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
// * Fetch Coupon list (campaign wall)
|
|
$brList = Cache::remember('allbrs', 60, fn () => MesBrController::getStaticBr());
|
|
|
|
$game = Game::query()->where('published', true)->first();
|
|
|
|
$staticPageModel = StaticPage::query()->where('type', StaticPageType::klub)->firstOrFail();
|
|
|
|
return view('fo.pages.klub.home', compact('crmCustomer', 'brList', 'game', 'staticPageModel'));
|
|
}
|
|
|
|
/**
|
|
* Display questions
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function questions(): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
|
$crmCustomer = $customer->api_data;
|
|
$questions = Customer::cacheCrm(
|
|
$customer->hashcode,
|
|
'questions',
|
|
fn () => Question::unansweredBy($crmCustomer)
|
|
);
|
|
|
|
return view('fo.pages.klub.questions', compact('crmCustomer', 'questions'));
|
|
}
|
|
|
|
/**
|
|
* Save Customer answers to Crm questions
|
|
*
|
|
* @param \App\Http\Requests\Fo\Customers\CrmQuestionsAnswersRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function questionsSubmit(CrmQuestionsAnswersRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
|
$crmCustomer = $customer->api_data;
|
|
$questions = Customer::cacheCrm(
|
|
$customer->hashcode,
|
|
'questions',
|
|
fn () => Question::unansweredBy($crmCustomer)
|
|
);
|
|
|
|
$responses = $request->input('question');
|
|
// * int cast on ids
|
|
\array_walk_recursive($responses, function (&$id) {
|
|
$id = intval($id);
|
|
});
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
$responses = Arr::mapWithKeys($questions, fn ($question, $index) => [
|
|
optional(Arr::get($questions, $index))->id => Arr::get($responses, $index)
|
|
]);
|
|
try {
|
|
foreach ($responses as $repId => $reponse) {
|
|
Answer::setQuestionAnswers($customer->hashcode, $repId, Arr::wrap($reponse));
|
|
}
|
|
} catch (ApiErrorException | CrmValidationException | CrmException $e) {
|
|
report($e);
|
|
return back()->withErrors(trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
|
|
} finally {
|
|
// * Clear Customer cache
|
|
Customer::clearCrmCached($customer->hashcode, 'questions');
|
|
}
|
|
return \response()->redirectToRoute('fo.klub.home')
|
|
->with('success', trans(
|
|
'Merci pour vos réponses, nous les avons bien pris en compte, vous pouvez maintenant profiter du Klub.'
|
|
));
|
|
}
|
|
}
|