Add a 'due date' field and display the number of comments on the board

This commit is contained in:
Frédéric Guillot
2014-03-05 22:27:48 -05:00
parent 1e994f3448
commit 544f992424
15 changed files with 195 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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();

View File

@@ -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(

View File

@@ -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(