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() . $iIndex . "/" . basename($oObject->$sField->value);
00064 
00065                     if ( file_exists( $sMasterPictureSource ) ) {
00066                         $sNewName = $this->_getArticleMasterPictureName( $oObject, $iNr );
00067 
00068                         // generating main product picture
00069                         $sType = "P" . $iNr . "@oxarticles__oxpic" . $iNr;
00070                         $aFiles['myfile']['name']     = array( $sType => $sNewName );
00071                         $aFiles['myfile']['tmp_name'] = array( $sType => $sMasterPictureSource );
00072                         $oUtilsFile->processFiles( $oObject, $aFiles, true );
00073                         //$oObject->$sField->value = $oUtilsFile->getImageDirByType( "P".$iIndex ) . $oObject->$sField->value;
00074 
00075                         // generating zoom picture
00076                         $sType = "Z" . $iNr . "@oxarticles__oxzoom" . $iNr;
00077                         $sField = "oxarticles__oxzoom" . $iNr;
00078                         $aFiles['myfile']['name']     = array( $sType => $sNewName );
00079                         $aFiles['myfile']['tmp_name'] = array( $sType => $sMasterPictureSource );
00080                         $oObject->$sField = new oxField();
00081                         $oUtilsFile->processFiles( $oObject, $aFiles, true );
00082                         //$oObject->$sField->value = $oUtilsFile->getImageDirByType( "Z".$iIndex ) . $oObject->$sField->value;
00083 
00084                         // if this is picture with number #1, also generating
00085                         // thumbnail and icon
00086                         if ( $iNr == 1 ) {
00087                             // Thumbnail
00088                             $sType = "TH@oxarticles__oxthumb";
00089                             $aFiles['myfile']['name']     = array( $sType => $sNewName );
00090                             $aFiles['myfile']['tmp_name'] = array( $sType => $sMasterPictureSource );
00091                             $oUtilsFile->processFiles( $oObject, $aFiles, true );
00092                             $oObject->oxarticles__oxthumb = new oxField();
00093 
00094                             //$oObject->oxarticles__oxthumb = new oxField();
00095                             $oObject->oxarticles__oxthumb = null;
00096 
00097                             // Icon
00098                             $sType = "ICO@oxarticles__oxicon";
00099                             $aFiles['myfile']['name']     = array( $sType => $sNewName );
00100                             $aFiles['myfile']['tmp_name'] = array( $sType => $sMasterPictureSource );
00101                             $oUtilsFile->processFiles( $oObject, $aFiles, true );
00102                             $oObject->oxarticles__oxicon = new oxField();
00103                         }
00104 
00105                         $iTotalGenerated++;
00106                     }
00107                 }
00108             }
00109 
00110             if ( $iTotalGenerated > $iGeneratedImages ) {
00111                 $oObject->updateAmountOfGeneratedPictures( $iIndex );
00112             }
00113         }
00114     }
00115 
00124     protected function _getArticleMasterPictureName( $oObject, $iIndex )
00125     {
00126         $sName = '';
00127 
00128         if ( $iIndex ) {
00129             $sName = $oObject->{"oxarticles__oxpic".$iIndex}->value;
00130             $sName = $this->_getBaseMasterImageFileName( $sName );
00131 
00132         }
00133 
00134         return $sName;
00135     }
00136 
00148     public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
00149     {
00150         $oDB        = oxDb::getDb( true );
00151         $myConfig   = $this->getConfig();
00152         $myUtilsPic = oxUtilsPic::getInstance();
00153         $oUtilsFile = oxUtilsFile::getInstance();
00154 
00155         $aDelPics = array();
00156         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00157         $sMasterImage = $oObject->{"oxarticles__oxpic".$iIndex}->value;
00158 
00159         if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
00160             return;
00161         }
00162 
00163         $aDelPics = array();
00164 
00165         if ( $blDeleteMasterPicture ) {
00166             // master picture
00167             $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00168                                 "sDir"      => $oUtilsFile->getImageDirByType( "M".$iIndex ),
00169                                 "sFileName" => $sMasterImage);
00170         }
00171 
00172         // general picture
00173         $aDelPics[] = array("sField"    => "oxpic".$iIndex,
00174                             "sDir"      => $oUtilsFile->getImageDirByType( "P".$iIndex ),
00175                             "sFileName" => $sMasterImage);
00176 
00177         // zoom picture
00178         $sZoomPicName = $this->getZoomName( $sMasterImage, $iIndex );
00179         $aDelPics[] = array("sField"    => "oxpic1",
00180                             "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00181                             "sFileName" => $sZoomPicName);
00182 
00183         if ( $iIndex == 1 ) {
00184             // deleting generated main icon picture if custom main icon
00185             // file name not equal with generated from master picture
00186             if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
00187                 $aDelPics[] = array("sField"    => "oxpic1",
00188                                     "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00189                                     "sFileName" => $this->getMainIconName( $sMasterImage ));
00190             }
00191 
00192             // deleting generated thumbnail picture if custom thumbnail
00193             // file name not equal with generated from master picture
00194             if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
00195                 $aDelPics[] = array("sField"    => "oxpic1",
00196                                     "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00197                                     "sFileName" => $this->getThumbName( $sMasterImage ));
00198             }
00199         }
00200 
00201         foreach ( $aDelPics as $aPic ) {
00202             $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00203         }
00204 
00205         //deleting custom zoom pic (compatibility mode)
00206         if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
00207             if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
00208                 // deleting old zoom picture
00209                 $this->deleteZoomPicture( $oObject, $iIndex );
00210             }
00211         }
00212 
00213     }
00214 
00222     public function deleteMainIcon( $oObject )
00223     {
00224         $oDB        = oxDb::getDb( true );
00225         $myConfig   = $this->getConfig();
00226         $myUtilsPic = oxUtilsPic::getInstance();
00227         $oUtilsFile = oxUtilsFile::getInstance();
00228 
00229         $sMainIcon = $oObject->oxarticles__oxicon->value;
00230 
00231         if ( !$sMainIcon ) {
00232             return;
00233         }
00234 
00235         $aDelPics = array();
00236         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00237 
00238         $aDelPics = array();
00239 
00240         // deleting article main icon and thumb picture
00241         $aPic = array("sField"    => "oxicon",
00242                       "sDir"      => $oUtilsFile->getImageDirByType( "ICO" ),
00243                       "sFileName" => $sMainIcon);
00244 
00245         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00246     }
00247 
00255     public function deleteThumbnail( $oObject )
00256     {
00257         $oDB        = oxDb::getDb( true );
00258         $myConfig   = $this->getConfig();
00259         $myUtilsPic = oxUtilsPic::getInstance();
00260         $oUtilsFile = oxUtilsFile::getInstance();
00261 
00262         $aDelPics = array();
00263         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00264         $sThumb = $oObject->oxarticles__oxthumb->value;
00265 
00266         if ( !$sThumb ) {
00267             return;
00268         }
00269 
00270         $aDelPics = array();
00271 
00272         // deleting article main icon and thumb picture
00273         $aPic = array("sField"    => "oxthumb",
00274                       "sDir"      => $oUtilsFile->getImageDirByType( "TH" ),
00275                       "sFileName" => $sThumb);
00276 
00277         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00278     }
00279 
00288     public function deleteZoomPicture( $oObject, $iIndex )
00289     {
00290         // checking if oxzoom field exists
00291         $oDbHandler = oxNew( "oxDbMetaDataHandler" );
00292         $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
00293 
00294         if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
00295             return;
00296         }
00297 
00298         $oDB        = oxDb::getDb( true );
00299         $myConfig   = $this->getConfig();
00300         $myUtilsPic = oxUtilsPic::getInstance();
00301         $oUtilsFile = oxUtilsFile::getInstance();
00302 
00303         $aDelPics = array();
00304         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
00305         $sZoomPicName = basename($oObject->{"oxarticles__oxzoom".$iIndex}->value);
00306 
00307         if ( !$sZoomPicName ) {
00308             return;
00309         }
00310 
00311         $aDelPics = array();
00312 
00313         // deleting zoom picture
00314         $aPic = array("sField"    => "oxzoom".$iIndex,
00315                       "sDir"      => $oUtilsFile->getImageDirByType( "Z".$iIndex ),
00316                       "sFileName" => $sZoomPicName);
00317 
00318         $myUtilsPic->safePictureDelete( $aPic["sFileName"], $myConfig->getAbsDynImageDir() . $aPic["sDir"], "oxarticles", $aPic["sField"] );
00319     }
00320 
00328     public function getIconName( $sFilename )
00329     {
00330         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', basename($sFilename) );
00331 
00332         return $sIconName;
00333     }
00334 
00342     public function getMainIconName( $sMasterImageFile )
00343     {
00344         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00345         $sIconName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', $sMasterImageFile );
00346 
00347         return $sIconName;
00348     }
00349 
00357     public function getThumbName( $sMasterImageFile )
00358     {
00359         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00360         $sThumbName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_th\\1', $sMasterImageFile );
00361 
00362         return $sThumbName;
00363     }
00364 
00373     public function getZoomName( $sMasterImageFile, $iIndex )
00374     {
00375         $sMasterImageFile = $this->_getBaseMasterImageFileName( $sMasterImageFile );
00376         $sZoomName = getStr()->preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_z'.$iIndex.'\\1', $sMasterImageFile );
00377 
00378         return $sZoomName;
00379     }
00380 
00388     protected function _getBaseMasterImageFileName( $sMasterImageFile )
00389     {
00390         $sMasterImageFile = getStr()->preg_replace( '/_p\d+(\.jpg|\.gif|\.png)$/i', '\\1', $sMasterImageFile );
00391 
00392         return basename( $sMasterImageFile );
00393     }
00394 }
00395