1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25
26namespace Ampache\Plugin;
27
28use Ampache\Config\AmpConfig;
29use Ampache\Repository\Model\Preference;
30use Ampache\Repository\Model\User;
31use Ampache\Repository\Model\Useractivity;
32use Ampache\Module\System\Core;
33use Ampache\Module\User\Activity\UserActivityRendererInterface;
34use Ampache\Module\Util\Ui;
35use Ampache\Repository\UserActivityRepositoryInterface;
36
37class AmpacheFriendsTimeline
38{
39    public $name        = 'Friends Timeline';
40    public $categories  = 'home';
41    public $description = 'Friends Timeline on homepage';
42    public $url         = '';
43    public $version     = '000001';
44    public $min_ampache = '370040';
45    public $max_ampache = '999999';
46
47    // These are internal settings used by this class, run this->load to fill them out
48    private $maxitems;
49
50    /**
51     * Constructor
52     * This function does nothing...
53     */
54    public function __construct()
55    {
56        $this->description = T_("Friend's Timeline on homepage");
57
58        return true;
59    }
60
61    /**
62     * install
63     * This is a required plugin function. It inserts our preferences
64     * into Ampache
65     */
66    public function install()
67    {
68        // Check and see if it's already installed
69        if (Preference::exists('ftl_max_items')) {
70            return false;
71        }
72
73        Preference::insert('ftl_max_items', T_('Friends timeline max items'), 5, 25, 'integer', 'plugins', $this->name);
74
75        return true;
76    }
77
78    /**
79     * uninstall
80     * This is a required plugin function. It removes our preferences from
81     * the database returning it to its original form
82     */
83    public function uninstall()
84    {
85        Preference::delete('ftl_max_items');
86
87        return true;
88    }
89
90    /**
91     * upgrade
92     * This is a recommended plugin function
93     */
94    public function upgrade()
95    {
96        return true;
97    }
98
99    /**
100     * display_home
101     * This display the module in home page
102     */
103    public function display_home()
104    {
105        if (AmpConfig::get('sociable')) {
106            $user_id = Core::get_global('user')->id;
107            if ($user_id) {
108                echo '<div class="home_plugin">';
109                $activities = $this->getUseractivityRepository()->getFriendsActivities(
110                    (int) $user_id,
111                    (int) $this->maxitems
112                );
113                if (count($activities) > 0) {
114                    Ui::show_box_top(T_('Friends Timeline'));
115                    Useractivity::build_cache($activities);
116
117                    $activityRenderer = $this->getUserActivityRenderer();
118
119                    foreach ($activities as $activity_id) {
120                        echo $activityRenderer->show(
121                            new Useractivity($activity_id)
122                        );
123                    }
124                    Ui::show_box_bottom();
125                }
126                echo '</div>';
127            }
128        }
129    }
130
131    /**
132     * load
133     * This loads up the data we need into this object, this stuff comes
134     * from the preferences.
135     * @param User $user
136     * @return boolean
137     */
138    public function load($user)
139    {
140        $user->set_preferences();
141        $data = $user->prefs;
142
143        $this->maxitems = (int)($data['ftl_max_items']);
144        if ($this->maxitems < 1) {
145            $this->maxitems = 10;
146        }
147
148        return true;
149    }
150
151    /**
152     * @deprecated
153     */
154    private function getUseractivityRepository(): UserActivityRepositoryInterface
155    {
156        global $dic;
157
158        return $dic->get(UserActivityRepositoryInterface::class);
159    }
160
161    /**
162     * @deprecated
163     */
164    private function getUserActivityRenderer(): UserActivityRendererInterface
165    {
166        global $dic;
167
168        return $dic->get(UserActivityRendererInterface::class);
169    }
170}
171