Passed
Push — hypernext ( d18b5c...96a12c )
by Nico
12:00
created

FavTags::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * @author Nicolas CARPi <[email protected]>
4
 * @copyright 2012 Nicolas CARPi
5
 * @see https://www.elabftw.net Official website
6
 * @license AGPL-3.0
7
 * @package elabftw
8
 */
9
10
namespace Elabftw\Models;
11
12
use Elabftw\Elabftw\Db;
13
use Elabftw\Interfaces\ContentParamsInterface;
14
use Elabftw\Interfaces\CrudInterface;
15
use Elabftw\Traits\SetIdTrait;
16
use PDO;
17
18
/**
19
 * The favorite tags of a user
20
 */
21
class FavTags implements CrudInterface
22
{
23
    use SetIdTrait;
24
25
    protected Db $Db;
26
27
    public function __construct(private Users $Users, ?int $id = null)
28
    {
29
        $this->setId($id);
30
        $this->Db = Db::getConnection();
31
    }
32
33
    public function create(ContentParamsInterface $params): int
34
    {
35
        $tag = $params->getContent();
36
        // get the tag id
37
        $sql = 'SELECT id FROM tags WHERE team = :team AND tag = :tag';
38
        $req = $this->Db->prepare($sql);
39
        $req->bindParam(':team', $this->Users->team, PDO::PARAM_INT);
40
        $req->bindParam(':tag', $tag, PDO::PARAM_STR);
41
        $this->Db->execute($req);
42
        $tagId = (int) $req->fetchColumn();
43
44
        if ($this->isFavorite($tagId)) {
45
            return 0;
46
        }
47
48
        // now add it as favorite
49
        $sql = 'INSERT INTO favtags2users (users_id, tags_id) VALUES (:userid, :tagId)';
50
        $req = $this->Db->prepare($sql);
51
        $req->bindParam(':tagId', $tagId, PDO::PARAM_INT);
52
        $req->bindParam(':userid', $this->Users->userData['userid'], PDO::PARAM_INT);
53
        return (int) $this->Db->execute($req);
54
    }
55
56
    public function read(ContentParamsInterface $params): array
57
    {
58
        $sql = 'SELECT users_id, tags_id, tag FROM favtags2users
59
           LEFT JOIN tags ON (tags.id = favtags2users.tags_id) WHERE users_id = :userid
60
           ORDER BY tags.tag';
61
        $req = $this->Db->prepare($sql);
62
        $req->bindParam(':userid', $this->Users->userData['userid'], PDO::PARAM_INT);
63
        $this->Db->execute($req);
64
        return $this->Db->fetchAll($req);
65
    }
66
67
    public function update(ContentParamsInterface $params): bool
68
    {
69
        return true;
70
    }
71
72
    public function destroy(): bool
73
    {
74
        $sql = 'DELETE FROM favtags2users WHERE users_id = :userid AND tags_id = :tagId';
75
        $req = $this->Db->prepare($sql);
76
        $req->bindParam(':tagId', $this->id, PDO::PARAM_INT);
77
        $req->bindParam(':userid', $this->Users->userData['userid'], PDO::PARAM_INT);
78
        return $this->Db->execute($req);
79
    }
80
81
    // check if a tag is not already favorite for the user
82
    private function isFavorite(int $tagId): bool
83
    {
84
        $sql = 'SELECT * FROM favtags2users WHERE tags_id = :tagId AND users_id = :userid';
85
        $req = $this->Db->prepare($sql);
86
        $req->bindParam(':tagId', $tagId, PDO::PARAM_INT);
87
        $req->bindParam(':userid', $this->Users->userData['userid'], PDO::PARAM_INT);
88
        $this->Db->execute($req);
89
        return $req->rowCount() > 0;
90
    }
91
}
92