[TASK] Remove Code Duplication in tslib_content_Media
authorSteffen Ritter <info@rs-websystems.de>
Sat, 31 Mar 2012 16:02:31 +0000 (18:02 +0200)
committerGeorg Ringer <mail@ringerge.org>
Wed, 11 Apr 2012 11:22:10 +0000 (13:22 +0200)
For every parameter the same code for overlaying Flexform value
over the value given in TypoScript is done.

Furthermore for every file the same url-processing is done.

Remove these code duplication by extracting these things to
external functions for a better readability and maintainability.

Change-Id: I2a614cc1d60f28c3ef4189e74fa3f4dea91fa85f
Resolves: #35440
Releases: 6.0, 4.7
Reviewed-on: http://review.typo3.org/10219
Tested-by: Anja Leichsenring
Reviewed-by: Oliver Klee
Reviewed-by: Anja Leichsenring
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer
typo3/sysext/cms/tslib/content/class.tslib_content_media.php

index 6fb49b5..17ea333 100644 (file)
@@ -54,169 +54,72 @@ class tslib_content_Media extends tslib_content_Abstract {
                        }
                }
                        // Type is video or audio
-               $mmType = isset($conf['parameter.']['mmType.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmType'], $conf['parameter.']['mmType.'])
-                       : $conf['parameter.']['mmType'];
-               $type = isset($conf['type.'])
-                       ? $this->cObj->stdWrap($conf['type'], $conf['type.'])
-                       : $conf['type'];
-               $conf['type'] = $mmType ? $mmType : $type;
+               $conf['type'] = $this->doFlexFormOverlay($conf, 'type');
 
                        // Video sources
-               $sources = isset($conf['sources.'])
-                       ? $this->cObj->stdWrap($conf['sources'], $conf['sources.'])
-                       : $conf['sources'];
-               $mmSources = isset($conf['parameter.']['mmSources.']['mmSourcesContainer.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmSources.']['mmSourcesContainer'], $conf['parameter.']['mmSources.']['mmSourcesContainer.'])
-                       : $conf['parameter.']['mmSources']['mmSourcesContainer'];
-               $sources = $mmSources ? $mmSources : $sources;
+               $sources = $this->doFlexFormOverlay($conf, 'sources', 'mmSourcesContainer');
                if (is_array($sources) && count($sources)) {
                        $conf['sources'] = array();
                        foreach ($sources as $key => $source) {
                                if (isset($source['mmSource'])) {
                                        $source = $source['mmSource'];
-                                       if (is_file(PATH_site . $source)) {
-                                               $conf['sources'][$key] = $GLOBALS['TSFE']->tmpl->getFileName($source);
-                                       } else {
-                                                       // Use media wizard to extract file from URL
-                                               $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($source);
-                                               if ($mediaWizard !== NULL) {
-                                                       $source = $mediaWizard->rewriteUrl($source);
-                                               }
-                                               $conf['sources'][$key] = $this->cObj->typoLink_URL(array(
-                                                       'parameter' => $source
-                                               ));
-                                       }
+                                       $conf['sources'][$key] = $this->retrieveMediaUrl($source);
                                }
                        }
-
                } else {
                        unset($conf['sources']);
                }
                        // Video fallback and backward compatibility file
-               $videoFallback = isset($conf['file.'])
-                       ? $this->cObj->stdWrap($conf['file'], $conf['file.'])
-                       : $conf['file'];
-               $mmVideoFallback = isset($conf['parameter.']['mmFile.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmFile'], $conf['parameter.']['mmFile.'])
-                       : $conf['parameter.']['mmFile'];
-               $videoFallback = $mmVideoFallback ? $mmVideoFallback : $videoFallback;
+               $videoFallback = $this->doFlexFormOverlay($conf, 'file');
                        // Backward compatibility file
-               $url = $videoFallback;
-               if ($videoFallback) {
-                       if (is_file(PATH_site . $videoFallback)) {
-                               $conf['file'] = $GLOBALS['TSFE']->tmpl->getFileName($videoFallback);
-                       } else {
-                                       // Use media wizard to extract file from URL
-                               $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($videoFallback);
-                               if ($mediaWizard !== NULL) {
-                                       $videoFallback = $mediaWizard->rewriteUrl($videoFallback);
-                               }
-                               $conf['file'] = $this->cObj->typoLink_URL(array(
-                                       'parameter' => $videoFallback
-                               ));
-                       }
+               if ($videoFallback !== NULL) {
+                       $conf['file'] = $this->retrieveMediaUrl($videoFallback);
                } else {
                        unset($conf['file']);
                }
 
                        // Audio sources
-               $audioSources = isset($conf['audioSources.'])
-                       ? $this->cObj->stdWrap($conf['audioSources'], $conf['audioSources.'])
-                       : $conf['audioSources'];
-               $mmAudioSources = isset($conf['parameter.']['mmAudioSources.']['mmAudioSourcesContainer.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmAudioSources.']['mmAudioSourcesContainer'], $conf['parameter.']['mmAudioSources.']['mmAudioSourcesContainer.'])
-                       : $conf['parameter.']['mmAudioSources']['mmAudioSourcesContainer'];
-               $audioSources = $mmAudioSources ? $mmAudioSources : $audioSources;
+               $audioSources = $this->doFlexFormOverlay($conf, 'audioSources', 'mmAudioSourcesContainer');
                if (is_array($audioSources) && count($audioSources)) {
                        $conf['audioSources'] = array();
                        foreach ($audioSources as $key => $source) {
                                if (isset($source['mmAudioSource'])) {
                                        $source = $source['mmAudioSource'];
-                                       if (is_file(PATH_site . $source)) {
-                                               $conf['audioSources'][$key] = $GLOBALS['TSFE']->tmpl->getFileName($source);
-                                       } else {
-                                                       // Use media wizard to extract file from URL
-                                               $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($source);
-                                               if ($mediaWizard !== NULL) {
-                                                       $source = $mediaWizard->rewriteUrl($source);
-                                               }
-                                               $conf['audioSources'][$key] = $this->cObj->typoLink_URL(array(
-                                                       'parameter' => $source
-                                               ));
-                                       }
+                                       $conf['audioSources'][$key] = $this->retrieveMediaUrl($source);
                                }
                        }
-
                } else {
                        unset($conf['audioSources']);
                }
 
                        // Audio fallback
-               $audioFallback = isset($conf['audioFallback.'])
-                       ? $this->cObj->stdWrap($conf['audioFallback'], $conf['audioFallback.'])
-                       : $conf['audioFallback'];
-               $mmAudioFallback = isset($conf['parameter.']['mmAudioFallback.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmAudioFallback'], $conf['parameter.']['mmAudioFallback.'])
-                       : $conf['parameter.']['mmAudioFallback'];
-               $audioFallback = $mmAudioFallback ? $mmAudioFallback : $audioFallback;
+               $audioFallback = $this->doFlexFormOverlay($conf, 'audioFallback');
                if ($audioFallback) {
-                       if (is_file(PATH_site . $audioFallback)) {
-                               $conf['audioFallback'] = $GLOBALS['TSFE']->tmpl->getFileName($audioFallback);
-                       } else {
-                                       // Use media wizard to extract file from URL
-                               $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($audioFallback);
-                               if ($mediaWizard !== NULL) {
-                                       $audioFallback = $mediaWizard->rewriteUrl($audioFallback);
-                               }
-                               $conf['audioFallback'] = $this->cObj->typoLink_URL(array(
-                                       'parameter' => $audioFallback
-                               ));
-                       }
+                       $conf['audioFallback'] = $this->retrieveMediaUrl($audioFallback);
                } else {
                        unset($conf['audioFallback']);
                }
+
                        // Backward compatibility
                if ($conf['type'] === 'audio' && !isset($conf['audioFallback'])) {
                        $conf['audioFallback'] = $conf['file'];
                }
 
                        // Caption file
-               $caption = isset($conf['caption.'])
-                       ? $this->cObj->stdWrap($conf['caption'], $conf['caption.'])
-                       : $conf['caption'];
-               $mmCaption = isset($conf['parameter.']['mmCaption.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmCaption'], $conf['parameter.']['mmCaption.'])
-                       : $conf['parameter.']['mmCaption'];
-               $caption = $mmCaption ? $mmCaption : $caption;
+               $caption = $this->doFlexFormOverlay($conf, 'caption');
                if ($caption) {
-                       if (is_file(PATH_site . $caption)) {
-                               $conf['caption'] = $GLOBALS['TSFE']->tmpl->getFileName($caption);
-                       } else {
-                                       // Use media wizard to extract file from URL
-                               $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($caption);
-                               if ($mediaWizard !== NULL) {
-                                       $caption = $mediaWizard->rewriteUrl($caption);
-                               }
-                               $conf['caption'] = $this->cObj->typoLink_URL(array(
-                                       'parameter' => $caption
-                               ));
-                       }
+                       $conf['caption'] = $this->retrieveMediaUrl($caption);
                } else {
                        unset($conf['caption']);
                }
                        // Establish render type
-               $renderType = isset($conf['renderType.'])
-                       ? $this->cObj->stdWrap($conf['renderType'], $conf['renderType.'])
-                       : $conf['renderType'];
-               $mmRenderType = isset($conf['parameter.']['mmRenderType.'])
-                       ? $this->cObj->stdWrap($conf['parameter.']['mmRenderType'], $conf['parameter.']['mmRenderType.'])
-                       : $conf['parameter.']['mmRenderType'];
-               $renderType = $mmRenderType ? $mmRenderType : $renderType;
+               $renderType = $this->doFlexFormOverlay($conf, 'renderType');
+
                if ($renderType === 'preferFlashOverHtml5') {
                        $conf['preferFlashOverHtml5'] = 1;
                        $renderType = 'auto';
                }
+
                if ($renderType === 'auto') {
                                // Default renderType is swf
                        $renderType = 'swf';
@@ -236,25 +139,10 @@ class tslib_content_Media extends tslib_content_Abstract {
                $conf['predefined'] = array();
 
                        // Width and height
-               $width = isset($conf['width.'])
-                       ? intval($this->cObj->stdWrap($conf['width'], $conf['width.']))
-                       : intval($conf['width']);
-               $width = $width ? $width : $typeConf['defaultWidth'];
-               $mmWidth = isset($conf['parameter.']['mmWidth.'])
-                       ? intval($this->cObj->stdWrap($conf['parameter.']['mmWidth'], $conf['parameter.']['mmWidth.']))
-                       : intval($conf['parameter.']['mmWidth']);
-               $conf['width'] = $mmWidth ? $mmWidth : $width;
-               $height = isset($conf['height.'])
-                       ? intval($this->cObj->stdWrap($conf['height'], $conf['height.']))
-                       : intval($conf['height']);
-               $height = $height ? $height : $typeConf['defaultHeight'];
-               $mmHeight = isset($conf['parameter.']['mmHeight.'])
-                       ? intval($this->cObj->stdWrap($conf['parameter.']['mmHeight'], $conf['parameter.']['mmHeight.']))
-                       : intval($conf['parameter.']['mmHeight']);
-               $conf['height'] = $mmHeight ? $mmHeight : $height;
+               $conf['width'] = intval($this->doFlexFormOverlay($conf, 'width'));
+               $conf['height'] = intval($this->doFlexFormOverlay($conf, 'height'));
 
                if (is_array($conf['parameter.']['mmMediaOptions'])) {
-                       $params = array();
                        foreach ($conf['parameter.']['mmMediaOptions'] as $key => $value) {
                                if ($key == 'mmMediaCustomParameterContainer') {
                                        foreach ($value as $val) {
@@ -326,9 +214,11 @@ class tslib_content_Media extends tslib_content_Abstract {
                                if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/hooks/class.tx_cms_mediaitems.php']['customMediaRender'])) {
                                        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/hooks/class.tx_cms_mediaitems.php']['customMediaRender'] as $classRef) {
                                                $hookObj = t3lib_div::getUserObj($classRef);
-                                               $conf['file'] = $url;
-                                               $conf['mode'] = is_file(PATH_site . $url) ? 'file' : 'url';
-                                               $content = $hookObj->customMediaRender($renderType, $conf, $this);
+                                               $conf['file'] = $videoFallback;
+                                               $conf['mode'] = is_file(PATH_site . $videoFallback) ? 'file' : 'url';
+                                               if (method_exists($hookObj, 'customMediaRender')) {
+                                                       $content = $hookObj->customMediaRender($renderType, $conf, $this);
+                                               }
                                        }
                                }
                                if (isset($conf['stdWrap.'])) {
@@ -339,6 +229,60 @@ class tslib_content_Media extends tslib_content_Abstract {
                return $content;
        }
 
+       /**
+        * Resolves the URL of an file
+        *
+        * @param string $file
+        * @return string|NULL
+        */
+       protected function retrieveMediaUrl($file) {
+               $returnValue = NULL;
+
+               if (is_file(PATH_site . $file)) {
+                               // get the path relative to the page currently outputted
+                       $returnValue = $GLOBALS['TSFE']->tmpl->getFileName($file);
+               } else {
+                               // Use media wizard to extract file from URL
+                       /** @var $mediaWizard tslib_mediaWizardProvider */
+                       $mediaWizard = tslib_mediaWizardManager::getValidMediaWizardProvider($file);
+                       if ($mediaWizard !== NULL) {
+                               $returnValue = $this->cObj->typoLink_URL(array(
+                                       'parameter' => $mediaWizard->rewriteUrl($file)
+                               ));
+                       }
+               }
+
+               return $returnValue;
+       }
+
+
+       /**
+        * Looks up if the key is set via flexform and returns the actual value.
+        * If not present in flexform, it processes the value which might be given in TS
+        * with stdWrap (if needed) and returns that value.
+        *
+        * @param array &$confArray
+        * @param string$key
+        * @param string $sectionKey
+        * @return mixed
+        */
+       protected function doFlexFormOverlay(array &$confArray, $key, $sectionKey = NULL) {
+               $flexValue = NULL;
+
+               $flexKey = 'mm' . ucfirst($key);
+               if ($sectionKey === NULL) {
+                       $flexValue = $confArray['parameter.'][$flexKey];
+               } else {
+                       $flexValue = $confArray['parameter.'][$flexKey][$sectionKey];
+               }
+
+               if ($flexValue === NULL) {
+                       $flexValue = isset($confArray[$key . '.'])
+                               ? $this->cObj->stdWrap($confArray[$key], $confArray[$key . '.'])
+                               : $confArray[$key];
+               }
+               return $flexValue;
+       }
 }
 
 
@@ -346,4 +290,4 @@ if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLA
        include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/content/class.tslib_content_media.php']);
 }
 
-?>
\ No newline at end of file
+?>