oxattributelist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxAttributeList extends oxList
00008 {
00016     public function __construct( $sObjectsInListName = 'oxattribute')
00017     {
00018         parent::__construct( 'oxattribute');
00019     }
00020 
00028     public function loadAttributesByIds( $aIds )
00029     {
00030         if (!count($aIds)) {
00031             return;
00032         }
00033 
00034         foreach ($aIds as $iKey => $sVal) {
00035             $aIds[$iKey] = oxDb::getInstance()->escapeString($sVal);
00036         }
00037 
00038         $sAttrViewName = getViewName( 'oxattribute' );
00039         $sViewName     = getViewName( 'oxobject2attribute' );
00040 
00041         $sSelect  = "select $sAttrViewName.oxid, $sAttrViewName.oxtitle, {$sViewName}.oxvalue, {$sViewName}.oxobjectid ";
00042         $sSelect .= "from {$sViewName} left join $sAttrViewName on $sAttrViewName.oxid = {$sViewName}.oxattrid ";
00043         $sSelect .= "where {$sViewName}.oxobjectid in ( '".implode("','", $aIds)."' ) ";
00044         $sSelect .= "order by {$sViewName}.oxpos, $sAttrViewName.oxpos";
00045 
00046         return $this->_createAttributeListFromSql( $sSelect);
00047     }
00048 
00056     protected function _createAttributeListFromSql( $sSelect )
00057     {
00058         $aAttributes = array();
00059         $rs = oxDb::getDb()->select( $sSelect );
00060         if ($rs != false && $rs->recordCount() > 0) {
00061             while (!$rs->EOF) {
00062                 if ( !isset( $aAttributes[$rs->fields[0]])) {
00063                     $aAttributes[$rs->fields[0]] = new stdClass();
00064                 }
00065 
00066                 $aAttributes[$rs->fields[0]]->title = $rs->fields[1];
00067                 if ( !isset( $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]])) {
00068                     $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]] = new stdClass();
00069                 }
00070                 $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]]->value = $rs->fields[2];
00071                 $rs->moveNext();
00072             }
00073         }
00074         return $aAttributes;
00075     }
00076 
00085     public function loadAttributes( $sArticleId, $sParentId = null )
00086     {
00087         if ( $sArticleId ) {
00088 
00089             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00090 
00091             $sAttrViewName = getViewName( 'oxattribute' );
00092             $sViewName     = getViewName( 'oxobject2attribute' );
00093 
00094             $sSelect  = "select {$sAttrViewName}.`oxid`, {$sAttrViewName}.`oxtitle`, o2a.`oxvalue` from {$sViewName} as o2a ";
00095             $sSelect .= "left join {$sAttrViewName} on {$sAttrViewName}.oxid = o2a.oxattrid ";
00096             $sSelect .= "where o2a.oxobjectid = '%s' and o2a.oxvalue != '' ";
00097             $sSelect .= "order by o2a.oxpos, {$sAttrViewName}.oxpos";
00098 
00099             $aAttributes = $oDb->getAll( sprintf( $sSelect, $sArticleId ) );
00100 
00101             if ( $sParentId ) {
00102                 $aParentAttributes = $oDb->getAll( sprintf( $sSelect, $sParentId ));
00103                 $aAttributes = $this->_mergeAttributes( $aAttributes, $aParentAttributes );
00104             }
00105 
00106             $this->assignArray( $aAttributes );
00107         }
00108 
00109     }
00110 
00118     public function loadAttributesDisplayableInBasket( $sArtId )
00119     {
00120         if ( $sArtId ) {
00121 
00122             $sAttrViewName = getViewName( 'oxattribute' );
00123             $sViewName     = getViewName( 'oxobject2attribute' );
00124 
00125             $sSelect  = "select {$sAttrViewName}.*, o2a.* from {$sViewName} as o2a ";
00126             $sSelect .= "left join {$sAttrViewName} on {$sAttrViewName}.oxid = o2a.oxattrid ";
00127             $sSelect .= "where o2a.oxobjectid = '{$sArtId}' and {$sAttrViewName}.oxdisplayinbasket  = 1 and o2a.oxvalue != '' ";
00128             $sSelect .= "order by o2a.oxpos, {$sAttrViewName}.oxpos";
00129 
00130             $this->selectString( $sSelect );
00131         }
00132     }
00133 
00134 
00144     public function getCategoryAttributes( $sCategoryId, $iLang )
00145     {
00146         $aSessionFilter = oxSession::getVar( 'session_attrfilter' );
00147 
00148         $oArtList = oxNew( "oxarticlelist");
00149         $oArtList->loadCategoryIDs( $sCategoryId, $aSessionFilter );
00150 
00151         // Only if we have articles
00152         if (count($oArtList) > 0 ) {
00153             $oDb = oxDb::getDb();
00154             $sArtIds = '';
00155             foreach (array_keys($oArtList->getArray()) as $sId ) {
00156                 if ($sArtIds) {
00157                     $sArtIds .= ',';
00158                 }
00159                 $sArtIds .= $oDb->quote($sId);
00160             }
00161 
00162             $sActCatQuoted = $oDb->quote( $sCategoryId );
00163             $sAttTbl = getViewName( 'oxattribute', $iLang );
00164             $sO2ATbl = getViewName( 'oxobject2attribute', $iLang );
00165             $sC2ATbl = getViewName( 'oxcategory2attribute', $iLang );
00166 
00167             $sSelect = "SELECT DISTINCT att.oxid, att.oxtitle, o2a.oxvalue ".
00168                        "FROM $sAttTbl as att, $sO2ATbl as o2a ,$sC2ATbl as c2a ".
00169                        "WHERE att.oxid = o2a.oxattrid AND c2a.oxobjectid = $sActCatQuoted AND c2a.oxattrid = att.oxid AND o2a.oxvalue !='' AND o2a.oxobjectid IN ($sArtIds) ".
00170                        "ORDER BY c2a.oxsort , att.oxpos, att.oxtitle, o2a.oxvalue";
00171 
00172             $rs = $oDb->select( $sSelect );
00173 
00174             if ( $rs != false && $rs->recordCount() > 0 ) {
00175                 while ( !$rs->EOF && list( $sAttId, $sAttTitle, $sAttValue ) = $rs->fields ) {
00176 
00177                     if ( !$this->offsetExists( $sAttId ) ) {
00178 
00179                         $oAttribute = oxNew( "oxattribute" );
00180                         $oAttribute->setTitle( $sAttTitle );
00181 
00182                         $this->offsetSet( $sAttId, $oAttribute );
00183                         $iLang = oxRegistry::getLang()->getBaseLanguage();
00184                         if ( isset( $aSessionFilter[$sCategoryId][$iLang][$sAttId] ) ) {
00185                             $oAttribute->setActiveValue( $aSessionFilter[$sCategoryId][$iLang][$sAttId] );
00186                         }
00187 
00188                     } else {
00189                         $oAttribute = $this->offsetGet( $sAttId );
00190                     }
00191 
00192                     $oAttribute->addValue( $sAttValue );
00193                     $rs->moveNext();
00194                 }
00195             }
00196         }
00197 
00198         return $this;
00199     }
00200 
00209     protected function _mergeAttributes( $aAttributes, $aParentAttributes )
00210     {
00211 
00212         if ( count( $aParentAttributes ) ) {
00213             $aAttrIds = array();
00214             foreach ( $aAttributes as $aAttribute ) {
00215                 $aAttrIds[] = $aAttribute['OXID'];
00216             }
00217 
00218             foreach ( $aParentAttributes as $aAttribute ) {
00219                 if ( !in_array( $aAttribute['OXID'], $aAttrIds ) ) {
00220                     $aAttributes[] = $aAttribute;
00221                 }
00222             }
00223         }
00224 
00225         return $aAttributes;
00226     }
00227 
00228 }