oxattributelist.php

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