oxpicturehandler.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPictureHandler extends oxSuperCfg
00007 {
00008 
00020     public function deleteArticleMasterPicture($oObject, $iIndex, $blDeleteMasterPicture = true)
00021     {
00022         $myConfig = $this->getConfig();
00023         $myUtilsPic = oxRegistry::get("oxUtilsPic");
00024         $oUtilsFile = oxRegistry::get("oxUtilsFile");
00025         $blGeneratedImagesOnly = !$blDeleteMasterPicture;
00026 
00027         $sAbsDynImageDir = $myConfig->getPictureDir(false);
00028         $sMasterImage = basename($oObject->{"oxarticles__oxpic" . $iIndex}->value);
00029         if (!$sMasterImage || $sMasterImage == "nopic.jpg") {
00030             return;
00031         }
00032 
00033         $aPic = array("sField"    => "oxpic" . $iIndex,
00034                       "sDir"      => $oUtilsFile->getImageDirByType("M" . $iIndex, $blGeneratedImagesOnly),
00035                       "sFileName" => $sMasterImage);
00036 
00037         $blDeleted = $myUtilsPic->safePictureDelete($aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"]);
00038         if ($blDeleted) {
00039             $this->deleteZoomPicture($oObject, $iIndex);
00040 
00041             $aDelPics = array();
00042             if ($iIndex == 1) {
00043                 // deleting generated main icon picture if custom main icon
00044                 // file name not equal with generated from master picture
00045                 if ($this->getMainIconName($sMasterImage) != basename($oObject->oxarticles__oxicon->value)) {
00046                     $aDelPics[] = array("sField"    => "oxpic1",
00047                                         "sDir"      => $oUtilsFile->getImageDirByType("ICO", $blGeneratedImagesOnly),
00048                                         "sFileName" => $this->getMainIconName($sMasterImage));
00049                 }
00050 
00051                 // deleting generated thumbnail picture if custom thumbnail
00052                 // file name not equal with generated from master picture
00053                 if ($this->getThumbName($sMasterImage) != basename($oObject->oxarticles__oxthumb->value)) {
00054                     $aDelPics[] = array("sField"    => "oxpic1",
00055                                         "sDir"      => $oUtilsFile->getImageDirByType("TH", $blGeneratedImagesOnly),
00056                                         "sFileName" => $this->getThumbName($sMasterImage));
00057                 }
00058             }
00059 
00060             foreach ($aDelPics as $aPic) {
00061                 $myUtilsPic->safePictureDelete($aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"]);
00062             }
00063         }
00064 
00065         //deleting custom zoom pic (compatibility mode)
00066         if ($oObject->{"oxarticles__oxzoom" . $iIndex}->value) {
00067             if (basename($oObject->{"oxarticles__oxzoom" . $iIndex}->value) !== "nopic.jpg") {
00068                 // deleting old zoom picture
00069                 $this->deleteZoomPicture($oObject, $iIndex);
00070             }
00071         }
00072 
00073     }
00074 
00080     public function deleteMainIcon($oObject)
00081     {
00082         if (($sMainIcon = $oObject->oxarticles__oxicon->value)) {
00083             $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("ICO");
00084             oxRegistry::get("oxUtilsPic")->safePictureDelete($sMainIcon, $sPath, "oxarticles", "oxicon");
00085         }
00086     }
00087 
00093     public function deleteThumbnail($oObject)
00094     {
00095         if (($sThumb = $oObject->oxarticles__oxthumb->value)) {
00096             // deleting article main icon and thumb picture
00097             $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("TH");
00098             oxRegistry::get("oxUtilsPic")->safePictureDelete($sThumb, $sPath, "oxarticles", "oxthumb");
00099         }
00100     }
00101 
00110     public function deleteZoomPicture($oObject, $iIndex)
00111     {
00112         // checking if oxzoom field exists
00113         $oDbHandler = oxNew("oxDbMetaDataHandler");
00114         $iZoomPicCount = (int) $this->getConfig()->getConfigParam('iZoomPicCount');
00115 
00116         if ($iIndex > $iZoomPicCount || !$oDbHandler->fieldExists("oxzoom" . $iIndex, "oxarticles")) {
00117             if ($sZoomPicName = $this->getZoomName($oObject->{"oxarticles__oxpic" . $iIndex}->value, $iIndex)) {
00118                 $sFieldToCheck = "oxpic" . $iIndex;
00119             } else {
00120                 return;
00121             }
00122         } else {
00123             $sZoomPicName = basename($oObject->{"oxarticles__oxzoom" . $iIndex}->value);
00124             $sFieldToCheck = "oxzoom" . $iIndex;
00125         }
00126 
00127         if ($sZoomPicName && $sZoomPicName != "nopic.jpg") {
00128             // deleting zoom picture
00129             $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("Z" . $iIndex);
00130             oxRegistry::get("oxUtilsPic")->safePictureDelete($sZoomPicName, $sPath, "oxarticles", $sFieldToCheck);
00131         }
00132     }
00133 
00141     public function getIconName($sFilename)
00142     {
00143         return $sFilename;
00144     }
00145 
00153     public function getMainIconName($sMasterImageFile)
00154     {
00155         return $this->_getBaseMasterImageFileName($sMasterImageFile);
00156     }
00157 
00165     public function getThumbName($sMasterImageFile)
00166     {
00167         return basename($sMasterImageFile);
00168     }
00169 
00178     public function getZoomName($sMasterImageFile, $iIndex)
00179     {
00180         return basename($sMasterImageFile);
00181     }
00182 
00190     protected function _getBaseMasterImageFileName($sMasterImageFile)
00191     {
00192         return basename($sMasterImageFile);
00193     }
00194 
00203     public function getImageSize($aImgSizes, $sIndex = null)
00204     {
00205         if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
00206             $aSize = explode('*', $aImgSizes[$sIndex]);
00207         } elseif (is_string($aImgSizes)) {
00208             $aSize = explode('*', $aImgSizes);
00209         }
00210         if (2 == count($aSize)) {
00211             $x = (int) $aSize[0];
00212             $y = (int) $aSize[1];
00213             if ($x && $y) {
00214                 return $aSize;
00215             }
00216         }
00217 
00218         return null;
00219     }
00220 
00233     protected function _getPictureInfo($sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null)
00234     {
00235         // custom server as image storage?
00236         if ($sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL)) {
00237             return array('path' => false, 'url' => $sAltUrl);
00238         }
00239 
00240         $oConfig = $this->getConfig();
00241         $sPath = $oConfig->getPicturePath($sFilePath . $sFile, $blAdmin, $iLang, $iShopId);
00242         if (!$sPath) {
00243             return array('path' => false, 'url' => false);
00244         }
00245 
00246         $sDirPrefix = $oConfig->getOutDir();
00247         $sUrlPrefix = $oConfig->getOutUrl($blSSL, $blAdmin, $oConfig->getConfigParam('blNativeImages'));
00248 
00249         return array('path' => $sPath, 'url' => str_replace($sDirPrefix, $sUrlPrefix, $sPath));
00250     }
00251 
00261     public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
00262     {
00263         $oConfig = $this->getConfig();
00264 
00265         $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
00266         if (!$sAltUrl) {
00267             $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
00268         }
00269 
00270         if ($sAltUrl) {
00271             if ((is_null($blSSL) && $oConfig->isSsl()) || $blSSL) {
00272 
00273                 $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
00274                 if (!$sSslAltUrl) {
00275                     $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
00276                 }
00277 
00278                 if ($sSslAltUrl) {
00279                     $sAltUrl = $sSslAltUrl;
00280                 }
00281             }
00282 
00283             if (!is_null($sFile)) {
00284                 $sAltUrl .= '/' . $sFilePath . $sFile;
00285             }
00286         }
00287 
00288         return $sAltUrl;
00289     }
00290 
00303     public function getPicUrl($sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false, $bSsl = null)
00304     {
00305         $sUrl = null;
00306         if ($sPath && $sFile && ($aSize = $this->getImageSize($sSize, $sIndex))) {
00307 
00308             $aPicInfo = $this->_getPictureInfo("master/" . ($sAltPath ? $sAltPath : $sPath), $sFile, $this->isAdmin(), $bSsl);
00309             if ($aPicInfo['url'] && $aSize[0] && $aSize[1]) {
00310                 $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam('sDefaultImageQuality');
00311                 $sUrl = str_replace("/master/" . ($sAltPath ? $sAltPath : $sPath), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url']);
00312             }
00313         }
00314 
00315         return $sUrl;
00316     }
00317 
00329     public function getProductPicUrl($sPath, $sFile, $sSize, $sIndex = null, $bSsl = null)
00330     {
00331         $sUrl = null;
00332         if (!$sFile || !($sUrl = $this->getPicUrl($sPath, $sFile, $sSize, $sIndex, false, $bSsl))) {
00333             $sUrl = $this->getPicUrl($sPath, "nopic.jpg", $sSize, $sIndex, "/", $bSsl);
00334         }
00335 
00336         return $sUrl;
00337     }
00338 }