00001 <?php
00002
00006 class oxPictureHandler extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00022 public static function getInstance()
00023 {
00024 return oxRegistry::get("oxPictureHandler");
00025 }
00026
00038 public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
00039 {
00040 $myConfig = $this->getConfig();
00041 $myUtilsPic = oxRegistry::get("oxUtilsPic");
00042 $oUtilsFile = oxRegistry::get("oxUtilsFile");
00043 $blGeneratedImagesOnly = !$blDeleteMasterPicture;
00044
00045 $sAbsDynImageDir = $myConfig->getPictureDir(false);
00046 $sMasterImage = basename( $oObject->{"oxarticles__oxpic".$iIndex}->value );
00047 if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00048 return;
00049 }
00050
00051 $aPic = array("sField" => "oxpic".$iIndex,
00052 "sDir" => $oUtilsFile->getImageDirByType( "M".$iIndex, $blGeneratedImagesOnly ),
00053 "sFileName" => $sMasterImage);
00054
00055 $blDeleted = $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00056 if ( $blDeleted ) {
00057 $this->deleteZoomPicture( $oObject, $iIndex );
00058
00059 $aDelPics = array();
00060 if ( $iIndex == 1 ) {
00061
00062
00063 if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00064 $aDelPics[] = array("sField" => "oxpic1",
00065 "sDir" => $oUtilsFile->getImageDirByType( "ICO", $blGeneratedImagesOnly ),
00066 "sFileName" => $this->getMainIconName( $sMasterImage ));
00067 }
00068
00069
00070
00071 if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00072 $aDelPics[] = array("sField" => "oxpic1",
00073 "sDir" => $oUtilsFile->getImageDirByType( "TH", $blGeneratedImagesOnly ),
00074 "sFileName" => $this->getThumbName( $sMasterImage ));
00075 }
00076 }
00077
00078 foreach ( $aDelPics as $aPic ) {
00079 $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00080 }
00081 }
00082
00083
00084 if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00085 if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00086
00087 $this->deleteZoomPicture( $oObject, $iIndex );
00088 }
00089 }
00090
00091 }
00092
00100 public function deleteMainIcon( $oObject )
00101 {
00102 if ( ( $sMainIcon = $oObject->oxarticles__oxicon->value ) ) {
00103 $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "ICO" );
00104 oxRegistry::get("oxUtilsPic")->safePictureDelete( $sMainIcon, $sPath, "oxarticles", "oxicon" );
00105 }
00106 }
00107
00115 public function deleteThumbnail( $oObject )
00116 {
00117 if ( ( $sThumb = $oObject->oxarticles__oxthumb->value ) ) {
00118
00119 $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "TH" );
00120 oxRegistry::get("oxUtilsPic")->safePictureDelete( $sThumb, $sPath, "oxarticles", "oxthumb" );
00121 }
00122 }
00123
00132 public function deleteZoomPicture( $oObject, $iIndex )
00133 {
00134
00135 $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00136 $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00137
00138 if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00139 if ( $sZoomPicName = $this->getZoomName( $oObject->{"oxarticles__oxpic".$iIndex}->value, $iIndex ) ) {
00140 $sFieldToCheck = "oxpic".$iIndex;
00141 } else {
00142 return;
00143 }
00144 } else {
00145 $sZoomPicName = basename( $oObject->{"oxarticles__oxzoom".$iIndex}->value );
00146 $sFieldToCheck = "oxzoom".$iIndex;
00147 }
00148
00149 if ( $sZoomPicName && $sZoomPicName != "nopic.jpg" ) {
00150
00151 $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "Z" . $iIndex );
00152 oxRegistry::get("oxUtilsPic")->safePictureDelete( $sZoomPicName, $sPath, "oxarticles", $sFieldToCheck );
00153 }
00154 }
00155
00163 public function getIconName( $sFilename )
00164 {
00165 return $sFilename;
00166 }
00167
00175 public function getMainIconName( $sMasterImageFile )
00176 {
00177 return $this->_getBaseMasterImageFileName( $sMasterImageFile );
00178 }
00179
00187 public function getThumbName( $sMasterImageFile )
00188 {
00189 return basename( $sMasterImageFile );
00190 }
00191
00200 public function getZoomName( $sMasterImageFile, $iIndex )
00201 {
00202 return basename( $sMasterImageFile );
00203 }
00204
00212 protected function _getBaseMasterImageFileName( $sMasterImageFile )
00213 {
00214 return basename( $sMasterImageFile );
00215 }
00216
00225 public function getImageSize($aImgSizes, $sIndex = null)
00226 {
00227 if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
00228 $aSize = explode('*', $aImgSizes[$sIndex]);
00229 } elseif (is_string ($aImgSizes)) {
00230 $aSize = explode('*', $aImgSizes);
00231 }
00232 if (2 == count($aSize)) {
00233 $x = (int)$aSize[0];
00234 $y = (int)$aSize[1];
00235 if ($x && $y) {
00236 return $aSize;
00237 }
00238 }
00239 return null;
00240 }
00241
00254 protected function _getPictureInfo( $sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null )
00255 {
00256
00257 if ( $sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL) ) {
00258 return array( 'path' => false, 'url'=> $sAltUrl );
00259 }
00260
00261 $oConfig = $this->getConfig();
00262 $sPath = $oConfig->getPicturePath( $sFilePath . $sFile, $blAdmin, $iLang, $iShopId );
00263 if ( !$sPath ) {
00264 return array( 'path'=> false, 'url' => false );
00265 }
00266
00267 $sDirPrefix = $oConfig->getOutDir();
00268 $sUrlPrefix = $oConfig->getOutUrl( $blSSL, $blAdmin, $oConfig->getConfigParam( 'blNativeImages' ) );
00269
00270 return array( 'path' => $sPath, 'url'=> str_replace( $sDirPrefix, $sUrlPrefix, $sPath ) );
00271 }
00272
00282 public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
00283 {
00284 $oConfig = $this->getConfig();
00285
00286 $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
00287 if ( !$sAltUrl ) {
00288 $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
00289 }
00290
00291 if ( $sAltUrl ) {
00292 if ( (is_null($blSSL) && $oConfig->isSsl()) || $blSSL) {
00293
00294 $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
00295 if ( !$sSslAltUrl ) {
00296 $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
00297 }
00298
00299 if ( $sSslAltUrl ) {
00300 $sAltUrl = $sSslAltUrl;
00301 }
00302 }
00303
00304 if ( !is_null( $sFile ) ) {
00305 $sAltUrl .= '/'.$sFilePath.$sFile;
00306 }
00307 }
00308
00309 return $sAltUrl;
00310 }
00311
00324 public function getPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false, $bSsl = null )
00325 {
00326 $sUrl = null;
00327 if ( $sPath && $sFile && ( $aSize = $this->getImageSize( $sSize, $sIndex ) ) ) {
00328
00329 $aPicInfo = $this->_getPictureInfo( "master/" . ( $sAltPath ? $sAltPath : $sPath ), $sFile, $this->isAdmin(), $bSsl );
00330 if ( $aPicInfo['url'] && $aSize[0] && $aSize[1] ) {
00331 $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam( 'sDefaultImageQuality' );
00332 $sUrl = str_replace( "/master/" . ( $sAltPath ? $sAltPath : $sPath ), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url'] );
00333 }
00334 }
00335 return $sUrl;
00336 }
00337
00349 public function getProductPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $bSsl = null )
00350 {
00351 $sUrl = null;
00352 if ( !$sFile || !( $sUrl = $this->getPicUrl( $sPath, $sFile, $sSize, $sIndex, false, $bSsl ) ) ) {
00353 $sUrl = $this->getPicUrl( $sPath, "nopic.jpg", $sSize, $sIndex, "/", $bSsl );
00354 }
00355 return $sUrl;
00356 }
00357 }