if ($activationRepository->completed($user)) { echo 'User is already activated. Try to log in.'; return; } //ops, cant activate! echo "Activation error!"; return; } //activated, do login echo 'Your account has been activated.'; $app->container->auth->login($user); $app->redirect('/'); return; }); $app->get('/user/poll(/:id)', function ($id = null) use($app) { $pollData = urukalo\CH\Poll::where('active', 1)->with('answers')->with('user_polls'); $user = $this->app->container->auth->check(); if (!is_null($id)) { $pollDataVote = clone $pollData; //check is voted if ($pollDataVote->whereHas('user_polls', function ($query) use($user) { $query->where('user_polls.idUser', $user->id); })->find((int) $id)) { echo "You can vote only one time"; //voted! show results if is public $pollData->where('public', 1)->find((int) $id); $app->twig->display('poll.html.twig', array("poll" => $pollData)); } else { //have premision to vote? if (!$user->hasAccess('poll.vote')) { echo "You don't have the permission to vote.";
} else { //show all polls $app->twig->display('polls-admin.html.twig', array("poll" => $pollData->get())); } } }); //save edited poll or create new one $app->post('/admin/poll(/:id)', function ($id = null) use($app) { $loggedUser = $app->container->auth->check(); if (!$loggedUser->hasAccess('poll.*')) { echo "You don't have the permission to access this page."; return; } $data = $app->request->post(); if (isset($data['id'])) { $poll = urukalo\CH\Poll::where('active', 1)->with('answers')->find((int) $id); } else { $poll = new urukalo\CH\Poll(); } $poll->name = $data['name']; $poll->question = $data['question']; $poll->public = isset($data['public']) && $data['public'] == 'on' ? 1 : 0; $poll->active = isset($data['active']) && $data['active'] == 'on' ? 1 : 0; $poll->archived = isset($data['archived']) && $data['archived'] == 'on' ? 1 : 0; $poll->save(); //save all answers too $answer = array(); foreach ($data['answer'] as $answerData) { $answer[] = new urukalo\CH\Answers($answerData); } $poll->answers()->saveMany($answer);