oxbase.php

Go to the documentation of this file.
00001 <?php
00002 
00006 DEFINE('ACTION_NA', 0);
00007 DEFINE('ACTION_DELETE', 1);
00008 DEFINE('ACTION_INSERT', 2);
00009 DEFINE('ACTION_UPDATE', 3);
00010 DEFINE('ACTION_UPDATE_STOCK', 4);
00011 
00016 class oxBase extends oxSuperCfg
00017 {
00022     protected $_sOXID = null;
00023 
00028     protected $_iShopId = null;
00029 
00035     protected $_blIsSimplyClonable = true;
00036 
00041     protected $_aErrors = array();
00042 
00047     protected $_sClassName = 'oxbase';
00048 
00054     protected $_sCoreTable = null;
00055 
00060     protected $_sViewTable  = null;
00061 
00062 
00068     protected $_aFieldNames = array('oxid' => 0);
00069 
00074     protected $_blIsNewCache = false;
00075 
00081     protected $_sCacheKey = null;
00082 
00088     protected $_blUseLazyLoading = false;
00089 
00095     protected $_aSkipSaveFields = array();
00096 
00101     protected $_sExistKey = "oxid";
00102 
00109     protected $_blIsDerived = null;
00110 
00120     protected static $_blDisableFieldCaching = array();
00121 
00127     protected $_blIsSeoObject = false;
00128 
00134     protected $_blReadOnly = false;
00135 
00141     protected $_blIsInList = false;
00142 
00148     protected $_isLoaded = false;
00149 
00153     public function __construct()
00154     {
00155         // set active shop
00156         $myConfig = $this->getConfig();
00157         $this->_sCacheKey = $this->getViewName();
00158         if ( $this->_blUseLazyLoading ) {
00159             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00160         } else {
00161             $this->_sCacheKey .= "allviews";
00162         }
00163 
00164         //do not cache for admin?
00165         if ( $this->isAdmin() ) {
00166             $this->_sCacheKey = null;
00167         }
00168 
00169         $this->setShopId( $myConfig->getShopId() );
00170     }
00171 
00180     public function __set($sName, $sValue)
00181     {
00182         $this->$sName = $sValue;
00183         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00184             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00185             if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00186                 $aAllFields = $this->_getAllFields(true);
00187                 if (isset($aAllFields[strtolower($sFieldName)])) {
00188                     $iFieldStatus = $this->_getFieldStatus($sFieldName);
00189                     $this->_addField($sFieldName, $iFieldStatus);
00190                 }
00191             }
00192         }
00193     }
00194 
00202     public function __get( $sName )
00203     {
00204         switch ($sName) {
00205             case 'blIsDerived':
00206                 return $this->isDerived();
00207                 break;
00208             case 'sOXID':
00209                 return $this->getId();
00210                 break;
00211             case 'blReadOnly':
00212                 return $this->isReadOnly();
00213                 break;
00214         }
00215 
00216         // implementing lazy loading fields
00217         // This part of the code is slow and normally is called before field cache is built.
00218         // Make sure it is not called after first page is loaded and cache data is fully built.
00219         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00220 
00221             //lazy load it
00222             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00223             //$sFieldName = $this->getSqlFieldName($sFieldName);
00224             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00225 
00226             $sSqlFieldName = $sFieldName;
00227             if ($iFieldStatus && $this->isMultilang() ) {
00228                 $sSqlFieldName =  $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00229             }
00230 
00231             if ( $this->getId() ) {
00232                 $oDb = oxDb::getDb();
00233                 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00234 
00235                 try {
00236                     //$sValue = $oDb->getOne( $sQ );
00237                     $rs = $oDb->execute( $sQ );
00238                     if ( $rs === false ) {
00239                         return null;
00240                     }
00241 
00242                     $this->_addField($sFieldName, $iFieldStatus);
00243                     $sValue = $rs->fields[0];
00244                     $this->_setFieldData( $sFieldName, $sValue );
00245 
00246                     //save names to cache for next loading
00247                     if ($this->_sCacheKey) {
00248                         $myUtils = oxUtils::getInstance();
00249                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00250                         $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00251                         $aFieldNames[$sFieldName] = $iFieldStatus;
00252                         $myUtils->toFileCache($sCacheKey, $aFieldNames);
00253                     }
00254 
00255                 } catch ( Exception $e ) {
00256                     return null;
00257                 }
00258 
00259                 //do not use field cache for this page
00260                 //as if we use it for lists then objects are loaded empty instead of lazy lodaing.
00261                 self::$_blDisableFieldCaching[get_class($this)] = true;
00262             }
00263 
00264             /*
00265             //save names to cache for next loading
00266             if ($this->_sCacheKey) {
00267                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00268                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00269                 $aFieldNames[$sFieldName] = $iFieldStatus;
00270                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00271             }*/
00272 
00273             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00274         }
00275 
00276         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00277         if (!isset( $this->$sName ) ) {
00278             $this->$sName = null;
00279         }
00280 
00281         return $this->$sName;
00282     }
00283 
00291     public function __isset($mVar)
00292     {
00293         return isset($this->$mVar);
00294     }
00295 
00301     public function __clone()
00302     {
00303         if (!$this->_blIsSimplyClonable) {
00304             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00305                 $sLongName = $this->_getFieldLongName( $sField );
00306                 if ( is_object($this->$sLongName)) {
00307                     $this->$sLongName = clone $this->$sLongName;
00308                 }
00309             }
00310         }
00311     }
00312 
00320     public function oxClone($oObject)
00321     {
00322         $aClass_vars = get_object_vars( $oObject);
00323         while (list($name, $value) = each($aClass_vars)) {
00324             if ( is_object( $oObject->$name ) ) {
00325                 $this->$name = clone $oObject->$name;
00326             } else {
00327                 $this->$name = $oObject->$name;
00328             }
00329         }
00330     }
00331 
00340     public function init( $sTableName = null, $blForceAllFields = false)
00341     {
00342 
00343         if ( !$sTableName ) {
00344             $sTableName = $this->_sCoreTable;
00345         } else {
00346             $this->_sCoreTable = $sTableName;
00347         }
00348 
00349         $this->_sViewTable = getViewName( $this->_sCoreTable );
00350 
00351         //do not use views?
00352 
00353         if ( count( $this->_aFieldNames ) <= 1 ) {
00354             $this->_initDataStructure( $blForceAllFields );
00355         }
00356     }
00357 
00365     public function assign( $dbRecord )
00366     {
00367         if ( !is_array( $dbRecord ) ) {
00368             return;
00369         }
00370 
00371 
00372         reset($dbRecord );
00373         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00374 
00375             // patch for IIS
00376             //TODO: test it on IIS do we still need it
00377             //if( is_array($value) && count( $value) == 1)
00378             //    $value = current( $value);
00379 
00380             $this->_setFieldData( $sName, $sValue );
00381         }
00382 
00383         $sOxidField = $this->_getFieldLongName( 'oxid' );
00384         $this->_sOXID = $this->$sOxidField->value;
00385 
00386     }
00387 
00393     public function getClassName()
00394     {
00395         return $this->_sClassName;
00396     }
00397 
00403     public function getCoreTableName()
00404     {
00405         return $this->_sCoreTable;
00406     }
00407 
00413     public function getId()
00414     {
00415         return $this->_sOXID;
00416     }
00417 
00425     public function setId($sOXID = null)
00426     {
00427         if ( $sOXID ) {
00428             $this->_sOXID = $sOXID;
00429         } else {
00430             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00431         }
00432 
00433         $sIdVarName = $this->_sCoreTable . "__oxid";
00434         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00435 
00436         return $this->_sOXID;
00437     }
00438 
00446     public function setShopId($iShopId)
00447     {
00448         $this->_iShopId = $iShopId;
00449     }
00450 
00456     public function getShopId()
00457     {
00458         return $this->_iShopId;
00459     }
00460 
00466     public function getViewName()
00467     {
00468         return $this->_sViewTable;
00469     }
00470 
00479     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00480     {
00481         if ( $blOverride ) {
00482             $this->_sCacheKey = $sCacheKey;
00483         } else {
00484             $this->_sCacheKey .= $sCacheKey;
00485         }
00486     }
00487 
00493     public function disableLazyLoading()
00494     {
00495         $this->_blUseLazyLoading = false;
00496         $this->_initDataStructure(true);
00497     }
00498 
00499 
00505     public function isDerived()
00506     {
00507 
00508         return $this->_blIsDerived;
00509     }
00510 
00518     public function setIsDerived($blVal)
00519     {
00520         $this->_blIsDerived = $blVal;
00521     }
00522 
00529     public function isMultilang()
00530     {
00531         return false;
00532     }
00533 
00543     public function load( $sOXID)
00544     {
00545         /*
00546         if( !isset($oxID)){
00547             $oEx = oxNew('oxObjectException','core');
00548             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00549             $oEx->setObject($this);
00550             throw $oEx;
00551         }*/
00552 
00553         //getting at least one field before lazy loading the object
00554         $this->_addField('oxid', 0);
00555 
00556         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00557 
00558         return $this->_isLoaded = $this->assignRecord( $sSelect );
00559     }
00560 
00566     public function isLoaded()
00567     {
00568         return $this->_isLoaded;
00569     }
00570 
00578     public function buildSelectString( $aWhere = null)
00579     {
00580         $oDB = oxDb::getDb(true);
00581         $myUtils = oxUtils::getInstance();
00582 
00583         $sGet = $this->getSelectFields();
00584         $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00585 
00586         if ( $aWhere) {
00587             reset($aWhere);
00588             while (list($name, $value) = each($aWhere)) {
00589                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00590             }
00591         }
00592 
00593         // add active shop
00594 
00595         //echo( "<br><br>$sSelect <br><br>\n\n\n");
00596 
00597         return $sSelect;
00598     }
00599 
00607     public function assignRecord( $sSelect)
00608     {
00609         $blRet = false;
00610 
00611         $oDB = oxDb::getDb(true);
00612 
00613         $rs = $oDB->execute( $sSelect);
00614         if ($rs != false && $rs->recordCount() > 0) {
00615             $blRet = true;
00616             $this->assign( $rs->fields);
00617         }
00618 
00619         return $blRet;
00620     }
00621 
00629     public function getFieldData( $sFieldName)
00630     {
00631         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00632         return $this->$sLongFieldName->value;
00633     }
00634 
00640     public function getSelectFields()
00641     {
00642         $aSelectFields = array();
00643 
00644         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00645             $sRealField = $this->getSqlFieldName($sKey);
00646             $sSelectSufix = "";
00647             if ( $sRealField !== $sKey ) {
00648                 $sSelectSufix = " as $sKey";
00649             }
00650             $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00651         }
00652 
00653         $sSelectFields = join( ", ", $aSelectFields );
00654         return $sSelectFields;
00655     }
00656 
00664     public function delete( $sOXID = null)
00665     {
00666         if ( !$sOXID ) {
00667             $sOXID = $this->getId();
00668 
00669             //do not allow derived deletion
00670             if ( !$this->allowDerivedDelete() ) {
00671                 return false;
00672             }
00673         }
00674 
00675         if ( !$sOXID ) {
00676             return false;
00677         }
00678 
00679 
00680         $oDB = oxDb::getDb(true);
00681         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00682         $rs = $oDB->execute( $sDelete );
00683         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00684             $this->onChange(ACTION_DELETE, $sOXID);
00685         }
00686 
00687         return $blDelete;
00688     }
00689 
00690 
00696     public function save()
00697     {
00698         if ( !is_array( $this->_aFieldNames ) ) {
00699             return false;
00700         }
00701 
00702         $blRet = false;
00703 
00704         // #739A - should be executed here because of date/time formatting feature
00705         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00706             foreach ($this->_aFieldNames as $sName => $sVal) {
00707                 $sLongName = $this->_getFieldLongName($sName);
00708                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00709                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00710                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00711                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00712                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00713                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00714                 }
00715             }
00716         }
00717 
00718         if ( $this->exists() ) {
00719 
00720             //do not allow derived update
00721             if ( !$this->allowDerivedUpdate() ) {
00722                 return false;
00723             }
00724 
00725             $blRet = $this->_update();
00726             $sAction = ACTION_UPDATE;
00727         } else {
00728             $blRet = $this->_insert();
00729             $sAction = ACTION_INSERT;
00730         }
00731 
00732         $this->onChange($sAction);
00733 
00734         if ( $blRet ) {
00735             return $this->getId();
00736         } else {
00737             return false;
00738         }
00739     }
00740 
00746     public function allowDerivedUpdate()
00747     {
00748         return !$this->isDerived();
00749     }
00750 
00756     public function allowDerivedDelete()
00757     {
00758         return !$this->isDerived();
00759     }
00760 
00768     public function exists( $sOXID = null)
00769     {
00770         if ( !$sOXID ) {
00771             $sOXID = $this->getId();
00772         }
00773         if ( !$sOXID ) {
00774             return false;
00775         }
00776 
00777         $oDB    = oxDb::getDb(true);
00778         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00779         return ( bool ) $oDB->getOne( $sSelect );
00780     }
00781 
00789     public function getSqlFieldName($sField)
00790     {
00791         return $sField;
00792     }
00793 
00801     public function getSqlActiveSnippet( $blForceCoreTable = false )
00802     {
00803         $sQ = '';
00804             $sTable = $this->getCoreTableName();
00805 
00806         // has 'active' field ?
00807         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00808             $sQ = " $sTable.oxactive = 1 ";
00809         }
00810 
00811         // has 'activefrom'/'activeto' fields ?
00812         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00813 
00814             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00815 
00816             $sQ = $sQ?" $sQ or ":'';
00817             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00818         }
00819 
00820         return $sQ;
00821     }
00822 
00829     public function validate()
00830     {
00831         $this->_aErrors = array();
00832         foreach ($this->_aFieldNames as $fName => $iVal) {
00833 
00834             $fName = $this->_getFieldLongName($fName);
00835 
00836             if ( method_exists ( $this, "validate_$fName")) {
00837                 $validatorMethod = "validate_$fName";
00838                 if ( $error = $this->$validatorMethod()) {
00839                     $this->_aErrors[$fName] = $error;
00840                 }
00841             }
00842         }
00843         return !$this->hasErrors();
00844     }
00845 
00854     public function beforeUpdate( $sOXID = null )
00855     {
00856     }
00857 
00868     public function onChange( $iAction = null, $sOXID = null)
00869     {
00870     }
00871 
00872 
00878     public function hasErrors()
00879     {
00880         return count($this->_aErrors) > 0;
00881     }
00882 
00888     public function getErrors()
00889     {
00890         return $this->_aErrors;
00891     }
00892 
00900     public function getError( $sField)
00901     {
00902         if (isset($this->_aErrors[$sField])) {
00903             return $this->_aErrors[$sField];
00904         }
00905 
00906         //T2007-10-19
00907         //return array();
00908         return null;
00909     }
00910 
00918     public function getHtmlError( $sField)
00919     {
00920         if ( $error = $this->getError($sField) ) {
00921             return $error;
00922         }
00923     }
00924 
00930     public function setInList()
00931     {
00932         $this->_blIsInList = true;
00933     }
00934 
00940     protected function _isInList()
00941     {
00942         return $this->_blIsInList;
00943     }
00944 
00953     protected function _getObjectViewName( $sTable, $sShopID = null)
00954     {
00955             return $sTable;
00956 
00957     }
00958 
00959 
00969     protected function _getAllFields($blReturnSimple = false)
00970     {
00971         $myUtils = oxUtils::getInstance();
00972 
00973         //T2008-09-04
00974         //The cache here saves almost 7% of execution time
00975         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00976         //TODO: Check what's up with that.
00977         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00978         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00979 
00980         if ($aMetaFields) {
00981             return $aMetaFields;
00982         }
00983 
00984         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00985 
00986         if ( !$blReturnSimple ) {
00987             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00988             return $aMetaFields;
00989         }
00990 
00991         //returning simple array
00992         $aRet = array();
00993         foreach ( $aMetaFields as $oVal ) {
00994             $aRet[strtolower( $oVal->name )] = 0;
00995         }
00996 
00997         $myUtils->toFileCache( $sCacheKey, $aRet);
00998 
00999         return $aRet;
01000     }
01001 
01010     protected function _initDataStructure($blForceFullStructure = false)
01011     {
01012         $myUtils = oxUtils::getInstance();
01013 
01014         //get field names from cache
01015         $aFieldNames = null;
01016         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01017         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01018             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01019         }
01020 
01021         if (!$aFieldNames) {
01022             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01023             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01024                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01025             }
01026         }
01027 
01028         if ( $aFieldNames !== false ) {
01029             foreach ( $aFieldNames as $sField => $sStatus ) {
01030                 $this->_addField($sField, $sStatus);
01031             }
01032         }
01033     }
01034 
01046     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01047     {
01048         //T2008-02-22
01049         //so if this method is executed on cached version we see it when profiling
01050         startProfile("!__CACHABLE__!");
01051 
01052         //case 1. (admin)
01053         if ($this->isAdmin()) {
01054             $aMetaFields = $this->_getAllFields();
01055             foreach ( $aMetaFields as $oField ) {
01056                 if ( $oField->max_length == -1 ) {
01057                     $oField->max_length = 10;      // double or float
01058                 }
01059 
01060                 if ( $oField->type == "datetime" ) {
01061                     $oField->max_length = 20;
01062                 }
01063 
01064                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01065             }
01066             stopProfile("!__CACHABLE__!");
01067             return false;
01068         }
01069 
01070         //case 2. (just get all fields)
01071         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01072             $aMetaFields = $this->_getAllFields(true);
01073             /*
01074             foreach ( $aMetaFields as $sFieldName => $sVal) {
01075                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01076             }*/
01077             stopProfile("!__CACHABLE__!");
01078             return $aMetaFields;
01079         }
01080 
01081         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01082         stopProfile("!__CACHABLE__!");
01083         return array("oxid" => 0);
01084     }
01085 
01094     protected function _getFieldStatus($sFieldName)
01095     {
01096         return 0;
01097     }
01098 
01109     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01110     {
01111         //preparation
01112         $sName = strtolower($sName);
01113 
01114         //TODO: remove this
01115         //this is left only for debug as this should never happen
01116         if ($sName == "oxnid") {
01117             throw new Exception("oxnid added");
01118         }
01119 
01120         //TODO: remove this
01121         //this is left only for debug as this should never happen
01122         if (!is_int($iStatus )) {
01123             throw new Exception('Non int status!');
01124         }
01125 
01126         //adding field names element
01127         $this->_aFieldNames[$sName] = $iStatus;
01128 
01129         //allready set?
01130         $sLongName = $this->_getFieldLongName($sName);
01131         if ( isset($this->$sLongName) ) {
01132             return;
01133         }
01134 
01135         //defining the field
01136         $oField = false;
01137 
01138         if (isset($sType)) {
01139             $oField = new oxField();
01140             $oField->fldtype = $sType;
01141             //T2008-01-29
01142             //can't clone as the fields are objects and are not fully cloned
01143             $this->_blIsSimplyClonable = false;
01144         }
01145 
01146         if (isset($sLength)) {
01147             if (!$oField) {
01148                 $oField = new oxField();
01149             }
01150             $oField->fldmax_length = $sLength;
01151             $this->_blIsSimplyClonable = false;
01152         }
01153 
01154         $this->$sLongName = $oField;
01155     }
01156 
01164     protected function _getFieldLongName( $sFieldName)
01165     {
01166         //trying to avoid strpos call as often as possible
01167         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01168             return $sFieldName;
01169         }
01170 
01171         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01172         return $sLongName;
01173     }
01174 
01184     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01185     {
01186 
01187 
01188         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01189         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01190 
01191         //T2008-03-14
01192         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01193         //situation: only first article is loaded fully for "select oxid from oxarticles"
01194         /*
01195         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01196             return;*/
01197 
01198         //in non lazy loading case we just add a field and do not care about it more
01199         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01200             $aFields = $this->_getAllFields(true);
01201             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01202                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01203             }
01204         }
01205         // if we have a double field we replace "," with "." in case somebody enters it in european format
01206         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01207             $sValue = str_replace( ",", ".", $sValue );
01208         }
01209 
01210         // isset is REQUIRED here not to use getter
01211         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01212             $this->$sLongFieldName->setValue($sValue, $iDataType);
01213         } else {
01214             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01215         }
01216 
01217     }
01218 
01227     protected function _getUpdateFieldValue($sFieldName, $oField)
01228     {
01229         $blPassNullValue = false;
01230         if ($oField instanceof oxField) {
01231             $value = $oField->getRawValue();
01232         } else {
01233             $value = $oField->value;
01234         }
01235         // R. check if this field value is null AND it can be null according to table description
01236         if (null === $value) {
01237             $aMetaData = $this->_getAllFields();
01238             foreach ($aMetaData as $oMetaInfo) {
01239                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01240                     $blPassNullValue = !$oMetaInfo->not_null;
01241                     break;
01242                 }
01243             }
01244         }
01245         if ($blPassNullValue) {
01246             return 'null';
01247         } else {
01248             return oxDb::getDb()->quote( $value );
01249         }
01250     }
01251 
01260     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01261     {
01262         $sSql = '';
01263         $blSep  = false;
01264         foreach (array_keys($this->_aFieldNames) as $sKey) {
01265             $sLongName = $this->_getFieldLongName($sKey);
01266             $oField = $this->$sLongName;
01267 
01268 
01269             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01270                 $sKey = $this->getSqlFieldName($sKey);
01271                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01272                 $blSep = true;
01273             }
01274         }
01275 
01276         return $sSql;
01277     }
01278 
01288     protected function _update()
01289     {
01290         //do not allow derived item update
01291         if ( !$this->allowDerivedUpdate() ) {
01292             return false;
01293         }
01294 
01295 
01296         if ( !$this->getId() ) {
01297             $oEx = oxNew( 'oxObjectException' );
01298             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01299             $oEx->setObject($this);
01300             throw $oEx;
01301         }
01302 
01303         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01304         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01305 
01306         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01307                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01308 
01309         //echo         $sUpdate."\n\n\n";
01310 
01311         //trigger event
01312         $this->beforeUpdate();
01313 
01314         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01315         $this->_rebuildCache();
01316 
01317         return $blRet;
01318     }
01319 
01327     protected function _insert()
01328     {
01329 
01330         $oDB      = oxDb::getDb(true);
01331         $myConfig = $this->getConfig();
01332         $myUtils  = oxUtils::getInstance();
01333 
01334         // let's get a new ID
01335         if ( !$this->getId()) {
01336             $this->setId();
01337         }
01338 
01339         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01340         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01341         $sInsert= "Insert into {$this->_sCoreTable} set ";
01342 
01343         //setting oxshopid
01344         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01345         if (isset($this->$sShopField) && !$this->$sShopField->value)
01346             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01347         $sInsert .= $this->_getUpdateFields( false );
01348 
01349         $blRet = (bool) $oDB->execute( $sInsert);
01350 
01351         $this->_rebuildCache();
01352 
01353         return $blRet;
01354 
01355     }
01356 
01362     protected function _rebuildCache()
01363     {
01364         if ( !$this->_blIsNewCache) {
01365             oxUtils::getInstance()->rebuildCache();
01366             $this->_blIsNewCache = true;
01367         }
01368     }
01369 
01379     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01380     {
01381         // filtering
01382         $sWhere = "";
01383         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01384             $sWhere = implode(" and ", $aWhere).' and ';
01385         }
01386 
01387         // SQL to set record number
01388         $sUpdate = "update {$this->getViewName()} as t1, (select (max($sMaxField)+1) as t2max from {$this->getViewName()} where $sWhere 1) as t2 set t1.$sMaxField=t2.t2max where t1.oxid = '".$this->getId()."'";
01389 
01390         // SQL to check record number dublicates
01391         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01392         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01393         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01394 
01395         do {
01396             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01397                 return false;
01398             }
01399 
01400             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01401         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01402 
01403         $sFieldName = $this->getViewName().'__'.$sMaxField;
01404         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01405 
01406         return ( $iChkCnt == 1 );
01407     }
01408 
01415     protected function _isDisabledFieldCache()
01416     {
01417         $sClass = get_class($this);
01418         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01419             return true;
01420         }
01421 
01422         return false;
01423     }
01424 
01430     public function isOx()
01431     {
01432         $sOxId = $this->getId();
01433         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01434             return true;
01435         }
01436         return false;
01437     }
01438 
01444     public function isReadOnly()
01445     {
01446         return $this->_blReadOnly;
01447     }
01448 
01456     public function setReadOnly( $blReadOnly )
01457     {
01458         $this->_blReadOnly = $blReadOnly;
01459     }
01460 
01461 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5