summaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorFrédéric Guillot <contact@fredericguillot.com>2014-03-05 22:27:48 -0500
committerFrédéric Guillot <contact@fredericguillot.com>2014-03-05 22:27:48 -0500
commit544f9924241f9b2caaf83ead203161ea41e5f1cf (patch)
treedcb5ba8a202fee673e395184795f570fd7c905ae /models
parent1e994f34486da72662ff39f1c3e130e4480e30ab (diff)
Add a 'due date' field and display the number of comments on the board
Diffstat (limited to 'models')
-rw-r--r--models/base.php18
-rw-r--r--models/comment.php8
-rw-r--r--models/schema.php5
-rw-r--r--models/task.php33
4 files changed, 60 insertions, 4 deletions
diff --git a/models/base.php b/models/base.php
index 2ecf4280..95c5b07f 100644
--- a/models/base.php
+++ b/models/base.php
@@ -12,13 +12,14 @@ require __DIR__.'/../vendor/SimpleValidator/Validators/Integer.php';
require __DIR__.'/../vendor/SimpleValidator/Validators/Equals.php';
require __DIR__.'/../vendor/SimpleValidator/Validators/AlphaNumeric.php';
require __DIR__.'/../vendor/SimpleValidator/Validators/GreaterThan.php';
+require __DIR__.'/../vendor/SimpleValidator/Validators/Date.php';
require __DIR__.'/../vendor/PicoDb/Database.php';
require __DIR__.'/schema.php';
abstract class Base
{
const APP_VERSION = 'master';
- const DB_VERSION = 8;
+ const DB_VERSION = 9;
private static $dbInstance = null;
protected $db;
@@ -59,4 +60,19 @@ abstract class Base
return hash('crc32b', $token);
}
+
+ public function getTimestampFromDate($value, $format)
+ {
+ $date = \DateTime::createFromFormat($format, $value);
+
+ if ($date !== false) {
+ $errors = \DateTime::getLastErrors();
+ if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
+ $timestamp = $date->getTimestamp();
+ return $timestamp > 0 ? $timestamp : 0;
+ }
+ }
+
+ return 0;
+ }
}
diff --git a/models/comment.php b/models/comment.php
index e0944249..9e5cf8fd 100644
--- a/models/comment.php
+++ b/models/comment.php
@@ -25,6 +25,14 @@ class Comment extends Base
->findAll();
}
+ public function count($task_id)
+ {
+ return $this->db
+ ->table(self::TABLE)
+ ->eq(self::TABLE.'.task_id', $task_id)
+ ->count();
+ }
+
public function create(array $values)
{
$values['date'] = time();
diff --git a/models/schema.php b/models/schema.php
index 2c3ba7e8..14604e60 100644
--- a/models/schema.php
+++ b/models/schema.php
@@ -2,6 +2,11 @@
namespace Schema;
+function version_9($pdo)
+{
+ $pdo->exec("ALTER TABLE tasks ADD COLUMN date_due INTEGER");
+}
+
function version_8($pdo)
{
$pdo->exec(
diff --git a/models/task.php b/models/task.php
index 77c620c8..017c7806 100644
--- a/models/task.php
+++ b/models/task.php
@@ -34,6 +34,7 @@ class Task extends Base
self::TABLE.'.description',
self::TABLE.'.date_creation',
self::TABLE.'.date_completed',
+ self::TABLE.'.date_due',
self::TABLE.'.color_id',
self::TABLE.'.project_id',
self::TABLE.'.column_id',
@@ -66,6 +67,7 @@ class Task extends Base
self::TABLE.'.description',
self::TABLE.'.date_creation',
self::TABLE.'.date_completed',
+ self::TABLE.'.date_due',
self::TABLE.'.color_id',
self::TABLE.'.project_id',
self::TABLE.'.column_id',
@@ -95,15 +97,23 @@ class Task extends Base
public function getAllByColumnId($project_id, $column_id, $status = array(1))
{
- return $this->db
+ $tasks = $this->db
->table(self::TABLE)
- ->columns('tasks.id', 'title', 'color_id', 'project_id', 'owner_id', 'column_id', 'position', 'score', 'users.username')
+ ->columns('tasks.id', 'title', 'color_id', 'project_id', 'owner_id', 'column_id', 'position', 'score', 'date_due', 'users.username')
->join('users', 'id', 'owner_id')
->eq('project_id', $project_id)
->eq('column_id', $column_id)
->in('is_active', $status)
->asc('position')
->findAll();
+
+ $commentModel = new Comment;
+
+ foreach ($tasks as &$task) {
+ $task['nb_comments'] = $commentModel->count($task['id']);
+ }
+
+ return $tasks;
}
public function countByColumnId($project_id, $column_id, $status = array(1))
@@ -122,10 +132,17 @@ class Task extends Base
unset($values['another_task']);
+ if (! empty($values['date_due'])) {
+ $values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
+ }
+
$values['date_creation'] = time();
$values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']);
- $this->db->table(self::TABLE)->save($values);
+ if (! $this->db->table(self::TABLE)->save($values)) {
+ $this->db->cancelTransaction();
+ return false;
+ }
$task_id = $this->db->getConnection()->getLastId();
@@ -136,9 +153,14 @@ class Task extends Base
public function update(array $values)
{
+ if (! empty($values['date_due'])) {
+ $values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
+ }
+
return $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values);
}
+ // Mark a task closed
public function close($task_id)
{
return $this->db->table(self::TABLE)
@@ -149,6 +171,7 @@ class Task extends Base
));
}
+ // Mark a task open
public function open($task_id)
{
return $this->db->table(self::TABLE)
@@ -159,11 +182,13 @@ class Task extends Base
));
}
+ // Remove a task
public function remove($task_id)
{
return $this->db->table(self::TABLE)->eq('id', $task_id)->remove();
}
+ // Move a task to another column or to another position
public function move($task_id, $column_id, $position)
{
return (bool) $this->db
@@ -184,6 +209,7 @@ class Task extends Base
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
+ new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
));
return array(
@@ -220,6 +246,7 @@ class Task extends Base
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
+ new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
));
return array(