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->$oxid->value;
00377         $this->_sOXID = $this->$sOxidField->value;
00378 
00379     }
00380 
00386     public function getClassName()
00387     {
00388         return $this->_sClassName;
00389     }
00390 
00396     public function getCoreTableName()
00397     {
00398         return $this->_sCoreTable;
00399     }
00400 
00406     public function getId()
00407     {
00408         return $this->_sOXID;
00409     }
00410 
00418     public function setId($sOXID = null)
00419     {
00420         if ( $sOXID ) {
00421             $this->_sOXID = $sOXID;
00422         } else {
00423             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00424         }
00425 
00426         $sIdVarName = $this->_sCoreTable . "__oxid";
00427         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00428 
00429         return $this->_sOXID;
00430     }
00431 
00439     public function setShopId($iShopId)
00440     {
00441         $this->_iShopId = $iShopId;
00442     }
00443 
00449     public function getShopId()
00450     {
00451         return $this->_iShopId;
00452     }
00453 
00459     public function getViewName()
00460     {
00461         return $this->_sViewTable;
00462     }
00463 
00472     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00473     {
00474         if ( $blOverride ) {
00475             $this->_sCacheKey = $sCacheKey;
00476         } else {
00477             $this->_sCacheKey .= $sCacheKey;
00478         }
00479     }
00480 
00486     public function disableLazyLoading()
00487     {
00488         $this->_blUseLazyLoading = false;
00489         $this->_initDataStructure(true);
00490     }
00491 
00492 
00498     public function isDerived()
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->isDerived() ) {
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->isDerived()) {
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 
00730     public function exists( $sOXID = null)
00731     {
00732         if ( !$sOXID ) {
00733             $sOXID = $this->getId();
00734         }
00735         if ( !$sOXID ) {
00736             return false;
00737         }
00738 
00739         $oDB    = oxDb::getDb(true);
00740         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00741         return ( bool ) $oDB->getOne( $sSelect );
00742     }
00743 
00751     public function getSqlFieldName($sField)
00752     {
00753         return $sField;
00754     }
00755 
00763     public function getSqlActiveSnippet( $blForceCoreTable = false )
00764     {
00765         $sQ = '';
00766             $sTable = $this->getCoreTableName();
00767 
00768         // has 'active' field ?
00769         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00770             $sQ = " $sTable.oxactive = 1 ";
00771         }
00772 
00773         // has 'activefrom'/'activeto' fields ?
00774         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00775 
00776             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00777 
00778             $sQ = $sQ?" $sQ or ":'';
00779             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00780         }
00781 
00782         return $sQ;
00783     }
00784 
00791     public function validate()
00792     {
00793         $this->_aErrors = array();
00794         foreach ($this->_aFieldNames as $fName => $iVal) {
00795 
00796             $fName = $this->_getFieldLongName($fName);
00797 
00798             if ( method_exists ( $this, "validate_$fName")) {
00799                 $validatorMethod = "validate_$fName";
00800                 if ( $error = $this->$validatorMethod()) {
00801                     $this->_aErrors[$fName] = $error;
00802                 }
00803             }
00804         }
00805         return !$this->hasErrors();
00806     }
00807 
00816     public function beforeUpdate( $sOXID = null )
00817     {
00818     }
00819 
00830     public function onChange( $iAction = null, $sOXID = null)
00831     {
00832     }
00833 
00834 
00840     public function hasErrors()
00841     {
00842         return count($this->_aErrors) > 0;
00843     }
00844 
00850     public function getErrors()
00851     {
00852         return $this->_aErrors;
00853     }
00854 
00862     public function getError( $sField)
00863     {
00864         if (isset($this->_aErrors[$sField])) {
00865             return $this->_aErrors[$sField];
00866         }
00867 
00868         //T2007-10-19
00869         //return array();
00870         return null;
00871     }
00872 
00880     public function getHtmlError( $sField)
00881     {
00882         if ( $error = $this->getError($sField) ) {
00883             return $error;
00884         }
00885     }
00886 
00892     public function setInList()
00893     {
00894         $this->_blIsInList = true;
00895     }
00896 
00902     protected function _isInList()
00903     {
00904         return $this->_blIsInList;
00905     }
00906 
00915     protected function _getObjectViewName( $sTable, $sShopID = null)
00916     {
00917             return $sTable;
00918 
00919     }
00920 
00921 
00931     protected function _getAllFields($blReturnSimple = false)
00932     {
00933         $myUtils = oxUtils::getInstance();
00934 
00935         //T2008-09-04
00936         //The cache here saves almost 7% of execution time
00937         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00938         //TODO: Check what's up with that.
00939         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00940         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00941 
00942         if ($aMetaFields) {
00943             return $aMetaFields;
00944         }
00945 
00946         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00947 
00948         if ( !$blReturnSimple ) {
00949             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00950             return $aMetaFields;
00951         }
00952 
00953         //returning simple array
00954         $aRet = array();
00955         foreach ( $aMetaFields as $oVal ) {
00956             $aRet[strtolower( $oVal->name )] = 0;
00957         }
00958 
00959         $myUtils->toFileCache( $sCacheKey, $aRet);
00960 
00961         return $aRet;
00962     }
00963 
00972     protected function _initDataStructure($blForceFullStructure = false)
00973     {
00974         $myUtils = oxUtils::getInstance();
00975 
00976         //get field names from cache
00977         $aFieldNames = null;
00978         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
00979         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
00980             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
00981         }
00982 
00983         if (!$aFieldNames) {
00984             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
00985             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
00986                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
00987             }
00988         }
00989 
00990         if ( $aFieldNames !== false ) {
00991             foreach ( $aFieldNames as $sField => $sStatus ) {
00992                 $this->_addField($sField, $sStatus);
00993             }
00994         }
00995     }
00996 
01008     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01009     {
01010         //T2008-02-22
01011         //so if this method is executed on cached version we see it when profiling
01012         startProfile("!__CACHABLE__!");
01013 
01014         //case 1. (admin)
01015         if ($this->isAdmin()) {
01016             $aMetaFields = $this->_getAllFields();
01017             foreach ( $aMetaFields as $oField ) {
01018                 if ( $oField->max_length == -1 ) {
01019                     $oField->max_length = 10;      // double or float
01020                 }
01021 
01022                 if ( $oField->type == "datetime" ) {
01023                     $oField->max_length = 20;
01024                 }
01025 
01026                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01027             }
01028             stopProfile("!__CACHABLE__!");
01029             return false;
01030         }
01031 
01032         //case 2. (just get all fields)
01033         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01034             $aMetaFields = $this->_getAllFields(true);
01035             /*
01036             foreach ( $aMetaFields as $sFieldName => $sVal) {
01037                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01038             }*/
01039             stopProfile("!__CACHABLE__!");
01040             return $aMetaFields;
01041         }
01042 
01043         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01044         stopProfile("!__CACHABLE__!");
01045         return array("oxid" => 0);
01046     }
01047 
01056     protected function _getFieldStatus($sFieldName)
01057     {
01058         return 0;
01059     }
01060 
01071     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01072     {
01073         //preparation
01074         $sName = strtolower($sName);
01075 
01076         //TODO: remove this
01077         //this is left only for debug as this should never happen
01078         if ($sName == "oxnid") {
01079             throw new Exception("oxnid added");
01080         }
01081 
01082         //TODO: remove this
01083         //this is left only for debug as this should never happen
01084         if (!is_int($iStatus )) {
01085             throw new Exception('Non int status!');
01086         }
01087 
01088         //adding field names element
01089         $this->_aFieldNames[$sName] = $iStatus;
01090 
01091         //allready set?
01092         $sLongName = $this->_getFieldLongName($sName);
01093         if ( isset($this->$sLongName) ) {
01094             return;
01095         }
01096 
01097         //defining the field
01098         $oField = false;
01099 
01100         if (isset($sType)) {
01101             $oField = new oxField();
01102             $oField->fldtype = $sType;
01103             //T2008-01-29
01104             //can't clone as the fields are objects and are not fully cloned
01105             $this->_blIsSimplyClonable = false;
01106         }
01107 
01108         if (isset($sLength)) {
01109             if (!$oField) {
01110                 $oField = new oxField();
01111             }
01112             $oField->fldmax_length = $sLength;
01113             $this->_blIsSimplyClonable = false;
01114         }
01115 
01116         $this->$sLongName = $oField;
01117     }
01118 
01126     protected function _getFieldLongName( $sFieldName)
01127     {
01128         //trying to avoid strpos call as often as possible
01129         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01130             return $sFieldName;
01131         }
01132 
01133         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01134         return $sLongName;
01135     }
01136 
01146     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01147     {
01148 
01149 
01150         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01151         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01152 
01153         //T2008-03-14
01154         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01155         //situation: only first article is loaded fully for "select oxid from oxarticles"
01156         /*
01157         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01158             return;*/
01159 
01160         //in non lazy loading case we just add a field and do not care about it more
01161         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01162             $aFields = $this->_getAllFields(true);
01163             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01164                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01165             }
01166         }
01167         // if we have a double field we replace "," with "." in case somebody enters it in european format
01168         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01169             $sValue = str_replace( ",", ".", $sValue );
01170         }
01171 
01172         // isset is REQUIRED here not to use getter
01173         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01174             $this->$sLongFieldName->setValue($sValue, $iDataType);
01175         } else {
01176             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01177         }
01178 
01179     }
01180 
01189     protected function _getUpdateFieldValue($sFieldName, $oField)
01190     {
01191         $blPassNullValue = false;
01192         if ($oField instanceof oxField) {
01193             $value = $oField->getRawValue();
01194         } else {
01195             $value = $oField->value;
01196         }
01197         // R. check if this field value is null AND it can be null according to table description
01198         if (null === $value) {
01199             $aMetaData = $this->_getAllFields();
01200             foreach ($aMetaData as $oMetaInfo) {
01201                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01202                     $blPassNullValue = !$oMetaInfo->not_null;
01203                     break;
01204                 }
01205             }
01206         }
01207         if ($blPassNullValue) {
01208             return 'null';
01209         } else {
01210             return oxDb::getDb()->quote( $value );
01211         }
01212     }
01213 
01222     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01223     {
01224         $sSql = '';
01225         $blSep  = false;
01226         foreach (array_keys($this->_aFieldNames) as $sKey) {
01227             $sLongName = $this->_getFieldLongName($sKey);
01228             $oField = $this->$sLongName;
01229 
01230 
01231             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01232                 $sKey = $this->getSqlFieldName($sKey);
01233                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01234                 $blSep = true;
01235             }
01236         }
01237 
01238         return $sSql;
01239     }
01240 
01250     protected function _update()
01251     {
01252         //do not allow derived item update
01253         if ( $this->isDerived() ) {
01254             return false;
01255         }
01256 
01257 
01258         if ( !$this->getId() ) {
01259             $oEx = oxNew( 'oxObjectException' );
01260             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01261             $oEx->setObject($this);
01262             throw $oEx;
01263         }
01264 
01265         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01266         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01267 
01268         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01269                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01270 
01271         //echo         $sUpdate."\n\n\n";
01272 
01273         //trigger event
01274         $this->beforeUpdate();
01275 
01276         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01277         $this->_rebuildCache();
01278 
01279         return $blRet;
01280     }
01281 
01289     protected function _insert()
01290     {
01291 
01292         $oDB      = oxDb::getDb(true);
01293         $myConfig = $this->getConfig();
01294         $myUtils  = oxUtils::getInstance();
01295 
01296         // let's get a new ID
01297         if ( !$this->getId()) {
01298             $this->setId();
01299         }
01300 
01301         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01302         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01303         $sInsert= "Insert into {$this->_sCoreTable} set ";
01304 
01305         //setting oxshopid
01306         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01307         if (isset($this->$sShopField) && !$this->$sShopField->value)
01308             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01309         $sInsert .= $this->_getUpdateFields( false );
01310 
01311         $blRet = (bool) $oDB->execute( $sInsert);
01312 
01313         $this->_rebuildCache();
01314 
01315         return $blRet;
01316 
01317     }
01318 
01324     protected function _rebuildCache()
01325     {
01326         if ( !$this->_blIsNewCache) {
01327             oxUtils::getInstance()->rebuildCache();
01328             $this->_blIsNewCache = true;
01329         }
01330     }
01331 
01341     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01342     {
01343         // filtering
01344         $sWhere = "";
01345         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01346             $sWhere = implode(" and ", $aWhere).' and ';
01347         }
01348 
01349         // SQL to set record number
01350         $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()."'";
01351 
01352         // SQL to check record number dublicates
01353         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01354         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01355         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01356 
01357         do {
01358             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01359                 return false;
01360             }
01361 
01362             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01363         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01364 
01365         $sFieldName = $this->getViewName().'__'.$sMaxField;
01366         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01367 
01368         return ( $iChkCnt == 1 );
01369     }
01370 
01377     protected function _isDisabledFieldCache()
01378     {
01379         $sClass = get_class($this);
01380         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01381             return true;
01382         }
01383 
01384         return false;
01385     }
01386 
01392     public function isOx()
01393     {
01394         $sOxId = $this->getId();
01395         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01396             return true;
01397         }
01398         return false;
01399     }
01400 
01406     public function isReadOnly()
01407     {
01408         return $this->_blReadOnly;
01409     }
01410 
01418     public function setReadOnly( $blReadOnly )
01419     {
01420         $this->_blReadOnly = $blReadOnly;
01421     }
01422 
01423 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5