oxpicturehandler.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPictureHandler extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00015 
00021     public static function getInstance()
00022     {
00023         // disable caching for test modules
00024         if ( defined( 'OXID_PHP_UNIT' ) ) {
00025             self::$_instance = modInstances::getMod( __CLASS__ );
00026         }
00027 
00028         if ( !self::$_instance instanceof oxPictureHandler ) {
00029 
00030             self::$_instance = oxNew( 'oxPictureHandler' );
00031             if ( defined( 'OXID_PHP_UNIT' ) ) {
00032                 modInstances::addMod( __CLASS__, self::$_instance);
00033             }
00034         }
00035         return self::$_instance;
00036     }
00037 
00047     public function generateArticlePictures( $oObject, $iIndex )
00048     {
00049         $iGeneratedImages = (int)$oObject->oxarticles__oxpicsgenerated->value;
00050         $oConfig = $this->getConfig();
00051         $oUtilsFile = oxUtilsFile::getInstance();
00052 
00053         if ( $iGeneratedImages < $iIndex ) {
00054 
00055             $iTotalGenerated = $iGeneratedImages;
00056 
00057             for ( $iNr = $iGeneratedImages + 1; $iNr <= $iIndex; $iNr++ ) {
00058 
00059                 $sField = "oxarticles__oxpic" . $iNr;
00060 
00061                 if ( $oObject->$sField->value ) {
00062 
00063                     $sMasterPictureSource = $oConfig->getMasterPictureDir() . $iNr . "/" . basename($oObject->$sField->value);
00064 
00065                     if ( file_exists( $sMasterPictureSource ) ) {
00066 
00067                         $sNewName = $this->_getArticleMasterPictureName( $oObject, $iNr );
00068 
00069                         $aFiles = array();
00070 
00071                         // main product picture
00072                         $sType = "P" . $iNr . "@oxarticles__oxpic" . $iNr;
00073                         $aFiles['myfile']['name'][$sType] = basename($oObject->{"oxarticles__oxpic".$iNr}->value);
00074                         $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00075 
00076                         // zoom picture
00077                         $sType = "Z" . $iNr . "@oxarticles__oxzoom" . $iNr;
00078                         $oObject->{"oxarticles__oxzoom" . $iNr} =  new oxField();
00079                         $aFiles['myfile']['name'][$sType] = $sNewName;
00080                         $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource ;
00081 
00082                         $oUtilsFile->processFiles( $oObject, $aFiles, true, false );
00083 
00084                         // if this is picture with number #1, also generating
00085                         // thumbnail and icon
00086                         if ( $iNr == 1 ) {
00087                             $aFiles = array();
00088                             // Thumbnail
00089                             $sType = "TH@oxarticles__oxthumb";
00090                             $aFiles['myfile']['name'][$sType] = $sNewName;
00091                             $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00092 
00093 
00094                             // Icon
00095                             $sType = "ICO@oxarticles__oxicon";
00096                             $aFiles['myfile']['name'][$sType] = $sNewName;
00097                             $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00098 
00099 
00100                             $oUtilsFile->processFiles( null, $aFiles, true, false );
00101                         }
00102 
00103                         $iTotalGenerated++;
00104                     }
00105                 }
00106             }
00107 
00108             if ( $iTotalGenerated > $iGeneratedImages ) {
00109                 $oObject->updateAmountOfGeneratedPictures( $iIndex );
00110             }
00111         }
00112     }
00113 
00122     protected function _getArticleMasterPictureName( $oObject, $iIndex )
00123     {
00124         $sName = '';
00125 
00126         if ( $iIndex ) {
00127             $sName = $oObject->{"oxarticles__oxpic".$iIndex}->value;
00128             $sName = $this->_getBaseMasterImageFileName( $sName );
00129 
00130         }
00131 
00132         return $sName;
00133     }
00134 
00146     public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
00147     {
00148         $oDB        = oxDb::getDb( true );
00149         $myConfig   = $this->getConfig();
00150         $myUtilsPic = oxUtilsPic::getInstance();
00151         $oUtilsFile = oxUtilsFile::getInstance();
00152 
00153         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00154         $sMasterImage = basename( $oObject->{"oxarticles__oxpic".$iIndex}->value );
00155 
00156         if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00157             return;
00158         }
00159 
00160 
00161         if ( $blDeleteMasterPicture ) {
00162             // master picture
00163             $aPic = array("sField"    => "oxpic".$iIndex,
00164                           "sDir"      => $oUtilsFile->getImageDirByType( "M".$iIndex ),
00165                           "sFileName" => $sMasterImage);
00166         } else {
00167             // general picture
00168             $aPic = array("sField"    => "oxpic".$iIndex,
00169                           "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00170                           "sFileName" => $sMasterImage);
00171         }
00172 
00173         $blDeleted = $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00174 
00175         if ( $blDeleted ) {
00176             $this->deleteZoomPicture( $oObject, $iIndex );
00177 
00178             $aDelPics = array();
00179 
00180             if ( $blDeleteMasterPicture ) {
00181                 //deleting general picture
00182                 $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00183                                     "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00184                                     "sFileName" => $sMasterImage);
00185             }
00186 
00187             if ( $iIndex == 1 ) {
00188                 // deleting generated main icon picture if custom main icon
00189                 // file name not equal with generated from master picture
00190                 if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00191                     $aDelPics[] = array("sField"    => "oxpic1",
00192                                         "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00193                                         "sFileName" => $this->getMainIconName( $sMasterImage ));
00194                 }
00195 
00196                 // deleting generated thumbnail picture if custom thumbnail
00197                 // file name not equal with generated from master picture
00198                 if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00199                     $aDelPics[] = array("sField"    => "oxpic1",
00200                                         "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00201                                         "sFileName" => $this->getThumbName( $sMasterImage ));
00202                 }
00203             }
00204             foreach ( $aDelPics as $aPic ) {
00205                 $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00206             }
00207         }
00208 
00209         //deleting custom zoom pic (compatibility mode)
00210         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00211             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00212                 // deleting old zoom picture
00213                 $this->deleteZoomPicture( $oObject, $iIndex );
00214             }
00215         }
00216 
00217     }
00218 
00226     public function deleteMainIcon( $oObject )
00227     {
00228         $oDB        = oxDb::getDb( true );
00229         $myConfig   = $this->getConfig();
00230         $myUtilsPic = oxUtilsPic::getInstance();
00231         $oUtilsFile = oxUtilsFile::getInstance();
00232 
00233         $sMainIcon = $oObject->oxarticles__oxicon->value;
00234 
00235         if ( !$sMainIcon ) {
00236             return;
00237         }
00238 
00239         $aDelPics = array();
00240         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00241 
00242         $aDelPics = array();
00243 
00244         // deleting article main icon and thumb picture
00245         $aPic = array("sField"    => "oxicon",
00246                       "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00247                       "sFileName" => $sMainIcon);
00248 
00249         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00250     }
00251 
00259     public function deleteThumbnail( $oObject )
00260     {
00261         $oDB        = oxDb::getDb( true );
00262         $myConfig   = $this->getConfig();
00263         $myUtilsPic = oxUtilsPic::getInstance();
00264         $oUtilsFile = oxUtilsFile::getInstance();
00265 
00266         $aDelPics = array();
00267         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00268         $sThumb = $oObject->oxarticles__oxthumb->value;
00269 
00270         if ( !$sThumb ) {
00271             return;
00272         }
00273 
00274         $aDelPics = array();
00275 
00276         // deleting article main icon and thumb picture
00277         $aPic = array("sField"    => "oxthumb",
00278                       "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00279                       "sFileName" => $sThumb);
00280 
00281         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00282     }
00283 
00292     public function deleteZoomPicture( $oObject, $iIndex )
00293     {
00294         // checking if oxzoom field exists
00295         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00296         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00297 
00298         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00299             if ( $sZoomPicName = $this->getZoomName( $oObject->{"oxarticles__oxpic".$iIndex}->value, $iIndex ) ) {
00300                 $sFieldToCheck = "oxpic".$iIndex;
00301             } else {
00302                 return;
00303             }
00304         } else {
00305             $sZoomPicName = basename( $oObject->{"oxarticles__oxzoom".$iIndex}->value );
00306             $sFieldToCheck = "oxzoom".$iIndex;
00307         }
00308 
00309         if ( $sZoomPicName == "nopic.jpg") {
00310             return;
00311         }
00312 
00313         $oDB        = oxDb::getDb( true );
00314         $myConfig   = $this->getConfig();
00315         $myUtilsPic = oxUtilsPic::getInstance();
00316         $oUtilsFile = oxUtilsFile::getInstance();
00317 
00318         $aDelPics = array();
00319         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00320 
00321         if ( !$sZoomPicName ) {
00322             return;
00323         }
00324 
00325         $aDelPics = array();
00326 
00327         // deleting zoom picture
00328         $aPic = array("sField"    => $sFieldToCheck,
00329                       "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00330                       "sFileName" => $sZoomPicName);
00331 
00332         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00333     }
00334 
00342     public function getIconName( $sFilename )
00343     {
00344         $sIconName = getStr()->preg_replace( '/(\.jpg|\.jpeg|\.gif|\.png)$/i', '_ico\\1', basename($sFilename) );
00345 
00346         return $sIconName;
00347     }
00348 
00356     public function getMainIconName( $sMasterImageFile )
00357     {
00358         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00359         $sIconName = getStr()->preg_replace( '/(\.jpg|\.jpeg|\.gif|\.png)$/i', '_ico\\1', $sMasterImageFile );
00360 
00361         return $sIconName;
00362     }
00363 
00371     public function getThumbName( $sMasterImageFile )
00372     {
00373         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00374         $sThumbName = getStr()->preg_replace( '/(\.jpg|\.jpeg|\.gif|\.png)$/i', '_th\\1', $sMasterImageFile );
00375 
00376         return $sThumbName;
00377     }
00378 
00387     public function getZoomName( $sMasterImageFile, $iIndex )
00388     {
00389         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00390 
00391         $sZoomName = getStr()->preg_replace( '/(\.jpg|\.jpeg|\.gif|\.png)$/i', '_z'.$iIndex.'\\1', $sMasterImageFile );
00392 
00393         return $sZoomName;
00394     }
00395 
00403     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00404     {
00405         $sMasterImageFile = getStr()->preg_replace( '/_p\d+(\.jpg|\.jpeg|\.gif|\.png)$/i', '\\1', $sMasterImageFile );
00406 
00407         return basename( $sMasterImageFile );
00408     }
00409 }
00410