summaryrefslogtreecommitdiff
path: root/app/Model/TaskTagModel.php
blob: 0553cc6cda5f5ae30457ab13590f1465aa01873d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;

/**
 * Class TaskTagModel
 *
 * @package Kanboard\Model
 * @author  Frederic Guillot
 */
class TaskTagModel extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'task_has_tags';

    /**
     * Get all tags not available in a project
     *
     * @access public
     * @param  integer $task_id
     * @param  integer $project_id
     * @return array
     */
    public function getTagIdsByTaskNotAvailableInProject($task_id, $project_id)
    {
        return $this->db->table(TagModel::TABLE)
            ->eq(self::TABLE.'.task_id', $task_id)
            ->notIn(TagModel::TABLE.'.project_id', array(0, $project_id))
            ->join(self::TABLE, 'tag_id', 'id')
            ->findAllByColumn(TagModel::TABLE.'.id');
    }

    /**
     * Get all tags associated to a task
     *
     * @access public
     * @param  integer $task_id
     * @return array
     */
    public function getTagsByTask($task_id)
    {
        return $this->db->table(TagModel::TABLE)
            ->columns(TagModel::TABLE.'.id', TagModel::TABLE.'.name')
            ->eq(self::TABLE.'.task_id', $task_id)
            ->join(self::TABLE, 'tag_id', 'id')
            ->findAll();
    }

    /**
     * Get all tags associated to a list of tasks
     *
     * @access public
     * @param  integer[] $task_ids
     * @return array
     */
    public function getTagsByTasks($task_ids)
    {
        if (empty($task_ids)) {
            return array();
        }

        $tags = $this->db->table(TagModel::TABLE)
            ->columns(TagModel::TABLE.'.id', TagModel::TABLE.'.name', self::TABLE.'.task_id')
            ->in(self::TABLE.'.task_id', $task_ids)
            ->join(self::TABLE, 'tag_id', 'id')
            ->findAll();

        return array_column_index($tags, 'task_id');
    }

    /**
     * Get dictionary of tags
     *
     * @access public
     * @param  integer $task_id
     * @return array
     */
    public function getList($task_id)
    {
        $tags = $this->getTagsByTask($task_id);
        return array_column($tags, 'name', 'id');
    }

    /**
     * Add or update a list of tags to a task
     *
     * @access public
     * @param  integer  $project_id
     * @param  integer  $task_id
     * @param  string[] $tags
     * @return boolean
     */
    public function save($project_id, $task_id, array $tags)
    {
        $task_tags = $this->getList($task_id);
        $tags = array_filter($tags);

        return $this->associateTags($project_id, $task_id, $task_tags, $tags) &&
            $this->dissociateTags($task_id, $task_tags, $tags);
    }

    /**
     * Associate a tag to a task
     *
     * @access public
     * @param  integer  $task_id
     * @param  integer  $tag_id
     * @return boolean
     */
    public function associateTag($task_id, $tag_id)
    {
        return $this->db->table(self::TABLE)->insert(array(
            'task_id' => $task_id,
            'tag_id' => $tag_id,
        ));
    }

    /**
     * Dissociate a tag from a task
     *
     * @access public
     * @param  integer  $task_id
     * @param  integer  $tag_id
     * @return boolean
     */
    public function dissociateTag($task_id, $tag_id)
    {
        return $this->db->table(self::TABLE)
            ->eq('task_id', $task_id)
            ->eq('tag_id', $tag_id)
            ->remove();
    }

    /**
     * Associate missing tags
     *
     * @access protected
     * @param  integer  $project_id
     * @param  integer  $task_id
     * @param  array    $task_tags
     * @param  string[] $tags
     * @return bool
     */
    protected function associateTags($project_id, $task_id, $task_tags, $tags)
    {
        foreach ($tags as $tag) {
            $tag_id = $this->tagModel->findOrCreateTag($project_id, $tag);

            if (! isset($task_tags[$tag_id]) && ! $this->associateTag($task_id, $tag_id)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Dissociate removed tags
     *
     * @access protected
     * @param  integer  $task_id
     * @param  array    $task_tags
     * @param  string[] $tags
     * @return bool
     */
    protected function dissociateTags($task_id, $task_tags, $tags)
    {
        foreach ($task_tags as $tag_id => $tag) {
            if (! in_array($tag, $tags)) {
                if (! $this->dissociateTag($task_id, $tag_id)) {
                    return false;
                }
            }
        }

        return true;
    }
}