oxpicturehandler.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPictureHandler extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     public static function getInstance()
00021     {
00022         // disable caching for test modules
00023         if ( defined( 'OXID_PHP_UNIT' ) ) {
00024             self::$_instance = modInstances::getMod( __CLASS__ );
00025         }
00026 
00027         if ( !self::$_instance instanceof oxPictureHandler ) {
00028             self::$_instance = oxNew( 'oxPictureHandler' );
00029             if ( defined( 'OXID_PHP_UNIT' ) ) {
00030                 modInstances::addMod( __CLASS__, self::$_instance);
00031             }
00032         }
00033         return self::$_instance;
00034     }
00035 
00047     public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
00048     {
00049         $myConfig   = $this->getConfig();
00050         $myUtilsPic = oxUtilsPic::getInstance();
00051         $oUtilsFile = oxUtilsFile::getInstance();
00052         $blGeneratedImagesOnly = !$blDeleteMasterPicture;
00053 
00054         $sAbsDynImageDir = $myConfig->getPictureDir(false);
00055         $sMasterImage = basename( $oObject->{"oxarticles__oxpic".$iIndex}->value );
00056         if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00057             return;
00058         }
00059 
00060         $aPic = array("sField"    => "oxpic".$iIndex,
00061                       "sDir"      => $oUtilsFile->getImageDirByType( "M".$iIndex, $blGeneratedImagesOnly ),
00062                       "sFileName" => $sMasterImage);
00063 
00064         $blDeleted = $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00065         if ( $blDeleted ) {
00066             $this->deleteZoomPicture( $oObject, $iIndex );
00067 
00068             $aDelPics = array();
00069             if ( $iIndex == 1 ) {
00070                 // deleting generated main icon picture if custom main icon
00071                 // file name not equal with generated from master picture
00072                 if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00073                     $aDelPics[] = array("sField"    => "oxpic1",
00074                                         "sDir"      => $oUtilsFile->getImageDirByType( "ICO", $blGeneratedImagesOnly ),
00075                                         "sFileName" => $this->getMainIconName( $sMasterImage ));
00076                 }
00077 
00078                 // deleting generated thumbnail picture if custom thumbnail
00079                 // file name not equal with generated from master picture
00080                 if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00081                     $aDelPics[] = array("sField"    => "oxpic1",
00082                                         "sDir"      => $oUtilsFile->getImageDirByType( "TH", $blGeneratedImagesOnly ),
00083                                         "sFileName" => $this->getThumbName( $sMasterImage ));
00084                 }
00085             }
00086 
00087             foreach ( $aDelPics as $aPic ) {
00088                 $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00089             }
00090         }
00091 
00092         //deleting custom zoom pic (compatibility mode)
00093         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00094             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00095                 // deleting old zoom picture
00096                 $this->deleteZoomPicture( $oObject, $iIndex );
00097             }
00098         }
00099 
00100     }
00101 
00109     public function deleteMainIcon( $oObject )
00110     {
00111         if ( ( $sMainIcon = $oObject->oxarticles__oxicon->value ) ) {
00112             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "ICO" );
00113             oxUtilsPic::getInstance()->safePictureDelete( $sMainIcon, $sPath, "oxarticles", "oxicon" );
00114         }
00115     }
00116 
00124     public function deleteThumbnail( $oObject )
00125     {
00126         if ( ( $sThumb = $oObject->oxarticles__oxthumb->value ) ) {
00127             // deleting article main icon and thumb picture
00128             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "TH" );
00129             oxUtilsPic::getInstance()->safePictureDelete( $sThumb, $sPath, "oxarticles", "oxthumb" );
00130         }
00131     }
00132 
00141     public function deleteZoomPicture( $oObject, $iIndex )
00142     {
00143         // checking if oxzoom field exists
00144         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00145         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00146 
00147         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00148             if ( $sZoomPicName = $this->getZoomName( $oObject->{"oxarticles__oxpic".$iIndex}->value, $iIndex ) ) {
00149                 $sFieldToCheck = "oxpic".$iIndex;
00150             } else {
00151                 return;
00152             }
00153         } else {
00154             $sZoomPicName = basename( $oObject->{"oxarticles__oxzoom".$iIndex}->value );
00155             $sFieldToCheck = "oxzoom".$iIndex;
00156         }
00157 
00158         if ( $sZoomPicName && $sZoomPicName != "nopic.jpg" ) {
00159             // deleting zoom picture
00160             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "Z" . $iIndex );
00161             oxUtilsPic::getInstance()->safePictureDelete( $sZoomPicName, $sPath, "oxarticles", $sFieldToCheck );
00162         }
00163     }
00164 
00172     public function getIconName( $sFilename )
00173     {
00174         return $sFilename;
00175     }
00176 
00184     public function getMainIconName( $sMasterImageFile )
00185     {
00186         return $this->_getBaseMasterImageFileName( $sMasterImageFile );
00187     }
00188 
00196     public function getThumbName( $sMasterImageFile )
00197     {
00198         return basename( $sMasterImageFile );
00199     }
00200 
00209     public function getZoomName( $sMasterImageFile, $iIndex )
00210     {
00211         return basename( $sMasterImageFile );
00212     }
00213 
00221     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00222     {
00223         return basename( $sMasterImageFile );
00224     }
00225 
00234     public function getImageSize($aImgSizes, $sIndex = null)
00235     {
00236         if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
00237             $aSize = explode('*', $aImgSizes[$sIndex]);
00238         } elseif (is_string ($aImgSizes)) {
00239             $aSize = explode('*', $aImgSizes);
00240         }
00241         if (2 == count($aSize)) {
00242             $x = (int)$aSize[0];
00243             $y = (int)$aSize[1];
00244             if ($x && $y) {
00245                 return $aSize;
00246             }
00247         }
00248         return null;
00249     }
00250 
00263     protected function _getPictureInfo( $sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null )
00264     {
00265         // custom server as image storage?
00266         if ( $sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL) ) {
00267             return array( 'path' => false, 'url'=> $sAltUrl );
00268         }
00269 
00270         $oConfig = $this->getConfig();
00271         $sPath = $oConfig->getPicturePath( $sFilePath . $sFile, $blAdmin, $iLang, $iShopId );
00272         if ( !$sPath ) {
00273             return array( 'path'=> false, 'url' => false );
00274         }
00275 
00276         $sDirPrefix = $oConfig->getOutDir();
00277         $sUrlPrefix = $oConfig->getOutUrl( $blSSL, $blAdmin, $oConfig->getConfigParam( 'blNativeImages' ) );
00278 
00279         return array( 'path' => $sPath, 'url'=> str_replace( $sDirPrefix, $sUrlPrefix, $sPath ) );
00280     }
00281 
00291     public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
00292     {
00293         $oConfig = $this->getConfig();
00294 
00295         $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
00296         if ( !$sAltUrl ) {
00297             $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
00298         }
00299 
00300         if ( $sAltUrl ) {
00301             if ( (is_null($blSSL) && $oConfig->isSsl()) || $blSSL) {
00302 
00303                 $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
00304                 if ( !$sSslAltUrl ) {
00305                     $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
00306                 }
00307 
00308                 if ( $sSslAltUrl ) {
00309                     $sAltUrl = $sSslAltUrl;
00310                 }
00311             }
00312 
00313             if ( !is_null( $sFile ) ) {
00314                 $sAltUrl .= '/'.$sFilePath.$sFile;
00315             }
00316         }
00317 
00318         return $sAltUrl;
00319     }
00320 
00333     public function getPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false, $bSsl = null )
00334     {
00335         $sUrl = null;
00336         if ( $sPath && $sFile && ( $aSize = $this->getImageSize( $sSize, $sIndex ) ) ) {
00337 
00338             $aPicInfo = $this->_getPictureInfo( "master/" . ( $sAltPath ? $sAltPath : $sPath ), $sFile, $this->isAdmin(), $bSsl );
00339             if ( $aPicInfo['url'] && $aSize[0] && $aSize[1] ) {
00340                 $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam( 'sDefaultImageQuality' );
00341                 $sUrl = str_replace( "/master/" . ( $sAltPath ? $sAltPath : $sPath ), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url'] );
00342             }
00343         }
00344         return $sUrl;
00345     }
00346 
00358     public function getProductPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $bSsl = null )
00359     {
00360         $sUrl = null;
00361         if ( !$sFile || !( $sUrl = $this->getPicUrl( $sPath, $sFile, $sSize, $sIndex, false, $bSsl ) ) ) {
00362             $sUrl = $this->getPicUrl( $sPath, "nopic.jpg", $sSize, $sIndex, "/", $bSsl );
00363         }
00364         return $sUrl;
00365     }
00366 }