summaryrefslogtreecommitdiff
path: root/app/Model/ColumnModel.php
blob: 5498ef542271b0a2aa15ff6a564b68c1392f3f79 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;

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

    /**
     * Get a column by the id
     *
     * @access public
     * @param  integer  $column_id    Column id
     * @return array
     */
    public function getById($column_id)
    {
        return $this->db->table(self::TABLE)->eq('id', $column_id)->findOne();
    }

    /**
     * Get projectId by the columnId
     *
     * @access public
     * @param  integer  $column_id    Column id
     * @return integer
     */
    public function getProjectId($column_id)
    {
        return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('project_id');
    }

    /**
     * Get the first column id for a given project
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @return integer
     */
    public function getFirstColumnId($project_id)
    {
        return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findOneColumn('id');
    }

    /**
     * Get the last column id for a given project
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @return integer
     */
    public function getLastColumnId($project_id)
    {
        return $this->db->table(self::TABLE)->eq('project_id', $project_id)->desc('position')->findOneColumn('id');
    }

    /**
     * Get the position of the last column for a given project
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @return integer
     */
    public function getLastColumnPosition($project_id)
    {
        return (int) $this->db
                        ->table(self::TABLE)
                        ->eq('project_id', $project_id)
                        ->desc('position')
                        ->findOneColumn('position');
    }

    /**
     * Get a column id by the name
     *
     * @access public
     * @param  integer  $project_id
     * @param  string   $title
     * @return integer
     */
    public function getColumnIdByTitle($project_id, $title)
    {
        return (int) $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('title', $title)->findOneColumn('id');
    }

    /**
     * Get a column title by the id
     *
     * @access public
     * @param  integer  $column_id
     * @return integer
     */
    public function getColumnTitleById($column_id)
    {
        return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('title');
    }

    /**
     * Get all columns sorted by position for a given project
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @return array
     */
    public function getAll($project_id)
    {
        return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findAll();
    }

    /**
     * Get the list of columns sorted by position [ column_id => title ]
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @param  boolean  $prepend      Prepend a default value
     * @return array
     */
    public function getList($project_id, $prepend = false)
    {
        $listing = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'title');
        return $prepend ? array(-1 => t('All columns')) + $listing : $listing;
    }

    /**
     * Add a new column to the board
     *
     * @access public
     * @param  integer $project_id  Project id
     * @param  string  $title       Column title
     * @param  integer $task_limit  Task limit
     * @param  string  $description Column description
     * @param  integer $hide_in_dashboard
     * @return bool|int
     */
    public function create($project_id, $title, $task_limit = 0, $description = '', $hide_in_dashboard = 0)
    {
        $values = array(
            'project_id' => $project_id,
            'title' => $title,
            'task_limit' => intval($task_limit),
            'position' => $this->getLastColumnPosition($project_id) + 1,
            'hide_in_dashboard' => $hide_in_dashboard,
            'description' => $description,
        );

        return $this->db->table(self::TABLE)->persist($values);
    }

    /**
     * Update a column
     *
     * @access public
     * @param  integer   $column_id     Column id
     * @param  string    $title         Column title
     * @param  integer   $task_limit    Task limit
     * @param  string    $description   Optional description
     * @param  integer   $hide_in_dashboard
     * @return boolean
     */
    public function update($column_id, $title, $task_limit = 0, $description = '', $hide_in_dashboard = 0)
    {
        return $this->db->table(self::TABLE)->eq('id', $column_id)->update(array(
            'title' => $title,
            'task_limit' => intval($task_limit),
            'hide_in_dashboard' => $hide_in_dashboard,
            'description' => $description,
        ));
    }

    /**
     * Remove a column and all tasks associated to this column
     *
     * @access public
     * @param  integer  $column_id    Column id
     * @return boolean
     */
    public function remove($column_id)
    {
        return $this->db->table(self::TABLE)->eq('id', $column_id)->remove();
    }

    /**
     * Change column position
     *
     * @access public
     * @param  integer  $project_id
     * @param  integer  $column_id
     * @param  integer  $position
     * @return boolean
     */
    public function changePosition($project_id, $column_id, $position)
    {
        if ($position < 1 || $position > $this->db->table(self::TABLE)->eq('project_id', $project_id)->count()) {
            return false;
        }

        $column_ids = $this->db->table(self::TABLE)->eq('project_id', $project_id)->neq('id', $column_id)->asc('position')->findAllByColumn('id');
        $offset = 1;
        $results = array();

        foreach ($column_ids as $current_column_id) {
            if ($offset == $position) {
                $offset++;
            }

            $results[] = $this->db->table(self::TABLE)->eq('id', $current_column_id)->update(array('position' => $offset));
            $offset++;
        }

        $results[] = $this->db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position));

        return !in_array(false, $results, true);
    }
}