summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
authorDracony <draconyster@gmail.com>2013-01-18 18:53:33 +0200
committerDracony <draconyster@gmail.com>2013-01-18 18:53:33 +0200
commit65a7f45a570dde73e1ca1fefc1af15491772e8fc (patch)
tree104be63aacdbb8683358e79b8475d0c307cd0fb2 /application
parent0a162ebe5ca518754fbe4668db39758cacbd5d4b (diff)
downloadPHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.zip
PHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.tar.gz
PHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.tar.bz2
some testing
Diffstat (limited to 'application')
-rw-r--r--application/classes/controller/home.php7
-rw-r--r--application/classes/controller/polls.php59
-rw-r--r--application/classes/model/option.php12
-rw-r--r--application/classes/model/poll.php13
-rw-r--r--application/views/add.php34
-rw-r--r--application/views/index.php16
-rw-r--r--application/views/main.php18
-rw-r--r--application/views/poll.php32
-rw-r--r--application/views/single.php1
9 files changed, 188 insertions, 4 deletions
diff --git a/application/classes/controller/home.php b/application/classes/controller/home.php
index 7ec6e86..2e353a0 100644
--- a/application/classes/controller/home.php
+++ b/application/classes/controller/home.php
@@ -2,10 +2,9 @@
class Home_Controller extends Controller {
public function action_index(){
- foreach(ORM::factory('fairy')->with ('tree.protector','tree.flower.protector')->find_all() as $fairy) {
- echo $fairy->tree->protector->name;
-
- }
+ $view = View::get('home');
+ $view->message = 'Have fun coding!';
+ $this->response->body=$view->render();
}
}
diff --git a/application/classes/controller/polls.php b/application/classes/controller/polls.php
new file mode 100644
index 0000000..c15be88
--- /dev/null
+++ b/application/classes/controller/polls.php
@@ -0,0 +1,59 @@
+<?php
+class Polls_Controller extends Controller {
+
+ public $view;
+ public $template;
+
+ public function before() {
+ $this->view = View::get('main');
+ $this->template=$this->request->param('action');
+ }
+ public function action_index(){
+ $this->view->polls = ORM::factory('poll')->find_all();
+ }
+
+ public function action_poll() {
+
+ if ($this->request->method == 'POST') {
+ $option_id = $this->request->post('option');
+ $option = ORM::factory('option')->where('id', $option_id)->find();
+ $option->votes += 1;
+ $option->save();
+ $this->response-> redirect('/polls/poll/'.$option->poll->id);
+ $this->execute=false;
+ return;
+ }
+
+ $id=$this->request->param('id');
+ $this->view->poll = ORM::factory('poll')->where('id', $id)->find();
+
+ }
+
+ public function action_add(){
+ if ($this->request->method == 'POST') {
+ $poll = ORM::factory('poll');
+ $poll->question = $this->request->post('question');
+ $poll->save();
+ foreach($this->request->post('options') as $text) {
+ if (empty($text))
+ continue;
+ $option = ORM::factory('option');
+ $option->text = $text;
+ $option->save();
+ $poll->add('options',$option);
+ }
+ $this->response->redirect('/polls/');
+ return;
+ }
+
+ $this->template='add';
+ }
+
+ public function after() {
+ $this->view->template=Misc::find_file('views',$this->template);
+ $this->response->body=$this->view->render();
+ }
+
+
+}
+?> \ No newline at end of file
diff --git a/application/classes/model/option.php b/application/classes/model/option.php
new file mode 100644
index 0000000..b143913
--- /dev/null
+++ b/application/classes/model/option.php
@@ -0,0 +1,12 @@
+<?php
+class Option_Model extends ORM{
+ public $belongs_to = array('poll');
+
+ public function get($property) {
+ if ($property == 'percent') {
+ if($this->poll->total_votes==0)
+ return 0;
+ return floor($this->votes/$this->poll->total_votes*100);
+ }
+ }
+} \ No newline at end of file
diff --git a/application/classes/model/poll.php b/application/classes/model/poll.php
new file mode 100644
index 0000000..a2cfe08
--- /dev/null
+++ b/application/classes/model/poll.php
@@ -0,0 +1,13 @@
+<?php
+class Poll_Model extends ORM{
+ public $has_many=array('options');
+
+ public function get($property){
+ if ($property == 'total_votes') {
+ $total=0;
+ foreach($this->options->find_all() as $option)
+ $total += $option->votes;
+ return $total;
+ }
+ }
+} \ No newline at end of file
diff --git a/application/views/add.php b/application/views/add.php
new file mode 100644
index 0000000..3fda246
--- /dev/null
+++ b/application/views/add.php
@@ -0,0 +1,34 @@
+<script>
+ $(function(){
+ $('#addOption').click(function(evt){
+ evt.preventDefault();
+
+ var newOption=$('.option:eq(0)').clone();
+ newOption.find('input').val('').attr('placeholder','Option #'+($('.option').length+1));
+ $('#options').append(newOption);
+ })
+ })
+</script>
+<form method="POST">
+<fieldset>
+ <legend>Add a new poll</legend>
+ <label>Question</label>
+ <input type="text" placeholder="Type your question here..." name="question" />
+ <label>Options</label>
+ <div id="options">
+ <div class="option">
+ <input type="text" name="options[]" placeholder="Option #1" />
+ </div>
+ <div class="option">
+ <input type="text" name="options[]" placeholder="Option #2" />
+ </div>
+ <div class="option">
+ <input type="text" name="options[]" placeholder="Option #3" />
+ </div>
+ </div>
+ <button class="btn" id="addOption"><i class="icon-plus"></i> Add option</button>
+ <button class="btn btn-primary"><i class="icon-ok icon-white"></i> Save poll </button>
+
+</fieldset>
+</form>
+<a class="btn btn-link" href="/polls">&lt; Back to polls</a> \ No newline at end of file
diff --git a/application/views/index.php b/application/views/index.php
new file mode 100644
index 0000000..5ba6d8d
--- /dev/null
+++ b/application/views/index.php
@@ -0,0 +1,16 @@
+<style>
+ .muted{
+ float:right;
+ }
+</style>
+<ul class="nav nav-tabs nav-stacked ">
+ <?php foreach($polls as $poll):?>
+ <li>
+ <a href="<?php echo "/polls/poll/{$poll->id}"; ?>" >
+ <?echo $poll->question;?>
+ <div class="muted"><?php echo $poll->total_votes; ?> Votes</div>
+ </a>
+ </li>
+ <?php endforeach;?>
+</ul>
+<a class="btn btn-block" href="/polls/add"><i class="icon-plus"></i> Add poll</a> \ No newline at end of file
diff --git a/application/views/main.php b/application/views/main.php
new file mode 100644
index 0000000..96cdbf3
--- /dev/null
+++ b/application/views/main.php
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Add a new poll</title>
+ <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
+ </head>
+ <body>
+ <div class="container">
+ <div class="span4"></div>
+ <div class="span4">
+ <h2>PHPixie Polls</h2>
+ <?php include($template);?>
+ </div>
+ <div class="span4"></div>
+ </div>
+ </body>
+</html> \ No newline at end of file
diff --git a/application/views/poll.php b/application/views/poll.php
new file mode 100644
index 0000000..3eaadae
--- /dev/null
+++ b/application/views/poll.php
@@ -0,0 +1,32 @@
+<style>
+ form{
+ margin-bottom:0px;
+ }
+
+ .filled{
+ background:#08C;
+ height:20px;
+ }
+ .bar{
+ width:100px;
+ }
+</style>
+<h3><?php echo $poll->question; ?></h3>
+<table class="table">
+ <?php foreach($poll->options->find_all() as $option):?>
+ <tr>
+ <td><?php echo $option->text;?></td>
+ <td><?php echo $option->votes;?></td>
+ <td class="bar">
+ <div class="filled" style="width:<?php echo $option->percent;?>%;"></div>
+ </td>
+ <td>
+ <form method="POST">
+ <input type="hidden" name="option" value="<?php echo $option->id; ?>" />
+ <button class="btn btn-mini">Vote</button>
+ </form>
+ </td>
+ </tr>
+ <?php endforeach;?>
+</table>
+<a class="btn btn-link" href="/polls">&lt; Back to polls</a> \ No newline at end of file
diff --git a/application/views/single.php b/application/views/single.php
new file mode 100644
index 0000000..d02deaa
--- /dev/null
+++ b/application/views/single.php
@@ -0,0 +1 @@
+<h3><?php echo $poll->question; ?></h3> \ No newline at end of file