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
25namespace Ampache\Repository\Model;
26
27use Ampache\Module\System\Core;
28use Ampache\Module\System\Dba;
29use PDOStatement;
30
31/**
32 * This manage bookmark on playable items
33 */
34class Bookmark extends database_object
35{
36    protected const DB_TABLENAME = 'bookmark';
37
38    // Public variables
39    public $id;
40    public $user;
41    public $object_id;
42    public $object_type;
43    public $position;
44    public $comment;
45    public $creation_date;
46    public $update_date;
47
48    /**
49     * Constructor
50     * This is run every time a new object is created, and requires
51     * the id and type of object that we need to pull for
52     * @param integer $object_id
53     * @param string $object_type
54     * @param integer $user_id
55     */
56    public function __construct($object_id, $object_type = null, $user_id = null)
57    {
58        if (!$object_id) {
59            return false;
60        }
61
62        if ($object_type === null) {
63            $info = $this->get_info($object_id);
64        } else {
65            if ($user_id === null) {
66                $user_id = Core::get_global('user')->id;
67            }
68
69            $sql        = "SELECT * FROM `bookmark` WHERE `object_type` = ? AND `object_id` = ? AND `user` = ?";
70            $db_results = Dba::read($sql, array($object_type, $object_id, $user_id));
71
72            if (!$db_results) {
73                return false;
74            }
75
76            $info = Dba::fetch_assoc($db_results);
77        }
78
79        // Foreach what we've got
80        foreach ($info as $key => $value) {
81            $this->$key = $value;
82        }
83
84        return true;
85    }
86
87    public function getId(): int
88    {
89        return (int) $this->id;
90    }
91
92    /**
93     * get_bookmark
94     * @param array $data
95     * @return integer[]
96     */
97    public static function get_bookmark($data)
98    {
99        $bookmarks   = array();
100        $comment_sql = isset($data['comment']) ? "AND `comment` = '" . scrub_in($data['comment']) . "'" : "";
101        $sql         = "SELECT `id` FROM `bookmark` WHERE `user` = ? AND `object_type` = ? AND `object_id` = ? " . $comment_sql;
102        $db_results  = Dba::read($sql, array($data['user'], $data['object_type'], $data['object_id']));
103        while ($results = Dba::fetch_assoc($db_results)) {
104            $bookmarks[] = (int) $results['id'];
105        }
106
107        return $bookmarks;
108    }
109
110    /**
111     * create
112     * @param array $data
113     * @param integer $userId
114     * @param integer $updateDate
115     * @return PDOStatement|boolean
116     */
117    public static function create(array $data, int $userId, int $updateDate)
118    {
119        $sql = "INSERT INTO `bookmark` (`user`, `position`, `comment`, `object_type`, `object_id`, `creation_date`, `update_date`) VALUES (?, ?, ?, ?, ?, ?, ?)";
120
121        return Dba::write($sql, array($userId, $data['position'], scrub_in($data['comment']), $data['object_type'], $data['object_id'], time(), $updateDate));
122    }
123
124    /**
125     * edit
126     * @param array $data
127     * @param integer $userId
128     * @param integer $updateDate
129     * @return PDOStatement|boolean
130     */
131    public static function edit($data, int $userId, int $updateDate)
132    {
133        $sql      = "UPDATE `bookmark` SET `position` = ?, `update_date` = ? WHERE `user` = ? AND `comment` = ? AND `object_type` = ? AND `object_id` = ?";
134
135        return Dba::write($sql, array($data['position'], $updateDate, $userId, scrub_in($data['comment']),  $data['object_type'], $data['object_id']));
136    }
137
138    /**
139     * Migrate an object associate stats to a new object
140     * @param string $object_type
141     * @param integer $old_object_id
142     * @param integer $new_object_id
143     * @return PDOStatement|boolean
144     */
145    public static function migrate($object_type, $old_object_id, $new_object_id)
146    {
147        $sql = "UPDATE IGNORE `bookmark` SET `object_id` = ? WHERE `object_id` = ? AND `object_type` = ?;";
148
149        return Dba::write($sql, array($new_object_id, $old_object_id, ucfirst($object_type)));
150    }
151
152    public function getUserName(): string
153    {
154        $user = new User($this->user);
155
156        return $user->username;
157    }
158}
159