Skip to content

Commit

Permalink
Merge pull request #8 from eSkiSo/add-cal-events
Browse files Browse the repository at this point in the history
Add cal events
  • Loading branch information
creecros committed Dec 6, 2018
2 parents 877f493 + 1f7648d commit a09ec61
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
120 changes: 120 additions & 0 deletions Model/SubtaskCalendarModel.php
@@ -0,0 +1,120 @@
<?php

namespace Kanboard\Plugin\Subtaskdate\Model;

use DateTime;
use Kanboard\Model\TimezoneModel;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Core\Base;

/**
* Subtask Calendar Model
*
* @package Kanboard\Plugin\Subtaskdate
* @author Craig Crosby
*/
class SubtaskCalendarModel extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'subtasks';
/**
* Get query to fetch all users
*
* @access public
* @param integer $group_id
* @return \PicoDb\Table
*/
public function getUserCalendarEvents($user_id, $start, $end)
{
$tasks = $this->db->table(self::TABLE)
->eq('user_id', $user_id)
->gte('due_date', strtotime($start))
->lte('due_date', strtotime($end))
->neq('status', 2)
->columns('task_id', 'title', 'due_date')
->findAll();

$events = array();

foreach ($tasks as $task) {

$fulltask = $this->taskFinderModel->getById($task['task_id']);

$startDate = new DateTime();
$startDate->setTimestamp($task['due_date']);

$allDay = $startDate == $startDate && $startDate->format('Hi') == '0000';
$format = $allDay ? 'Y-m-d' : 'Y-m-d\TH:i:s';

$events[] = array(
'timezoneParam' => $this->timezoneModel->getCurrentTimezone(),
'id' => $task['task_id'],
'title' => t('#%d', $task['task_id']).' '.$task['title'],
'backgroundColor' => $this->colorModel->getBackgroundColor('grey'),
'borderColor' => $this->colorModel->getBorderColor('red'),
'textColor' => 'black',
'url' => $this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['task_id'], 'project_id' => $fulltask['project_id'])),
'start' => $startDate->format($format),
'end' => $startDate->format($format),
'editable' => $allDay,
'allday' => $allDay,
);
}

return $events;
}

public function getProjectCalendarEvents($project_id, $start, $end)
{
$alltasks = $this->taskFinderModel->getAllIds($project_id);

$tasks = array();

foreach ($alltasks as $lonetask) {

$foundtasks = $this->db->table(self::TABLE)
->eq('task_id', $lonetask['id'])
->gte('due_date', strtotime($start))
->lte('due_date', strtotime($end))
->neq('status', 2)
->columns('task_id', 'title', 'due_date')
->findAll();

$tasks = array_merge($tasks, $foundtasks);

}

$events = array();

foreach ($tasks as $task) {

$fulltask = $this->taskFinderModel->getById($task['task_id']);

$startDate = new DateTime();
$startDate->setTimestamp($task['due_date']);

$allDay = $startDate == $startDate && $startDate->format('Hi') == '0000';
$format = $allDay ? 'Y-m-d' : 'Y-m-d\TH:i:s';

$events[] = array(
'timezoneParam' => $this->timezoneModel->getCurrentTimezone(),
'id' => $task['task_id'],
'title' => t('#%d', $task['task_id']).' '.$task['title'],
'backgroundColor' => $this->colorModel->getBackgroundColor('grey'),
'borderColor' => $this->colorModel->getBorderColor('red'),
'textColor' => 'black',
'url' => $this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['task_id'], 'project_id' => $fulltask['project_id'])),
'start' => $startDate->format($format),
'end' => $startDate->format($format),
'editable' => $allDay,
'allday' => $allDay,
);
}

return $events;
}
}
17 changes: 16 additions & 1 deletion Plugin.php
Expand Up @@ -7,6 +7,7 @@
use Kanboard\Model\TaskModel;
use Kanboard\Plugin\Subtaskdate\Filter\SubTaskDueDateFilter;
use Kanboard\Model\SubtaskModel;
use Kanboard\Plugin\Subtaskdate\Model\SubtaskCalendarModel;
use Kanboard\Plugin\Subtaskdate\Api\Procedure\NewSubtaskProcedure;
use PicoDb\Table;
use PicoDb\Database;
Expand Down Expand Up @@ -47,6 +48,20 @@ public function initialize()
// API
$this->api->getProcedureHandler()->withClassAndMethod('createSubtaskdd', new NewSubtaskProcedure($this->container), 'createSubtaskdd');
$this->api->getProcedureHandler()->withClassAndMethod('updateSubtaskdd', new NewSubtaskProcedure($this->container), 'updateSubtaskdd');

//Events
$container = $this->container;

$this->hook->on('controller:calendar:user:events', function($user_id, $start, $end) use ($container) {
$model = new SubtaskCalendarModel($container);
return $model->getUserCalendarEvents($user_id, $start, $end); // Return new events
});

$this->hook->on('controller:calendar:project:events', function($project_id, $start, $end) use ($container) {
$model = new SubtaskCalendarModel($container);
return $model->getProjectCalendarEvents($project_id, $start, $end); // Return new events
});

}
public function onStartup()
{
Expand Down Expand Up @@ -81,7 +96,7 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.0.5';
return '1.1.0';
}

public function getPluginHomepage()
Expand Down

0 comments on commit a09ec61

Please sign in to comment.