php class PollController extends Phalcon Mvc Controller public functio

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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)
));
}
}