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 
00017 class oxBase extends oxSuperCfg
00018 {
00023     protected $_sOXID = null;
00024 
00029     protected $_iShopId = null;
00030 
00036     protected $_blIsSimplyClonable = true;
00037 
00042     protected $_aErrors = array();
00043 
00048     protected $_sClassName = 'oxbase';
00049 
00055     protected $_sCoreTable = null;
00056 
00061     protected $_sViewTable  = null;
00062 
00063 
00069     protected $_aFieldNames = array('oxid' => 0);
00070 
00075     protected $_blIsNewCache = false;
00076 
00082     protected $_sCacheKey = null;
00083 
00089     protected $_blUseLazyLoading = false;
00090 
00096     protected $_aSkipSaveFields = array();
00097 
00102     protected $_sExistKey = "oxid";
00103 
00110     protected $_blIsDerived = null;
00111 
00121     protected static $_blDisableFieldCaching = array();
00122 
00128     protected $_blIsSeoObject = false;
00129 
00133     public function __construct()
00134     {
00135         // set active shop
00136         $myConfig = $this->getConfig();
00137         $this->_sCacheKey = $this->getViewName();
00138         if ( $this->_blUseLazyLoading ) {
00139             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00140         } else {
00141             $this->_sCacheKey .= "allviews";
00142         }
00143 
00144         //do not cache for admin?
00145         if ( $this->isAdmin() ) {
00146             $this->_sCacheKey = null;
00147         }
00148 
00149         $this->setShopId( $myConfig->getShopID() );
00150     }
00151 
00160     public function __set($sName, $sValue)
00161     {
00162         $this->$sName = $sValue;
00163         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00164             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00165             if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00166                 $aAllFields = $this->_getAllFields(true);
00167                 if (isset($aAllFields[strtolower($sFieldName)])) {
00168                     $iFieldStatus = $this->_getFieldStatus($sFieldName);
00169                     $this->_addField($sFieldName, $iFieldStatus);
00170                 }
00171             }
00172         }
00173     }
00174 
00182     public function __get( $sName )
00183     {
00184         if ( $sName == 'blIsDerived' ) {
00185             return $this->isDerived();
00186         }
00187 
00188         if ( $sName == 'sOXID' ) {
00189             return $this->getId();
00190         }
00191 
00192         // implementing lazy loading fields
00193         // This part of the code is slow and normally is called before field cache is built.
00194         // Make sure it is not called after first page is loaded and cache data is fully built.
00195         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00196 
00197             //lazy load it
00198             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00199             //$sFieldName = $this->getSqlFieldName($sFieldName);
00200             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00201 
00202             $sSqlFieldName = $sFieldName;
00203             if ($iFieldStatus && $this->isMultilang() ) {
00204                 $sSqlFieldName =  $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00205             }
00206 
00207             if ( $this->getId() ) {
00208                 $oDb = oxDb::getDb();
00209                 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00210 
00211                 try {
00212                     //$sValue = $oDb->getOne( $sQ );
00213                     $rs = $oDb->execute( $sQ );
00214                     if ( $rs === false ) {
00215                         return null;
00216                     }
00217 
00218                     $this->_addField($sFieldName, $iFieldStatus);
00219                     $sValue = $rs->fields[0];
00220                     $this->_setFieldData( $sFieldName, $sValue );
00221 
00222                     //save names to cache for next loading
00223                     if ($this->_sCacheKey) {
00224                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00225                         $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00226                         $aFieldNames[$sFieldName] = $iFieldStatus;
00227                         oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00228                     }
00229 
00230                 } catch ( Exception $e ) {
00231                     return null;
00232                 }
00233 
00234                 //do not use field cache for this page
00235                 //as if we use it for lists then objects are laoded empty instead of lazy lodaing.
00236                 self::$_blDisableFieldCaching[get_class($this)] = true;
00237             }
00238 
00239             /*
00240             //save names to cache for next loading
00241             if ($this->_sCacheKey) {
00242                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00243                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00244                 $aFieldNames[$sFieldName] = $iFieldStatus;
00245                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00246             }*/
00247 
00248             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00249         }
00250 
00251         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00252         if (!isset( $this->$sName ) ) {
00253             $this->$sName = null;
00254         }
00255 
00256         return $this->$sName;
00257     }
00258 
00266     public function __isset($mVar)
00267     {
00268         return isset($this->$mVar);
00269     }
00270 
00276     public function __clone()
00277     {
00278         if (!$this->_blIsSimplyClonable) {
00279             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00280                 $sLongName = $this->_getFieldLongName( $sField );
00281                 if ( is_object($this->$sLongName)) {
00282                     $this->$sLongName = clone $this->$sLongName;
00283                 }
00284             }
00285         }
00286     }
00287 
00295     public function oxClone($oObject)
00296     {
00297         $aClass_vars = get_object_vars( $oObject);
00298         while (list($name, $value) = each($aClass_vars)) {
00299             if ( is_object( $oObject->$name ) ) {
00300                 $this->$name = clone $oObject->$name;
00301             } else {
00302                 $this->$name = $oObject->$name;
00303             }
00304         }
00305     }
00306 
00315     public function init( $sTableName = null, $blForceAllFields = false)
00316     {
00317 
00318         if ( !$sTableName ) {
00319             $sTableName = $this->_sCoreTable;
00320         } else {
00321             $this->_sCoreTable = $sTableName;
00322         }
00323 
00324         $this->_sViewTable = getViewName( $this->_sCoreTable );
00325 
00326         //do not use views?
00327 
00328         if ( count( $this->_aFieldNames ) <= 1 ) {
00329             $this->_initDataStructure( $blForceAllFields );
00330         }
00331     }
00332 
00340     public function assign( $dbRecord )
00341     {
00342         if ( !is_array( $dbRecord ) ) {
00343             return;
00344         }
00345 
00346 
00347         reset($dbRecord);
00348         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00349 
00350             // patch for IIS
00351             //TODO: test it on IIS do we still need it
00352             //if( is_array($value) && count( $value) == 1)
00353             //    $value = current( $value);
00354 
00355             $this->_setFieldData( $sName, $sValue);
00356         }
00357 
00358         $sOxidField = $this->_getFieldLongName('oxid');
00359         //$this->_sOXID = $this->$oxid->value;
00360         $this->_sOXID = $this->$sOxidField->value;
00361 
00362     }
00363 
00369     public function getClassName()
00370     {
00371         return $this->_sClassName;
00372     }
00373 
00379     public function getCoreTableName()
00380     {
00381         return $this->_sCoreTable;
00382     }
00383 
00389     public function getId()
00390     {
00391         return $this->_sOXID;
00392     }
00393 
00401     public function setId($sOXID = null)
00402     {
00403         if ( $sOXID ) {
00404             $this->_sOXID = $sOXID;
00405         } else {
00406             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00407         }
00408 
00409         $sIdVarName = $this->_sCoreTable . "__oxid";
00410         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00411 
00412         return $this->_sOXID;
00413     }
00414 
00422     public function setShopId($iShopId)
00423     {
00424         $this->_iShopId = $iShopId;
00425     }
00426 
00432     public function getShopId()
00433     {
00434         return $this->_iShopId;
00435     }
00436 
00442     public function getViewName()
00443     {
00444         return $this->_sViewTable;
00445     }
00446 
00455     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00456     {
00457         if ( $blOverride ) {
00458             $this->_sCacheKey = $sCacheKey;
00459         } else {
00460             $this->_sCacheKey .= $sCacheKey;
00461         }
00462     }
00463 
00469     public function disableLazyLoading()
00470     {
00471         $this->_blUseLazyLoading = false;
00472         $this->_initDataStructure(true);
00473     }
00474 
00475 
00481     public function isDerived()
00482     {
00483         return $this->_blIsDerived;
00484     }
00485 
00493     public function setIsDerived($blVal)
00494     {
00495         $this->_blIsDerived = $blVal;
00496     }
00497 
00504     public function isMultilang()
00505     {
00506         return false;
00507     }
00508 
00518     public function load( $sOXID)
00519     {
00520         /*
00521         if( !isset($oxID)){
00522             $oEx = oxNew('oxObjectException','core');
00523             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00524             $oEx->setObject($this);
00525             throw $oEx;
00526         }*/
00527 
00528         //getting at least one field before lazy loading the object
00529         $this->_addField('oxid', 0);
00530 
00531         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00532 
00533         return $this->assignRecord( $sSelect);
00534     }
00535 
00543     public function buildSelectString( $aWhere = null)
00544     {
00545         $oDB = oxDb::getDb(true);
00546         $myUtils = oxUtils::getInstance();
00547 
00548         $sGet = $this->getSelectFields();
00549         $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00550 
00551         if ( $aWhere) {
00552             reset($aWhere);
00553             while (list($name, $value) = each($aWhere)) {
00554                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00555             }
00556         }
00557 
00558         // add active shop
00559 
00560         //echo( "<br><br>$sSelect <br><br>\n\n\n");
00561 
00562         return $sSelect;
00563     }
00564 
00572     public function assignRecord( $sSelect)
00573     {
00574         $blRet = false;
00575 
00576         $oDB = oxDb::getDb(true);
00577 
00578         $rs = $oDB->execute( $sSelect);
00579         if ($rs != false && $rs->recordCount() > 0) {
00580             $blRet = true;
00581             $this->assign( $rs->fields);
00582         }
00583 
00584         return $blRet;
00585     }
00586 
00594     public function getFieldData( $sFieldName)
00595     {
00596         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00597         return $this->$sLongFieldName->value;
00598     }
00599 
00605     public function getSelectFields()
00606     {
00607         $aSelectFields = array();
00608 
00609         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00610             $sRealField = $this->getSqlFieldName($sKey);
00611             $sSelectSufix = "";
00612             if ( $sRealField !== $sKey ) {
00613                 $sSelectSufix = " as $sKey";
00614             }
00615             $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00616         }
00617 
00618         $sSelectFields = join( ", ", $aSelectFields );
00619         return $sSelectFields;
00620     }
00621 
00629     public function delete( $sOXID = null)
00630     {
00631         if ( !$sOXID ) {
00632             $sOXID = $this->getId();
00633 
00634             //do not allow derived deletion
00635             if ( $this->isDerived() ) {
00636                 return false;
00637             }
00638         }
00639 
00640         if ( !$sOXID ) {
00641             return false;
00642         }
00643 
00644 
00645         $oDB = oxDb::getDb(true);
00646         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00647         $rs = $oDB->execute( $sDelete );
00648         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00649             $this->onChange(ACTION_DELETE, $sOXID);
00650         }
00651 
00652         return $blDelete;
00653     }
00654 
00655 
00661     public function save()
00662     {
00663         if ( !is_array( $this->_aFieldNames ) ) {
00664             return false;
00665         }
00666 
00667         $myConfig = $this->getConfig();
00668         $blRet = false;
00669 
00670         // #739A - should be executed here because of date/time formatting feature
00671         if ( $this->isAdmin() && !$myConfig->getConfigParam( 'blSkipFormatConversion' ) ) {
00672             foreach ($this->_aFieldNames as $sName => $sVal) {
00673                 $sLongName = $this->_getFieldLongName($sName);
00674                 if ( $this->$sLongName->fldtype == "datetime") {
00675                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00676                 } elseif ( $this->$sLongName->fldtype == "timestamp") {
00677                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00678                 } elseif ( $this->$sLongName->fldtype == "date") {
00679                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00680                 }
00681             }
00682         }
00683 
00684         if ( $this->exists() ) {
00685 
00686             //do not allow derived update
00687             if ( $this->isDerived()) {
00688                 return false;
00689             }
00690 
00691             $blRet = $this->_update();
00692             $sAction = ACTION_UPDATE;
00693         } else {
00694             $blRet = $this->_insert();
00695             $sAction = ACTION_INSERT;
00696         }
00697 
00698         $this->onChange($sAction);
00699 
00700         if ( $blRet ) {
00701             return $this->getId();
00702         } else {
00703             return false;
00704         }
00705     }
00706 
00714     public function exists( $sOXID = null)
00715     {
00716         if ( !$sOXID ) {
00717             $sOXID = $this->getId();
00718         }
00719         if ( !$sOXID ) {
00720             return false;
00721         }
00722 
00723         $oDB    = oxDb::getDb(true);
00724         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00725         return ( bool ) $oDB->getOne( $sSelect );
00726     }
00727 
00735     public function getSqlFieldName($sField)
00736     {
00737         return $sField;
00738     }
00739 
00747     public function getSqlActiveSnippet( $blForceCoreTable = false )
00748     {
00749         $sQ = '';
00750             $sTable = $this->getCoreTableName();
00751 
00752         // has 'active' field ?
00753         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00754             $sQ = " $sTable.oxactive = 1 ";
00755         }
00756 
00757         // has 'activefrom'/'activeto' fields ?
00758         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00759 
00760             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00761 
00762             $sQ = $sQ?" $sQ or ":'';
00763             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00764         }
00765 
00766         return $sQ;
00767     }
00768 
00775     public function validate()
00776     {
00777         $this->_aErrors = array();
00778         foreach ($this->_aFieldNames as $fName => $iVal) {
00779 
00780             $fName = $this->_getFieldLongName($fName);
00781 
00782             if ( method_exists ( $this, "validate_$fName")) {
00783                 $validatorMethod = "validate_$fName";
00784                 if ( $error = $this->$validatorMethod()) {
00785                     $this->_aErrors[$fName] = $error;
00786                 }
00787             }
00788         }
00789         return !$this->hasErrors();
00790     }
00791 
00800     public function beforeUpdate( $sOXID = null )
00801     {
00802     }
00803 
00814     public function onChange( $iAction = null, $sOXID = null)
00815     {
00816     }
00817 
00818 
00824     public function hasErrors()
00825     {
00826         return count($this->_aErrors) > 0;
00827     }
00828 
00834     public function getErrors()
00835     {
00836         return $this->_aErrors;
00837     }
00838 
00846     public function getError( $sField)
00847     {
00848         if (isset($this->_aErrors[$sField])) {
00849             return $this->_aErrors[$sField];
00850         }
00851 
00852         //T2007-10-19
00853         //return array();
00854         return null;
00855     }
00856 
00864     public function getHtmlError( $sField)
00865     {
00866         if ( $error = $this->getError($sField) ) {
00867             return $error;
00868         }
00869     }
00870 
00879     protected function _getObjectViewName( $sTable, $sShopID = null)
00880     {
00881             return $sTable;
00882 
00883     }
00884 
00885 
00895     protected function _getAllFields($blReturnSimple = false)
00896     {
00897         $myUtils = oxUtils::getInstance();
00898 
00899         //T2008-09-04
00900         //The cache here saves almost 7% of execution time
00901         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00902         //TODO: Check what's up with that.
00903         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00904         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00905 
00906         if ($aMetaFields) {
00907             return $aMetaFields;
00908         }
00909 
00910         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00911 
00912         if ( !$blReturnSimple ) {
00913             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00914             return $aMetaFields;
00915         }
00916 
00917         //returning simple array
00918         $aRet = array();
00919         foreach ( $aMetaFields as $oVal ) {
00920             $aRet[strtolower( $oVal->name )] = 0;
00921         }
00922 
00923         $myUtils->toFileCache( $sCacheKey, $aRet);
00924 
00925         return $aRet;
00926     }
00927 
00936     protected function _initDataStructure($blForceFullStructure = false)
00937     {
00938         $myUtils = oxUtils::getInstance();
00939 
00940         //get field names from cache
00941         $aFieldNames = null;
00942         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
00943         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
00944             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
00945         }
00946 
00947         if (!$aFieldNames) {
00948             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
00949             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
00950                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
00951             }
00952         }
00953 
00954         if ( $aFieldNames !== false ) {
00955             foreach ( $aFieldNames as $sField => $sStatus ) {
00956                 $this->_addField($sField, $sStatus);
00957             }
00958         }
00959     }
00960 
00972     protected function _getNonCachedFieldNames($blForceFullStructure = false)
00973     {
00974         //T2008-02-22
00975         //so if this method is executed on cached version we see it when profiling
00976         startProfile("!__CACHABLE__!");
00977 
00978         //case 1. (admin)
00979         if ($this->isAdmin()) {
00980             $aMetaFields = $this->_getAllFields();
00981             foreach ( $aMetaFields as $oField ) {
00982                 if ( $oField->max_length == -1 ) {
00983                     $oField->max_length = 10;      // double or float
00984                 }
00985 
00986                 if ( $oField->type == "datetime" ) {
00987                     $oField->max_length = 20;
00988                 }
00989 
00990                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
00991             }
00992             stopProfile("!__CACHABLE__!");
00993             return false;
00994         }
00995 
00996         //case 2. (just get all fields)
00997         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
00998             $aMetaFields = $this->_getAllFields(true);
00999             /*
01000             foreach ( $aMetaFields as $sFieldName => $sVal) {
01001                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01002             }*/
01003             stopProfile("!__CACHABLE__!");
01004             return $aMetaFields;
01005         }
01006 
01007         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01008         stopProfile("!__CACHABLE__!");
01009         return array("oxid" => 0);
01010     }
01011 
01020     protected function _getFieldStatus($sFieldName)
01021     {
01022         return 0;
01023     }
01024 
01035     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01036     {
01037         //preparation
01038         $sName = strtolower($sName);
01039 
01040         //TODO: remove this
01041         //this is left only for debug as this should never happen
01042         if ($sName == "oxnid") {
01043             throw new Exception("oxnid added");
01044         }
01045 
01046         //TODO: remove this
01047         //this is left only for debug as this should never happen
01048         if (!is_int($iStatus )) {
01049             throw new Exception('Non int status!');
01050         }
01051 
01052         //adding field names element
01053         $this->_aFieldNames[$sName] = $iStatus;
01054 
01055         //allready set?
01056         $sLongName = $this->_getFieldLongName($sName);
01057         if ( isset($this->$sLongName) ) {
01058             return;
01059         }
01060 
01061         //defining the field
01062         $oField = false;
01063 
01064         if (isset($sType)) {
01065             $oField = new oxField();
01066             $oField->fldtype = $sType;
01067             //T2008-01-29
01068             //can't clone as the fields are objects and are not fully cloned
01069             $this->_blIsSimplyClonable = false;
01070         }
01071 
01072         if (isset($sLength)) {
01073             if (!$oField) {
01074                 $oField = new oxField();
01075             }
01076             $oField->fldmax_length = $sLength;
01077             $this->_blIsSimplyClonable = false;
01078         }
01079 
01080         $this->$sLongName = $oField;
01081     }
01082 
01090     protected function _getFieldLongName( $sFieldName)
01091     {
01092         //trying to avoid strpos call as often as possible
01093         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01094             return $sFieldName;
01095         }
01096 
01097         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01098         return $sLongName;
01099     }
01100 
01110     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01111     {
01112 
01113 
01114         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01115         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01116 
01117         //T2008-03-14
01118         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01119         //situation: only first article is loaded fully for "select oxid from oxarticles"
01120         /*
01121         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01122             return;*/
01123 
01124         //in non lazy loading case we just add a field and do not care about it more
01125         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01126             $aFields = $this->_getAllFields(true);
01127             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01128                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01129             }
01130         }
01131         // if we have a double field we replace "," with "." in case somebody enters it in european format
01132         if (isset($this->$sLongFieldName) && $this->$sLongFieldName->fldtype == "double") {
01133             $sValue = str_replace( ",", ".", $sValue );
01134         }
01135 
01136         // isset is REQUIRED here not to use getter
01137         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01138             $this->$sLongFieldName->setValue($sValue, $iDataType);
01139         } else {
01140             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01141         }
01142 
01143     }
01144 
01153     protected function _getUpdateFieldValue($sFieldName, $oField)
01154     {
01155         $blPassNullValue = false;
01156         if ($oField instanceof oxField) {
01157             $value = $oField->getRawValue();
01158         } else {
01159             $value = $oField->value;
01160         }
01161         // R. check if this field value is null AND it can be null according to table description
01162         if (null === $value) {
01163             $aMetaData = $this->_getAllFields();
01164             foreach ($aMetaData as $oMetaInfo) {
01165                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01166                     $blPassNullValue = !$oMetaInfo->not_null;
01167                     break;
01168                 }
01169             }
01170         }
01171         if ($blPassNullValue) {
01172             return 'null';
01173         } else {
01174             return oxDb::getDb()->quote( $value );
01175         }
01176     }
01177 
01186     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01187     {
01188         $sSql = '';
01189         $blSep  = false;
01190         foreach (array_keys($this->_aFieldNames) as $sKey) {
01191             $sLongName = $this->_getFieldLongName($sKey);
01192             $oField = $this->$sLongName;
01193 
01194 
01195             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01196                 $sKey = $this->getSqlFieldName($sKey);
01197                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01198                 $blSep = true;
01199             }
01200         }
01201 
01202         return $sSql;
01203     }
01204 
01214     protected function _update()
01215     {
01216         //do not allow derived item update
01217         if ( $this->isDerived() ) {
01218             return false;
01219         }
01220 
01221 
01222         if ( !$this->getId() ) {
01223             $oEx = oxNew( 'oxObjectException' );
01224             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01225             $oEx->setObject($this);
01226             throw $oEx;
01227         }
01228 
01229         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01230         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01231 
01232         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01233                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01234 
01235         //echo         $sUpdate."\n\n\n";
01236 
01237         //trigger event
01238         $this->beforeUpdate();
01239 
01240         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01241         $this->_rebuildCache();
01242 
01243         return $blRet;
01244     }
01245 
01253     protected function _insert()
01254     {
01255 
01256         $oDB      = oxDb::getDb(true);
01257         $myConfig = $this->getConfig();
01258 
01259         // let's get a new ID
01260         if ( !$this->getId()) {
01261             $this->setId();
01262         }
01263 
01264         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01265         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01266         $sInsert= "Insert into {$this->_sCoreTable} set ";
01267 
01268         //setting oxshopid
01269         $sShopField = oxUtils::getInstance()->getArrFldName($this->_sCoreTable.".oxshopid");
01270         if (isset($this->$sShopField) && !$this->$sShopField->value)
01271             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01272         $sInsert .= $this->_getUpdateFields( false );
01273 
01274         $blRet = (bool) $oDB->execute( $sInsert);
01275 
01276         $this->_rebuildCache();
01277 
01278         return $blRet;
01279 
01280     }
01281 
01287     protected function _rebuildCache()
01288     {
01289         if ( !$this->_blIsNewCache) {
01290             oxUtils::getInstance()->rebuildCache();
01291             $this->_blIsNewCache = true;
01292         }
01293     }
01294 
01304     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01305     {
01306         // filtering
01307         $sWhere = "";
01308         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01309             $sWhere = implode(" and ", $aWhere).' and ';
01310         }
01311 
01312         // SQL to set record number
01313         $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()."'";
01314 
01315         // SQL to check record number dublicates
01316         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01317         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01318         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01319 
01320         do {
01321             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01322                 return false;
01323             }
01324 
01325             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01326         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01327 
01328         $sFieldName = $this->getViewName().'__'.$sMaxField;
01329         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01330 
01331         return ( $iChkCnt == 1 );
01332     }
01333 
01340     protected function _isDisabledFieldCache()
01341     {
01342         $sClass = get_class($this);
01343         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01344             return true;
01345         }
01346 
01347         return false;
01348     }
01349 
01350 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5