forked from effective-webwork/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentRepository.php
More file actions
807 lines (714 loc) · 26.6 KB
/
DocumentRepository.php
File metadata and controls
807 lines (714 loc) · 26.6 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<?php
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Kitodo\Dlf\Domain\Repository;
use Doctrine\DBAL\ForwardCompatibility\Result;
use Kitodo\Dlf\Common\AbstractDocument;
use Kitodo\Dlf\Common\Helper;
use Kitodo\Dlf\Common\Solr\SolrSearch;
use Kitodo\Dlf\Domain\Model\Collection;
use Kitodo\Dlf\Domain\Model\Document;
use Kitodo\Dlf\Domain\Model\Structure;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
/**
* Document repository.
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class DocumentRepository extends Repository
{
/**
* @access protected
* @var array The controller settings passed to the repository for some special actions.
*/
protected $settings;
/**
* Find one document by given parameters
*
* GET parameters may be:
*
* - 'id': the uid of the document
* - 'location': the URL of the location of the XML file
* - 'recordId': the record_id of the document
*
* Currently used by EXT:slub_digitalcollections
*
* @access public
*
* @param array $parameters
*
* @return Document|null
*/
public function findOneByParameters($parameters)
{
$doc = null;
$document = null;
if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) {
$document = $this->findOneByIdAndSettings($parameters['id']);
} else if (isset($parameters['recordId'])) {
$document = $this->findOneByRecordId($parameters['recordId']);
} else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) {
$doc = AbstractDocument::getInstance($parameters['location'], [], true);
if ($doc->recordId) {
$document = $this->findOneByRecordId($doc->recordId);
}
if ($document === null) {
// create new (dummy) Document object
$document = GeneralUtility::makeInstance(Document::class);
$document->setLocation($parameters['location']);
}
}
if ($document !== null && $doc === null) {
$doc = AbstractDocument::getInstance($document->getLocation(), [], true);
}
if ($doc !== null) {
$document->setCurrentDocument($doc);
}
return $document;
}
/**
* Find the oldest document
*
* @access public
*
* @return Document|null
*/
public function findOldestDocument()
{
$query = $this->createQuery();
$query->setOrderings(['tstamp' => QueryInterface::ORDER_ASCENDING]);
$query->setLimit(1);
return $query->execute()->getFirst();
}
/**
* @access public
*
* @param int $partOf
* @param Structure $structure
*
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function getChildrenOfYearAnchor($partOf, $structure)
{
$query = $this->createQuery();
$query->matching($query->equals('structure', $structure));
$query->matching($query->equals('partof', $partOf));
$query->setOrderings([
'mets_orderlabel' => QueryInterface::ORDER_ASCENDING
]);
return $query->execute();
}
/**
* Finds all documents for the given settings
*
* @access public
*
* @param int $uid
* @param array $settings
*
* @return Document|null
*/
public function findOneByIdAndSettings($uid, $settings = [])
{
$settings = ['documentSets' => $uid];
return $this->findDocumentsBySettings($settings)->getFirst();
}
/**
* Finds all documents for the given settings
*
* @access public
*
* @param array $settings
*
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findDocumentsBySettings($settings = [])
{
$query = $this->createQuery();
$constraints = [];
if ($settings['documentSets']) {
$constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets']));
}
if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) {
$query->getQuerySettings()->setRespectStoragePage(false);
}
if (count($constraints)) {
$query->matching(
$query->logicalAnd($constraints)
);
}
return $query->execute();
}
/**
* Finds all documents for the given collections and conditions
*
* @access public
*
* @param array $collections
* @param int $limit
* @param int $offset
*
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByCollectionsLimited($collections, int $limit = 50, int $offset = 0)
{
$query = $this->createQuery();
// order by start_date -> start_time...
$query->setOrderings(
['tstamp' => QueryInterface::ORDER_DESCENDING]
);
$constraints = [];
if ($collections) {
$constraints[] = $query->in('collections.uid', $collections);
}
if (count($constraints)) {
$query->matching(
$query->logicalAnd($constraints)
);
}
if ($limit > 0) {
$query->setLimit((int) $limit);
$query->setOffset($offset);
}
return $query->execute();
}
/**
* Count the titles and volumes for statistics
*
* Volumes are documents that are both
* a) "leaf" elements i.e. partof != 0
* b) "root" elements that are not referenced by other documents ("root" elements that have no descendants)
*
* @access public
*
* @param array $settings
*
* @return array
*/
public function getStatisticsForSelectedCollection($settings)
{
if ($settings['collections']) {
// Include only selected collections.
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$countTitles = $queryBuilder
->count('tx_dlf_documents.uid')
->from('tx_dlf_documents')
->innerJoin(
'tx_dlf_documents',
'tx_dlf_relations',
'tx_dlf_relations_joins',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_local',
'tx_dlf_documents.uid'
)
)
->innerJoin(
'tx_dlf_relations_joins',
'tx_dlf_collections',
'tx_dlf_collections_join',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_foreign',
'tx_dlf_collections_join.uid'
)
)
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->eq('tx_dlf_documents.partof', 0),
$queryBuilder->expr()->in('tx_dlf_collections_join.uid', $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', $settings['collections']), Connection::PARAM_INT_ARRAY)),
$queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', $queryBuilder->createNamedParameter('docs_colls'))
)
->execute()
->fetchFirstColumn();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$subQuery = $subQueryBuilder
->select('tx_dlf_documents.partof')
->from('tx_dlf_documents')
->where(
$subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0)
)
->groupBy('tx_dlf_documents.partof')
->getSQL();
$countVolumes = $queryBuilder
->count('tx_dlf_documents.uid')
->from('tx_dlf_documents')
->innerJoin(
'tx_dlf_documents',
'tx_dlf_relations',
'tx_dlf_relations_joins',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_local',
'tx_dlf_documents.uid'
)
)
->innerJoin(
'tx_dlf_relations_joins',
'tx_dlf_collections',
'tx_dlf_collections_join',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_foreign',
'tx_dlf_collections_join.uid'
)
)
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery),
$queryBuilder->expr()->in('tx_dlf_collections_join.uid', $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', $settings['collections']), Connection::PARAM_INT_ARRAY)),
$queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', $queryBuilder->createNamedParameter('docs_colls'))
)
->execute()
->fetchFirstColumn();
} else {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
// Include all collections.
$countTitles = $queryBuilder
->count('tx_dlf_documents.uid')
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->eq('tx_dlf_documents.partof', 0),
Helper::whereExpression('tx_dlf_documents')
)
->execute()
->fetchFirstColumn();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$subQuery = $subQueryBuilder
->select('tx_dlf_documents.partof')
->from('tx_dlf_documents')
->where(
$subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0)
)
->groupBy('tx_dlf_documents.partof')
->getSQL();
$countVolumes = $queryBuilder
->count('tx_dlf_documents.uid')
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])),
$queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery)
)
->execute()
->fetchFirstColumn();
}
return ['titles' => $countTitles, 'volumes' => $countVolumes];
}
/**
* Build table of contents
*
* @access public
*
* @param int $uid
* @param int $pid
* @param array $settings
*
* @return Result
*/
public function getTableOfContentsFromDb($uid, $pid, $settings)
{
// Build table of contents from database.
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$excludeOtherWhere = '';
if ($settings['excludeOther']) {
$excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['storagePid']);
}
// Check if there are any metadata to suggest.
return $queryBuilder
->select(
'tx_dlf_documents.uid AS uid',
'tx_dlf_documents.title AS title',
'tx_dlf_documents.volume AS volume',
'tx_dlf_documents.year AS year',
'tx_dlf_documents.mets_label AS mets_label',
'tx_dlf_documents.mets_orderlabel AS mets_orderlabel',
'tx_dlf_structures_join.index_name AS type'
)
->innerJoin(
'tx_dlf_documents',
'tx_dlf_structures',
'tx_dlf_structures_join',
$queryBuilder->expr()->eq(
'tx_dlf_structures_join.uid',
'tx_dlf_documents.structure'
)
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)),
$queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)),
$excludeOtherWhere
)
->add('orderBy', 'cast(volume_sorting as UNSIGNED) asc')
->addOrderBy('tx_dlf_documents.mets_orderlabel')
->execute();
}
/**
* Find one document by given settings and identifier
*
* @access public
*
* @param array $settings
* @param array $parameters
*
* @return array The found document object
*/
public function getOaiRecord($settings, $parameters)
{
$where = '';
if (!$settings['show_userdefined']) {
$where .= 'AND tx_dlf_collections.fe_cruser_id=0 ';
}
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_dlf_documents');
$sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .
'FROM `tx_dlf_documents` ' .
'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .
'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .
'WHERE `tx_dlf_documents`.`record_id` = ? ' .
'AND `tx_dlf_relations`.`ident`="docs_colls" ' .
$where;
$values = [
$parameters['identifier']
];
$types = [
Connection::PARAM_STR
];
// Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query
$statement = $connection->executeQuery($sql, $values, $types);
return $statement->fetchAssociative();
}
/**
* Finds all documents for the given settings
*
* @access public
*
* @param array $documentsToProcess
*
* @return Result The found document objects
*/
public function getOaiDocumentList($documentsToProcess): Result
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_dlf_documents');
$sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .
'FROM `tx_dlf_documents` ' .
'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .
'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .
'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' .
'AND `tx_dlf_relations`.`ident`="docs_colls" ' .
'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' .
'GROUP BY `tx_dlf_documents`.`uid` ';
$values = [
$documentsToProcess,
];
$types = [
Connection::PARAM_INT_ARRAY,
];
// Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query
return $connection->executeQuery($sql, $values, $types);
}
/**
* Finds all documents with given uids
*
* @access public
*
* @param array $uids
* @param bool $checkPartof Whether or not to also match $uids against partof.
*
* @return array
*/
public function findAllByUids($uids, $checkPartof = false)
{
// get all documents from db we are talking about
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_documents');
// Fetch document info for UIDs in $documentSet from DB
$exprDocumentMatchesUid = $queryBuilder->expr()->in('tx_dlf_documents.uid', $uids);
if ($checkPartof) {
$exprDocumentMatchesUid = $queryBuilder->expr()->orX(
$exprDocumentMatchesUid,
$queryBuilder->expr()->in('tx_dlf_documents.partof', $uids)
);
}
$kitodoDocuments = $queryBuilder
->select(
'tx_dlf_documents.uid AS uid',
'tx_dlf_documents.title AS title',
'tx_dlf_documents.structure AS structure',
'tx_dlf_documents.thumbnail AS thumbnail',
'tx_dlf_documents.volume_sorting AS volumeSorting',
'tx_dlf_documents.mets_orderlabel AS metsOrderlabel',
'tx_dlf_documents.partof AS partOf'
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->in('tx_dlf_documents.pid', $this->settings['storagePid']),
$exprDocumentMatchesUid
)
->add('orderBy', 'cast(volume_sorting as UNSIGNED) asc')
->addOrderBy('tx_dlf_documents.mets_orderlabel', 'asc')
->execute();
$allDocuments = [];
$documentStructures = Helper::getDocumentStructures($this->settings['storagePid']);
// Process documents in a usable array structure
while ($resArray = $kitodoDocuments->fetchAssociative()) {
$resArray['structure'] = $documentStructures[$resArray['structure']];
$allDocuments[$resArray['uid']] = $resArray;
}
return $allDocuments;
}
/**
* @access public
*
* @param array $uids
*
* @return array
*/
public function findChildrenOfEach(array $uids)
{
$allDocuments = $this->findAllByUids($uids, true);
$result = [];
foreach ($allDocuments as $doc) {
if ($doc['partOf']) {
$result[$doc['partOf']][] = $doc;
}
}
return $result;
}
/**
* Find all documents with given collection from Solr
*
* @access public
*
* @param Collection $collection
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
*
* @return SolrSearch
*/
public function findSolrByCollection(Collection $collection, $settings, $searchParams, $listedMetadata = null)
{
return $this->findSolr([$collection], $settings, $searchParams, $listedMetadata);
}
/**
* Find all documents with given collections from Solr
*
* @access public
*
* @param array|QueryResultInterface $collections
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
*
* @return SolrSearch
*/
public function findSolrByCollections($collections, $settings, $searchParams, $listedMetadata = null): SolrSearch
{
return $this->findSolr($collections, $settings, $searchParams, $listedMetadata);
}
/**
* Find all documents without given collection from Solr
*
* @access public
*
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
*
* @return SolrSearch
*/
public function findSolrWithoutCollection($settings, $searchParams, $listedMetadata = null): SolrSearch
{
return $this->findSolr([], $settings, $searchParams, $listedMetadata);
}
/**
* Find all documents with from Solr
*
* @access private
*
* @param array|QueryResultInterface $collections
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
*
* @return SolrSearch
*/
private function findSolr($collections, $settings, $searchParams, $listedMetadata = null): SolrSearch
{
// set settings global inside this repository
// (may be necessary when SolrSearch calls back)
$this->settings = $settings;
$search = new SolrSearch($this, $collections, $settings, $searchParams, $listedMetadata);
$search->prepare();
return $search;
}
/**
* Find the uid of the previous document relative to the current document uid.
* Otherwise backtrack the closest previous leaf node.
*
* @access public
*
* @param int $uid
*
* @return int
*/
public function getPreviousDocumentUid($uid)
{
$currentDocument = $this->findOneByUid($uid);
if ($parentId = $currentDocument->getPartof()) {
$currentVolume['volume_sorting'] = $currentDocument->getVolumeSorting();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
// Grab previous volume
$prevDocument = $queryBuilder
->select(
'tx_dlf_documents.uid AS uid'
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($parentId)),
'tx_dlf_documents.volume_sorting < \'' . strval($currentVolume['volume_sorting']) . '\''
)
->add('orderBy', 'volume_sorting desc')
->addOrderBy('tx_dlf_documents.volume_sorting')
->execute()
->fetch();
if (!empty($prevDocument))
{
return $prevDocument['uid'];
}
$previousDocumentId = $this->getPreviousDocumentUid($parentId);
if ($previousDocumentId) {
return $this->getLastChild($previousDocumentId);
}
}
}
/**
* Find the uid of the next document relative to the current document uid.
* Otherwise backtrack the closest next leaf node.
*
* @access public
*
* @param int $uid
*
* @return int
*/
public function getNextDocumentUid($uid)
{
$currentDocument = $this->findOneByUid($uid);
if ($parentId = $currentDocument->getPartof()) {
$currentVolume['volume_sorting'] = $currentDocument->getVolumeSorting();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
// Grab next volume
$nextDocument = $queryBuilder
->select(
'tx_dlf_documents.uid AS uid'
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($parentId)),
'tx_dlf_documents.volume_sorting > \'' . strval($currentVolume['volume_sorting']) . '\''
)
->add('orderBy', 'volume_sorting asc')
->addOrderBy('tx_dlf_documents.volume_sorting')
->execute()
->fetch();
if (!empty($nextDocument))
{
return $nextDocument['uid'];
}
$nextDocumentId = $this->getNextDocumentUid($parentId);
if ($nextDocumentId) {
return $this->getFirstChild($nextDocumentId);
}
}
}
/**
* Find the uid of the first leaf node
*
* @access public
*
* @param int $uid
*
* @return int
*/
public function getFirstChild($uid) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$child = $queryBuilder
->select(
'tx_dlf_documents.uid AS uid'
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid))
)
->add('orderBy', 'volume_sorting asc')
->addOrderBy('tx_dlf_documents.volume_sorting')
->execute()
->fetch();
if(empty($child['uid']))
{
return $uid;
}
return $this->getFirstChild($child['uid']);
}
/**
* Find the uid of the last leaf node
*
* @access public
*
* @param int $uid
*
* @return int
*/
public function getLastChild($uid) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$child = $queryBuilder
->select(
'tx_dlf_documents.uid AS uid'
)
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid))
)
->add('orderBy', 'volume_sorting desc')
->addOrderBy('tx_dlf_documents.volume_sorting')
->execute()
->fetch();
if(empty($child['uid']))
{
return $uid;
}
return $this->getFirstChild($child['uid']);
}
}