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