summaryrefslogtreecommitdiff
path: root/plugins/Group_assign/Model/GroupAssignCalendarModel.php
blob: 5b45ca09ad671900aaedc019e377fd1ceff4d32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace Kanboard\Plugin\Group_assign\Model;

use DateTime;
use Kanboard\Model\GroupMemberModel;
use Kanboard\Plugin\Group_assign\Model\MultiselectMemberModel;
use Kanboard\Model\TimezoneModel;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\ColorModel;
use Kanboard\Core\Base;

/**
 * Group_assign Calendar Model
 *
 * @package  Kanboard\Plugin\Group_assign
 * @author   Craig Crosby
 */
class GroupAssignCalendarModel extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'tasks';
    /**
     * Get query to fetch all users
     *
     * @access public
     * @param  integer $group_id
     * @return \PicoDb\Table
     */
    public function getUserCalendarEvents($user_id, $start, $end)
    {
        $getMS_Ids = $this->db->table(MultiselectMemberModel::TABLE)
            ->eq('user_id', $user_id)
            ->findAllByColumn('group_id');

        $getGr_Ids = $this->db->table(GroupMemberModel::TABLE)
            ->eq('user_id', $user_id)
            ->findAllByColumn('group_id');

         $tasks = $this->db->table(self::TABLE)
            ->beginOr()
            ->eq('owner_id', $user_id)
            ->in('owner_gp', $getGr_Ids)
            ->in('owner_ms', $getMS_Ids)
            ->closeOr()
            ->gte('date_due', strtotime($start))
            ->lte('date_due', strtotime($end))
                           ->neq('is_active', 0);

         $tasks = $tasks->findAll();

         $events = array();

         foreach ($tasks as $task) {

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

         $endDate = new DateTime();
         $endDate->setTimestamp($task['date_due']);

         if ($startDate->getTimestamp() == 0) { $startDate = $endDate; }

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

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

         return $events;
    }

}