@@ -645,4 +645,172 @@ private function findSolr($collections, $settings, $searchParams, $listedMetadat
645645 $ search ->prepare ();
646646 return $ search ;
647647 }
648+
649+ /**
650+ * Find the uid of the previous document relative to the current document uid.
651+ * Otherwise backtrack the closest previous leaf node.
652+ *
653+ * @access public
654+ *
655+ * @param int $uid
656+ *
657+ * @return int|null
658+ */
659+ public function getPreviousDocumentUid ($ uid )
660+ {
661+ $ currentDocument = $ this ->findOneByUid ($ uid );
662+ if ($ currentDocument ) {
663+ $ currentVolume = '' ;
664+ $ parentId = $ currentDocument ->getPartof ();
665+
666+ if ($ parentId ) {
667+
668+ $ currentVolume = (string ) $ currentDocument ->getVolumeSorting ();
669+
670+ $ connectionPool = GeneralUtility::makeInstance (ConnectionPool::class);
671+ $ queryBuilder = $ connectionPool ->getQueryBuilderForTable ('tx_dlf_documents ' );
672+
673+ // Grab previous volume
674+ $ prevDocument = $ queryBuilder
675+ ->select (
676+ 'tx_dlf_documents.uid AS uid '
677+ )
678+ ->from ('tx_dlf_documents ' )
679+ ->where (
680+ $ queryBuilder ->expr ()->eq ('tx_dlf_documents.partof ' , (int ) $ parentId ),
681+ 'tx_dlf_documents.volume_sorting < \'' . $ currentVolume . '\''
682+ )
683+ ->add ('orderBy ' , 'volume_sorting desc ' )
684+ ->addOrderBy ('tx_dlf_documents.volume_sorting ' )
685+ ->execute ()
686+ ->fetch ();
687+
688+ if (!empty ($ prevDocument )) {
689+ return $ prevDocument ['uid ' ];
690+ }
691+
692+ return $ this ->getLastChild ($ this ->getPreviousDocumentUid ($ parentId ));
693+ }
694+ }
695+
696+ return null ;
697+ }
698+
699+ /**
700+ * Find the uid of the next document relative to the current document uid.
701+ * Otherwise backtrack the closest next leaf node.
702+ *
703+ * @access public
704+ *
705+ * @param int $uid
706+ *
707+ * @return int|null
708+ */
709+ public function getNextDocumentUid ($ uid )
710+ {
711+ $ currentDocument = $ this ->findOneByUid ($ uid );
712+ if ($ currentDocument ) {
713+ $ currentVolume = '' ;
714+ $ parentId = $ currentDocument ->getPartof ();
715+
716+ if ($ parentId ) {
717+
718+ $ currentVolume = (string ) $ currentDocument ->getVolumeSorting ();
719+
720+ $ connectionPool = GeneralUtility::makeInstance (ConnectionPool::class);
721+ $ queryBuilder = $ connectionPool ->getQueryBuilderForTable ('tx_dlf_documents ' );
722+
723+ // Grab next volume
724+ $ nextDocument = $ queryBuilder
725+ ->select (
726+ 'tx_dlf_documents.uid AS uid '
727+ )
728+ ->from ('tx_dlf_documents ' )
729+ ->where (
730+ $ queryBuilder ->expr ()->eq ('tx_dlf_documents.partof ' , (int ) $ parentId ),
731+ 'tx_dlf_documents.volume_sorting > \'' . $ currentVolume . '\''
732+ )
733+ ->add ('orderBy ' , 'volume_sorting asc ' )
734+ ->addOrderBy ('tx_dlf_documents.volume_sorting ' )
735+ ->execute ()
736+ ->fetch ();
737+
738+ if (!empty ($ nextDocument )) {
739+ return $ nextDocument ['uid ' ];
740+ }
741+
742+ return $ this ->getFirstChild ($ this ->getNextDocumentUid ($ parentId ));
743+ }
744+ }
745+
746+ return null ;
747+ }
748+
749+ /**
750+ * Find the uid of the first leaf node
751+ *
752+ * @access public
753+ *
754+ * @param int $uid
755+ *
756+ * @return int
757+ */
758+ public function getFirstChild ($ uid )
759+ {
760+ $ connectionPool = GeneralUtility::makeInstance (ConnectionPool::class);
761+ $ queryBuilder = $ connectionPool ->getQueryBuilderForTable ('tx_dlf_documents ' );
762+
763+ $ child = $ queryBuilder
764+ ->select (
765+ 'tx_dlf_documents.uid AS uid '
766+ )
767+ ->from ('tx_dlf_documents ' )
768+ ->where (
769+ $ queryBuilder ->expr ()->eq ('tx_dlf_documents.partof ' , (int ) $ uid )
770+ )
771+ ->add ('orderBy ' , 'volume_sorting asc ' )
772+ ->addOrderBy ('tx_dlf_documents.volume_sorting ' )
773+ ->execute ()
774+ ->fetch ();
775+
776+ if (empty ($ child ['uid ' ])) {
777+ return $ uid ;
778+ }
779+
780+ return $ this ->getFirstChild ($ child ['uid ' ]);
781+ }
782+
783+ /**
784+ * Find the uid of the last leaf node
785+ *
786+ * @access public
787+ *
788+ * @param int $uid
789+ *
790+ * @return int
791+ */
792+ public function getLastChild ($ uid )
793+ {
794+ $ connectionPool = GeneralUtility::makeInstance (ConnectionPool::class);
795+ $ queryBuilder = $ connectionPool ->getQueryBuilderForTable ('tx_dlf_documents ' );
796+
797+ $ child = $ queryBuilder
798+ ->select (
799+ 'tx_dlf_documents.uid AS uid '
800+ )
801+ ->from ('tx_dlf_documents ' )
802+ ->where (
803+ $ queryBuilder ->expr ()->eq ('tx_dlf_documents.partof ' , (int ) $ uid )
804+ )
805+ ->add ('orderBy ' , 'volume_sorting desc ' )
806+ ->addOrderBy ('tx_dlf_documents.volume_sorting ' )
807+ ->execute ()
808+ ->fetch ();
809+
810+ if (empty ($ child ['uid ' ])) {
811+ return $ uid ;
812+ }
813+
814+ return $ this ->getFirstChild ($ child ['uid ' ]);
815+ }
648816}
0 commit comments