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] = $oObject->{"oxarticles__oxpic".$iNr}->value;;
00073                         $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00074 
00075                         // zoom picture
00076                         $sType = "Z" . $iNr . "@oxarticles__oxzoom" . $iNr;
00077                         $oObject->{"oxarticles__oxzoom" . $iNr} =  new oxField();
00078                         $aFiles['myfile']['name'][$sType] = $sNewName;
00079                         $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource ;
00080 
00081                         $oUtilsFile->processFiles( $oObject, $aFiles, true, false );
00082 
00083                         // if this is picture with number #1, also generating
00084                         // thumbnail and icon
00085                         if ( $iNr == 1 ) {
00086                             $aFiles = array();
00087                             // Thumbnail
00088                             $sType = "TH@oxarticles__oxthumb";
00089                             $aFiles['myfile']['name'][$sType] = $sNewName;
00090                             $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00091 
00092                             // Icon
00093                             $sType = "ICO@oxarticles__oxicon";
00094                             $aFiles['myfile']['name'][$sType] = $sNewName;
00095                             $aFiles['myfile']['tmp_name'][$sType] = $sMasterPictureSource;
00096 
00097                             $oUtilsFile->processFiles( null, $aFiles, true, false );
00098                         }
00099 
00100                         $iTotalGenerated++;
00101                     }
00102                 }
00103             }
00104 
00105             if ( $iTotalGenerated > $iGeneratedImages ) {
00106                 $oObject->updateAmountOfGeneratedPictures( $iIndex );
00107             }
00108         }
00109     }
00110 
00119     protected function _getArticleMasterPictureName( $oObject, $iIndex )
00120     {
00121         $sName = '';
00122 
00123         if ( $iIndex ) {
00124             $sName = $oObject->{"oxarticles__oxpic".$iIndex}->value;
00125             $sName = $this->_getBaseMasterImageFileName( $sName );
00126 
00127         }
00128 
00129         return $sName;
00130     }
00131 
00143     public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
00144     {
00145         $oDB        = oxDb::getDb( true );
00146         $myConfig   = $this->getConfig();
00147         $myUtilsPic = oxUtilsPic::getInstance();
00148         $oUtilsFile = oxUtilsFile::getInstance();
00149 
00150         $aDelPics = array();
00151         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00152         $sMasterImage = $oObject->{"oxarticles__oxpic".$iIndex}->value;
00153 
00154         if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00155             return;
00156         }
00157 
00158         $aDelPics = array();
00159 
00160         if ( $blDeleteMasterPicture ) {
00161             // master picture
00162             $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00163                                 "sDir"      => $oUtilsFile->getImageDirByType( "M".$iIndex ),
00164                                 "sFileName" => $sMasterImage);
00165         }
00166 
00167         // general picture
00168         $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00169                             "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00170                             "sFileName" => $sMasterImage);
00171 
00172         // zoom picture
00173         $sZoomPicName = $this->getZoomName( $sMasterImage, $iIndex );
00174         $aDelPics[] = array("sField"    => "oxpic1",
00175                             "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00176                             "sFileName" => $sZoomPicName);
00177 
00178         if ( $iIndex == 1 ) {
00179             // deleting generated main icon picture if custom main icon
00180             // file name not equal with generated from master picture
00181             if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00182                 $aDelPics[] = array("sField"    => "oxpic1",
00183                                     "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00184                                     "sFileName" => $this->getMainIconName( $sMasterImage ));
00185             }
00186 
00187             // deleting generated thumbnail picture if custom thumbnail
00188             // file name not equal with generated from master picture
00189             if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00190                 $aDelPics[] = array("sField"    => "oxpic1",
00191                                     "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00192                                     "sFileName" => $this->getThumbName( $sMasterImage ));
00193             }
00194         }
00195 
00196         foreach ( $aDelPics as $aPic ) {
00197             $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00198         }
00199 
00200         //deleting custom zoom pic (compatibility mode)
00201         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00202             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00203                 // deleting old zoom picture
00204                 $this->deleteZoomPicture( $oObject, $iIndex );
00205             }
00206         }
00207 
00208     }
00209 
00217     public function deleteMainIcon( $oObject )
00218     {
00219         $oDB        = oxDb::getDb( true );
00220         $myConfig   = $this->getConfig();
00221         $myUtilsPic = oxUtilsPic::getInstance();
00222         $oUtilsFile = oxUtilsFile::getInstance();
00223 
00224         $sMainIcon = $oObject->oxarticles__oxicon->value;
00225 
00226         if ( !$sMainIcon ) {
00227             return;
00228         }
00229 
00230         $aDelPics = array();
00231         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00232 
00233         $aDelPics = array();
00234 
00235         // deleting article main icon and thumb picture
00236         $aPic = array("sField"    => "oxicon",
00237                       "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00238                       "sFileName" => $sMainIcon);
00239 
00240         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00241     }
00242 
00250     public function deleteThumbnail( $oObject )
00251     {
00252         $oDB        = oxDb::getDb( true );
00253         $myConfig   = $this->getConfig();
00254         $myUtilsPic = oxUtilsPic::getInstance();
00255         $oUtilsFile = oxUtilsFile::getInstance();
00256 
00257         $aDelPics = array();
00258         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00259         $sThumb = $oObject->oxarticles__oxthumb->value;
00260 
00261         if ( !$sThumb ) {
00262             return;
00263         }
00264 
00265         $aDelPics = array();
00266 
00267         // deleting article main icon and thumb picture
00268         $aPic = array("sField"    => "oxthumb",
00269                       "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00270                       "sFileName" => $sThumb);
00271 
00272         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00273     }
00274 
00283     public function deleteZoomPicture( $oObject, $iIndex )
00284     {
00285         // checking if oxzoom field exists
00286         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00287         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00288 
00289         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00290             return;
00291         }
00292 
00293         $oDB        = oxDb::getDb( true );
00294         $myConfig   = $this->getConfig();
00295         $myUtilsPic = oxUtilsPic::getInstance();
00296         $oUtilsFile = oxUtilsFile::getInstance();
00297 
00298         $aDelPics = array();
00299         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00300         $sZoomPicName = basename($oObject->{"oxarticles__oxzoom".$iIndex}->value);
00301 
00302         if ( !$sZoomPicName ) {
00303             return;
00304         }
00305 
00306         $aDelPics = array();
00307 
00308         // deleting zoom picture
00309         $aPic = array("sField"    => "oxzoom".$iIndex,
00310                       "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00311                       "sFileName" => $sZoomPicName);
00312 
00313         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00314     }
00315 
00323     public function getIconName( $sFilename )
00324     {
00325         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', basename($sFilename) );
00326 
00327         return $sIconName;
00328     }
00329 
00337     public function getMainIconName( $sMasterImageFile )
00338     {
00339         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00340         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', $sMasterImageFile );
00341 
00342         return $sIconName;
00343     }
00344 
00352     public function getThumbName( $sMasterImageFile )
00353     {
00354         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00355         $sThumbName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_th\\1', $sMasterImageFile );
00356 
00357         return $sThumbName;
00358     }
00359 
00368     public function getZoomName( $sMasterImageFile, $iIndex )
00369     {
00370         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00371         $sZoomName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_z'.$iIndex.'\\1', $sMasterImageFile );
00372 
00373         return $sZoomName;
00374     }
00375 
00383     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00384     {
00385         $sMasterImageFile = getStr()->preg_replace( '/_p\d+(\.jpg|\.gif|\.png)$/i', '\\1', $sMasterImageFile );
00386 
00387         return basename( $sMasterImageFile );
00388     }
00389 }
00390