summaryrefslogtreecommitdiffstats
path: root/app/class/object.class.php
blob: dc988ae0e9f773de75501b1c60cb4ae05af4be38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

/*
 * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to UNLICENSE
 */

require_once "class/table.class.php";

/*
 * This is a generic database object.  This is a supertype of all Scrott
 * datatypes and defines fields common to all of them.
 */
class object extends table
{
    /*
     * Constructor
     */
    public function __construct(?string $guid = NULL)
    {
        $this->fields['objects'] = array(
            "guid",
            "owner",
            "parent",
            "name",
            "created",
            "updated",

            "membModify",
            "membMemb",
            "membAccs",
            "membCres",
            "membModifys",
            "membMembs",
            "pubAcc",
            "pubAccs",
            "pubCres",

            "objtype",
        );

        parent::__construct($guid);
    }

    /*
     * Get the object type for the given GUID
     */
    public static function typeOf(string $guid) : string
    {
        $obj = new object($guid);
        return $obj->objtype;
    }

    /*
     * Get the owner of this object.  Either a user object or a group
     * object will be returned.  If this object does not have an owner,
     * NULL will be returned.
     */
    public function getOwner() : ?agent
    {
        if (!isset($this->owner) || $this->owner == "")
            return NULL;

        if (self::typeOf($this->owner) == "group")
            return new group($this->owner);

        return new user($this->owner);
    }

    /*
     * Update the owner of this object
     */
    public function setOwner(agent $owner) : void
    {
        $this->owner = $owner->guid;
    }

    /*
     * Get the parent of this object.  If this object does not have a
     * parent, NULL will be returned.
     */
    public function getParent() : ?object
    {
        if (!isset($this->parent) || $this->parent == "")
            return NULL;

        $parent = new object($this->parent);
        return new $parent->objtype($parent->guid);
    }

    /*
     * Update the parent of this object
     */
    public function setParent(object $parent) : void
    {
        $this->parent = $parent->guid;
    }

    /*
     * Get an array of all members of this object
     */
    public function getMembers() : array
    {
        $memb = array();
        $query = "SELECT member FROM members WHERE guid = '" . database::esc($this->guid) . "'";
        $res = database::query($query);

        foreach ($res as $m)
            $memb[] = new user($m['member']);

        return $memb;
    }

    /*
     * Add a user as a member of this object.  Returns false if user is
     * already a member, or if another error occurs; true otherwise.
     */
    public function addMember(user $user) : bool
    {
        if ($user->isMemberOf($this) || !isset($user->guid))
            return false;

        $query = "INSERT INTO members (guid, member) VALUES ('" . database::esc($this->guid) . "', '" .
            database::esc($user->guid) . "')";
        database::query($query);
        return true;
    }

    /*
     * Remove a user as a member of this object.  Returns false if user
     * is not a member, or if another error occurs; true otherwise.
     */
    public function remMember(user $user) : bool
    {
        if (!$user->isMemberOf($this) || !isset($user->guid))
            return false;

        $query = "DELETE FROM members WHERE guid = '" . database::esc($this->guid) . "' AND " .
            "member = '" . database::esc($user->guid) . "'";
        database::query($query);
        return true;
    }
}

?>