1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\DAV;
6use Sabre\DAV\MkCol;
7use Sabre\DAVACL;
8use Sabre\Uri;
9
10/**
11 * AddressBook Home class
12 *
13 * This collection contains a list of addressbooks associated with one user.
14 *
15 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
16 * @author Evert Pot (http://evertpot.com/)
17 * @license http://sabre.io/license/ Modified BSD License
18 */
19class AddressBookHome extends DAV\Collection implements DAV\IExtendedCollection, DAVACL\IACL {
20
21    use DAVACL\ACLTrait;
22
23    /**
24     * Principal uri
25     *
26     * @var array
27     */
28    protected $principalUri;
29
30    /**
31     * carddavBackend
32     *
33     * @var Backend\BackendInterface
34     */
35    protected $carddavBackend;
36
37    /**
38     * Constructor
39     *
40     * @param Backend\BackendInterface $carddavBackend
41     * @param string $principalUri
42     */
43    function __construct(Backend\BackendInterface $carddavBackend, $principalUri) {
44
45        $this->carddavBackend = $carddavBackend;
46        $this->principalUri = $principalUri;
47
48    }
49
50    /**
51     * Returns the name of this object
52     *
53     * @return string
54     */
55    function getName() {
56
57        list(, $name) = Uri\split($this->principalUri);
58        return $name;
59
60    }
61
62    /**
63     * Updates the name of this object
64     *
65     * @param string $name
66     * @return void
67     */
68    function setName($name) {
69
70        throw new DAV\Exception\MethodNotAllowed();
71
72    }
73
74    /**
75     * Deletes this object
76     *
77     * @return void
78     */
79    function delete() {
80
81        throw new DAV\Exception\MethodNotAllowed();
82
83    }
84
85    /**
86     * Returns the last modification date
87     *
88     * @return int
89     */
90    function getLastModified() {
91
92        return null;
93
94    }
95
96    /**
97     * Creates a new file under this object.
98     *
99     * This is currently not allowed
100     *
101     * @param string $filename
102     * @param resource $data
103     * @return void
104     */
105    function createFile($filename, $data = null) {
106
107        throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported');
108
109    }
110
111    /**
112     * Creates a new directory under this object.
113     *
114     * This is currently not allowed.
115     *
116     * @param string $filename
117     * @return void
118     */
119    function createDirectory($filename) {
120
121        throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported');
122
123    }
124
125    /**
126     * Returns a single addressbook, by name
127     *
128     * @param string $name
129     * @todo needs optimizing
130     * @return AddressBook
131     */
132    function getChild($name) {
133
134        foreach ($this->getChildren() as $child) {
135            if ($name == $child->getName())
136                return $child;
137
138        }
139        throw new DAV\Exception\NotFound('Addressbook with name \'' . $name . '\' could not be found');
140
141    }
142
143    /**
144     * Returns a list of addressbooks
145     *
146     * @return array
147     */
148    function getChildren() {
149
150        $addressbooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
151        $objs = [];
152        foreach ($addressbooks as $addressbook) {
153            $objs[] = new AddressBook($this->carddavBackend, $addressbook);
154        }
155        return $objs;
156
157    }
158
159    /**
160     * Creates a new address book.
161     *
162     * @param string $name
163     * @param MkCol $mkCol
164     * @throws DAV\Exception\InvalidResourceType
165     * @return void
166     */
167    function createExtendedCollection($name, MkCol $mkCol) {
168
169        if (!$mkCol->hasResourceType('{' . Plugin::NS_CARDDAV . '}addressbook')) {
170            throw new DAV\Exception\InvalidResourceType('Unknown resourceType for this collection');
171        }
172        $properties = $mkCol->getRemainingValues();
173        $mkCol->setRemainingResultCode(201);
174        $this->carddavBackend->createAddressBook($this->principalUri, $name, $properties);
175
176    }
177
178    /**
179     * Returns the owner principal
180     *
181     * This must be a url to a principal, or null if there's no owner
182     *
183     * @return string|null
184     */
185    function getOwner() {
186
187        return $this->principalUri;
188
189    }
190
191}
192