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                         $sNewName = $this->_getArticleMasterPictureName( $oObject, $iNr );
00067 
00068                         $aFiles = array();
00069 
00070                         // main product picture
00071                         $sType = "P" . $iNr . "@oxarticles__oxpic" . $iNr;
00072                         $aFiles['myfile']['name'][$sType] = $sNewName;
00073                         //$aFiles['myfile']['name'][$sType] = $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             $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00180                                 "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00181                                 "sFileName" => $sMasterImage);
00182             if ( $iIndex == 1 ) {
00183                 // deleting generated main icon picture if custom main icon
00184                 // file name not equal with generated from master picture
00185                 if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00186                     $aDelPics[] = array("sField"    => "oxpic1",
00187                                         "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00188                                         "sFileName" => $this->getMainIconName( $sMasterImage ));
00189                 }
00190 
00191                 // deleting generated thumbnail picture if custom thumbnail
00192                 // file name not equal with generated from master picture
00193                 if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00194                     $aDelPics[] = array("sField"    => "oxpic1",
00195                                         "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00196                                         "sFileName" => $this->getThumbName( $sMasterImage ));
00197                 }
00198             }
00199             foreach ( $aDelPics as $aPic ) {
00200                 $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00201             }
00202         }
00203 
00204         //deleting custom zoom pic (compatibility mode)
00205         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00206             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00207                 // deleting old zoom picture
00208                 $this->deleteZoomPicture( $oObject, $iIndex );
00209             }
00210         }
00211 
00212     }
00213 
00221     public function deleteMainIcon( $oObject )
00222     {
00223         $oDB        = oxDb::getDb( true );
00224         $myConfig   = $this->getConfig();
00225         $myUtilsPic = oxUtilsPic::getInstance();
00226         $oUtilsFile = oxUtilsFile::getInstance();
00227 
00228         $sMainIcon = $oObject->oxarticles__oxicon->value;
00229 
00230         if ( !$sMainIcon ) {
00231             return;
00232         }
00233 
00234         $aDelPics = array();
00235         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00236 
00237         $aDelPics = array();
00238 
00239         // deleting article main icon and thumb picture
00240         $aPic = array("sField"    => "oxicon",
00241                       "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00242                       "sFileName" => $sMainIcon);
00243 
00244         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00245     }
00246 
00254     public function deleteThumbnail( $oObject )
00255     {
00256         $oDB        = oxDb::getDb( true );
00257         $myConfig   = $this->getConfig();
00258         $myUtilsPic = oxUtilsPic::getInstance();
00259         $oUtilsFile = oxUtilsFile::getInstance();
00260 
00261         $aDelPics = array();
00262         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00263         $sThumb = $oObject->oxarticles__oxthumb->value;
00264 
00265         if ( !$sThumb ) {
00266             return;
00267         }
00268 
00269         $aDelPics = array();
00270 
00271         // deleting article main icon and thumb picture
00272         $aPic = array("sField"    => "oxthumb",
00273                       "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00274                       "sFileName" => $sThumb);
00275 
00276         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00277     }
00278 
00287     public function deleteZoomPicture( $oObject, $iIndex )
00288     {
00289         // checking if oxzoom field exists
00290         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00291         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00292 
00293         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00294             return;
00295         }
00296 
00297         $oDB        = oxDb::getDb( true );
00298         $myConfig   = $this->getConfig();
00299         $myUtilsPic = oxUtilsPic::getInstance();
00300         $oUtilsFile = oxUtilsFile::getInstance();
00301 
00302         $aDelPics = array();
00303         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00304         $sZoomPicName = basename($oObject->{"oxarticles__oxzoom".$iIndex}->value);
00305 
00306         if ( !$sZoomPicName ) {
00307             return;
00308         }
00309 
00310         $aDelPics = array();
00311 
00312         // deleting zoom picture
00313         $aPic = array("sField"    => "oxzoom".$iIndex,
00314                       "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00315                       "sFileName" => $sZoomPicName);
00316 
00317         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00318     }
00319 
00327     public function getIconName( $sFilename )
00328     {
00329         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', basename($sFilename) );
00330 
00331         return $sIconName;
00332     }
00333 
00341     public function getMainIconName( $sMasterImageFile )
00342     {
00343         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00344         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', $sMasterImageFile );
00345 
00346         return $sIconName;
00347     }
00348 
00356     public function getThumbName( $sMasterImageFile )
00357     {
00358         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00359         $sThumbName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_th\\1', $sMasterImageFile );
00360 
00361         return $sThumbName;
00362     }
00363 
00372     public function getZoomName( $sMasterImageFile, $iIndex )
00373     {
00374         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00375         $sZoomName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_z'.$iIndex.'\\1', $sMasterImageFile );
00376 
00377         return $sZoomName;
00378     }
00379 
00387     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00388     {
00389         $sMasterImageFile = getStr()->preg_replace( '/_p\d+(\.jpg|\.gif|\.png)$/i', '\\1', $sMasterImageFile );
00390 
00391         return basename( $sMasterImageFile );
00392     }
00393 }
00394