1111
1212namespace Kitodo \Dlf \Controller ;
1313
14- use Kitodo \Dlf \Common \AbstractDocument ;
1514use Kitodo \Dlf \Common \Helper ;
1615use Kitodo \Dlf \Domain \Model \Document ;
1716use Kitodo \Dlf \Domain \Repository \DocumentRepository ;
17+ use Kitodo \Dlf \Service \DocumentService ;
1818use Psr \Log \LoggerAwareInterface ;
1919use Psr \Log \LoggerAwareTrait ;
2020use TYPO3 \CMS \Core \Configuration \ExtensionConfiguration ;
2626use TYPO3 \CMS \Core \Pagination \PaginatorInterface ;
2727use TYPO3 \CMS \Extbase \Mvc \Controller \ActionController ;
2828
29+
2930/**
3031 * Abstract controller class for most of the plugin controller.
3132 *
@@ -40,24 +41,6 @@ abstract class AbstractController extends ActionController implements LoggerAwar
4041{
4142 use LoggerAwareTrait;
4243
43- /**
44- * @access protected
45- * @var DocumentRepository
46- */
47- protected DocumentRepository $ documentRepository ;
48-
49- /**
50- * @access public
51- *
52- * @param DocumentRepository $documentRepository
53- *
54- * @return void
55- */
56- public function injectDocumentRepository (DocumentRepository $ documentRepository ): void
57- {
58- $ this ->documentRepository = $ documentRepository ;
59- }
60-
6144 /**
6245 * @access protected
6346 * @var Document|null This holds the current document
@@ -87,7 +70,30 @@ public function injectDocumentRepository(DocumentRepository $documentRepository)
8770 * @var int
8871 */
8972 protected int $ pageUid ;
73+ /**
74+ * @access protected
75+ * @var DocumentRepository
76+ */
77+ protected DocumentRepository $ documentRepository ;
78+ /**
79+ * @access protected
80+ * @var DocumentService
81+ */
82+ protected DocumentService $ documentService ;
83+
84+ public function __construct ()
85+ {
86+ $ this ->initialize ();
87+ }
9088
89+ public function injectDocumentService (DocumentService $ documentService )
90+ {
91+ $ this ->documentService = $ documentService ;
92+ }
93+ public function injectDocumentRepository (DocumentRepository $ repo ): void
94+ {
95+ $ this ->documentRepository = $ repo ;
96+ }
9197 /**
9298 * Initialize the plugin controller
9399 *
@@ -114,56 +120,16 @@ protected function initialize(): void
114120 'requestData ' => $ this ->requestData
115121 ];
116122 }
117-
118123 /**
119- * Loads the current document into $this->document
124+ * Load the current Document into Memory just once with Document Service - it will then be available for all controllers.
120125 *
121126 * @access protected
122- *
123- * @param int $documentId The document's UID (fallback: $this->requestData[id])
124- *
125127 * @return void
126128 */
127- protected function loadDocument (int $ documentId = 0 ): void
129+ protected function loadDocument (): void
128130 {
129- // Sanitize FlexForm settings to avoid later casting.
130131 $ this ->sanitizeSettings ();
131-
132- // Get document ID from request data if not passed as parameter.
133- if ($ documentId === 0 && !empty ($ this ->requestData ['id ' ])) {
134- $ documentId = $ this ->requestData ['id ' ];
135- }
136-
137- // Try to get document format from database
138- if (!empty ($ documentId )) {
139-
140- $ doc = null ;
141-
142- if (MathUtility::canBeInterpretedAsInteger ($ documentId )) {
143- $ doc = $ this ->getDocumentByUid ($ documentId );
144- } elseif (GeneralUtility::isValidUrl ($ documentId )) {
145- $ doc = $ this ->getDocumentByUrl ($ documentId );
146- }
147-
148- if ($ this ->document !== null && $ doc !== null ) {
149- $ this ->document ->setCurrentDocument ($ doc );
150- }
151-
152- } elseif (!empty ($ this ->requestData ['recordId ' ])) {
153-
154- $ this ->document = $ this ->documentRepository ->findOneByRecordId ($ this ->requestData ['recordId ' ]);
155-
156- if ($ this ->document !== null ) {
157- $ doc = AbstractDocument::getInstance ($ this ->document ->getLocation (), $ this ->settings , true );
158- if ($ doc !== null ) {
159- $ this ->document ->setCurrentDocument ($ doc );
160- } else {
161- $ this ->logger ->error ('Failed to load document with record ID " ' . $ this ->requestData ['recordId ' ] . '" ' );
162- }
163- }
164- } else {
165- $ this ->logger ->error ('Invalid ID " ' . $ documentId . '" or PID " ' . $ this ->settings ['storagePid ' ] . '" for document loading ' );
166- }
132+ $ this ->document = $ this ->documentService ->getDocument ($ this ->requestData ['id ' ], $ this ->settings );
167133 }
168134
169135 /**
@@ -368,18 +334,7 @@ protected function setDefaultPage(): void
368334 $ this ->viewData ['requestData ' ] = $ this ->requestData ;
369335 }
370336
371- /**
372- * This is the constructor
373- *
374- * @access public
375- *
376- * @return void
377- */
378- public function __construct ()
379- {
380- $ this ->initialize ();
381- }
382-
337+
383338 /**
384339 * build simple pagination
385340 *
@@ -500,65 +455,4 @@ protected function buildSimplePagination(PaginationInterface $pagination, Pagina
500455 'pagesR ' => $ pagesSect
501456 ];
502457 }
503-
504- /**
505- * Get document from repository by uid.
506- *
507- * @access private
508- *
509- * @param int $documentId The document's UID
510- *
511- * @return AbstractDocument
512- */
513- private function getDocumentByUid (int $ documentId )
514- {
515- $ doc = null ;
516- $ this ->document = $ this ->documentRepository ->findOneByIdAndSettings ($ documentId );
517-
518- if ($ this ->document ) {
519- $ doc = AbstractDocument::getInstance ($ this ->document ->getLocation (), $ this ->settings , false );
520- } else {
521- $ this ->logger ->error ('Invalid UID " ' . $ documentId . '" or PID " ' . $ this ->settings ['storagePid ' ] . '" for document loading ' );
522- }
523-
524- return $ doc ;
525- }
526-
527- /**
528- * Get document by URL.
529- *
530- * @access private
531- *
532- * @param string $documentId The document's URL
533- *
534- * @return AbstractDocument
535- */
536- private function getDocumentByUrl (string $ documentId )
537- {
538- $ doc = AbstractDocument::getInstance ($ documentId , $ this ->settings , true );
539-
540- if ($ doc !== null ) {
541- if ($ doc ->recordId ) {
542- // find document from repository by recordId
543- $ docFromRepository = $ this ->documentRepository ->findOneByRecordId ($ doc ->recordId );
544- if ($ docFromRepository !== null ) {
545- $ this ->document = $ docFromRepository ;
546- } else {
547- // create new dummy Document object
548- $ this ->document = GeneralUtility::makeInstance (Document::class);
549- }
550- }
551-
552- // Make sure configuration PID is set when applicable
553- if ($ doc ->cPid == 0 ) {
554- $ doc ->cPid = max ($ this ->settings ['storagePid ' ], 0 );
555- }
556-
557- $ this ->document ->setLocation ($ documentId );
558- } else {
559- $ this ->logger ->error ('Invalid location given " ' . $ documentId . '" for document loading ' );
560- }
561-
562- return $ doc ;
563- }
564458}
0 commit comments