middleware(function (Request $request, \Closure $next) { $this->authorize('fo.showRe7'); return $next($request); }); } /** * Show Teaser page * * @return \Illuminate\Contracts\View\View */ public function teaser(): View { return view('theme::pages.teaser'); } /** * Show Teaser page * * @return \Illuminate\Contracts\View\View */ public function endGame(): View { return view('theme::pages.end-game'); } // * -- Participation /** * Show prize page * * @param \App\Models\Prize $prize * @return \Illuminate\Contracts\View\View */ public function prize(Prize $prize): View { return view('theme::pages.participation.won', [ 'prizeModel' => $prize ]); } /** * Show discount page * * @return \Illuminate\Contracts\View\View */ public function discount(): View { /** @var \App\Models\Settings */ $settings = app(Settings::class); /** @var \App\Lib\Discount */ $discount = Arr::get( $settings->web_coupons, $this->getRandomWeightedElementIndex($settings->web_coupons) ); $crmCoupon = new Reduction(); return view('theme::pages.participation.won', compact('discount', 'crmCoupon')); } /** * Show loose page * * @return \Illuminate\Contracts\View\View */ public function loose(): View { return view('theme::pages.participation.loose'); } // * -- END Participation /** * View won discount mail * * @param \App\Models\Prize $prize * @return \Illuminate\Contracts\View\View */ public function mailPrize(Prize $prize): View { return view('back::pages.re7-preview-mail', [ 'render_url' => route('re7.mail-print', [ 'type' => 'prize', 'prize' => $prize ]) ]); } /** * View won discount mail * * @return \Illuminate\Contracts\View\View */ public function mailDiscount(): View { return view('back::pages.re7-preview-mail', [ 'render_url' => route('re7.mail-print', [ 'type' => 'discount' ]) ]); } /** * Print a specific mail * * @param string $type * @param \App\Models\Prize|null $prize * @return \Illuminate\Http\Response */ public function mailPrint(string $type, Prize|null $prize = null): Response { /** @var \App\Models\User */ $user = auth('backend')->user(); /** @var \App\Models\Settings */ $settings = app(Settings::class); switch ($type) { case 'discount': /** @var \App\Lib\Discount */ $discount = Arr::get( $settings->web_coupons, $this->getRandomWeightedElementIndex($settings->web_coupons) ); return response((new DiscountWonMail( new Address( $user->email, $user->first_name ), $settings->name, $discount, config('app.url') ))->render()); case 'prize': return response((new PrizeWonMail( new Address( $user->email, $user->first_name ), $settings->name, $prize, config('app.url') ))->render()); default: return response('', 404); } //end switch } /** * Utility function for getting random values with weighting. * Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50) * An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%. * The return value is the array key, A, B, or C in this case. Note that the values assigned * do not have to be percentages. The values are simply relative to each other. If one value * weight was 2, and the other weight of 1, the value with the weight of 2 has about a 66% * chance of being selected. Also note that weights should be integers. * * @param array<\App\Lib\Discount> $weightedValues * @return integer The winning key * @url https://stackoverflow.com/a/11872928/4355295 */ private function getRandomWeightedElementIndex(array $weightedValues): int { mt_srand(time()); $rand = mt_rand(1, max((int)collect($weightedValues)->sum('percentage'), 1)); foreach ($weightedValues as $key => $value) { $rand -= $value->percentage; if ($rand <= 0) { return $key; } } } }