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 
00053         $sAbsDynImageDir = $myConfig->getPictureDir(false);
00054         $sMasterImage = basename( $oObject->{"oxarticles__oxpic".$iIndex}->value );
00055 
00056         if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00057             return;
00058         }
00059 
00060 
00061         if ( $blDeleteMasterPicture ) {
00062             // master picture
00063             $aPic = array("sField"    => "oxpic".$iIndex,
00064                           "sDir"      => $oUtilsFile->getImageDirByType( "M".$iIndex ),
00065                           "sFileName" => $sMasterImage);
00066         } else {
00067             // general picture
00068             $aPic = array("sField"    => "oxpic".$iIndex,
00069                           "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00070                           "sFileName" => $sMasterImage);
00071         }
00072 
00073         $blDeleted = $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getPictureDir(false) . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00074 
00075         if ( $blDeleted ) {
00076             $this->deleteZoomPicture( $oObject, $iIndex );
00077 
00078             $aDelPics = array();
00079 
00080             if ( $blDeleteMasterPicture ) {
00081                 //deleting general picture
00082                 $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00083                                     "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00084                                     "sFileName" => $sMasterImage);
00085             }
00086 
00087             if ( $iIndex == 1 ) {
00088                 // deleting generated main icon picture if custom main icon
00089                 // file name not equal with generated from master picture
00090                 if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00091                     $aDelPics[] = array("sField"    => "oxpic1",
00092                                         "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00093                                         "sFileName" => $this->getMainIconName( $sMasterImage ));
00094                 }
00095 
00096                 // deleting generated thumbnail picture if custom thumbnail
00097                 // file name not equal with generated from master picture
00098                 if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00099                     $aDelPics[] = array("sField"    => "oxpic1",
00100                                         "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00101                                         "sFileName" => $this->getThumbName( $sMasterImage ));
00102                 }
00103             }
00104             foreach ( $aDelPics as $aPic ) {
00105                 $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getPictureDir(false) . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00106             }
00107         }
00108 
00109         //deleting custom zoom pic (compatibility mode)
00110         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00111             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00112                 // deleting old zoom picture
00113                 $this->deleteZoomPicture( $oObject, $iIndex );
00114             }
00115         }
00116 
00117     }
00118 
00126     public function deleteMainIcon( $oObject )
00127     {
00128         if ( ( $sMainIcon = $oObject->oxarticles__oxicon->value ) ) {
00129             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "ICO" );
00130             oxUtilsPic::getInstance()->safePictureDelete( $sMainIcon, $sPath, "oxarticles", "oxicon" );
00131         }
00132     }
00133 
00141     public function deleteThumbnail( $oObject )
00142     {
00143         if ( ( $sThumb = $oObject->oxarticles__oxthumb->value ) ) {
00144             // deleting article main icon and thumb picture
00145             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "TH" );
00146             oxUtilsPic::getInstance()->safePictureDelete( $sThumb, $sPath, "oxarticles", "oxthumb" );
00147         }
00148     }
00149 
00158     public function deleteZoomPicture( $oObject, $iIndex )
00159     {
00160         // checking if oxzoom field exists
00161         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00162         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00163 
00164         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00165             if ( $sZoomPicName = $this->getZoomName( $oObject->{"oxarticles__oxpic".$iIndex}->value, $iIndex ) ) {
00166                 $sFieldToCheck = "oxpic".$iIndex;
00167             } else {
00168                 return;
00169             }
00170         } else {
00171             $sZoomPicName = basename( $oObject->{"oxarticles__oxzoom".$iIndex}->value );
00172             $sFieldToCheck = "oxzoom".$iIndex;
00173         }
00174 
00175         if ( $sZoomPicName && $sZoomPicName != "nopic.jpg" ) {
00176             // deleting zoom picture
00177             $sPath = $this->getConfig()->getPictureDir( false ) . oxUtilsFile::getInstance()->getImageDirByType( "Z" . $iIndex );
00178             oxUtilsPic::getInstance()->safePictureDelete( $sZoomPicName, $sPath, "oxarticles", $sFieldToCheck );
00179         }
00180     }
00181 
00189     public function getIconName( $sFilename )
00190     {
00191         return $sFilename;
00192     }
00193 
00201     public function getMainIconName( $sMasterImageFile )
00202     {
00203         return $this->_getBaseMasterImageFileName( $sMasterImageFile );
00204     }
00205 
00213     public function getThumbName( $sMasterImageFile )
00214     {
00215         return basename( $sMasterImageFile );
00216     }
00217 
00226     public function getZoomName( $sMasterImageFile, $iIndex )
00227     {
00228         return basename( $sMasterImageFile );
00229     }
00230 
00238     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00239     {
00240         return basename( $sMasterImageFile );
00241     }
00242 
00251     public function getImageSize($aImgSizes, $sIndex = null)
00252     {
00253         if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
00254             $aSize = explode('*', $aImgSizes[$sIndex]);
00255         } elseif (is_string ($aImgSizes)) {
00256             $aSize = explode('*', $aImgSizes);
00257         }
00258         if (2 == count($aSize)) {
00259             $x = (int)$aSize[0];
00260             $y = (int)$aSize[1];
00261             if ($x && $y) {
00262                 return $aSize;
00263             }
00264         }
00265         return null;
00266     }
00267 
00280     protected function _getPictureInfo( $sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null )
00281     {
00282         // custom server as image storage?
00283         if ( $sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL) ) {
00284             return array( 'path' => false, 'url'=> $sAltUrl );
00285         }
00286 
00287         $oConfig = $this->getConfig();
00288         $sPath = $oConfig->getPicturePath( $sFilePath . $sFile, $blAdmin, $iLang, $iShopId );
00289         if ( !$sPath ) {
00290             return array( 'path'=> false, 'url' => false );
00291         }
00292 
00293         $sDirPrefix = $oConfig->getOutDir();
00294         $sUrlPrefix = $oConfig->getOutUrl( $blSSL, $blAdmin, $oConfig->getConfigParam( 'blNativeImages' ) );
00295 
00296         return array( 'path' => $sPath, 'url'=> str_replace( $sDirPrefix, $sUrlPrefix, $sPath ) );
00297     }
00298 
00308     public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
00309     {
00310         $oConfig = $this->getConfig();
00311 
00312         $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
00313         if ( !$sAltUrl ) {
00314             $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
00315         }
00316 
00317         if ( $sAltUrl ) {
00318             if ( $blSSL && $oConfig->isSsl() ) {
00319 
00320                 $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
00321                 if ( !$sSslAltUrl ) {
00322                     $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
00323                 }
00324 
00325                 if ( $sSslAltUrl ) {
00326                     $sAltUrl = $sSslAltUrl;
00327                 }
00328             }
00329 
00330             if ( !is_null( $sFile ) ) {
00331                 $sAltUrl .= '/'.$sFilePath.$sFile;
00332             }
00333         }
00334 
00335         return $sAltUrl;
00336     }
00337 
00349     public function getPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false)
00350     {
00351         $sUrl = null;
00352         if ( $sPath && $sFile && ( $aSize = $this->getImageSize( $sSize, $sIndex ) ) ) {
00353 
00354             $aPicInfo = $this->_getPictureInfo( "master/" . ( $sAltPath ? $sAltPath : $sPath ), $sFile, $this->isAdmin() );
00355             if ( $aPicInfo['url'] && $aSize[0] && $aSize[1] ) {
00356                 $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam( 'sDefaultImageQuality' );
00357                 $sUrl = str_replace( "/master/" . ( $sAltPath ? $sAltPath : $sPath ), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url'] );
00358             }
00359         }
00360         return $sUrl;
00361     }
00362 
00373     public function getProductPicUrl( $sPath, $sFile, $sSize, $sIndex = null )
00374     {
00375         $sUrl = null;
00376         if ( !$sFile || !( $sUrl = $this->getPicUrl( $sPath, $sFile, $sSize, $sIndex ) ) ) {
00377             $sUrl = $this->getPicUrl( $sPath, "nopic.jpg", $sSize, $sIndex, "/" );
00378         }
00379         return $sUrl;
00380     }
00381 }