[FEATURE] Add Preview for Rendering 12/15512/1
authorBastian Bringenberg <mail@bastian-bringenberg.de>
Thu, 11 Oct 2012 11:39:49 +0000 (13:39 +0200)
committerBastian Bringenberg <mail@bastian-bringenberg.de>
Thu, 11 Oct 2012 11:39:49 +0000 (13:39 +0200)
Now a preview button is included and renders a preview.

Change-Id: I3e7db3a88bf23c960728534963c904cebac2b62b
Fixes: #41469 @1h00

Classes/Controller/PageController.php
Classes/Helper/RenderHelper.php
Resources/Private/Partials/Page/Preview.html [new file with mode: 0644]
Resources/Private/Templates/Page/Edit.html

index 79a8791..b10ed90 100755 (executable)
@@ -110,9 +110,11 @@ class Tx_Typo3wiki_Controller_PageController extends Tx_Extbase_MVC_Controller_A
         *
         * @param Tx_Typo3wiki_Domain_Model_Page $page
         * @dontvalidate $page
+     * @param string $unrenderedText
+     * @dontvalidate $unrenderedText
         * @return void
         */
-       public function editAction(Tx_Typo3wiki_Domain_Model_Page $page = NULL) {
+       public function editAction(Tx_Typo3wiki_Domain_Model_Page $page = NULL, $unrenderedText = NULL) {
                if($page === NULL) $page = $this->pageRepository->findOneByPageTitle($this->request->getArgument('page'));
                if($page === NULL){
                        $page = $this->objectManager->get('Tx_Typo3wiki_Domain_Model_Page');
@@ -121,6 +123,12 @@ class Tx_Typo3wiki_Controller_PageController extends Tx_Extbase_MVC_Controller_A
                        $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
                        $persistenceManager->persistAll();
                }
+        $preview = NULL;
+        if($unrenderedText !== NULL){
+            $renderHelper = $this->createRenderHelper();
+            $preview = $renderHelper->renderText($unrenderedText);
+        }
+        $this->view->assign('preview', $preview);
                $this->view->assign('page', $page);
        }
 
@@ -131,10 +139,21 @@ class Tx_Typo3wiki_Controller_PageController extends Tx_Extbase_MVC_Controller_A
         * @return void
         */
        public function updateAction(Tx_Typo3wiki_Domain_Model_Page $page) {
-               if($page === NULL) $page = $this->pageRepository->findOneByPageTitle($this->request->getArgument('page'));
-               $text = $this->request->getArgument('text') ;
+               if($page === NULL) $page = $this->pageRepository->findOneByPageTitle($this->request->getArgument('page'));
+               $text = $this->request->getArgument('text');
+        try{
+            $this->request->getArgument('preview');
+            $preview = TRUE;
+        }catch(Exception $e){
+            $preview = FALSE;
+        }
+
+        if(isset($page) && $preview){
+            $this->forward('edit', NULL, NULL, array('page'=>$page, 'unrenderedText'=>$text));
+        }
+
 
-               $revision = $this->objectManager->get('Tx_Typo3wiki_Domain_Model_TextRevision');
+        $revision = $this->objectManager->get('Tx_Typo3wiki_Domain_Model_TextRevision');
                $revision->setUnrenderedText($text);
                $revision->setWriteDate(new DateTime('NOW'));
                $revision->setRenderedText('');
index c9ba1ce..950abf4 100644 (file)
        class Tx_Typo3wiki_Helper_RenderHelper extends MarkdownExtra_Parser {
 
                /**
-                * Method for getting a PageObject by Title. If Object does not exists, it will be created.
-                *
-                * @param string $title
-                * @return Tx_Typo3wiki_Domain_Model_Page
-                */
-               private function createPageIfNotExists($title){
-                       $returnPage = $this->pageRepository->findOneByPageTitle($title);
-                       if($returnPage === NULL){
-                               $returnPage = $this->objectManager->get('Tx_Typo3wiki_Domain_Model_Page');
-                               $returnPage->setPageTitle($title);
-                               $this->pageRepository->add($returnPage);
-                               $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
-                               $persistenceManager->persistAll();
-                       }
-                       return $returnPage;
-
-               }
-
-               /**
                 * relatedPageSearch
                 *
                 * @var string
                        return $text;
                }
 
+        /**
+         * Method for getting a PageObject by Title. If Object does not exists, it will be created.
+         *
+         * @param string $title
+         * @return Tx_Typo3wiki_Domain_Model_Page
+         */
+        private function createPageIfNotExists($title){
+            $returnPage = $this->pageRepository->findOneByPageTitle($title);
+            if($returnPage === NULL){
+                $returnPage = $this->objectManager->get('Tx_Typo3wiki_Domain_Model_Page');
+                $returnPage->setPageTitle($title);
+                $this->pageRepository->add($returnPage);
+                $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
+                $persistenceManager->persistAll();
+            }
+            return $returnPage;
+
+        }
+
                /**
                 * Removes Cache of Related Categories; Fired on update
                 *
                                }
 
                        }
-
-                       foreach($this->helper as $relatedPage){
-                               if($relatedPage->getMainRevision() === NULL){
-                                       if(!$relatedPage->getRelatedPages()->contains($this->relatedPage)){
-                                               $relatedPage->addRelatedPage($this->relatedPage);
-                                       }
-                               }
-                       }
+            if($this->relatedPage !== NULL){
+                foreach($this->helper as $relatedPage){
+                    if($relatedPage->getMainRevision() === NULL){
+                        if(!$relatedPage->getRelatedPages()->contains($this->relatedPage)){
+                            $relatedPage->addRelatedPage($this->relatedPage);
+                        }
+                    }
+                }
+            }
                        $this->helper = NULL;
                        return $text;
                }
                        if($cat[0] === '{TOC}') return $cat[0];
                        if($cat[0] === '{LOC}') return $cat[0];
                        $this->helper[1][] = $cat[1];
-                       $tmpPage = $this->createPageIfNotExists($cat[1]);
-                       if(!$tmpPage->getCategoryPages()->contains($this->relatedPage)) $tmpPage->addCategoryPage($this->relatedPage);
-                       if($tmpPage->getIsCategory() === FALSE)     $tmpPage->setIsCategory(TRUE);
+            if($this->relatedPage !== NULL){
+                           $tmpPage = $this->createPageIfNotExists($cat[1]);
+                       if(!$tmpPage->getCategoryPages()->contains($this->relatedPage)) $tmpPage->addCategoryPage($this->relatedPage);
+                           if($tmpPage->getIsCategory() === FALSE)     $tmpPage->setIsCategory(TRUE);
+            }
                        return '';
                }
 
                 */
                private function _renderCategoriesList($text){
                        if(strpos($text, '{LOC}') !== FALSE){
-                               $tocView = $this->createViewHelper('CategoryList');
-                               $tocView->assign('pages', $this->relatedPage->getCategoryPages());
-                               $helper = $tocView->render();
-                               $text = str_replace('{LOC}', $helper, $text);
+                if($this->relatedPage !== NULL){
+                                   $tocView = $this->createViewHelper('CategoryList');
+                                   $tocView->assign('pages', $this->relatedPage->getCategoryPages());
+                                   $helper = $tocView->render();
+                                   $text = str_replace('{LOC}', $helper, $text);
+                }
                        }
                        return $text;
                }
diff --git a/Resources/Private/Partials/Page/Preview.html b/Resources/Private/Partials/Page/Preview.html
new file mode 100644 (file)
index 0000000..6b01c38
--- /dev/null
@@ -0,0 +1,3 @@
+<div class="textPreview">
+    <f:format.raw>{text}</f:format.raw>
+</div>
\ No newline at end of file
index 9cd83bf..14e32ae 100755 (executable)
@@ -3,12 +3,13 @@
     <h1>Edit Page</h1>
     <f:if condition="{preview}">
         <f:then>
-            <f:render partial="Edit/Preview" arguments="{text:preview}"/>
+            <f:render partial="Page/Preview" arguments="{text:preview}"/>
         </f:then>
     </f:if>
     <h2>{page.pageTitle}</h2>
     <f:form action="update" arguments="{page:page}">
         <f:form.textarea cols="20" rows="5" name="text" value="{page.mainRevision.unrenderedText}" />
         <f:form.submit name="command" value="Save" />
+        <f:form.submit name="preview" value="Preview" />
     </f:form>
 </f:section>
\ No newline at end of file