summaryrefslogtreecommitdiff
path: root/plugins/kolab_tags/lib/kolab_tags_backend.php
blob: 032215dbd7c66ef7d2bdbb3488a9b16fd33b8652 (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
<?php

/**
 * Kolab Tags backend
 *
 * @author Aleksander Machniak <machniak@kolabsys.com>
 *
 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

class kolab_tags_backend
{
    private $tag_cols = array('name', 'category', 'color', 'parent', 'iconName', 'priority', 'members');

    const O_TYPE     = 'relation';
    const O_CATEGORY = 'tag';


    /**
     * Tags list
     *
     * @param array $filter Search filter
     *
     * @return array List of tags
     */
    public function list_tags($filter = array())
    {
        $config     = kolab_storage_config::get_instance();
        $default    = true;
        $filter[]   = array('type', '=', self::O_TYPE);
        $cat_filter = array('category' => self::O_CATEGORY);

        return $config->get_objects($filter, $default, $cat_filter);
    }

    /**
     * Create tag object
     *
     * @param array $tag Tag data
     *
     * @return boolean|array Tag data on success, False on failure
     */
    public function create($tag)
    {
        $config = kolab_storage_config::get_instance();
        $tag    = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
        $tag['category'] = self::O_CATEGORY;

        // Create the object
        $result = $config->save($tag, self::O_TYPE);

        return $result ? $tag : false;
    }

    /**
     * Update tag object
     *
     * @param array $tag Tag data
     *
     * @return boolean|array Tag data on success, False on failure
     */
    public function update($tag)
    {
        // get tag object data, we need _mailbox
        $list    = $this->list_tags(array(array('uid', '=', $tag['uid'])));
        $old_tag = $list[0];

        if (!$old_tag) {
            return false;
        }

        $config = kolab_storage_config::get_instance();
        $tag    = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
        $tag    = array_merge($old_tag, $tag);

        // Update the object
        $result = $config->save($tag, self::O_TYPE, $tag['uid']);

        return $result ? $tag : false;
    }

    /**
     * Remove tag object
     *
     * @param string $uid Object unique identifier
     *
     * @return boolean True on success, False on failure
     */
    public function remove($uid)
    {
        $config = kolab_storage_config::get_instance();

        return $config->delete($uid);
    }
}