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 
00146     public function __construct()
00147     {
00148         // set active shop
00149         $myConfig = $this->getConfig();
00150         $this->_sCacheKey = $this->getViewName();
00151         if ( $this->_blUseLazyLoading ) {
00152             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00153         } else {
00154             $this->_sCacheKey .= "allviews";
00155         }
00156 
00157         //do not cache for admin?
00158         if ( $this->isAdmin() ) {
00159             $this->_sCacheKey = null;
00160         }
00161 
00162         $this->setShopId( $myConfig->getShopId() );
00163     }
00164 
00173     public function __set($sName, $sValue)
00174     {
00175         $this->$sName = $sValue;
00176         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00177             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00178             if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00179                 $aAllFields = $this->_getAllFields(true);
00180                 if (isset($aAllFields[strtolower($sFieldName)])) {
00181                     $iFieldStatus = $this->_getFieldStatus($sFieldName);
00182                     $this->_addField($sFieldName, $iFieldStatus);
00183                 }
00184             }
00185         }
00186     }
00187 
00195     public function __get( $sName )
00196     {
00197         if ( $sName == 'blIsDerived' ) {
00198             return $this->isDerived();
00199         }
00200 
00201         if ( $sName == 'sOXID' ) {
00202             return $this->getId();
00203         }
00204 
00205         if ( $sName == 'blReadOnly' ) {
00206             return $this->isReadOnly();
00207         }
00208         // implementing lazy loading fields
00209         // This part of the code is slow and normally is called before field cache is built.
00210         // Make sure it is not called after first page is loaded and cache data is fully built.
00211         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00212 
00213             //lazy load it
00214             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00215             //$sFieldName = $this->getSqlFieldName($sFieldName);
00216             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00217 
00218             $sSqlFieldName = $sFieldName;
00219             if ($iFieldStatus && $this->isMultilang() ) {
00220                 $sSqlFieldName =  $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00221             }
00222 
00223             if ( $this->getId() ) {
00224                 $oDb = oxDb::getDb();
00225                 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00226 
00227                 try {
00228                     //$sValue = $oDb->getOne( $sQ );
00229                     $rs = $oDb->execute( $sQ );
00230                     if ( $rs === false ) {
00231                         return null;
00232                     }
00233 
00234                     $this->_addField($sFieldName, $iFieldStatus);
00235                     $sValue = $rs->fields[0];
00236                     $this->_setFieldData( $sFieldName, $sValue );
00237 
00238                     //save names to cache for next loading
00239                     if ($this->_sCacheKey) {
00240                         $myUtils = oxUtils::getInstance();
00241                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00242                         $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00243                         $aFieldNames[$sFieldName] = $iFieldStatus;
00244                         $myUtils->toFileCache($sCacheKey, $aFieldNames);
00245                     }
00246 
00247                 } catch ( Exception $e ) {
00248                     return null;
00249                 }
00250 
00251                 //do not use field cache for this page
00252                 //as if we use it for lists then objects are laoded empty instead of lazy lodaing.
00253                 self::$_blDisableFieldCaching[get_class($this)] = true;
00254             }
00255 
00256             /*
00257             //save names to cache for next loading
00258             if ($this->_sCacheKey) {
00259                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00260                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00261                 $aFieldNames[$sFieldName] = $iFieldStatus;
00262                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00263             }*/
00264 
00265             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00266         }
00267 
00268         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00269         if (!isset( $this->$sName ) ) {
00270             $this->$sName = null;
00271         }
00272 
00273         return $this->$sName;
00274     }
00275 
00283     public function __isset($mVar)
00284     {
00285         return isset($this->$mVar);
00286     }
00287 
00293     public function __clone()
00294     {
00295         if (!$this->_blIsSimplyClonable) {
00296             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00297                 $sLongName = $this->_getFieldLongName( $sField );
00298                 if ( is_object($this->$sLongName)) {
00299                     $this->$sLongName = clone $this->$sLongName;
00300                 }
00301             }
00302         }
00303     }
00304 
00312     public function oxClone($oObject)
00313     {
00314         $aClass_vars = get_object_vars( $oObject);
00315         while (list($name, $value) = each($aClass_vars)) {
00316             if ( is_object( $oObject->$name ) ) {
00317                 $this->$name = clone $oObject->$name;
00318             } else {
00319                 $this->$name = $oObject->$name;
00320             }
00321         }
00322     }
00323 
00332     public function init( $sTableName = null, $blForceAllFields = false)
00333     {
00334 
00335         if ( !$sTableName ) {
00336             $sTableName = $this->_sCoreTable;
00337         } else {
00338             $this->_sCoreTable = $sTableName;
00339         }
00340 
00341         $this->_sViewTable = getViewName( $this->_sCoreTable );
00342 
00343         //do not use views?
00344 
00345         if ( count( $this->_aFieldNames ) <= 1 ) {
00346             $this->_initDataStructure( $blForceAllFields );
00347         }
00348     }
00349 
00357     public function assign( $dbRecord )
00358     {
00359         if ( !is_array( $dbRecord ) ) {
00360             return;
00361         }
00362 
00363 
00364         reset($dbRecord );
00365         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00366 
00367             // patch for IIS
00368             //TODO: test it on IIS do we still need it
00369             //if( is_array($value) && count( $value) == 1)
00370             //    $value = current( $value);
00371 
00372             $this->_setFieldData( $sName, $sValue );
00373         }
00374 
00375         $sOxidField = $this->_getFieldLongName( 'oxid' );
00376         $this->_sOXID = $this->$sOxidField->value;
00377 
00378     }
00379 
00385     public function getClassName()
00386     {
00387         return $this->_sClassName;
00388     }
00389 
00395     public function getCoreTableName()
00396     {
00397         return $this->_sCoreTable;
00398     }
00399 
00405     public function getId()
00406     {
00407         return $this->_sOXID;
00408     }
00409 
00417     public function setId($sOXID = null)
00418     {
00419         if ( $sOXID ) {
00420             $this->_sOXID = $sOXID;
00421         } else {
00422             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00423         }
00424 
00425         $sIdVarName = $this->_sCoreTable . "__oxid";
00426         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00427 
00428         return $this->_sOXID;
00429     }
00430 
00438     public function setShopId($iShopId)
00439     {
00440         $this->_iShopId = $iShopId;
00441     }
00442 
00448     public function getShopId()
00449     {
00450         return $this->_iShopId;
00451     }
00452 
00458     public function getViewName()
00459     {
00460         return $this->_sViewTable;
00461     }
00462 
00471     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00472     {
00473         if ( $blOverride ) {
00474             $this->_sCacheKey = $sCacheKey;
00475         } else {
00476             $this->_sCacheKey .= $sCacheKey;
00477         }
00478     }
00479 
00485     public function disableLazyLoading()
00486     {
00487         $this->_blUseLazyLoading = false;
00488         $this->_initDataStructure(true);
00489     }
00490 
00491 
00497     public function isDerived()
00498     {
00499 
00500         return $this->_blIsDerived;
00501     }
00502 
00510     public function setIsDerived($blVal)
00511     {
00512         $this->_blIsDerived = $blVal;
00513     }
00514 
00521     public function isMultilang()
00522     {
00523         return false;
00524     }
00525 
00535     public function load( $sOXID)
00536     {
00537         /*
00538         if( !isset($oxID)){
00539             $oEx = oxNew('oxObjectException','core');
00540             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00541             $oEx->setObject($this);
00542             throw $oEx;
00543         }*/
00544 
00545         //getting at least one field before lazy loading the object
00546         $this->_addField('oxid', 0);
00547 
00548         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00549 
00550         return $this->assignRecord( $sSelect);
00551     }
00552 
00560     public function buildSelectString( $aWhere = null)
00561     {
00562         $oDB = oxDb::getDb(true);
00563         $myUtils = oxUtils::getInstance();
00564 
00565         $sGet = $this->getSelectFields();
00566         $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00567 
00568         if ( $aWhere) {
00569             reset($aWhere);
00570             while (list($name, $value) = each($aWhere)) {
00571                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00572             }
00573         }
00574 
00575         // add active shop
00576 
00577         //echo( "<br><br>$sSelect <br><br>\n\n\n");
00578 
00579         return $sSelect;
00580     }
00581 
00589     public function assignRecord( $sSelect)
00590     {
00591         $blRet = false;
00592 
00593         $oDB = oxDb::getDb(true);
00594 
00595         $rs = $oDB->execute( $sSelect);
00596         if ($rs != false && $rs->recordCount() > 0) {
00597             $blRet = true;
00598             $this->assign( $rs->fields);
00599         }
00600 
00601         return $blRet;
00602     }
00603 
00611     public function getFieldData( $sFieldName)
00612     {
00613         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00614         return $this->$sLongFieldName->value;
00615     }
00616 
00622     public function getSelectFields()
00623     {
00624         $aSelectFields = array();
00625 
00626         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00627             $sRealField = $this->getSqlFieldName($sKey);
00628             $sSelectSufix = "";
00629             if ( $sRealField !== $sKey ) {
00630                 $sSelectSufix = " as $sKey";
00631             }
00632             $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00633         }
00634 
00635         $sSelectFields = join( ", ", $aSelectFields );
00636         return $sSelectFields;
00637     }
00638 
00646     public function delete( $sOXID = null)
00647     {
00648         if ( !$sOXID ) {
00649             $sOXID = $this->getId();
00650 
00651             //do not allow derived deletion
00652             if ( !$this->allowDerivedDelete() ) {
00653                 return false;
00654             }
00655         }
00656 
00657         if ( !$sOXID ) {
00658             return false;
00659         }
00660 
00661 
00662         $oDB = oxDb::getDb(true);
00663         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00664         $rs = $oDB->execute( $sDelete );
00665         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00666             $this->onChange(ACTION_DELETE, $sOXID);
00667         }
00668 
00669         return $blDelete;
00670     }
00671 
00672 
00678     public function save()
00679     {
00680         if ( !is_array( $this->_aFieldNames ) ) {
00681             return false;
00682         }
00683 
00684         $blRet = false;
00685 
00686         // #739A - should be executed here because of date/time formatting feature
00687         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00688             foreach ($this->_aFieldNames as $sName => $sVal) {
00689                 $sLongName = $this->_getFieldLongName($sName);
00690                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00691                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00692                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00693                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00694                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00695                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00696                 }
00697             }
00698         }
00699 
00700         if ( $this->exists() ) {
00701 
00702             //do not allow derived update
00703             if ( !$this->allowDerivedUpdate() ) {
00704                 return false;
00705             }
00706 
00707             $blRet = $this->_update();
00708             $sAction = ACTION_UPDATE;
00709         } else {
00710             $blRet = $this->_insert();
00711             $sAction = ACTION_INSERT;
00712         }
00713 
00714         $this->onChange($sAction);
00715 
00716         if ( $blRet ) {
00717             return $this->getId();
00718         } else {
00719             return false;
00720         }
00721     }
00722 
00728     public function allowDerivedUpdate()
00729     {
00730         return !$this->isDerived();
00731     }
00732 
00738     public function allowDerivedDelete()
00739     {
00740         return !$this->isDerived();
00741     }
00742 
00750     public function exists( $sOXID = null)
00751     {
00752         if ( !$sOXID ) {
00753             $sOXID = $this->getId();
00754         }
00755         if ( !$sOXID ) {
00756             return false;
00757         }
00758 
00759         $oDB    = oxDb::getDb(true);
00760         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00761         return ( bool ) $oDB->getOne( $sSelect );
00762     }
00763 
00771     public function getSqlFieldName($sField)
00772     {
00773         return $sField;
00774     }
00775 
00783     public function getSqlActiveSnippet( $blForceCoreTable = false )
00784     {
00785         $sQ = '';
00786             $sTable = $this->getCoreTableName();
00787 
00788         // has 'active' field ?
00789         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00790             $sQ = " $sTable.oxactive = 1 ";
00791         }
00792 
00793         // has 'activefrom'/'activeto' fields ?
00794         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00795 
00796             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00797 
00798             $sQ = $sQ?" $sQ or ":'';
00799             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00800         }
00801 
00802         return $sQ;
00803     }
00804 
00811     public function validate()
00812     {
00813         $this->_aErrors = array();
00814         foreach ($this->_aFieldNames as $fName => $iVal) {
00815 
00816             $fName = $this->_getFieldLongName($fName);
00817 
00818             if ( method_exists ( $this, "validate_$fName")) {
00819                 $validatorMethod = "validate_$fName";
00820                 if ( $error = $this->$validatorMethod()) {
00821                     $this->_aErrors[$fName] = $error;
00822                 }
00823             }
00824         }
00825         return !$this->hasErrors();
00826     }
00827 
00836     public function beforeUpdate( $sOXID = null )
00837     {
00838     }
00839 
00850     public function onChange( $iAction = null, $sOXID = null)
00851     {
00852     }
00853 
00854 
00860     public function hasErrors()
00861     {
00862         return count($this->_aErrors) > 0;
00863     }
00864 
00870     public function getErrors()
00871     {
00872         return $this->_aErrors;
00873     }
00874 
00882     public function getError( $sField)
00883     {
00884         if (isset($this->_aErrors[$sField])) {
00885             return $this->_aErrors[$sField];
00886         }
00887 
00888         //T2007-10-19
00889         //return array();
00890         return null;
00891     }
00892 
00900     public function getHtmlError( $sField)
00901     {
00902         if ( $error = $this->getError($sField) ) {
00903             return $error;
00904         }
00905     }
00906 
00912     public function setInList()
00913     {
00914         $this->_blIsInList = true;
00915     }
00916 
00922     protected function _isInList()
00923     {
00924         return $this->_blIsInList;
00925     }
00926 
00935     protected function _getObjectViewName( $sTable, $sShopID = null)
00936     {
00937             return $sTable;
00938 
00939     }
00940 
00941 
00951     protected function _getAllFields($blReturnSimple = false)
00952     {
00953         $myUtils = oxUtils::getInstance();
00954 
00955         //T2008-09-04
00956         //The cache here saves almost 7% of execution time
00957         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00958         //TODO: Check what's up with that.
00959         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00960         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00961 
00962         if ($aMetaFields) {
00963             return $aMetaFields;
00964         }
00965 
00966         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00967 
00968         if ( !$blReturnSimple ) {
00969             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00970             return $aMetaFields;
00971         }
00972 
00973         //returning simple array
00974         $aRet = array();
00975         foreach ( $aMetaFields as $oVal ) {
00976             $aRet[strtolower( $oVal->name )] = 0;
00977         }
00978 
00979         $myUtils->toFileCache( $sCacheKey, $aRet);
00980 
00981         return $aRet;
00982     }
00983 
00992     protected function _initDataStructure($blForceFullStructure = false)
00993     {
00994         $myUtils = oxUtils::getInstance();
00995 
00996         //get field names from cache
00997         $aFieldNames = null;
00998         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
00999         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01000             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01001         }
01002 
01003         if (!$aFieldNames) {
01004             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01005             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01006                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01007             }
01008         }
01009 
01010         if ( $aFieldNames !== false ) {
01011             foreach ( $aFieldNames as $sField => $sStatus ) {
01012                 $this->_addField($sField, $sStatus);
01013             }
01014         }
01015     }
01016 
01028     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01029     {
01030         //T2008-02-22
01031         //so if this method is executed on cached version we see it when profiling
01032         startProfile("!__CACHABLE__!");
01033 
01034         //case 1. (admin)
01035         if ($this->isAdmin()) {
01036             $aMetaFields = $this->_getAllFields();
01037             foreach ( $aMetaFields as $oField ) {
01038                 if ( $oField->max_length == -1 ) {
01039                     $oField->max_length = 10;      // double or float
01040                 }
01041 
01042                 if ( $oField->type == "datetime" ) {
01043                     $oField->max_length = 20;
01044                 }
01045 
01046                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01047             }
01048             stopProfile("!__CACHABLE__!");
01049             return false;
01050         }
01051 
01052         //case 2. (just get all fields)
01053         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01054             $aMetaFields = $this->_getAllFields(true);
01055             /*
01056             foreach ( $aMetaFields as $sFieldName => $sVal) {
01057                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01058             }*/
01059             stopProfile("!__CACHABLE__!");
01060             return $aMetaFields;
01061         }
01062 
01063         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01064         stopProfile("!__CACHABLE__!");
01065         return array("oxid" => 0);
01066     }
01067 
01076     protected function _getFieldStatus($sFieldName)
01077     {
01078         return 0;
01079     }
01080 
01091     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01092     {
01093         //preparation
01094         $sName = strtolower($sName);
01095 
01096         //TODO: remove this
01097         //this is left only for debug as this should never happen
01098         if ($sName == "oxnid") {
01099             throw new Exception("oxnid added");
01100         }
01101 
01102         //TODO: remove this
01103         //this is left only for debug as this should never happen
01104         if (!is_int($iStatus )) {
01105             throw new Exception('Non int status!');
01106         }
01107 
01108         //adding field names element
01109         $this->_aFieldNames[$sName] = $iStatus;
01110 
01111         //allready set?
01112         $sLongName = $this->_getFieldLongName($sName);
01113         if ( isset($this->$sLongName) ) {
01114             return;
01115         }
01116 
01117         //defining the field
01118         $oField = false;
01119 
01120         if (isset($sType)) {
01121             $oField = new oxField();
01122             $oField->fldtype = $sType;
01123             //T2008-01-29
01124             //can't clone as the fields are objects and are not fully cloned
01125             $this->_blIsSimplyClonable = false;
01126         }
01127 
01128         if (isset($sLength)) {
01129             if (!$oField) {
01130                 $oField = new oxField();
01131             }
01132             $oField->fldmax_length = $sLength;
01133             $this->_blIsSimplyClonable = false;
01134         }
01135 
01136         $this->$sLongName = $oField;
01137     }
01138 
01146     protected function _getFieldLongName( $sFieldName)
01147     {
01148         //trying to avoid strpos call as often as possible
01149         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01150             return $sFieldName;
01151         }
01152 
01153         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01154         return $sLongName;
01155     }
01156 
01166     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01167     {
01168 
01169 
01170         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01171         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01172 
01173         //T2008-03-14
01174         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01175         //situation: only first article is loaded fully for "select oxid from oxarticles"
01176         /*
01177         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01178             return;*/
01179 
01180         //in non lazy loading case we just add a field and do not care about it more
01181         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01182             $aFields = $this->_getAllFields(true);
01183             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01184                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01185             }
01186         }
01187         // if we have a double field we replace "," with "." in case somebody enters it in european format
01188         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01189             $sValue = str_replace( ",", ".", $sValue );
01190         }
01191 
01192         // isset is REQUIRED here not to use getter
01193         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01194             $this->$sLongFieldName->setValue($sValue, $iDataType);
01195         } else {
01196             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01197         }
01198 
01199     }
01200 
01209     protected function _getUpdateFieldValue($sFieldName, $oField)
01210     {
01211         $blPassNullValue = false;
01212         if ($oField instanceof oxField) {
01213             $value = $oField->getRawValue();
01214         } else {
01215             $value = $oField->value;
01216         }
01217         // R. check if this field value is null AND it can be null according to table description
01218         if (null === $value) {
01219             $aMetaData = $this->_getAllFields();
01220             foreach ($aMetaData as $oMetaInfo) {
01221                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01222                     $blPassNullValue = !$oMetaInfo->not_null;
01223                     break;
01224                 }
01225             }
01226         }
01227         if ($blPassNullValue) {
01228             return 'null';
01229         } else {
01230             return oxDb::getDb()->quote( $value );
01231         }
01232     }
01233 
01242     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01243     {
01244         $sSql = '';
01245         $blSep  = false;
01246         foreach (array_keys($this->_aFieldNames) as $sKey) {
01247             $sLongName = $this->_getFieldLongName($sKey);
01248             $oField = $this->$sLongName;
01249 
01250 
01251             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01252                 $sKey = $this->getSqlFieldName($sKey);
01253                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01254                 $blSep = true;
01255             }
01256         }
01257 
01258         return $sSql;
01259     }
01260 
01270     protected function _update()
01271     {
01272         //do not allow derived item update
01273         if ( !$this->allowDerivedUpdate() ) {
01274             return false;
01275         }
01276 
01277 
01278         if ( !$this->getId() ) {
01279             $oEx = oxNew( 'oxObjectException' );
01280             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01281             $oEx->setObject($this);
01282             throw $oEx;
01283         }
01284 
01285         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01286         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01287 
01288         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01289                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01290 
01291         //echo         $sUpdate."\n\n\n";
01292 
01293         //trigger event
01294         $this->beforeUpdate();
01295 
01296         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01297         $this->_rebuildCache();
01298 
01299         return $blRet;
01300     }
01301 
01309     protected function _insert()
01310     {
01311 
01312         $oDB      = oxDb::getDb(true);
01313         $myConfig = $this->getConfig();
01314         $myUtils  = oxUtils::getInstance();
01315 
01316         // let's get a new ID
01317         if ( !$this->getId()) {
01318             $this->setId();
01319         }
01320 
01321         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01322         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01323         $sInsert= "Insert into {$this->_sCoreTable} set ";
01324 
01325         //setting oxshopid
01326         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01327         if (isset($this->$sShopField) && !$this->$sShopField->value)
01328             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01329         $sInsert .= $this->_getUpdateFields( false );
01330 
01331         $blRet = (bool) $oDB->execute( $sInsert);
01332 
01333         $this->_rebuildCache();
01334 
01335         return $blRet;
01336 
01337     }
01338 
01344     protected function _rebuildCache()
01345     {
01346         if ( !$this->_blIsNewCache) {
01347             oxUtils::getInstance()->rebuildCache();
01348             $this->_blIsNewCache = true;
01349         }
01350     }
01351 
01361     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01362     {
01363         // filtering
01364         $sWhere = "";
01365         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01366             $sWhere = implode(" and ", $aWhere).' and ';
01367         }
01368 
01369         // SQL to set record number
01370         $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()."'";
01371 
01372         // SQL to check record number dublicates
01373         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01374         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01375         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01376 
01377         do {
01378             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01379                 return false;
01380             }
01381 
01382             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01383         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01384 
01385         $sFieldName = $this->getViewName().'__'.$sMaxField;
01386         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01387 
01388         return ( $iChkCnt == 1 );
01389     }
01390 
01397     protected function _isDisabledFieldCache()
01398     {
01399         $sClass = get_class($this);
01400         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01401             return true;
01402         }
01403 
01404         return false;
01405     }
01406 
01412     public function isOx()
01413     {
01414         $sOxId = $this->getId();
01415         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01416             return true;
01417         }
01418         return false;
01419     }
01420 
01426     public function isReadOnly()
01427     {
01428         return $this->_blReadOnly;
01429     }
01430 
01438     public function setReadOnly( $blReadOnly )
01439     {
01440         $this->_blReadOnly = $blReadOnly;
01441     }
01442 
01443 }

Generated on Wed Jun 17 12:09:01 2009 for OXID eShop CE by  doxygen 1.5.5