oxattribute.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxAttribute extends oxI18n
00008 {
00014     protected $_sClassName = 'oxattribute';
00015 
00021     protected $_sCoreTbl = 'oxattribute';
00022 
00023 
00029     protected $_sActiveValue = null;
00030     
00036     protected $_sTitle = null;
00037     
00043     protected $_aValues = null;
00044 
00048     public function __construct()
00049     {
00050         parent::__construct();
00051         $this->init( $this->_sCoreTbl);
00052     }
00053 
00061     public function delete( $sOXID = null )
00062     {
00063         if( !$sOXID)
00064             $sOXID = $this->getId();
00065         if( !$sOXID)
00066             return false;
00067 
00068 
00069         // remove attributes from articles also
00070         $oDB = oxDb::getDb();
00071         $sOxidQuoted = $oDB->quote($sOXID);
00072         $sDelete = "delete from oxobject2attribute where oxattrid = ".$sOxidQuoted;
00073         $rs = $oDB->execute( $sDelete);
00074 
00075         // #657 ADDITIONAL removes attribute connection to category
00076         $sDelete = "delete from oxcategory2attribute where oxattrid = ".$sOxidQuoted;
00077         $rs = $oDB->execute( $sDelete);
00078 
00079         return parent::delete( $sOXID);
00080     }
00081 
00090     public function assignVarToAttribute( $aMDVariants, $aSelTitle )
00091     {
00092         $myLang    = oxLang::getInstance();
00093         $aConfLanguages = $myLang->getLanguageIds();
00094         $sAttrId = $this->_getAttrId( $aSelTitle[0] );
00095         if ( !$sAttrId ) {
00096             $sAttrId = $this->_createAttribute( $aSelTitle );
00097         }
00098         foreach ( $aMDVariants as $sVarId => $oValue ) {
00099             if ( strpos( $sVarId, "mdvar_" ) === 0 ) {
00100                 foreach ( $oValue as $sId ) {
00101                     //var_dump($sVarId, $oAttribute->oxattribute__oxid->value);
00102                     $sVarId = substr($sVarId, 6);
00103                     $oNewAssign = oxNew( "oxbase" );
00104                     $oNewAssign->init( "oxobject2attribute" );
00105                     $sNewId = oxUtilsObject::getInstance()->generateUID();
00106                     if ($oNewAssign->load($sId)) {
00107                         $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
00108                         $oNewAssign->setId($sNewId);
00109                         $oNewAssign->save();
00110                     }
00111                 }
00112             } else {
00113                 $oNewAssign = oxNew( "oxi18n" );
00114                 $oNewAssign->setEnableMultilang( false );
00115                 $oNewAssign->init( "oxobject2attribute" );
00116                 $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
00117                 $oNewAssign->oxobject2attribute__oxattrid   = new oxField($sAttrId);
00118                 foreach ($aConfLanguages as $sKey => $sLang) {
00119                     $sPrefix = $myLang->getLanguageTag($sKey);
00120                     $oNewAssign->{'oxobject2attribute__oxvalue'.$sPrefix} = new oxField($oValue[$sKey]->name);
00121                 }
00122                 $oNewAssign->save();
00123             }
00124         }
00125     }
00126 
00134     protected function _getAttrId( $sSelTitle )
00135     {
00136         $oDb = oxDb::getDB();
00137         $sAttViewName = getViewName('oxattribute');
00138         return $oDb->getOne("select oxid from $sAttViewName where LOWER(oxtitle) = " . $oDb->quote(getStr()->strtolower($sSelTitle)));
00139     }
00140 
00148     protected function _createAttribute( $aSelTitle )
00149     {
00150         $myLang = oxLang::getInstance();
00151         $aConfLanguages = $myLang->getLanguageIds();
00152         $oAttr = oxNew('oxI18n');
00153         $oAttr->setEnableMultilang( false );
00154         $oAttr->init('oxattribute');
00155         foreach ($aConfLanguages as $sKey => $sLang) {
00156            $sPrefix = $myLang->getLanguageTag($sKey);
00157            $oAttr->{'oxattribute__oxtitle'.$sPrefix} = new oxField($aSelTitle[$sKey]);
00158         }
00159         $oAttr->save();
00160         return $oAttr->getId();
00161     }
00162 
00170     public function getAttributeAssigns( $sArtId)
00171     {
00172         if ( !$sArtId) {
00173             return;
00174         }
00175         $sSelect  = "select o2a.oxid ";
00176         $sSelect .= "from oxobject2attribute as o2a ";
00177         $sSelect .= "where o2a.oxobjectid = '$sArtId' ";
00178         $sSelect .= "order by o2a.oxpos";
00179 
00180         $aIds = array();
00181         $rs = oxDb::getDb()->Execute( $sSelect);
00182         if ($rs != false && $rs->recordCount() > 0) {
00183             while (!$rs->EOF) {
00184                 $aIds[] = $rs->fields[0];
00185                 $rs->moveNext();
00186             }
00187         }
00188         return $aIds;
00189     }
00190     
00191     
00192     
00200     public function setTitle( $sTitle )
00201     {
00202         $this->_sTitle = getStr()->htmlspecialchars( $sTitle );
00203     }
00204     
00210     public function getTitle()
00211     {
00212         return $this->_sTitle;
00213     }
00214     
00222     public function addValue( $sValue )
00223     {
00224         $this->_aValues[] = getStr()->htmlspecialchars( $sValue );
00225     }
00226     
00234     public function setActiveValue( $sValue )
00235     {
00236         $this->_sActiveValue = getStr()->htmlspecialchars( $sValue );
00237     }
00238     
00244     public function getActiveValue()
00245     {
00246             
00247         return $this->_sActiveValue;
00248     }
00249     
00255     public function getValues()
00256     {
00257         return $this->_aValues;
00258     }
00259     
00260 }