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