oxattributelist.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxAttributeList extends oxList
00007 {
00015     public function __construct( $sObjectsInListName = 'oxattribute')
00016     {
00017         parent::__construct( 'oxattribute');
00018     }
00019 
00027     public function loadAttributesByIds( $aIds )
00028     {
00029         if (!count($aIds)) {
00030             return;
00031         }
00032 
00033         foreach ($aIds as $iKey => $sVal) {
00034             $aIds[$iKey] = mysql_real_escape_string($sVal);
00035         }
00036 
00037         $sAttrViewName = getViewName( 'oxattribute' );
00038         $sViewName     = getViewName( 'oxobject2attribute' );
00039 
00040         $sSelect  = "select $sAttrViewName.oxid, $sAttrViewName.oxtitle, {$sViewName}.oxvalue, {$sViewName}.oxobjectid ";
00041         $sSelect .= "from {$sViewName} left join $sAttrViewName on $sAttrViewName.oxid = {$sViewName}.oxattrid ";
00042         $sSelect .= "where {$sViewName}.oxobjectid in ( '".implode("','", $aIds)."' ) ";
00043         $sSelect .= "order by {$sViewName}.oxpos, $sAttrViewName.oxpos";
00044 
00045         return $this->_createAttributeListFromSql( $sSelect);
00046     }
00047 
00055     protected function _createAttributeListFromSql( $sSelect)
00056     {
00057         $aAttributes = array();
00058         $rs = oxDb::getDb()->execute( $sSelect);
00059         if ($rs != false && $rs->recordCount() > 0) {
00060             while (!$rs->EOF) {
00061                 if ( !isset( $aAttributes[$rs->fields[0]])) {
00062                     $aAttributes[$rs->fields[0]] = new stdClass();
00063                 }
00064 
00065                 $aAttributes[$rs->fields[0]]->title = $rs->fields[1];
00066                 if ( !isset( $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]])) {
00067                     $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]] = new stdClass();
00068                 }
00069                 $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]]->value = $rs->fields[2];
00070                 $rs->moveNext();
00071             }
00072         }
00073         return $aAttributes;
00074     }
00075 
00084     public function loadAttributes( $sArtId, $sParentId = null )
00085     {
00086         if ( $sArtId ) {
00087 
00088             $sAttrTableName = getViewName( 'oxattribute' );
00089             $sObject2AtrrTableName  = getViewName( 'oxobject2attribute' );
00090 
00091             $sSelect  = "SELECT `Attributes`.`oxid`, `Attributes`.`oxtitle`,  ";
00092 
00093             if ( !$sParentId ) {
00094                 $sSelect .= "`ArticleAtrr`.`oxvalue` AS `oxvalue` ";
00095             } else {
00096                 $sSelect .= "IFNULL( `ArticleAtrr`.`oxvalue`, `ParentAtrr`.`oxvalue` ) AS `oxvalue` ";
00097             }
00098 
00099             $sSelect .= "FROM {$sAttrTableName} AS `Attributes` ";
00100             $sSelect .= "LEFT JOIN {$sObject2AtrrTableName} AS `ArticleAtrr` ON `Attributes`.`oxid` = `ArticleAtrr`.`oxattrid` ";
00101             $sSelect .= ($sParentId) ? "LEFT JOIN {$sObject2AtrrTableName} AS `ParentAtrr` ON `Attributes`.`oxId` = `ParentAtrr`.`oxattrid` " : "";
00102             $sSelect .= "WHERE 1 ";
00103             $sSelect .= "AND `ArticleAtrr`.`oxobjectid` = '{$sArtId}' AND `ArticleAtrr`.`oxvalue` != '' ";
00104             $sSelect .= ($sParentId) ? "OR `ParentAtrr`.`oxobjectid` = '{$sParentId}' AND `ParentAtrr`.`oxvalue` != '' " : "";
00105             $sSelect .= "ORDER BY ";
00106             $sSelect .= "`ArticleAtrr`.`oxpos`, ";
00107             $sSelect .= ($sParentId) ? "`ParentAtrr`.`oxpos`, " : "";
00108             $sSelect .= "`Attributes`.`oxpos`";
00109 
00110             $this->selectString( $sSelect );
00111         }
00112     }
00113 
00123     public function getCategoryAttributes( $sCategoryId, $iLang )
00124     {
00125         $aSessionFilter = oxSession::getVar( 'session_attrfilter' );
00126 
00127         $oArtList = oxNew( "oxarticlelist");
00128         $oArtList->loadCategoryIDs( $sCategoryId, $aSessionFilter );
00129 
00130         // Only if we have articles
00131         if (count($oArtList) > 0 ) {
00132             $oDb = oxDb::getDb();
00133             $sArtIds = '';
00134             foreach (array_keys($oArtList->getArray()) as $sId ) {
00135                 if ($sArtIds) {
00136                     $sArtIds .= ',';
00137                 }
00138                 $sArtIds .= $oDb->quote($sId);
00139             }
00140 
00141             $sActCatQuoted = $oDb->quote( $sCategoryId );
00142             $sAttTbl = getViewName( 'oxattribute', $iLang );
00143             $sO2ATbl = getViewName( 'oxobject2attribute', $iLang );
00144             $sC2ATbl = getViewName( 'oxcategory2attribute', $iLang );
00145 
00146             $sSelect = "SELECT DISTINCT att.oxid, att.oxtitle, o2a.oxvalue ".
00147                        "FROM $sAttTbl as att, $sO2ATbl as o2a ,$sC2ATbl as c2a ".
00148                        "WHERE att.oxid = o2a.oxattrid AND c2a.oxobjectid = $sActCatQuoted AND c2a.oxattrid = att.oxid AND o2a.oxvalue !='' AND o2a.oxobjectid IN ($sArtIds) ".
00149                        "ORDER BY c2a.oxsort , att.oxpos, att.oxtitle, o2a.oxvalue";
00150 
00151 
00152             $rs = $oDb->execute( $sSelect );
00153 
00154             if ( $rs != false && $rs->recordCount() > 0 ) {
00155                 while ( !$rs->EOF && list( $sAttId, $sAttTitle, $sAttValue ) = $rs->fields ) {
00156 
00157                     if ( !$this->offsetExists( $sAttId ) ) {
00158 
00159                         $oAttribute = oxNew( "oxattribute" );
00160                         $oAttribute->setTitle( $sAttTitle );
00161 
00162                         $this->offsetSet( $sAttId, $oAttribute );
00163                         $iLang = oxLang::getInstance()->getBaseLanguage();
00164                         if ( isset( $aSessionFilter[$sCategoryId][$iLang][$sAttId] ) ) {
00165                             $oAttribute->setActiveValue( $aSessionFilter[$sCategoryId][$iLang][$sAttId] );
00166                         }
00167 
00168                     } else {
00169                         $oAttribute = $this->offsetGet( $sAttId );
00170                     }
00171 
00172                     $oAttribute->addValue( $sAttValue );
00173                     $rs->moveNext();
00174                 }
00175             }
00176         }
00177 
00178         return $this;
00179     }
00180 }