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