00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Catalog_Model_Mysql4_Convert
00029 {
00030 protected $_productsBySku;
00031 protected $_productEntity;
00032 protected $_skuAttribute;
00033
00034 public function getConnection()
00035 {
00036 return Mage::getSingleton('core/resource')->getConnection('catalog_write');
00037 }
00038
00039 public function getSelect()
00040 {
00041 return $this->getConnection()->select();
00042 }
00043
00044 public function getTable($table)
00045 {
00046 return Mage::getSingleton('core/resource')->getTableName($table);
00047 }
00048
00049 public function getProductEntity($field=null)
00050 {
00051 if (!$this->_productEntity) {
00052 $this->_productEntity = Mage::getResourceModel('catalog/product')
00053 ->loadAllAttributes();
00054 }
00055 return is_null($field) ? $this->_productEntity : $this->_productEntity->getData($field);
00056 }
00057
00058 public function getSkuAttribute($field='attribute_id')
00059 {
00060 if (!$this->_skuAttribute) {
00061 $this->_skuAttribute = $this->getProductEntity()->getAttribute('sku');
00062 }
00063 return $this->_skuAttribute->getData($field);
00064 }
00065
00066 public function getProductIdBySku($sku)
00067 {
00068 if (!$this->_productsBySku) {
00069 $select = $this->getSelect()
00070 ->from($this->getTable('catalog/product'), array('entity_id', 'sku'));
00071 $products = $this->getConnection()->fetchAll($select);
00072
00073 $this->_productsBySku = array();
00074 foreach ($products as $p) {
00075 $this->_productsBySku[$p['sku']] = $p['entity_id'];
00076 }
00077 }
00078 return isset($this->_productsBySku[$sku]) ? $this->_productsBySku[$sku] : false;
00079 }
00080
00081 public function addProductToStore($productId, $storeId)
00082 {
00083 $write = $this->getConnection();
00084 $table = $this->getTable('catalog/product_store');
00085 try {
00086 if (!$write->fetchOne("select * from $table where product_id=".(int)$productId." and store_id=".(int)$storeId)) {
00087 $write->query("insert into $table (product_id, store_id) values (".(int)$productId.",".(int)$storeId.")");
00088 }
00089 } catch (Exception $e) {
00090 throw $e;
00091 }
00092 return $this;
00093 }
00094
00095 public function exportAttributes()
00096 {
00097 $attributeFields = array(
00098 'attribute_code',
00099 'frontend_label', 'frontend_input', 'frontend_class', 'frontend_model',
00100 'backend_type', 'backend_table', 'backend_model',
00101 'source_model', 'attribute_model',
00102 'is_visible', 'is_user_defined', 'is_global', 'is_required', 'is_unique',
00103 'is_visible_on_front', 'is_searchable', 'is_filterable', 'is_comparable',
00104 'default_value', 'apply_to', 'use_in_super_product',
00105 );
00106
00107 $select = $this->getSelect()
00108 ->from(array('et'=>$this->getTable('eav/entity_type')), 'entity_type_code')
00109 ->join(array('a'=>$this->getTable('eav/attribute')), 'a.entity_type_id=et.entity_type_id', $attributeFields)
00110 ->where('et.entity_type_code in (?)', array('catalog_product', 'catalog_category'))
00111 ->order('if(not a.is_user_defined, 1, 2)')->order('attribute_code');
00112
00113 $attributes = $this->getConnection()->fetchAll($select);
00114
00115 return $attributes;
00116 }
00117
00118 public function exportAttributeSets()
00119 {
00120 $select = $this->getSelect()
00121 ->from(array('et'=>$this->getTable('eav/entity_type')), 'entity_type_code')
00122 ->join(array('s'=>$this->getTable('eav/attribute_set')), 's.entity_type_id=et.entity_type_id', 'attribute_set_name')
00123 ->join(array('g'=>$this->getTable('eav/attribute_group')), 'g.attribute_set_id=s.attribute_set_id', 'attribute_group_name')
00124 ->join(array('ea'=>$this->getTable('eav/entity_attribute')), 'ea.attribute_group_id=g.attribute_group_id', array())
00125 ->join(array('a'=>$this->getTable('eav/attribute')), 'a.attribute_id=ea.attribute_id', 'attribute_code')
00126 ->where('et.entity_type_code in (?)', array('catalog_product', 'catalog_category'))
00127 ->order('et.entity_type_code')->order('s.sort_order')->order('g.sort_order');
00128
00129 $sets = $this->getConnection()->fetchAll($select);
00130
00131 return $sets;
00132 }
00133
00134 public function exportAttributeOptions()
00135 {
00136 $select = $this->getSelect()
00137 ->from(array('et'=>$this->getTable('eav/entity_type')), 'entity_type_code')
00138 ->join(array('a'=>$this->getTable('eav/attribute')), 'a.entity_type_id=et.entity_type_id', 'attribute_code')
00139 ->join(array('ao'=>$this->getTable('eav/attribute_option')), 'ao.attribute_id=a.attribute_id', array())
00140 ->where('et.entity_type_code in (?)', array('catalog_product', 'catalog_category'))
00141 ->order('a.attribute_code')->order('ao.sort_order');
00142
00143 $stores = Mage::getConfig()->getNode('stores')->children();
00144 foreach ($stores as $storeName=>$storeConfig) {
00145 $select->joinLeft(
00146 array($storeName=>$this->getTable('eav/attribute_option_value')),
00147 "$storeName.option_id=ao.option_id and $storeName.store_id=".$storeConfig->descend('system/store/id'),
00148 array($storeName=>"$storeName.value")
00149 );
00150 }
00151
00152 $options = $this->getConnection()->fetchAll($select);
00153
00154 return $options;
00155 }
00156
00157 public function exportProductLinks()
00158 {
00159 $skuTable = $this->getTable('catalog/product').'_'.$this->getSkuAttribute('backend_type');
00160 $skuCond = ' and sku.store_id=0 and sku.attribute_id='.$this->getSkuAttribute('attribute_id');
00161
00162 $select = $this->getSelect()
00163 ->from(array('lt'=>$this->getTable('catalog/product_link_type')), array('link_type'=>'code'))
00164 ->join(array('l'=>$this->getTable('catalog/product_link')), 'l.link_type_id=lt.link_type_id', array())
00165 ->join(array('sku'=>$skuTable), 'sku.entity_id=l.product_id'.$skuCond, array('sku'=>'value'))
00166 ->join(array('linked'=>$skuTable), 'linked.entity_id=l.product_id'.$skuCond, array('linked'=>'value'))
00167 ->order('sku')->order('link_type');
00168 $links = $this->getConnection()->fetchAll($select);
00169
00170 return $links;
00171 }
00172
00173 public function exportProductsInCategories()
00174 {
00175 $skuTable = $this->getTable('catalog/product').'_'.$this->getSkuAttribute('backend_type');
00176 $skuCond = ' and sku.store_id=0 and sku.attribute_id='.$this->getSkuAttribute('attribute_id');
00177
00178 $select = $this->getSelect()
00179 ->from(array('cp'=>$this->getTable('catalog/category_product')), array('category_id', 'position'))
00180 ->join(array('sku'=>$skuTable), 'sku.entity_id=cp.product_id'.$skuCond, array('sku'=>'value'))
00181 ->order('category_id')->order('position')->order('sku');
00182
00183 $prodCats = $this->getConnection()->fetchAll($select);
00184
00185 return $prodCats;
00186 }
00187
00188 public function exportProductsInStores()
00189 {
00190 $skuTable = $this->getTable('catalog/product').'_'.$this->getSkuAttribute('backend_type');
00191 $skuCond = ' and sku.store_id=0 and sku.attribute_id='.$this->getSkuAttribute('attribute_id');
00192
00193 $select = $this->getSelect()
00194 ->from(array('ps'=>$this->getTable('catalog/product_store')), array())
00195 ->join(array('s'=>$this->getTable('core/store')), 's.store_id=ps.store_id', array('store'=>'code'))
00196 ->join(array('sku'=>$skuTable), 'sku.entity_id=ps.product_id'.$skuCond, array('sku'=>'value'))
00197 ->order('store')->order('sku');
00198
00199 $prodStores = $this->getConnection()->fetchAll($select);
00200
00201 return $prodStores;
00202 }
00203
00204 public function exportCategories()
00205 {
00206 $collection = Mage::getResourceModel('catalog/category_collection')
00207 ->addAttributeToSelect('*')
00208 ->load();
00209
00210 $categories = array();
00211 foreach ($collection as $object) {
00212 $row = $object->getData();
00213 $categories[] = $row;
00214 }
00215
00216 return $categories;
00217 }
00218
00219 public function exportProducts()
00220 {
00221 $attrSets = Mage::getResourceModel('eav/entity_attribute_set_collection')->load();
00222 $attrSetName = array();
00223 foreach ($attrSets as $attrSet) {
00224 $attrSetName[$attrSet->getId()] = $attrSet->getAttributeSetName();
00225 }
00226
00227 $select = $this->getSelect()
00228 ->from(array('ao'=>$this->getTable('eav/attribute_option')), array('attribute_id', 'option_id'))
00229 ->join(array('aov'=>$this->getTable('eav/attribute_option_value')), 'aov.option_id=ao.option_id', array('value_id', 'value'))
00230 ->where('aov.store_id=0');
00231
00232 echo $select->__toString();
00233 die();
00234
00235 $collection = Mage::getResourceModel('catalog/product_collection')
00236 ->addAttributeToSelect('*')
00237 ->load();
00238
00239 $products = array();
00240 foreach ($collection as $object) {
00241 $r = $object->getData();
00242
00243 unset($r['entity_id'], $r['entity_type_id']);
00244 $r['attribute_set_id'] = $attrSetName[$r['attribute_set_id']];
00245
00246 $products[] = $r;
00247 }
00248
00249 return $products;
00250 }
00251
00252 public function exportImageGallery()
00253 {
00254 return array();
00255 }
00256
00257 public function getProductAttributeOption($attribute, $value)
00258 {
00259 #$attribute = Mage::get
00260 }
00261
00262 public function importProducts(array $data)
00263 {
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 }
00278 }