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\Config\AmpConfig;
28
29/**
30 * This is the class responsible for handling the PrivateMsg object
31 * it is related to the user_pvmsg table in the database.
32 */
33class PrivateMsg extends database_object implements PrivateMessageInterface
34{
35    protected const DB_TABLENAME = 'user_pvmsg';
36
37    /* Variables from DB */
38    /**
39     * @var integer $id
40     */
41    private $id;
42
43    /**
44     * @var string $subject
45     */
46    private $subject;
47
48    /**
49     * @var string $message
50     */
51    private $message;
52
53    /**
54     * @var integer $from_user
55     */
56    private $from_user;
57
58    /**
59     * @var integer $to_user
60     */
61    private $to_user;
62
63    /**
64     * @var integer $creation_date
65     */
66    private $creation_date;
67
68    /**
69     * @var boolean $is_read
70     */
71    private $is_read;
72
73    /**
74     * @param integer $pm_id
75     */
76    public function __construct($pm_id)
77    {
78        $info = $this->get_info($pm_id, 'user_pvmsg');
79        foreach ($info as $key => $value) {
80            $this->$key = $value;
81        }
82
83        return true;
84    }
85
86    public function getId(): int
87    {
88        return (int) $this->id;
89    }
90
91    public function isNew(): bool
92    {
93        return $this->getId() === 0;
94    }
95
96    public function getSenderUserLink(): string
97    {
98        $from_user = new User((int) $this->from_user);
99        $from_user->format();
100
101        return $from_user->f_link;
102    }
103
104    public function getRecipientUserLink(): string
105    {
106        $to_user = new User((int) $this->to_user);
107        $to_user->format();
108
109        return $to_user->f_link;
110    }
111
112    public function getCreationDate(): int
113    {
114        return (int) $this->creation_date;
115    }
116
117    public function getCreationDateFormatted(): string
118    {
119        return get_datetime((int) $this->creation_date);
120    }
121
122    public function getLinkFormatted(): string
123    {
124        return sprintf(
125            '<a href="%s/pvmsg.php?pvmsg_id=%d">%s</a>',
126            AmpConfig::get('web_path'),
127            $this->id,
128            $this->getSubjectFormatted()
129        );
130    }
131
132    public function getSubjectFormatted(): string
133    {
134        return scrub_out((string) $this->subject);
135    }
136
137    public function isRead(): bool
138    {
139        return (int) $this->is_read === 1;
140    }
141
142    public function getRecipientUserId(): int
143    {
144        return (int) $this->to_user;
145    }
146
147    public function getSenderUserId(): int
148    {
149        return (int) $this->from_user;
150    }
151
152    public function getMessage(): string
153    {
154        return (string) $this->message;
155    }
156
157    public function getSubject(): string
158    {
159        return (string) $this->subject;
160    }
161}
162