From: <gem...@li...> - 2011-12-15 15:54:29
|
Revision: 360 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=360&view=rev Author: matijsdejong Date: 2011-12-15 15:54:19 +0000 (Thu, 15 Dec 2011) Log Message: ----------- Added initRawOutput() and disableLayout() to MUtil_Controller_Action Renamed MUtil/Html/Link to MUtil/Html/Code Modified Paths: -------------- trunk/library/classes/Gems/Controller/BrowseEditAction.php trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php trunk/library/classes/Gems/Default/TrackActionAbstract.php trunk/library/classes/Gems/Pdf.php trunk/library/classes/MUtil/Controller/Action.php trunk/library/classes/MUtil/Controller/html-view.phtml trunk/library/classes/MUtil/Html/ProgressPanel.php trunk/library/layouts/scripts/gems.phtml Added Paths: ----------- trunk/library/classes/MUtil/Html/Code/ trunk/library/classes/MUtil/Html/Code/DynamicAbstract.php trunk/library/classes/MUtil/Html/Code/JavaScript.php Removed Paths: ------------- trunk/library/classes/MUtil/Html/Link/ Modified: trunk/library/classes/Gems/Controller/BrowseEditAction.php =================================================================== --- trunk/library/classes/Gems/Controller/BrowseEditAction.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/Gems/Controller/BrowseEditAction.php 2011-12-15 15:54:19 UTC (rev 360) @@ -275,7 +275,7 @@ // MUtil_Model::$verbose = true; // We do not need to return the layout, just the above table - Zend_Layout::resetMvcInstance(); + $this->disableLayout(); $this->html[] = $this->_createTable(); $this->html->raw(MUtil_Echo::out()); Modified: trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php =================================================================== --- trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php 2011-12-15 15:54:19 UTC (rev 360) @@ -415,6 +415,10 @@ public function pdfAction() { + // Make sure nothing else is output + $this->initRawOutput(); + + // Output the PDF $this->loader->getPdf()->echoPdfBySurveyId($this->_getParam(MUtil_Model::REQUEST_ID)); } Modified: trunk/library/classes/Gems/Default/TrackActionAbstract.php =================================================================== --- trunk/library/classes/Gems/Default/TrackActionAbstract.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/Gems/Default/TrackActionAbstract.php 2011-12-15 15:54:19 UTC (rev 360) @@ -400,6 +400,10 @@ */ public function pdfAction() { + // Make sure nothing else is output + $this->initRawOutput(); + + // Output the PDF $this->loader->getPdf()->echoPdfByTokenId($this->_getIdParam()); } Modified: trunk/library/classes/Gems/Pdf.php =================================================================== --- trunk/library/classes/Gems/Pdf.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/Gems/Pdf.php 2011-12-15 15:54:19 UTC (rev 360) @@ -118,12 +118,10 @@ * @param Zend_Pdf $pdf * @param string $filename * @param boolean $download + * @param boolean $exit Should the application stop running after output */ - protected function echoPdf(Zend_Pdf $pdf, $filename, $download = false) + protected function echoPdf(Zend_Pdf $pdf, $filename, $download = false, $exit = true) { - // We do not need to return the layout, just the above table - Zend_Layout::resetMvcInstance(); - $content = $pdf->render(); // MUtil_Echo::track($filename); @@ -140,6 +138,11 @@ header('Pragma: public'); echo $content; + + if ($exit) { + // No further output + exit; + } } /** Modified: trunk/library/classes/MUtil/Controller/Action.php =================================================================== --- trunk/library/classes/MUtil/Controller/Action.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/MUtil/Controller/Action.php 2011-12-15 15:54:19 UTC (rev 360) @@ -1,6 +1,5 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. @@ -26,15 +25,14 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 + * + * * @package MUtil * @subpackage Controller - * @copyright Copyright (c) 2010 Erasmus MC (www.erasmusmc.nl) & MagnaFacta (www.magnafacta.nl) + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ /** @@ -44,15 +42,18 @@ * - title attribute for use in htm/head/title element * - flashMessenger use standardised and simplified * - use of Zend_Translate simplified and shortened in code + * - disable Zend_Layout and Zend_View with initRawOutput() and $useRawOutput. * * MUtil_Html functionality provided: * - semi automatic MUtil_Html_Sequence initiation * - view script set to html-view.phtml when using html * - snippet usage for repeatably used snippets of html on a page * - * @author Matijs de Jong * @package MUtil * @subpackage Controller + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 */ abstract class MUtil_Controller_Action extends Zend_Controller_Action { @@ -94,13 +95,28 @@ /** * Set to true in child class for automatic creation of $this->html. * - * Otherwise call $this->initHtml() + * To initiate the use of $this->html from the code call $this->initHtml() * + * Overrules $useRawOutput. + * + * @see $useRawOutput * @var boolean $useHtmlView */ public $useHtmlView = false; /** + * Set to true in child class for automatic use of raw (e.g. echo) output only. + * + * Otherwise call $this->initRawOutput() to switch to raw echo output. + * + * Overruled in initialization if $useHtmlView is true. + * + * @see $useHtmlView + * @var boolean $useRawOutput + */ + public $useRawOutput = false; + + /** * A ssession based message store. * * Standard the flash messenger for storing messages @@ -232,6 +248,24 @@ } /** + * Disable the use of Zend_Layout + * + * @return Zend_Controller_Action (continuation pattern) + */ + public function disableLayout() + { + // Actually I would like a check if there is a + // layout instance in the first place. + $layout = Zend_Layout::getMvcInstance(); + if ($layout instanceof Zend_Layout) { + $layout->disableLayout(); + } + // Zend_Layout::resetMvcInstance(); + + return $this; + } + + /** * Returns a session based message store for adding messages to. * * @return Zend_Controller_Action_Helper_FlashMessenger @@ -318,6 +352,7 @@ if (null === $translate) { // Make sure there always is a translator $translate = new MUtil_Translate_Adapter_Potemkin(); + Zend_Registry::set('Zend_Translate', $translate); } $this->setTranslate($translate); @@ -348,6 +383,8 @@ if ($this->useHtmlView) { $this->initHtml(); + } elseif ($this->useRawOutput) { + $this->initRawOutput(); } } @@ -369,10 +406,30 @@ $this->view->setScriptPath(dirname(__FILE__)); $this->_helper->viewRenderer->setNoController(); $this->_helper->viewRenderer->setScriptAction('html-view'); + + $this->useHtmlView = true; + $this->useRawOutput = false; } } /** + * Intializes the raw (echo) output component. + * + * @return void + */ + public function initRawOutput() + { + // Disable layout ((if any) + $this->disableLayout(); + + // Set view rendering off + $this->_helper->viewRenderer->setNoRender(true); + + $this->useHtmlView = false; + $this->useRawOutput = true; + } + + /** * Stub for overruling default snippet loader initiation. */ protected function loadSnippetLoader() Modified: trunk/library/classes/MUtil/Controller/html-view.phtml =================================================================== --- trunk/library/classes/MUtil/Controller/html-view.phtml 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/MUtil/Controller/html-view.phtml 2011-12-15 15:54:19 UTC (rev 360) @@ -1 +1 @@ -<?php echo $this->html->render($this); ?> +<?php echo $this->html->render($this); ?> \ No newline at end of file Property changes on: trunk/library/classes/MUtil/Html/Code ___________________________________________________________________ Added: bugtraq:url + http://survey.erasmusmc.nl/support/mantis/view.php?id=%BUGID% Added: bugtraq:logregex + #(\d+) Added: trunk/library/classes/MUtil/Html/Code/DynamicAbstract.php =================================================================== --- trunk/library/classes/MUtil/Html/Code/DynamicAbstract.php (rev 0) +++ trunk/library/classes/MUtil/Html/Code/DynamicAbstract.php 2011-12-15 15:54:19 UTC (rev 360) @@ -0,0 +1,162 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ + */ + +/** + * + * + * @package MUtil + * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +abstract class MUtil_Html_Code_DynamicAbstract implements MUtil_Html_HtmlInterface +{ + /** + * Contains the content to output. Can be a mix of filenames and string content. + * + * @var array Numeric array of strings or MUtil_Html_HtmlInterface elements + */ + protected $_content = array(); + + /** + * The fields that must be replaced in the content before output. + * + * @var array Key => string array + */ + protected $_fields = array(); + + /** + * + * @var string The seperator used to join multiple content items together + */ + protected $_seperator = "\n"; + + /** + * Creates the object storing any values with a name as a field, unless + * there exists a set{Name} function. Other values are treated as content. + * + * @param mixed $args_array MUtil_Ra::args() parameters + */ + public function __construct($args_array = null) + { + $args = MUtil_Ra::args(func_get_args()); + + foreach ($args as $name => $value) { + if (is_integer($name)) { + $this->addContent($value); + } else { + $function = 'set' . ucfirst($name); + if (method_exists($this, $function)) { + $this->$function($value); + } else { + $this->setField($name, $value); + } + } + } + } + + /** + * Add a filename or some other content that can be rendered. + * + * @param mixed $content + */ + public function addContent($content) + { + $this->_content[] = $content; + } + + /** + * Renders the content + * + * @param Zend_View_Abstract $view + * @return string + */ + protected function getContentOutput(Zend_View_Abstract $view) + { + if (! $this->_content) { + return null; + } + + $output = array(); + + foreach ($this->_content as $content) { + if (! is_string($content)) { + $content = MUtil_Html::renderAny($view, $content); + } + + if ((false === strpos($content, "\n")) && file_exists($content)) { + $content = file_get_contents($content); + } + + $output[] = $content; + } + + if ($this->_fields) { + $output = str_replace(array_keys($this->_fields), $this->_fields, $output); + } + + return implode($this->_seperator, $output); + } + + /** + * Set a field to search and replace in the content. + * + * No markers are used. If you want to replace '{path}' with 'x', you + * must specificy the name '{path}', not 'path'. + * + * @param string $name Full name to replace. + * @param string $value The value placed. + * @return MUtil_Html_Link_LinkAbstract (continuation pattern) + */ + public function setField($name, $value) + { + $this->_fields[$name] = $value; + return $this; + } + + /** + * + * @param string $seperator + * @return MUtil_Html_Link_LinkAbstract (continuation pattern) + */ + public function setSeperator($seperator) + { + $this->_seperator = $seperator; + return $this; + } +} Added: trunk/library/classes/MUtil/Html/Code/JavaScript.php =================================================================== --- trunk/library/classes/MUtil/Html/Code/JavaScript.php (rev 0) +++ trunk/library/classes/MUtil/Html/Code/JavaScript.php 2011-12-15 15:54:19 UTC (rev 360) @@ -0,0 +1,102 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ + */ + +/** + * + * + * @package MUtil + * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class MUtil_Html_Code_JavaScript extends MUtil_Html_Code_DynamicAbstract +{ + protected $_inHeader = true; + + /** + * When true the output should be displayed in the result HEAD, + * otherwise in the BODY. + * + * @return boolean + */ + public function getInHeader() + { + if ($this->_inHeader instanceof MUtil_Lazy_LazyInterface) { + return (boolean) MUtil_Lazy::raise($this->_inHeader); + } else { + return (boolean) $this->_inHeader; + } + } + /** + * Renders the element into a html string + * + * The $view is used to correctly encode and escape the output + * + * @param Zend_View_Abstract $view + * @return string Correctly encoded and escaped html output + */ + public function render(Zend_View_Abstract $view) + { + $content = $this->getContentOutput($view); + + // Of course this setting makes little difference if you have optimized + // your JavaScript loading by putting all script tags at the end of + // your body. (Except that inlineScript is always loaded last.) + if ($this->getInHeader()) { + $scriptTag = $view->headScript(); + } else { + $scriptTag = $view->inlineScript(); + } + $scriptTag->appendScript($content); + + return ''; + } + + /** + * When true the result is displayed in the result HEAD, + * otherwise in the BODY. + * + * @param boolean $value + * @return MUtil_Html_Code_JavaScript (continuation pattern) + */ + public function setInHeader($value = true) + { + $this->_inHeader = $value; + return $this; + } +} Modified: trunk/library/classes/MUtil/Html/ProgressPanel.php =================================================================== --- trunk/library/classes/MUtil/Html/ProgressPanel.php 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/classes/MUtil/Html/ProgressPanel.php 2011-12-15 15:54:19 UTC (rev 360) @@ -46,6 +46,8 @@ */ class MUtil_Html_ProgressPanel extends MUtil_Html_HtmlElement { + const CODE = "MUtil_Html_ProgressPanel_Code"; + /** * Usually no text is appended after an element, but for certain elements we choose * to add a "\n" newline character instead, to keep the output readable in source @@ -87,6 +89,28 @@ } /** + * Returns the JavaScript object associated with this object. + * + * WARNING: calling this object sets it's position in the order the + * objects are rendered. If you use MUtil_Lazy objects, make sure they + * have the correct value when rendering. + * + * @return MUtil_Html_Code_JavaScript + */ + public function getCode() + { + if (! $this->offsetExists(self::CODE)) { + $js = new MUtil_Html_Code_JavaScript(dirname(__FILE__) . '/ProgressPanel.js'); + // $js->setInHeader(false); + $js->setField('FUNCTION_PREFIX', __CLASS__); + + $this->offsetSet(self::CODE, $js); + } + + return $this->offsetGet(self::CODE); + } + + /** * Creates a 'div' progress panel * * @param mixed $arg_array A MUtil_Ra::args data collection. @@ -110,11 +134,9 @@ */ protected function renderElement(Zend_View_Abstract $view) { - $js = new MUtil_Html_Link_JavaScript(dirname(__FILE__) . '/ProgressPanel.js'); - $js->setField('FUNCTION_PREFIX', __CLASS__); + // Make sure the JS code is added + $this->getCode(); - $this->append($js); - return parent::renderElement($view); } } Modified: trunk/library/layouts/scripts/gems.phtml =================================================================== --- trunk/library/layouts/scripts/gems.phtml 2011-12-15 15:40:20 UTC (rev 359) +++ trunk/library/layouts/scripts/gems.phtml 2011-12-15 15:54:19 UTC (rev 360) @@ -50,6 +50,8 @@ ?></div> </div><?php echo MUtil_Echo::out(); + + // Scripts here for load optimization echo $this->headScript(); echo $this->inlineScript(); ?></body> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |