summaryrefslogtreecommitdiff
path: root/app/Model/Column.php
blob: 286b6140f80d5658629451d0c437845340281b9a (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
<?php

namespace Kanboard\Model;

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

    /**
     * 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();
    }

    /**
     * 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);
    }
}