aboutsummaryrefslogtreecommitdiff
path: root/lib/models/WidgetUser.php
blob: 84bb0c3b8992edb9112b551c2921c3812fa26d4f (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
<?php
/**
 * WidgetUser.php
 * model class for table widget_user
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @property int $id database column
 * @property int $pluginid database column
 * @property int $position database column
 * @property string $range_id database column
 * @property int $col database column
 * @property User $range belongs_to User
 */

class WidgetUser extends SimpleORMap
{
    /**
     * Configure the database mapping.
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'widget_user';

        $config['belongs_to']['range'] = [
            'class_name'  => User::class,
            'foreign_key' => 'range_id'
        ];

        parent::configure($config);
    }

    /**
     * Store the default layout for the specified user.
     *
     * @param string $user_id
     */
    public static function setInitialWidgets($user_id): void
    {
        if (self::countBySQL('range_id = ?', [$user_id]) === 0) {
            $stmt = DBManager::get()->prepare(
                'INSERT INTO widget_user (pluginid, position, range_id, col)
                 SELECT pluginid, position, :user_id, col FROM widget_default WHERE perm = :perm'
            );
            $stmt->execute([
                'user_id' => $user_id,
                'perm' => $GLOBALS['perm']->get_perm($user_id)
            ]);
        }
    }

    /**
     * Return the list of widgets (by column) shown to this user.
     *
     * @param string $user_id
     *
     * @return array array of columns with widget ids
     */
    public static function getWidgets($user_id): array
    {
        $widgets = self::findBySQL('range_id = ? ORDER BY position', [$user_id]);
        $result = [];

        foreach ($widgets as $widget) {
            $result[$widget->col][] = $widget->pluginid;
        }

        if (empty($result)) {
            $result = WidgetDefault::getWidgets($GLOBALS['perm']->get_perm($user_id));
        }

        return $result;
    }

    /**
     * Return whether the user has a certain widget enabled.
     *
     * @param string $user_id
     * @param int    $plugin_id
     *
     * @return bool
     */
    public static function hasWidget($user_id, $plugin_id): bool
    {
        $widgets = self::getWidgets($user_id);

        return in_array($plugin_id, array_merge(...$widgets));
    }

    /**
     * Add a widget for the given user (left column).
     *
     * @param string $user_id
     * @param string $plugin_id
     *
     * @return WidgetUser inserted WidgetUser instance
     */
    public static function addWidget($user_id, $plugin_id): WidgetUser
    {
        self::setInitialWidgets($user_id);

        $stmt = DBManager::get()->prepare('SELECT MAX(position) + 1 FROM widget_user WHERE range_id = ?');
        $stmt->execute([$user_id]);
        $position = $stmt->fetchColumn() ?: 0;

        return self::create([
            'pluginid' => $plugin_id,
            'position' => $position,
            'range_id' => $user_id
        ]);
    }

    /**
     * Remove a widget for the given user (if enabled).
     *
     * @param string $user_id
     * @param string $plugin_id
     *
     * @return int number of removed widgets
     */
    public static function removeWidget($user_id, $plugin_id): int
    {
        self::setInitialWidgets($user_id);

        return self::deleteBySQL('pluginid = ? AND range_id = ?', [$plugin_id, $user_id]);
    }
}