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, $sParentId = null  )
00119     {
00120         if ( $sArtId ) {
00121 
00122             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00123 
00124             $sAttrViewName = getViewName( 'oxattribute' );
00125             $sViewName     = getViewName( 'oxobject2attribute' );
00126 
00127             $sSelect  = "select {$sAttrViewName}.*, o2a.* from {$sViewName} as o2a ";
00128             $sSelect .= "left join {$sAttrViewName} on {$sAttrViewName}.oxid = o2a.oxattrid ";
00129             $sSelect .= "where o2a.oxobjectid = '%s' and {$sAttrViewName}.oxdisplayinbasket  = 1 and o2a.oxvalue != '' ";
00130             $sSelect .= "order by o2a.oxpos, {$sAttrViewName}.oxpos";
00131 
00132             $aAttributes = $oDb->getAll( sprintf( $sSelect, $sArtId ) );
00133 
00134             if ( $sParentId ) {
00135                 $aParentAttributes = $oDb->getAll( sprintf( $sSelect, $sParentId ));
00136                 $aAttributes = $this->_mergeAttributes( $aAttributes, $aParentAttributes );
00137             }
00138 
00139             $this->assignArray( $aAttributes );
00140         }
00141     }
00142 
00143 
00153     public function getCategoryAttributes( $sCategoryId, $iLang )
00154     {
00155         $aSessionFilter = oxSession::getVar( 'session_attrfilter' );
00156 
00157         $oArtList = oxNew( "oxarticlelist");
00158         $oArtList->loadCategoryIDs( $sCategoryId, $aSessionFilter );
00159 
00160         // Only if we have articles
00161         if (count($oArtList) > 0 ) {
00162             $oDb = oxDb::getDb();
00163             $sArtIds = '';
00164             foreach (array_keys($oArtList->getArray()) as $sId ) {
00165                 if ($sArtIds) {
00166                     $sArtIds .= ',';
00167                 }
00168                 $sArtIds .= $oDb->quote($sId);
00169             }
00170 
00171             $sActCatQuoted = $oDb->quote( $sCategoryId );
00172             $sAttTbl = getViewName( 'oxattribute', $iLang );
00173             $sO2ATbl = getViewName( 'oxobject2attribute', $iLang );
00174             $sC2ATbl = getViewName( 'oxcategory2attribute', $iLang );
00175 
00176             $sSelect = "SELECT DISTINCT att.oxid, att.oxtitle, o2a.oxvalue ".
00177                        "FROM $sAttTbl as att, $sO2ATbl as o2a ,$sC2ATbl as c2a ".
00178                        "WHERE att.oxid = o2a.oxattrid AND c2a.oxobjectid = $sActCatQuoted AND c2a.oxattrid = att.oxid AND o2a.oxvalue !='' AND o2a.oxobjectid IN ($sArtIds) ".
00179                        "ORDER BY c2a.oxsort , att.oxpos, att.oxtitle, o2a.oxvalue";
00180 
00181             $rs = $oDb->select( $sSelect );
00182 
00183             if ( $rs != false && $rs->recordCount() > 0 ) {
00184                 while ( !$rs->EOF && list( $sAttId, $sAttTitle, $sAttValue ) = $rs->fields ) {
00185 
00186                     if ( !$this->offsetExists( $sAttId ) ) {
00187 
00188                         $oAttribute = oxNew( "oxattribute" );
00189                         $oAttribute->setTitle( $sAttTitle );
00190 
00191                         $this->offsetSet( $sAttId, $oAttribute );
00192                         $iLang = oxRegistry::getLang()->getBaseLanguage();
00193                         if ( isset( $aSessionFilter[$sCategoryId][$iLang][$sAttId] ) ) {
00194                             $oAttribute->setActiveValue( $aSessionFilter[$sCategoryId][$iLang][$sAttId] );
00195                         }
00196 
00197                     } else {
00198                         $oAttribute = $this->offsetGet( $sAttId );
00199                     }
00200 
00201                     $oAttribute->addValue( $sAttValue );
00202                     $rs->moveNext();
00203                 }
00204             }
00205         }
00206 
00207         return $this;
00208     }
00209 
00218     protected function _mergeAttributes( $aAttributes, $aParentAttributes )
00219     {
00220 
00221         if ( count( $aParentAttributes ) ) {
00222             $aAttrIds = array();
00223             foreach ( $aAttributes as $aAttribute ) {
00224                 $aAttrIds[] = $aAttribute['OXID'];
00225             }
00226 
00227             foreach ( $aParentAttributes as $aAttribute ) {
00228                 if ( !in_array( $aAttribute['OXID'], $aAttrIds ) ) {
00229                     $aAttributes[] = $aAttribute;
00230                 }
00231             }
00232         }
00233 
00234         return $aAttributes;
00235     }
00236 
00237 }