Offline Server  1.6
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DomainApi.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Return offline domains where current user is affected
4  *
5  * @author Anakeen
6  * @version $Id: $
7  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
8  * @package OFFLINE
9  */
10 /**
11  */
12 
13 include_once ("FDL/Class.SearchDoc.php");
14 include_once ("DATA/Class.Collection.php");
15 include_once ("OFFLINE/Class.ExceptionCode.php");
16 
17 class DomainApi
18 {
19  /**
20  * internal domain document
21  * @var _OFFLINEDOMAIN
22  */
23  private $domain = null;
24  public function __construct(Dir &$domain = null)
25  {
26  $this->domain = $domain;
27  }
28  private static function setError($err)
29  {
30  throw new Exception($err);
31  }
32  /**
33  * List all domain availables by current user
34  * @return object document List
35  */
36  public static function getDomains()
37  {
38  include_once ("OFFLINE/Class.DomainManager.php");
39  $col = new Fdl_Collection();
40  $col->useDocumentList(DomainManager::getDomains());
41  $col->setContentOnlyValue(true);
42  $col->setContentCompleteProperties(false);
43  $out = $col->getContent();
44 
45  return $out;
46  }
47 
48  /**
49  * user mode
50  * @return stdClass ->userMode : advanced or standard
51  */
52  public function getUserMode()
53  {
54  $out = '';
55  if ($this->domain) {
56 
57  $uid = $this->domain->getSystemUserId();
58  $out->userMode = $this->domain->getUserMode($uid);
59  } else {
60  $this->setError(_("domain not set"));
61  }
62  return $out;
63  }
64 
65  /**
66  * book document into user space
67  * @return Fdl_Document
68  */
69  public function bookDocument($config)
70  {
71  $out = '';
72  if ($this->domain) {
73  $docid = $config->docid;
74 
75  $doc = new_doc($this->dbaccess, $docid, true);
76  $err = $doc->CanLockFile();
77  if (!$err) $err = $this->domain->insertUserDocument($docid, $this->domain->getSystemUserId(), true);
78  if ($err) $this->setError($err);
79  $fdoc = new Fdl_Document($docid);
80  $out = $fdoc->getDocument(true, false);
81 
82  } else {
83  $this->setError(_("domain not set"));
84  }
85  return $out;
86  }
87 
88  /**
89  * unbook document into user space
90  * @return Fdl_Document
91  */
92  public function unbookDocument($config)
93  {
94  $out = '';
95  if ($this->domain) {
96  $docid = $config->docid;
97  $err = $this->domain->setReservation($docid, 0);
98  if ($err) $this->setError($err);
99  $fdoc = new Fdl_Document($docid);
100  $out = $fdoc->getDocument(true, false);
101 
102  } else {
103  $this->setError(_("domain not set"));
104  }
105  return $out;
106  }
107 
108  /**
109  * revert document
110  * @return Fdl_Document
111  */
112  public function revertDocument($config)
113  {
114  $out = '';
115  if ($this->domain) {
116  $docid = $config->docid;
117  $doc = new_doc(getDbaccess(), $docid, true);
118  if ($doc->isAlive()) {
119  include_once ("FDL/Class.DocWaitManager.php");
120  $this->updateUnResolvedLocalLinks($doc);
121  $uid=$this->domain->getSystemUserId();
122  if ($doc->lockdomainid == $this->domain->id && $doc->locked==$uid) {
123  $wdoc=new DocWait($this->domain->dbaccess, array($doc->initid, $uid));
124  if ($wdoc->isAffected()) $wdoc->resetWaitingDocument();
125  } else {
126  $err = DocWaitManager::clearWaitingDocs($this->domain->id, $uid, $doc->initid);
127  }
128 
129  if ($err) $this->setError($err);
130  $fdoc = new Fdl_Document(0, null, $doc);
131  $out = $fdoc->getDocument(true, false);
132  } else {
133  $this->setError(sprintf(_("document %s not found"), $docid));
134  }
135  } else {
136  $this->setError(_("domain not set"));
137  }
138  return $out;
139  }
140 
141  private function updateUnResolvedLocalLinks(Doc &$doc) {
142  $oas = $doc->getNormalAttributes();
143  $unresolvedLinks=DocWaitManager::getUnresolvedLocalLinks($this->domain->id, $this->domain->getSystemUserId());
144  $localIds=array_keys($unresolvedLinks);
145  $serverIds=array_values($unresolvedLinks);
146  foreach ( $oas as $aid => $oa ) {
147  if ($oa->type == "docid") {
148  $value = $doc->getValue($aid);
149  if ($value) {
150  $nvalue = str_replace($serverIds, $localIds, $value);
151  if ($nvalue != $value) {
152  $doc->$aid=$nvalue; // need to by pass setValue cause incorrect docid syntax
153  }
154  }
155  }
156  }
157  }
158 
159  /**
160  * put document into user space read only
161  * @return Fdl_Document
162  */
163  public function insertUserDocument($config)
164  {
165  $out = '';
166  if ($this->domain) {
167  $docid = $config->docid;
168 
169  $err = $this->domain->insertUserDocument($docid, $this->domain->getSystemUserId(), false);
170  if ($err) $this->setError($err);
171  $fdoc = new Fdl_Document($docid);
172  $out = $fdoc->getDocument(true, false);
173 
174  } else {
175  $this->setError(_("domain not set"));
176  }
177  return $out;
178  }
179 
180  /**
181  * remove document from user space read only
182  * @return Fdl_Document
183  */
184  public function removeUserDocument($config)
185  {
186  $out = '';
187  if ($this->domain) {
188  $docid = $config->docid;
189 
190  $err = $this->domain->removeUserDocument($docid, $this->domain->getSystemUserId());
191  if ($err) $this->setError($err);
192  $fdoc = new Fdl_Document($docid);
193  $out = $fdoc->getDocument(true, false);
194 
195  } else {
196  $this->setError(_("domain not set"));
197  }
198  return $out;
199  }
200 
201  /**
202  * put document into user space read only
203  * @return Fdl_Document
204  */
205  public function insertSharedDocument($config)
206  {
207  $out = '';
208  if ($this->domain) {
209  $docid = $config->docid;
210 
211  $err = $this->domain->insertSharedDocument($docid);
212  if ($err) $this->setError($err);
213  $fdoc = new Fdl_Document($docid);
214  $out = $fdoc->getDocument(true, false);
215 
216  } else {
217  $this->setError(_("domain not set"));
218  }
219  return $out;
220  }
221 
222  /**
223  * remove document from user space read only
224  * @return Fdl_Document
225  */
226  public function removeSharedDocument($config)
227  {
228  $out = '';
229  if ($this->domain) {
230  $docid = $config->docid;
231 
232  $err = $this->domain->removeSharedDocument($docid, $this->domain->getSystemUserId());
233  if ($err) $this->setError($err);
234  $fdoc = new Fdl_Document($docid);
235  $out = $fdoc->getDocument(true, false);
236 
237  } else {
238  $this->setError(_("domain not set"));
239  }
240  return $out;
241  }
242 
243  /**
244  * return families restriction
245  * @return DocumentList of families
246  */
247  public function getAvailableFamilies()
248  {
249  include_once ("FDL/Class.DocumentList.php");
250 
251  if ($this->domain) {
252  $families = $this->domain->getFamilies();
253  $list = new DocumentList();
254  $list->addDocumentIdentificators($families);
255 
256  $domain = $this->domain;
257  $callback = function (&$family) use($domain)
258  {
259  $maskId = $domain->getOfflineMask($family->id);
260  if ($maskId) {
261  $family->applyMask($maskId, true);
262  }
263  };
264  $list->listMap($callback); // apply specific offline mask
265  $col = new Fdl_Collection();
266  $col->useDocumentList($list);
267  $col->setContentOnlyValue(false);
268  $col->setContentCompleteProperties(false);
269  $out = $col->getContent();
270 
271  } else {
272  $this->setError(_("domain not set"));
273  }
274  return $out;
275  }
276 
277  /**
278  * delete all documents from user folder
279  * @return void
280  */
281  public function clearUserDocuments()
282  {
283  if ($this->domain) {
284  $err = $this->domain->clearUserFolder($this->domain->getSystemUserId(), false);
285  if ($err) $this->setError($err);
286 
287  } else {
288  $this->setError(_("domain not set"));
289  }
290  return;
291  }
292 
293  /**
294  * delete all documents from user folder
295  * @return void
296  */
297  public function clearSharedDocuments()
298  {
299  if ($this->domain) {
300  $err = $this->domain->clearSharedFolder(false);
301  if ($err) $this->setError($err);
302 
303  } else {
304  $this->setError(_("domain not set"));
305  }
306  return;
307  }
308 
309  /**
310  * get user folder documents
311  * @param function $callback filter on documents
312  * @return DocumentList
313  */
314  public function getUserDocuments($config, $callback = null)
315  {
316  $date = $config->until;
317  if ($this->domain) {
318  $folder = $this->domain->getUserFolder();
319  if ($folder) {
320  $out = $this->getFolderDocuments($folder, $date, $callback);
321  } else {
322  $this->domain->disableEditControl();
323  $err = $this->domain->createSubDirectories();
324  $this->domain->enableEditControl();
325  if ($err) {
326  $this->setError(_("user folder not found:").$err);
327  } else {
328  $folder = $this->domain->getUserFolder();
329  if ($folder) {
330  $out = $this->getFolderDocuments($folder, $date, $callback);
331  } else {
332  $this->setError(_("user folder not found"));
333  }
334  }
335  }
336  } else {
337  $this->setError(_("domain not set"));
338  }
339  return $out;
340  }
341 
342  /**
343  * get share folder documents
344  * @param function $callback filter on documents
345  * @return DocumentList
346  */
347  public function getSharedDocuments($config, $callback = null)
348  {
349  $date = $config->until;
350  if ($this->domain) {
351  $folder = $this->domain->getSharedFolder();
352  if ($folder) {
353  $out = $this->getFolderDocuments($folder, $date, $callback);
354  } else {
355  $this->setError(_("no share folder"));
356  }
357  } else {
358  $this->setError(_("domain not set"));
359  }
360  return $out;
361  }
362 
363  /**
364  * get folder documents
365  * @param Dir $folder the folder to inspect
366  * @param string $mdate filter on modified date
367  * @param function $callback filter on documents
368  * @return DocumentList
369  */
370  private function getFolderDocuments(Dir &$folder, $mdate = '', $callback = null)
371  {
372  include_once ("FDL/Class.DocumentList.php");
373 
374  if ($this->domain) {
375  $col = new Fdl_Collection(0, null, $folder);
376 
377  if ($callback) $col->setContentMap($callback);
378  $col->setContentOnlyValue(true);
379  $col->setContentCompleteProperties(false);
380  if ($mdate) {
381  $umdate = stringDateToUnixTs($mdate);
382  $sqlDate = sprintf("revdate > %d", $umdate);
383  $col->setContentFilter($sqlDate);
384  }
385  //$col->setContentSlice(1000);
386  $out = $col->getContent();
387 
388  if ($out->totalCount > 0) $this->setReserveWaitingDocument($folder);
389 
390  } else {
391  $this->setError(_("domain not set"));
392  }
393  return $out;
394  }
395 
396  /**
397  * init waiting doc for reserved document
398  * @param Dir $folder
399  */
400  private function setReserveWaitingDocument(Dir &$folder)
401  {
402  include_once ("FDL/Class.DocWaitManager.php");
403  if ($folder) {
404 
405  $s = new SearchDoc($this->dbaccess);
406  $s->useCollection($folder->initid);
407  $s->setObjectReturn();
408  $uid = $folder->getSystemUserId();
409  $s->addFilter("locked = %d", $uid);
410  $s->addFilter("lockdomainid = %d", $this->domain->id);
411  $dl = $s->search()->getDocumentList();
412  $w = new DocWait($this->dbaccess);
413  foreach ( $dl as $k => $v ) {
414  if (!$w->select(array(
415  $v->initid,
416  $uid
417  ))) {
418  DocWaitManager::saveWaitingDoc($v, $this->domain->id);
419  }
420  }
421  }
422  }
423 
424  /**
425  * get document locked by user
426  * @return DocumentList
427  */
428  public function getReservedDocumentIds()
429  {
430  include_once ("FDL/Class.DocumentList.php");
431  $out = null;
432  if ($this->domain) {
433  $ids = $this->domain->getReservedDocumentIds();
434 
435  $out->reservedDocumentIds = $ids;
436 
437  } else {
438  $this->setError(_("domain not set"));
439  }
440  return $out;
441  }
442 
443  /**
444  * return object sync
445  * @return DomainSyncApi
446  */
447  public function sync()
448  {
449  include_once ("OFFLINE/Class.DomainSyncApi.php");
450  return new DomainSyncApi($this->domain, $this);
451  }
452 
453  /**
454  * return object sync
455  * @return DomainViewApi
456  */
457  public function view()
458  {
459  include_once ("OFFLINE/Class.DomainViewApi.php");
460  return new DomainViewApi($this->domain, $this);
461  }
462 }
463 ?>
insertSharedDocument($config)
removeSharedDocument($config)
getSharedDocuments($config, $callback=null)
__construct(Dir &$domain=null)
getUserDocuments($config, $callback=null)
static getDomains()
removeUserDocument($config)
revertDocument($config)
unbookDocument($config)
bookDocument($config)
insertUserDocument($config)
← centre documentaire © anakeen - published under CC License - Dynacase