00001 <?php
00002
00006 class oxPictureHandler extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 public static function getInstance()
00021 {
00022
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
00063 $aPic = array("sField" => "oxpic".$iIndex,
00064 "sDir" => $oUtilsFile->getImageDirByType( "M".$iIndex ),
00065 "sFileName" => $sMasterImage);
00066 } else {
00067
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
00082 $aDelPics[] = array("sField" => "oxpic".$iIndex,
00083 "sDir" => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00084 "sFileName" => $sMasterImage);
00085 }
00086
00087 if ( $iIndex == 1 ) {
00088
00089
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
00097
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
00110 if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00111 if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00112
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
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
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
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 $oConfig = $this->getConfig();
00283 if (!isset($blSSL)) {
00284 $blSSL = $oConfig->isSsl();
00285 }
00286
00287
00288 $sAltUrl = $oConfig->getConfigParam( 'sAltImageUrl' );
00289 if ( $sAltUrl ) {
00290 if ( $blSSL && $oConfig->isSsl() ) {
00291 $sSslAltUrl = $oConfig->getConfigParam( 'sSSLAltImageUrl' );
00292 if ( $sSslAltUrl ) {
00293 $sAltUrl = $sSslAltUrl;
00294 }
00295 }
00296
00297 if ( !is_null( $sFile ) ) {
00298 $sAltUrl .= $sFilePath . $sFile;
00299 }
00300
00301 return array( 'path' => false, 'url'=> $sAltUrl );
00302 }
00303
00304 $sPath = $oConfig->getPicturePath( $sFilePath . $sFile, $blAdmin, $iLang, $iShopId );
00305 if ( !$sPath ) {
00306 return array( 'path'=> false, 'url' => false );
00307 }
00308
00309 $sDirPrefix = $oConfig->getOutDir();
00310 $sUrlPrefix = $oConfig->getOutUrl( $blSSL, $blAdmin, $oConfig->getConfigParam( 'blNativeImages' ) );
00311
00312 return array( 'path' => $sPath, 'url'=> str_replace( $sDirPrefix, $sUrlPrefix, $sPath ) );
00313 }
00314
00326 public function getPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false )
00327 {
00328 $sUrl = null;
00329 if ( $sPath && $sFile && ( $aSize = $this->getImageSize( $sSize, $sIndex ) ) ) {
00330
00331 $aPicInfo = $this->_getPictureInfo( "master/" . ( $sAltPath ? $sAltPath : $sPath ), $sFile, $this->isAdmin() );
00332 if ( $aPicInfo['url'] && $aSize[0] && $aSize[1] ) {
00333 $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam( 'sDefaultImageQuality' );
00334 $sUrl = str_replace( "/master/" . ( $sAltPath ? $sAltPath : $sPath ), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url'] );
00335 }
00336 }
00337 return $sUrl;
00338 }
00339
00350 public function getProductPicUrl( $sPath, $sFile, $sSize, $sIndex = null )
00351 {
00352 $sUrl = null;
00353 if ( !$sFile || !( $sUrl = $this->getPicUrl( $sPath, $sFile, $sSize, $sIndex ) ) ) {
00354 $sUrl = $this->getPicUrl( $sPath, "nopic.jpg", $sSize, $sIndex, "/" );
00355 }
00356 return $sUrl;
00357 }
00358 }