summaryrefslogtreecommitdiff
path: root/autoload/Todo/Model
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-19 22:45:31 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-19 22:45:31 +0100
commitefbdc3bbd6cba8691391ec2192ea96adbfb8c029 (patch)
treebe6a69fab84d15d9fc1f8a50a02e8d57e9388c3b /autoload/Todo/Model
downloadtodo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.tar.gz
todo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.tar.bz2
todo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.zip
Initial commit
Diffstat (limited to 'autoload/Todo/Model')
-rw-r--r--autoload/Todo/Model/TodoList.php100
-rw-r--r--autoload/Todo/Model/User.php66
2 files changed, 166 insertions, 0 deletions
diff --git a/autoload/Todo/Model/TodoList.php b/autoload/Todo/Model/TodoList.php
new file mode 100644
index 0000000..4470c34
--- /dev/null
+++ b/autoload/Todo/Model/TodoList.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Todo\Model;
+
+class TodoList extends \DB\SQL\Mapper {
+ public function __construct($db) {
+ parent::__construct($db, 'lists');
+ }
+
+ public function byID($id) {
+ $this->load(array('id=?', $id));
+ return !$this->dry();
+ }
+
+ public function itemsToArray() {
+ $items = array();
+ $it = new \DB\SQL\Mapper($this->db, 'items');
+ for($it->load(array('list=?', $this->id), array('order' => 'ord ASC')); !$it->dry(); $it->next()) {
+ $items[] = array(
+ 'id' => $it->id,
+ 'ord' => $it->ord,
+ 'text' => $it->text,
+ 'date' => $it->date,
+ 'checked' => $it->checked,
+ );
+ }
+
+ return $items;
+ }
+
+ public function countItems() {
+ $it = new \DB\SQL\Mapper($this->db, 'items');
+ return $it->count(array('list=?', $this->id));
+ }
+
+ public function getItem($id) {
+ $it = new \DB\SQL\Mapper($this->db, 'items');
+ $it->load(array('list=? AND id=?', $this->id, $id));
+ return $it->dry() ? NULL : $it;
+ }
+
+ public function moveItem($id, $movement) {
+ $it = new \DB\SQL\Mapper($this->db, 'items');
+ $it->load(array('list=? AND id=?', $this->id, $id));
+ if($it->dry()) {
+ return;
+ }
+ $ordOld = $it->ord;
+ $it->reset();
+
+ $ordNew = min(max(0, $ordOld + $movement), $this->countItems() - 1);
+ if($ordNew === $ordOld) {
+ return;
+ }
+
+ $moveSgn = ($movement > 0) ? 1 : -1;
+
+ if($moveSgn === 1) {
+ $it->load(array('list=? AND ord>? AND ord<=?', $this->id, $ordOld, $ordNew));
+ } else {
+ $it->load(array('list=? AND ord>=? AND ord<?', $this->id, $ordNew, $ordOld));
+ }
+ for(;!$it->dry(); $it->next()) {
+ $it->ord -= $moveSgn;
+ $it->save();
+ }
+
+ $it->reset();
+ $it->load(array('list=? AND id=?', $this->id, $id));
+ $it->ord = $ordNew;
+ $it->save();
+ }
+
+ public function addItem($text) {
+ $n = $this->countItems();
+
+ $now = new \DateTime('now');
+
+ $it = new \DB\SQL\Mapper($this->db, 'items');
+ $it->text = $text;
+ $it->ord = $n;
+ $it->date = $now->format('Y-m-d H:i:s');
+ $it->checked = false;
+ $it->list = $this->id;
+ $it->save();
+ }
+
+ public function delItem($id) {
+ $this->db->exec('DELETE FROM `items` WHERE `list` = :l AND `id` = :i', array(':l' => $this->id, ':i' => $id));
+ }
+
+ public function deleteList() {
+ $this->db->exec('DELETE FROM `items` WHERE `list` = :l', array(':l' => $this->id));
+ $this->erase();
+ }
+
+ public function deleteChecked() {
+ $this->db->exec('DELETE FROM `items` WHERE `list` = :l AND `checked` = :c', array(':l' => $this->id, ':c' => true));
+ }
+} \ No newline at end of file
diff --git a/autoload/Todo/Model/User.php b/autoload/Todo/Model/User.php
new file mode 100644
index 0000000..53c8f67
--- /dev/null
+++ b/autoload/Todo/Model/User.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Todo\Model;
+
+class User extends \DB\SQL\Mapper {
+ public function __construct(\DB\SQL $db) {
+ parent::__construct($db, 'users');
+ }
+
+ public function makeCode() {
+ $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
+ $len = strlen($alphabet);
+ $this->code = '';
+ for($i = 0; $i < 16; $i++) {
+ $this->code .= substr($alphabet, mt_rand(0, $len-1), 1);
+ }
+ }
+
+ public function register($name, $email, $password) {
+ $this->load(array('name=? OR email=?', $name, $email));
+ if(!$this->dry()) {
+ $this->reset();
+ return false;
+ }
+
+ $this->name = $name;
+ $this->email = $email;
+ $this->pwhash = \Bcrypt::instance()->hash($password);
+ $this->active = false;
+ $this->makeCode();
+ $this->save();
+ return true;
+ }
+
+ public function byName($name) {
+ $this->load(array('name=?', $name));
+ return !$this->dry();
+ }
+
+ public function byID($id) {
+ $this->load(array('id=?', $id));
+ return !$this->dry();
+ }
+
+ public function byEmail($email) {
+ $this->load(array('email=?', $email));
+ return !$this->dry();
+ }
+
+ public function verifyPass($password) {
+ return \Bcrypt::instance()->verify($password, $this->pwhash);
+ }
+
+ public function deleteUser() {
+ $this->db->exec('DELETE FROM `items` WHERE `list` IN (SELECT `id` FROM `lists` WHERE `user` = :u)', array(':u' => $this->id));
+ $this->db->exec('DELETE FROM `lists` WHERE `user` = :u', array(':u' => $this->id));
+ $this->erase();
+ }
+
+ # Returns a TodoList object that can be iterated with ->next().
+ public function lists() {
+ $l = new \Todo\Model\TodoList($this->db);
+ $l->load(array('user=?', $this->id), array('order' => 'name ASC'));
+ return $l;
+ }
+} \ No newline at end of file