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 */
22declare(strict_types=0);
23
24namespace Ampache\Plugin;
25
26use Ampache\Config\AmpConfig;
27use Ampache\Repository\Model\Plugin;
28use Ampache\Repository\Model\Preference;
29use Ampache\Repository\Model\Song;
30use Ampache\Repository\Model\User;
31use Ampache\Repository\Model\Userflag;
32use Ampache\Module\Api\Ajax;
33use Ampache\Module\Playback\Stream_Playlist;
34use Ampache\Module\Util\Ui;
35
36class AmpacheCatalogFavorites
37{
38    public $name        = 'Catalog Favorites';
39    public $categories  = 'home';
40    public $description = 'Catalog favorites on homepage';
41    public $url         = '';
42    public $version     = '000002';
43    public $min_ampache = '370021';
44    public $max_ampache = '999999';
45
46    // These are internal settings used by this class, run this->load to fill them out
47    private $maxitems;
48    private $gridview;
49
50    /**
51     * Constructor
52     * This function does nothing...
53     */
54    public function __construct()
55    {
56        $this->description = T_('Catalog favorites 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('catalogfav_max_items')) {
70            return false;
71        }
72
73        Preference::insert('catalogfav_max_items', T_('Catalog favorites max items'), 5, 25, 'integer', 'plugins',
74            $this->name);
75        Preference::insert('catalogfav_gridview', T_('Catalog favorites grid view display'), '0', 25, 'boolean',
76            'plugins', $this->name);
77
78        return true;
79    }
80
81    /**
82     * uninstall
83     * This is a required plugin function. It removes our preferences from
84     * the database returning it to its original form
85     */
86    public function uninstall()
87    {
88        Preference::delete('catalogfav_max_items');
89        Preference::delete('catalogfav_gridview');
90
91        return true;
92    }
93
94    /**
95     * upgrade
96     * This is a recommended plugin function
97     */
98    public function upgrade()
99    {
100        $from_version = Plugin::get_plugin_version($this->name);
101        if ($from_version < 2) {
102            Preference::insert('catalogfav_gridview', T_('Catalog favorites grid view display'), '0', 25, 'boolean',
103                'plugins');
104        }
105
106        return true;
107    }
108
109    /**
110     * display_home
111     * This display the module in home page
112     */
113    public function display_home()
114    {
115        if (AmpConfig::get('userflags')) {
116            $userflags = Userflag::get_latest('song', 0, $this->maxitems);
117            $count     = 0;
118            echo '<div class="home_plugin">';
119            Ui::show_box_top(T_('Highlight'));
120            echo '<table class="tabledata striped-rows';
121            if (!$this->gridview) {
122                echo " disablegv";
123            }
124            echo '">';
125            foreach ($userflags as $userflag) {
126                $item = new Song($userflag);
127                $item->format();
128
129                if ($item->id) {
130                    echo '<tr id="song_' . $userflag . '" class="libitem_menu">';
131                    if ($this->gridview) {
132                        echo '<td class="cel_song"><span style="font-weight: bold;">' . $item->f_link . '</span><br> ';
133                        echo '<span style="margin-right: 10px;">';
134                        if (AmpConfig::get('directplay')) {
135                            echo Ajax::button('?page=stream&action=directplay&object_type=song&object_id=' . $userflag,
136                                'play', T_('Play'), 'play_song_' . $userflag);
137                            if (Stream_Playlist::check_autoplay_next()) {
138                                echo Ajax::button('?page=stream&action=directplay&object_type=song&object_id=' . $userflag . '&playnext=true',
139                                    'play_next', T_('Play next'),
140                                    'nextplay_song_' . $userflag);
141                            }
142                            if (Stream_Playlist::check_autoplay_append()) {
143                                echo Ajax::button('?page=stream&action=directplay&object_type=song&object_id=' . $userflag . '&append=true',
144                                    'play_add', T_('Play last'),
145                                    'addplay_song_' . $userflag);
146                            }
147                        }
148                        echo Ajax::button('?action=basket&type=song&id=' . $userflag, 'add', T_('Add to Temporary Playlist'), 'play_full_' . $userflag);
149                        echo '</span></td>';
150                    }
151                    echo '<td class=grid_cover>';
152                    $thumb = ($this->gridview && UI::is_grid_view('album')) ? 1 : 12; // default to 150x150
153                    $item->display_art($thumb, true);
154                    echo '</td>';
155
156                    if (!$this->gridview) {
157                        echo '<td>' . $item->f_link . '</td>';
158                    }
159
160                    echo '<td class="optional">';
161                    echo '<div style="white-space: normal;">' . $item->get_description() . '</div>';
162                    echo '</div>';
163                    echo '</td></tr>';
164
165                    $count++;
166                }
167            }
168            echo '</table>';
169            Ui::show_box_bottom();
170            echo '</div>';
171        }
172    }
173
174    /**
175     * load
176     * This loads up the data we need into this object, this stuff comes from the preferences.
177     * @param User $user
178     * @return boolean
179     */
180    public function load($user)
181    {
182        $user->set_preferences();
183        $data = $user->prefs;
184
185        $this->maxitems = (int)($data['catalogfav_max_items']);
186        if ($this->maxitems < 1) {
187            $this->maxitems = 5;
188        }
189        $this->gridview = ($data['catalogfav_gridview'] == '1');
190
191        return true;
192    }
193}
194