<?php
class PollController extends \Phalcon\Mvc\Controller
{
public function showAction($pollId)
{
$this->view->poll = Poll::findFirstById($pollId);
if (Poll::count(array('conditions'=>'id = ?0', 'bind'=>array($pollId))) < 1) {
$this->response->redirect('/', null, 404);
}
$this->view->options = Options::find(array(
'conditions' => 'poll_id = ?0',
'bind' => array($pollId),
'order' => 'count DESC'
));
$this->view->comments = Comments::find(array(
'conditions' => 'poll_id = ?0',
'bind' => array($pollId),
'order' => 'id desc'
));
}
public function commentAction($pollId)
{
if ($this->request->isPost()) {
$new_comment = new Comments();
$new_comment->poll_id = $pollId;
$new_comment->name = !empty($_POST['name']) ? $_POST['name'] : 'Аноним';
$new_comment->text = $_POST['text'];
$new_comment->time = time();
$new_comment->user_ip = ip2long($this->request->getClientAddress());
if ($new_comment->save() == false) {
var_dump($_POST);
foreach($new_comment->getMessages() as $msg) {
echo $msg . "<br />";
}
exit;
}
$this->response->redirect('poll/show/' . $pollId);
}
}
public function addAction()
{
if ($this->request->isPost()) {
$new_poll = new Poll();
$new_poll->question = $_POST['question'];
$new_poll->time = time();
if ($new_poll->save() == false) {
foreach ($new_poll->getMessages() as $msg) {
echo $msg . "<br />";
}
exit;
}
$options_count = $_POST['option'];
$options_count = array_diff($options_count, array(''));
$options_count = count($options_count);
for ($i = 0; $i < $options_count; $i++) {
$new_option = new Options();
$new_option->name = $_POST['option'][$i];
$new_option->count = 0;
$new_option->poll_id = $new_poll->id;
if ($new_option->save() == false) {
foreach ($new_option->getMessages() as $msg) {
echo $msg . " <- option <br />";
}
exit;
}
}
}
}
public function voteAction($optionId)
{
$user_ip = ip2long($this->request->getClientAddress());
$option = Options::findFirstById($optionId);
$vote = Votes::count(array(
'conditions' => 'poll_id = ?0 and user_ip = ?1',
'bind' => array($option->poll_id, $user_ip),
));
/*
print_r(Votes::count(array(
'conditions' => 'option_id = ?0 and poll_id = ?1 and user_ip = ?2',
'bind' => array($option->id, $option->poll_id, $user_ip),
)));
exit;
*/
if ($vote < 1) {
$new_vote = new Votes();
$new_vote->id=0;
$new_vote->poll_id = $option->poll_id;
$new_vote->option_id = $option->id;
$new_vote->user_ip = $user_ip;
$new_vote->time = time();
$new_vote->save();
$option->count++;
$option->save();
// return $this->response->redirect('poll/show/' . $option->poll_id);
} else {
$this->view->error = 'Вы уже голосовали в этом голосовании'; // O_O
}
return $this->dispatcher->forward(array(
'action' => 'show',
'params' => array($option->poll_id)
));
}
}