| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CloudControl\Cms\components { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     use CloudControl\Cms\storage\entities\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     use CloudControl\Cms\storage\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * Class DocumentComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Has optional parameter `folder` to prefix the relative url with a folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * Has optional parameter `document` to select a given document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Has optional parameter `documentParameterName` to select the parametername to be used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      *        to set the found document to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @package CloudControl\Cms\components | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     class DocumentComponent extends NotFoundComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         protected $documentParameterName = self::PARAMETER_DOCUMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         const DOCUMENT_STATE_UNPUBLISHED = 'unpublished'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         const DOCUMENT_STATE_PUBLISHED = 'published'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         const DOCUMENT_TYPE_FOLDER = 'folder'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         const PARAMETER_DOCUMENT = 'document'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         const PARAMETER_DOCUMENT_PARAMETER_NAME = 'documentParameterName'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |          * @param Storage $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |          * @return mixed|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |          * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         public function run(Storage $storage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             parent::run($storage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->checkParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $this->runLikeApplicationComponent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $this->runLikeRegularComponent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |          * Checks to see if any parameters were defined in the cms and acts according | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         protected function checkParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (isset($this->parameters[self::PARAMETER_DOCUMENT_PARAMETER_NAME])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $this->documentParameterName = $this->parameters[self::PARAMETER_DOCUMENT_PARAMETER_NAME]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |          * Run as application component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |          * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         protected function runLikeApplicationComponent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (isset($this->parameters[self::PARAMETER_DOCUMENT])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters[self::PARAMETER_DOCUMENT]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 unset($this->parameters[self::PARAMETER_DOCUMENT]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 throw new \RuntimeException('When used as application component, you need to specify a document.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          * Run as regular component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         protected function runLikeRegularComponent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if ($this->matchedSitemapItem->regex === false || isset($this->parameters[self::PARAMETER_DOCUMENT])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $this->runWithoutRegex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $this->runWithRegex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |          * Run without regex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |          * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         protected function runWithoutRegex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (isset($this->parameters[self::PARAMETER_DOCUMENT])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->runByDocumentParameter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 throw new \RuntimeException('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |          * Run with regex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |          * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         protected function runWithRegex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $relativeDocumentUri = $this->checkForSpecificFolder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $state = $this->getState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $document = $this->storage->getDocuments()->getDocumentBySlug($relativeDocumentUri, $state); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if ($document instanceof Document && $document->state === self::DOCUMENT_STATE_PUBLISHED && $document->type !== self::DOCUMENT_TYPE_FOLDER) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $this->parameters[$this->documentParameterName] = $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->set404Header(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $this->set404Template(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |          * Run using the given `document` parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |          * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         protected function runByDocumentParameter() | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             $state = $this->getState(); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             $document = $this->storage->getDocuments()->getDocumentBySlug($this->parameters[self::PARAMETER_DOCUMENT], $state); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |             if ($document instanceof Document) { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                 $this->parameters[$this->documentParameterName] = $document; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |                 $this->set404Header(); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                 $this->set404Template(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |          * @return mixed|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         protected function checkForSpecificFolder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             if (isset($this->parameters[self::DOCUMENT_TYPE_FOLDER])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 if (substr($this->parameters[self::DOCUMENT_TYPE_FOLDER], -1) !== '/') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     $this->parameters[self::DOCUMENT_TYPE_FOLDER] .= '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 $relativeDocumentUri = $this->parameters[self::DOCUMENT_TYPE_FOLDER] . $relativeDocumentUri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             return $relativeDocumentUri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |          * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         protected function getState() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             $state = CmsComponent::isCmsLoggedIn() ? self::DOCUMENT_STATE_UNPUBLISHED : self::DOCUMENT_STATE_PUBLISHED; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             return $state; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  | } |