summaryrefslogtreecommitdiffstats
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
parent0a162ebe5ca518754fbe4668db39758cacbd5d4b (diff)
downloadPHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.zip
PHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.tar.gz
PHPixie-65a7f45a570dde73e1ca1fefc1af15491772e8fc.tar.bz2
some testing
-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
-rw-r--r--modules/database/classes/driver/mysql/db.php11
-rw-r--r--modules/database/classes/driver/mysql/result.php3
-rw-r--r--modules/orm/classes/orm.php91
12 files changed, 242 insertions, 55 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
diff --git a/modules/database/classes/driver/mysql/db.php b/modules/database/classes/driver/mysql/db.php
index ab0a3bb..f53d618 100644
--- a/modules/database/classes/driver/mysql/db.php
+++ b/modules/database/classes/driver/mysql/db.php
@@ -42,12 +42,15 @@ class DB_Mysql_Driver extends DB{
*
* @param string $table Name of the table to get columns from
* @return array Array of column names
+ * @throw Exception if table doesn't exist
* @access public
*/
public function list_columns($table) {
$columns=array();
$table_desc = $this->execute("DESCRIBE `$table`");
-
+ Debug::log($table_desc);
+ if (!$table_desc->valid())
+ throw new Exception("Table '{$table}' doesn't exist");
foreach($table_desc as $column)
$columns[] = $column->Field;
@@ -105,9 +108,7 @@ class DB_Mysql_Driver extends DB{
}
$cursor->execute();
$res = $cursor->get_result();
- if (is_object($res)){
- $res=new Result_Mysql_Driver($res);
- }
- return $res;
+ return new Result_Mysql_Driver($res);
+
}
} \ No newline at end of file
diff --git a/modules/database/classes/driver/mysql/result.php b/modules/database/classes/driver/mysql/result.php
index a461c81..f058a97 100644
--- a/modules/database/classes/driver/mysql/result.php
+++ b/modules/database/classes/driver/mysql/result.php
@@ -16,7 +16,8 @@ class Result_Mysql_Driver extends Result_Database {
*/
public function __construct($result) {
$this->_result = $result;
- $this->_row=$this->_result->fetch_object();
+ if(!empty($result))
+ $this->_row=$this->_result->fetch_object();
}
/**
diff --git a/modules/orm/classes/orm.php b/modules/orm/classes/orm.php
index 7d36ad5..2c948ce 100644
--- a/modules/orm/classes/orm.php
+++ b/modules/orm/classes/orm.php
@@ -204,52 +204,54 @@ class ORM {
*/
public function find_all() {
$paths = array();
- $fields = array();
- $this_alias=$this->query->last_alias();
- foreach($this->columns() as $column)
- $fields[]=array("{$this_alias}.{$column}","{$this_alias}__{$column}");
- foreach($this->_with as $target) {
- $model = $this;
- $model_alias=$this_alias;
- $rels = explode('.', $target);
- foreach($rels as $key => $rel_name) {
- $path = implode('.', array_slice($rels, 0, $key + 1));
- if (isset($paths[$path])) {
- $model = $paths[$path]['model'];
- $model_alias=$paths[$path]['alias'];
- continue;
- }
- $alias=$this->query->add_alias();
- $model_rels = array_merge($model->has_one, $model->has_many,$model->belongs_to);
- $rel = Misc::arr($model_rels, $rel_name, false);
-
- if (!$rel)
- throw new Exception("Model '{$model->model_name}' doesn't have a '{$rel_name}' relation defined");
- if ($rel['type'] == 'has_many')
- throw new Exception("Relationship '{$rel_name}' is of has_many type and cannot be preloaded view with()");
- $rel_model = ORM::factory($rel['model']);
-
- if ($rel['type'] == 'belongs_to') {
- $this->query->join(array($rel_model->table, $alias), array(
- $model_alias.'.'.$rel['key'],
- $alias.'.'.$rel_model->id_field,
- ),'left');
- }else {
- $this->query->join(array($rel_model->table, $alias), array(
- $model_alias.'.'.$model->id_field,
- $alias.'.'.$rel['key'],
- ), 'left');
+ if(!empty($this->_with)){
+ $fields = array();
+ $this_alias=$this->query->last_alias();
+ foreach($this->columns() as $column)
+ $fields[]=array("{$this_alias}.{$column}","{$this_alias}__{$column}");
+ foreach($this->_with as $target) {
+ $model = $this;
+ $model_alias=$this_alias;
+ $rels = explode('.', $target);
+ foreach($rels as $key => $rel_name) {
+ $path = implode('.', array_slice($rels, 0, $key + 1));
+ if (isset($paths[$path])) {
+ $model = $paths[$path]['model'];
+ $model_alias=$paths[$path]['alias'];
+ continue;
+ }
+ $alias=$this->query->add_alias();
+ $model_rels = array_merge($model->has_one, $model->has_many,$model->belongs_to);
+ $rel = Misc::arr($model_rels, $rel_name, false);
+
+ if (!$rel)
+ throw new Exception("Model '{$model->model_name}' doesn't have a '{$rel_name}' relation defined");
+ if ($rel['type'] == 'has_many')
+ throw new Exception("Relationship '{$rel_name}' is of has_many type and cannot be preloaded view with()");
+ $rel_model = ORM::factory($rel['model']);
+
+ if ($rel['type'] == 'belongs_to') {
+ $this->query->join(array($rel_model->table, $alias), array(
+ $model_alias.'.'.$rel['key'],
+ $alias.'.'.$rel_model->id_field,
+ ),'left');
+ }else {
+ $this->query->join(array($rel_model->table, $alias), array(
+ $model_alias.'.'.$model->id_field,
+ $alias.'.'.$rel['key'],
+ ), 'left');
+ }
+
+ foreach($rel_model->columns() as $column)
+ $fields[]=array("{$alias}.{$column}","{$alias}__{$column}");
+ $model = $rel_model;
+ $model_alias = $alias;
+ $paths[$path] = array('alias' => $alias, 'model' => $model);
}
-
- foreach($rel_model->columns() as $column)
- $fields[]=array("{$alias}.{$column}","{$alias}__{$column}");
- $model = $rel_model;
- $model_alias = $alias;
- $paths[$path] = array('alias' => $alias, 'model' => $model);
}
+
+ call_user_func_array(array($this->query, 'fields'), $fields);
}
-
- call_user_func_array(array($this->query,'fields'),$fields);
return new ORMResult(get_class($this), $res=$this->query->execute(),$paths);
}
@@ -347,7 +349,6 @@ class ORM {
return $val;
}
$relations = array_merge($this->has_one, $this->has_many, $this->belongs_to);
-
if ($target = Misc::arr($relations, $column, false)) {
$model = ORM::factory($target['model']);
$model->query = clone $this->query;
@@ -380,7 +381,7 @@ class ORM {
), 'inner');
}
}
- $model->query->fields(array("$new_alias.*"));
+ $model->query->fields("$new_alias.*");
if ($target['type'] != 'has_many' && $this->loaded() ) {
$model = $model->find();
$this->cached[$column]=$model;