|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dkd\PhpCmis\DataObjects; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of php-cmis-lib. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Sascha Egerer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
use Dkd\PhpCmis\CmisObject\CmisObjectInterface; |
|
14
|
|
|
use Dkd\PhpCmis\Constants; |
|
15
|
|
|
use Dkd\PhpCmis\Data\FileableCmisObjectInterface; |
|
16
|
|
|
use Dkd\PhpCmis\Data\FolderInterface; |
|
17
|
|
|
use Dkd\PhpCmis\Data\ObjectDataInterface; |
|
18
|
|
|
use Dkd\PhpCmis\Data\ObjectIdInterface; |
|
19
|
|
|
use Dkd\PhpCmis\Data\ObjectTypeInterface; |
|
20
|
|
|
use Dkd\PhpCmis\Data\PropertyIdInterface; |
|
21
|
|
|
use Dkd\PhpCmis\Data\PropertyStringInterface; |
|
22
|
|
|
use Dkd\PhpCmis\Enum\BaseTypeId; |
|
23
|
|
|
use Dkd\PhpCmis\Enum\IncludeRelationships; |
|
24
|
|
|
use Dkd\PhpCmis\Exception\CmisRuntimeException; |
|
25
|
|
|
use Dkd\PhpCmis\OperationContextInterface; |
|
26
|
|
|
use Dkd\PhpCmis\PropertyIds; |
|
27
|
|
|
use Dkd\PhpCmis\SessionInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Base class for all fileable persistent session object classes. |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class AbstractFileableCmisObject extends AbstractCmisObject implements FileableCmisObjectInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @param SessionInterface $session |
|
36
|
|
|
* @param ObjectTypeInterface $objectType |
|
37
|
|
|
* @param OperationContextInterface $context |
|
38
|
|
|
* @param ObjectDataInterface|null $objectData |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
SessionInterface $session, |
|
42
|
|
|
ObjectTypeInterface $objectType, |
|
43
|
|
|
OperationContextInterface $context, |
|
44
|
|
|
ObjectDataInterface $objectData = null |
|
45
|
|
|
) { |
|
46
|
|
|
$this->initialize($session, $objectType, $context, $objectData); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the parents of this object. |
|
51
|
|
|
* |
|
52
|
|
|
* @param OperationContextInterface|null $context the OperationContext to use to fetch the parent folder objects |
|
53
|
|
|
* @return FolderInterface[] the list of parent folders of this object or an empty list if this object is unfiled |
|
54
|
|
|
* or if this object is the root folder |
|
55
|
|
|
* @throws CmisRuntimeException Throws exception if invalid data is returned by the repository |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getParents(OperationContextInterface $context = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$context = $this->ensureContext($context); |
|
60
|
|
|
|
|
61
|
|
|
// get object ids of the parent folders |
|
62
|
|
|
$bindingParents = $this->getBinding()->getNavigationService()->getObjectParents( |
|
63
|
|
|
$this->getRepositoryId(), |
|
64
|
|
|
$this->getId(), |
|
65
|
|
|
$this->getPropertyQueryName(PropertyIds::OBJECT_ID), |
|
66
|
|
|
false, |
|
67
|
|
|
IncludeRelationships::cast(IncludeRelationships::NONE), |
|
68
|
|
|
Constants::RENDITION_NONE, |
|
69
|
|
|
false, |
|
70
|
|
|
null |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$parents = array(); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($bindingParents as $parent) { |
|
76
|
|
|
if ($parent->getObject()->getProperties() === null) { |
|
77
|
|
|
// that should never happen |
|
78
|
|
|
throw new CmisRuntimeException('Repository sent invalid data!'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$parentProperties = $parent->getObject()->getProperties()->getProperties(); |
|
82
|
|
|
// get id property |
|
83
|
|
|
$idProperty = null; |
|
84
|
|
|
if (isset($parentProperties[PropertyIds::OBJECT_ID])) { |
|
85
|
|
|
$idProperty = $parentProperties[PropertyIds::OBJECT_ID]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$idProperty instanceof PropertyIdInterface && !$idProperty instanceof PropertyStringInterface) { |
|
89
|
|
|
// the repository sent an object without a valid object id... |
|
90
|
|
|
throw new CmisRuntimeException('Repository sent invalid data! No object id!'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// fetch the object and make sure it is a folder |
|
94
|
|
|
$parentFolder = $this->getSession()->getObject( |
|
95
|
|
|
$this->session->createObjectId((String) $idProperty->getFirstValue()), |
|
96
|
|
|
$context |
|
97
|
|
|
); |
|
98
|
|
|
if (!$parentFolder instanceof FolderInterface) { |
|
99
|
|
|
// the repository sent an object that is not a folder... |
|
100
|
|
|
throw new CmisRuntimeException('Repository sent invalid data! Object is not a folder!'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$parents[] = $parentFolder; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $parents; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the paths of this object. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string[] the list of paths of this object or an empty list if this object is unfiled or if this object |
|
113
|
|
|
* is the root folder |
|
114
|
|
|
* @throws CmisRuntimeException Throws exception if repository sends invalid data |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getPaths() |
|
117
|
|
|
{ |
|
118
|
|
|
$folderType = $this->getSession()->getTypeDefinition((string) BaseTypeId::cast(BaseTypeId::CMIS_FOLDER)); |
|
119
|
|
|
|
|
120
|
|
|
$propertyDefinition = $folderType->getPropertyDefinition(PropertyIds::PATH); |
|
121
|
|
|
$pathQueryName = ($propertyDefinition === null) ? null : $propertyDefinition->getQueryName(); |
|
122
|
|
|
|
|
123
|
|
|
// get object paths of the parent folders |
|
124
|
|
|
$bindingParents = $this->getBinding()->getNavigationService()->getObjectParents( |
|
125
|
|
|
$this->getRepositoryId(), |
|
126
|
|
|
$this->getId(), |
|
127
|
|
|
$pathQueryName, |
|
128
|
|
|
false, |
|
129
|
|
|
IncludeRelationships::cast(IncludeRelationships::NONE), |
|
130
|
|
|
Constants::RENDITION_NONE, |
|
131
|
|
|
true, |
|
132
|
|
|
null |
|
133
|
|
|
); |
|
134
|
|
|
$paths = array(); |
|
135
|
|
|
foreach ($bindingParents as $parent) { |
|
136
|
|
|
if ($parent->getObject()->getProperties() === null) { |
|
137
|
|
|
// that should never happen but could in case of an faulty repository implementation |
|
138
|
|
|
throw new CmisRuntimeException('Repository sent invalid data! No properties given.'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$parentProperties = $parent->getObject()->getProperties()->getProperties(); |
|
142
|
|
|
$pathProperty = null; |
|
143
|
|
|
|
|
144
|
|
|
if (isset($parentProperties[PropertyIds::PATH])) { |
|
145
|
|
|
$pathProperty = $parentProperties[PropertyIds::PATH]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!$pathProperty instanceof PropertyStringInterface) { |
|
149
|
|
|
// the repository sent an object without a valid path... |
|
150
|
|
|
throw new CmisRuntimeException('Repository sent invalid data! Path is not set!'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if ($parent->getRelativePathSegment() === null) { |
|
154
|
|
|
throw new CmisRuntimeException('Repository sent invalid data! No relative path segement!'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$folderPath = rtrim((string) $pathProperty->getFirstValue(), '/') . '/'; |
|
158
|
|
|
$paths[] = $folderPath . $parent->getRelativePathSegment(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $paths; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Moves this object. |
|
166
|
|
|
* |
|
167
|
|
|
* @param ObjectIdInterface $sourceFolderId the object ID of the source folder |
|
168
|
|
|
* @param ObjectIdInterface $targetFolderId the object ID of the target folder |
|
169
|
|
|
* @param OperationContextInterface|null $context the OperationContext to use to fetch the moved object |
|
170
|
|
|
* @return FileableCmisObjectInterface the moved object |
|
171
|
|
|
* @throws CmisRuntimeException Throws exception if the repository returns an invalid object after the object has |
|
172
|
|
|
* been moved |
|
173
|
|
|
*/ |
|
174
|
|
|
public function move( |
|
175
|
|
|
ObjectIdInterface $sourceFolderId, |
|
176
|
|
|
ObjectIdInterface $targetFolderId, |
|
177
|
|
|
OperationContextInterface $context = null |
|
178
|
|
|
) { |
|
179
|
|
|
$context = $this->ensureContext($context); |
|
180
|
|
|
|
|
181
|
|
|
$originalId = $this->getId(); |
|
182
|
|
|
$newObjectId = $this->getId(); |
|
183
|
|
|
|
|
184
|
|
|
$this->getBinding()->getObjectService()->moveObject( |
|
185
|
|
|
$this->getRepositoryId(), |
|
186
|
|
|
$newObjectId, |
|
187
|
|
|
$targetFolderId->getId(), |
|
188
|
|
|
$sourceFolderId->getId(), |
|
189
|
|
|
null |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
// invalidate path cache |
|
193
|
|
|
$this->getSession()->removeObjectFromCache($this->getSession()->createObjectId($originalId)); |
|
194
|
|
|
|
|
195
|
|
|
if (empty($newObjectId)) { |
|
196
|
|
|
return null; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$movedObject = $this->getSession()->getObject( |
|
200
|
|
|
$this->getSession()->createObjectId((string) $newObjectId), |
|
201
|
|
|
$context |
|
202
|
|
|
); |
|
203
|
|
|
if (!$movedObject instanceof FileableCmisObjectInterface) { |
|
204
|
|
|
throw new CmisRuntimeException( |
|
205
|
|
|
'Moved object is invalid because it must be of type FileableCmisObjectInterface but is not.' |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $movedObject; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Adds this object to a folder. |
|
214
|
|
|
* |
|
215
|
|
|
* @param ObjectIdInterface $folderId The folder into which the object is to be filed. |
|
216
|
|
|
* @param boolean $allVersions Add all versions of the object to the folder if the repository supports |
|
217
|
|
|
* version-specific filing. Defaults to <code>true</code>. |
|
218
|
|
|
*/ |
|
219
|
|
View Code Duplication |
public function addToFolder(ObjectIdInterface $folderId, $allVersions = true) |
|
220
|
|
|
{ |
|
221
|
|
|
$objectId = $this->getId(); |
|
222
|
|
|
$this->getBinding()->getMultiFilingService()->addObjectToFolder( |
|
223
|
|
|
$this->getRepositoryId(), |
|
224
|
|
|
$objectId, |
|
225
|
|
|
$folderId->getId(), |
|
226
|
|
|
$allVersions |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
// remove object form cache |
|
230
|
|
|
$this->getSession()->removeObjectFromCache($this->getSession()->createObjectId($objectId)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Removes this object from a folder. |
|
235
|
|
|
* |
|
236
|
|
|
* @param ObjectIdInterface $folderId the object ID of the folder from which this object should be removed |
|
237
|
|
|
*/ |
|
238
|
|
View Code Duplication |
public function removeFromFolder(ObjectIdInterface $folderId) |
|
239
|
|
|
{ |
|
240
|
|
|
$objectId = $this->getId(); |
|
241
|
|
|
|
|
242
|
|
|
$this->getBinding()->getMultiFilingService()->removeObjectFromFolder( |
|
243
|
|
|
$this->getRepositoryId(), |
|
244
|
|
|
$objectId, |
|
245
|
|
|
$folderId->getId(), |
|
246
|
|
|
null |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
// remove object form cache |
|
250
|
|
|
$this->getSession()->removeObjectFromCache($this->getSession()->createObjectId($objectId)); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param ObjectIdInterface|null $objectId |
|
255
|
|
|
* @param OperationContextInterface|null $context |
|
256
|
|
|
* @return CmisObjectInterface|null |
|
257
|
|
|
* @throws CmisRuntimeException Throws exception if newly created object is not a document as expected |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function getNewlyCreatedObject( |
|
260
|
|
|
ObjectIdInterface $objectId = null, |
|
261
|
|
|
OperationContextInterface $context = null |
|
262
|
|
|
) { |
|
263
|
|
|
// if no context is provided the object will not be fetched |
|
264
|
|
|
if ($context === null || $objectId === null) { |
|
265
|
|
|
return null; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
// get the new object |
|
269
|
|
|
return $this->getSession()->getObject($objectId, $context); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Ensures that the context is set. If the given context is <code>null</code> the session default context will |
|
274
|
|
|
* be returned. |
|
275
|
|
|
* |
|
276
|
|
|
* @param OperationContextInterface|null $context |
|
277
|
|
|
* |
|
278
|
|
|
* @return OperationContextInterface |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function ensureContext(OperationContextInterface $context = null) |
|
281
|
|
|
{ |
|
282
|
|
|
if ($context === null) { |
|
283
|
|
|
$context = $this->getSession()->getDefaultContext(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $context; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|