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\library_item;
28use Ampache\Repository\Model\Preference;
29use Ampache\Repository\Model\User;
30
31class AmpacheFlattr
32{
33    public $name        = 'Flattr';
34    public $categories  = 'user';
35    public $description = 'Flattr donation button on user page';
36    public $url         = '';
37    public $version     = '000001';
38    public $min_ampache = '370034';
39    public $max_ampache = '999999';
40
41    // These are internal settings used by this class, run this->load to fill them out
42    private $user;
43    private $user_id;
44
45    /**
46     * Constructor
47     * This function does nothing...
48     */
49    public function __construct()
50    {
51        $this->description = T_('Flattr donation button on user page');
52
53        return true;
54    }
55
56    /**
57     * install
58     * This is a required plugin function. It inserts our preferences
59     * into Ampache
60     */
61    public function install()
62    {
63        // Check and see if it's already installed
64        if (Preference::exists('flattr_user_id')) {
65            return false;
66        }
67
68        Preference::insert('flattr_user_id', T_('Flattr User ID'), '', 25, 'string', 'plugins', $this->name);
69
70        return true;
71    }
72
73    /**
74     * uninstall
75     * This is a required plugin function. It removes our preferences from
76     * the database returning it to its original form
77     */
78    public function uninstall()
79    {
80        Preference::delete('flattr_user_id');
81
82        return true;
83    }
84
85    /**
86     * upgrade
87     * This is a recommended plugin function
88     */
89    public function upgrade()
90    {
91        return true;
92    }
93
94    /**
95     * display_user_field
96     * This display the module in user page
97     * @param library_item|null $libitem
98     */
99    public function display_user_field(library_item $libitem = null)
100    {
101        $name = ($libitem != null) ? $libitem->get_fullname() : (T_('User') . " `" . $this->user->fullname . "` " . T_('on') . " " . AmpConfig::get('site_title'));
102        $link = ($libitem != null && $libitem->link) ? $libitem->link : $this->user->link;
103
104        echo "<a class='nohtml' href='https://flattr.com/submit/auto?user_id=" . scrub_out($this->user_id) . "&url=" . rawurlencode($link) . "&category=audio&title=" . rawurlencode($name) . "' target='_blank'><img src='//button.flattr.com/flattr-badge-large.png' alt='" . T_('Flattr this') . "' title='" . T_('Flattr this') . "'></a>";
105    }
106
107    /**
108     * load
109     * This loads up the data we need into this object, this stuff comes
110     * from the preferences.
111     * @param User $user
112     * @return boolean
113     */
114    public function load($user)
115    {
116        $this->user = $user;
117        $user->set_preferences();
118        $data = $user->prefs;
119        // load system when nothing is given
120        if (!strlen(trim($data['flattr_user_id']))) {
121            $data                   = array();
122            $data['flattr_user_id'] = Preference::get_by_user(-1, 'flattr_user_id');
123        }
124
125        $this->user_id = trim($data['flattr_user_id']);
126        if (!strlen($this->user_id)) {
127            debug_event('flattr.plugin', 'No Flattr User ID, user field plugin skipped', 3);
128
129            return false;
130        }
131
132        return true;
133    }
134}
135