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
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected $_storeId = null;
00038
00039 protected $_loaded = false;
00040
00041 protected $_nodes = array();
00042
00043
00044
00045
00046
00047
00048 protected $_inactiveCategoryIds = null;
00049
00050 protected $_isRebuilt = null;
00051
00052 protected function _construct()
00053 {
00054 $this->_init('catalog/category_flat', 'entity_id');
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 public function setStoreId($storeId)
00064 {
00065 $this->_storeId = $storeId;
00066 return $this;
00067 }
00068
00069
00070
00071
00072
00073
00074 public function getStoreId()
00075 {
00076 if (is_null($this->_storeId)) {
00077 return Mage::app()->getStore()->getId();
00078 }
00079 return $this->_storeId;
00080 }
00081
00082
00083
00084
00085
00086
00087 public function getMainTable()
00088 {
00089 return $this->getMainStoreTable($this->getStoreId());
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 public function getMainStoreTable($storeId = 0)
00099 {
00100 $table = parent::getMainTable();
00101 if (is_string($storeId)) {
00102 $storeId = intval($storeId);
00103 }
00104 if ($this->getUseStoreTables() && $storeId) {
00105 $table .= '_store_'.$storeId;
00106 }
00107 return $table;
00108 }
00109
00110
00111
00112
00113
00114
00115 public function getUseStoreTables()
00116 {
00117 return true;
00118 }
00119
00120
00121
00122
00123
00124
00125 public function addInactiveCategoryIds($ids)
00126 {
00127 if (!is_array($this->_inactiveCategoryIds)) {
00128 $this->_initInactiveCategoryIds();
00129 }
00130 $this->_inactiveCategoryIds = array_merge($ids, $this->_inactiveCategoryIds);
00131 return $this;
00132 }
00133
00134
00135
00136
00137
00138
00139 protected function _initInactiveCategoryIds()
00140 {
00141 $this->_inactiveCategoryIds = array();
00142 Mage::dispatchEvent('catalog_category_tree_init_inactive_category_ids', array('tree'=>$this));
00143 return $this;
00144 }
00145
00146
00147
00148
00149
00150
00151 public function getInactiveCategoryIds()
00152 {
00153 if (!is_array($this->_inactiveCategoryIds)) {
00154 $this->_initInactiveCategoryIds();
00155 }
00156
00157 return $this->_inactiveCategoryIds;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 protected function _loadNodes($parentNode = null, $recursionLevel = 0, $storeId = 0)
00169 {
00170 $_conn = $this->_getReadAdapter();
00171 $startLevel = 1;
00172 $parentPath = '';
00173 if ($parentNode instanceof Mage_Catalog_Model_Category) {
00174 $parentPath = $parentNode->getPath();
00175 $startLevel = $parentNode->getLevel();
00176 } elseif (is_numeric($parentNode)) {
00177 $selectParent = $_conn->select()
00178 ->from($this->getMainStoreTable())
00179 ->where('entity_id = ?', $parentNode)
00180 ->where('store_id = ?', '0');
00181 if ($parentNode = $_conn->fetchRow($selectParent)) {
00182 $parentPath = $parentNode['path'];
00183 $startLevel = $parentNode['level'];
00184 }
00185 }
00186 $select = $_conn->select()
00187 ->from(array('main_table'=>$this->getMainStoreTable($storeId)), array('main_table.entity_id', 'main_table.name', 'main_table.path', 'main_table.is_active', 'main_table.is_anchor'))
00188 ->joinLeft(
00189 array('url_rewrite'=>$this->getTable('core/url_rewrite')),
00190 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND url_rewrite.product_id IS NULL AND url_rewrite.store_id="'.$storeId.'" AND url_rewrite.id_path LIKE "category/%"',
00191 array('request_path' => 'url_rewrite.request_path'))
00192 ->where('main_table.is_active = ?', '1')
00193
00194 ->order('main_table.position', 'ASC');
00195
00196
00197
00198 if ($parentPath) {
00199 $select->where($_conn->quoteInto("main_table.path like ?", "$parentPath/%"));
00200 }
00201 if ($recursionLevel != 0) {
00202 $select->where("main_table.level <= ?", $startLevel + $recursionLevel);
00203 }
00204
00205 $inactiveCategories = $this->getInactiveCategoryIds();
00206
00207 if (!empty($inactiveCategories)) {
00208 $select->where('main_table.entity_id NOT IN (?)', $inactiveCategories);
00209 }
00210
00211 $arrNodes = $_conn->fetchAll($select);
00212 $nodes = array();
00213 foreach ($arrNodes as $node) {
00214 $node['id'] = $node['entity_id'];
00215 $nodes[$node['id']] = Mage::getModel('catalog/category')->setData($node);
00216 }
00217
00218 return $nodes;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228 public function addChildNodes($children, $path, $parent)
00229 {
00230 if (isset($children[$path])) {
00231 foreach ($children[$path] as $child) {
00232 $childrenNodes = $parent->getChildrenNodes();
00233 if ($childrenNodes && isset($childrenNodes[$child->getId()])) {
00234 $childrenNodes[$child['entity_id']]->setChildrenNodes(array($child->getId()=>$child));
00235 } else {
00236 if ($childrenNodes) {
00237 $childrenNodes[$child->getId()] = $child;
00238 } else {
00239 $childrenNodes = array($child->getId()=>$child);
00240 }
00241 $parent->setChildrenNodes($childrenNodes);
00242 }
00243
00244 if ($path) {
00245 $childrenPath = explode('/', $path);
00246 } else {
00247 $childrenPath = array();
00248 }
00249 $childrenPath[] = $child->getId();
00250 $childrenPath = implode('/', $childrenPath);
00251 $this->addChildNodes($children, $childrenPath, $child);
00252 }
00253 }
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 public function getNodes($parentId, $recursionLevel = 0, $storeId = 0)
00265 {
00266 if (!$this->_loaded) {
00267 $selectParent = $this->_getReadAdapter()->select()
00268 ->from($this->getMainStoreTable())
00269 ->where('entity_id = ?', $parentId);
00270 if ($parentNode = $this->_getReadAdapter()->fetchRow($selectParent)) {
00271 $parentNode['id'] = $parentNode['entity_id'];
00272 $parentNode = Mage::getModel('catalog/category')->setData($parentNode);
00273 $this->_nodes[$parentNode->getId()] = $parentNode;
00274 $nodes = $this->_loadNodes($parentNode, $recursionLevel, $storeId);
00275 $childrenItems = array();
00276 foreach ($nodes as $node) {
00277 $pathToParent = explode('/', $node->getPath());
00278 array_pop($pathToParent);
00279 $pathToParent = implode('/', $pathToParent);
00280 $childrenItems[$pathToParent][] = $node;
00281 }
00282 $this->addChildNodes($childrenItems, $parentNode->getPath(), $parentNode);
00283 $childrenNodes = $this->_nodes[$parentNode->getId()];
00284 if ($childrenNodes->getChildrenNodes()) {
00285 $this->_nodes = $childrenNodes->getChildrenNodes();
00286 }
00287 else {
00288 $this->_nodes = array();
00289 }
00290 $this->_loaded = true;
00291 }
00292 }
00293 return $this->_nodes;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 public function getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
00307 {
00308 if ($asCollection) {
00309 $parentPath = $this->_getReadAdapter()->fetchOne(new Zend_Db_Expr("
00310 SELECT path FROM {$this->getMainStoreTable()} WHERE entity_id = {$parent}
00311 "));
00312 $collection = Mage::getModel('catalog/category')->getCollection()
00313 ->addNameToResult()
00314 ->addUrlRewriteToResult()
00315 ->addParentPathFilter($parentPath)
00316 ->addStoreFilter()
00317 ->addSortedField($sorted);
00318 if ($toLoad) {
00319 return $collection->load();
00320 }
00321 return $collection;
00322 }
00323 return $this->getNodes($parent, $recursionLevel, Mage::app()->getStore()->getId());
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333 public function getNodeById($nodeId, $nodes = null)
00334 {
00335 if (is_null($nodes)) {
00336 $nodes = $this->getNodes();
00337 }
00338 if (isset($nodes[$nodeId])) {
00339 return $nodes[$nodeId];
00340 }
00341 foreach ($nodes as $node) {
00342
00343
00344
00345 if ($node->getChildrenNodes()) {
00346 return $this->getNodeById($nodeId, $node->getChildrenNodes());
00347 }
00348 }
00349 return array();
00350 }
00351
00352
00353
00354
00355
00356
00357 public function isRebuilt()
00358 {
00359 if ($this->_isRebuilt === null) {
00360 $select = $this->_getReadAdapter()->select()
00361 ->from($this->getMainStoreTable($this->getStoreId()), 'entity_id')
00362 ->limit(1);
00363 try {
00364 $this->_isRebuilt = (bool) $this->_getReadAdapter()->fetchOne($select);
00365 } catch (Exception $e) {
00366 $this->_isRebuilt = false;
00367 }
00368 }
00369 return $this->_isRebuilt;
00370 }
00371
00372 protected function _getTableSqlSchema($storeId = 0)
00373 {
00374 $storeId = Mage::app()->getStore($storeId)->getId();
00375 $schema = "CREATE TABLE `{$this->getMainStoreTable($storeId)}` (
00376 `entity_id` int(10) unsigned not null,
00377 `store_id` smallint(5) unsigned not null default '0',
00378 `parent_id` int(10) unsigned not null default '0',
00379 `path` varchar(255) not null default '',
00380 `level` int(11) not null default '0',
00381 `position` int(11) not null default '0',
00382 `children_count` int(11) not null,
00383 `created_at` datetime not null default '0000-00-00 00:00:00',
00384 `updated_at` datetime not null default '0000-00-00 00:00:00',
00385 KEY `CATEGORY_FLAT_CATEGORY_ID` (`entity_id`),
00386 KEY `CATEGORY_FLAT_STORE_ID` (`store_id`),
00387 KEY `path` (`path`),
00388 KEY `IDX_LEVEL` (`level`),
00389 CONSTRAINT `FK_CATEGORY_FLAT_CATEGORY_ID_STORE_{$storeId}` FOREIGN KEY (`entity_id`)
00390 REFERENCES `{$this->getTable('catalog/category')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00391 CONSTRAINT `FK_CATEGORY_FLAT_STORE_ID_STORE_{$storeId}` FOREIGN KEY (`store_id`)
00392 REFERENCES `{$this->getTable('core/store')}` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
00393 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
00394 return $schema;
00395 }
00396
00397
00398
00399
00400
00401
00402 public function rebuild()
00403 {
00404 $_read = $this->_getReadAdapter();
00405 if ($this->getUseStoreTables()) {
00406 $stores = array();
00407 $selectStores = $_read->select()
00408 ->from($this->getTable('core/store'), 'store_id');
00409 $stores = array();
00410 foreach ($_read->fetchAll($selectStores) as $store) {
00411 $stores[] = $store['store_id'];
00412 }
00413 $this->_createTable($stores);
00414 } else {
00415 $this->_createTable(0);
00416 }
00417 $select = $this->_getReadAdapter()->select()
00418 ->from($this->getTable('catalog/category'), 'entity_id');
00419 $_categories = $this->_getReadAdapter()->fetchAll($select);
00420 foreach ($_categories as $_category) {
00421 foreach ($stores as $store) {
00422 $_tmpCategory = Mage::getModel('catalog/category')
00423 ->setStoreId($store)
00424 ->load($_category['entity_id']);
00425 if ($_tmpCategory->getId()) {
00426 $this->_synchronize($_tmpCategory, 'insert');
00427 }
00428 }
00429 }
00430 $_tmpCategory = null;
00431 return $this;
00432 }
00433
00434
00435
00436
00437
00438
00439
00440 public function createTable($stores)
00441 {
00442 return $this->_createTable($stores);
00443 }
00444
00445
00446
00447
00448
00449
00450
00451 protected function _createTable($stores)
00452 {
00453 if (!is_array($stores)) {
00454 $stores = (int) $stores;
00455 }
00456 $_read = $this->_getReadAdapter();
00457 $_write = $this->_getWriteAdapter();
00458 if ($this->getUseStoreTables() && is_array($stores)) {
00459 foreach ($stores as $store) {
00460 $_read->query("DROP TABLE IF EXISTS `{$this->getMainStoreTable($store)}`");
00461 $_read->query($this->_getTableSqlSchema($store));
00462 }
00463 } else {
00464 $_read->query("DROP TABLE IF EXISTS `{$this->getMainStoreTable($stores)}`");
00465 $_read->query($this->_getTableSqlSchema($stores));
00466 }
00467 $selectAttribute = $_read->select()
00468 ->from($this->getTable('eav/entity_type'), array())
00469 ->join(
00470 $this->getTable('eav/attribute'),
00471 $this->getTable('eav/attribute').'.entity_type_id = '.$this->getTable('eav/entity_type').'.entity_type_id',
00472 $this->getTable('eav/attribute').'.*'
00473 )
00474 ->where($this->getTable('eav/entity_type').'.entity_type_code=?', 'catalog_category');
00475 $resultAttribute = $_read->fetchAll($selectAttribute);
00476 foreach ($resultAttribute as $attribute) {
00477 $type = '';
00478 switch ($attribute['backend_type']) {
00479 case 'varchar':
00480 $type = 'varchar(255) not null default \'\'';
00481 break;
00482 case 'int':
00483 $type = 'int(10) not null default \'0\'';
00484 break;
00485 case 'text':
00486 $type = 'text';
00487 break;
00488 case 'datetime':
00489 $type = 'datetime not null default \'0000-00-00 00:00:00\'';
00490 break;
00491 case 'decimal':
00492 $type = 'decimal(10,2) not null default \'0.00\'';
00493 break;
00494 }
00495 if ($type) {
00496 if ($this->getUseStoreTables() && is_array($stores)) {
00497 foreach ($stores as $store) {
00498 $_write->addColumn($this->getMainStoreTable($store), $attribute['attribute_code'], $type);
00499 }
00500 } else {
00501 $_write->addColumn($this->getMainStoreTable($stores), $attribute['attribute_code'], $type);
00502 }
00503 }
00504 }
00505 return $this;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514 public function deleteStores($stores)
00515 {
00516 $this->_deleteTable($stores);
00517 return $this;
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 protected function _deleteTable($stores)
00527 {
00528 if (!is_array($stores)) {
00529 $stores = array($stores);
00530 }
00531 foreach ($stores as $store) {
00532 $_tableExist = $this->_getReadAdapter()->query(
00533 "DROP TABLE IF EXISTS `{$this->getMainStoreTable($store)}`"
00534 );
00535 }
00536 return $this;
00537 }
00538
00539
00540
00541
00542
00543
00544
00545
00546 protected function _synchronize($category, $action = null)
00547 {
00548 if (is_null($action)) {
00549 $select = $this->_getReadAdapter()->select()
00550 ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00551 ->where('entity_id = ?', $category->getId());
00552 if ($result = $this->_getReadAdapter()->fetchOne($select)) {
00553 $action = 'update';
00554 } else {
00555 $action = 'insert';
00556 }
00557 }
00558
00559 if ($action == 'update') {
00560
00561 $this->_getWriteAdapter()->update(
00562 $this->getMainStoreTable($category->getStoreId()),
00563 $this->_prepareDataForAllFields($category),
00564 $this->_getReadAdapter()->quoteInto('entity_id = ?', $category->getId())
00565 );
00566 } elseif ($action == 'insert') {
00567
00568 $this->_getWriteAdapter()->insert(
00569 $this->getMainStoreTable($category->getStoreId()),
00570 $this->_prepareDataForAllFields($category)
00571 );
00572 }
00573 return $this;
00574 }
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584 protected function _move($prevParentPath, $parentPath)
00585 {
00586 $_staticFields = array(
00587 'parent_id',
00588 'path',
00589 'level',
00590 'position',
00591 'children_count',
00592 'updated_at'
00593 );
00594 $select = $this->_getReadAdapter()->select()
00595 ->from($this->getTable('core/store'), 'store_id');
00596 $stores = $this->_getReadAdapter()->fetchAll($select);
00597 foreach ($stores as $store) {
00598 $update = "UPDATE {$this->getMainStoreTable($store['store_id'])}, {$this->getTable('catalog/category')} SET";
00599 foreach ($_staticFields as $field) {
00600 $update .= " {$this->getMainStoreTable($store['store_id'])}.".$field."={$this->getTable('catalog/category')}.".$field.",";
00601 }
00602 $update = substr($update, 0, -1);
00603 $update .= " WHERE {$this->getMainStoreTable($store['store_id'])}.entity_id = {$this->getTable('catalog/category')}.entity_id AND " .
00604 "({$this->getTable('catalog/category')}.path like '$parentPath/%' OR " .
00605 "{$this->getTable('catalog/category')}.path like '$prevParentPath/%')";
00606 $this->_getWriteAdapter()->query($update);
00607 }
00608 return $this;
00609 }
00610
00611
00612
00613
00614
00615
00616
00617
00618 public function synchronize($category = null, $storeIds = array())
00619 {
00620 if (is_null($category)) {
00621 $storesCondition = '';
00622 if (!empty($storeIds)) {
00623 $storesCondition = $this->_getReadAdapter()->quoteInto(
00624 ' AND s.store_id IN (?)', $storeIds
00625 );
00626 }
00627 $stores = $this->_getReadAdapter()->fetchAll("
00628 SELECT
00629 s.store_id, s.website_id, c.path AS root_path
00630 FROM
00631 {$this->getTable('core/store')} AS s,
00632 {$this->getTable('core/store_group')} AS sg,
00633 {$this->getTable('catalog/category')} AS c
00634 WHERE
00635 sg.group_id=s.group_id
00636 AND c.entity_id=sg.root_category_id
00637 {$storesCondition}
00638 ");
00639 foreach ($stores as $store) {
00640 $select = $this->_getReadAdapter()->select()
00641 ->from($this->getTable('catalog/category'), 'entity_id')
00642 ->where('path LIKE ?', "{$store['root_path']}/%")
00643 ->orWhere('path = ?', $store['root_path']);
00644 $_categories = $this->_getReadAdapter()->fetchAll($select);
00645 if (!$this->_getReadAdapter()->showTableStatus($this->getMainStoreTable($store['store_id']))) {
00646 $this->_createTable($store['store_id']);
00647 }
00648 $this->_getWriteAdapter()->delete(
00649 $this->getMainStoreTable($store['store_id']),
00650 $this->_getReadAdapter()->quoteInto('store_id = ?', $store['store_id'])
00651 );
00652 foreach ($_categories as $_category) {
00653 $_tmpCategory = Mage::getModel('catalog/category')
00654 ->setStoreId($store['store_id'])
00655 ->load($_category['entity_id']);
00656 $this->_synchronize($_tmpCategory, 'insert');
00657 }
00658 }
00659 $_tmpCategory = null;
00660 } elseif ($category instanceof Mage_Catalog_Model_Category) {
00661 foreach ($category->getStoreIds() as $storeId) {
00662 $_tmpCategory = Mage::getModel('catalog/category')
00663 ->setStoreId($storeId)
00664 ->load($category->getId());
00665 $_tmpCategory->setStoreId($storeId);
00666 $this->_synchronize($_tmpCategory);
00667 }
00668 $_tmpCategory = null;
00669 }
00670 return $this;
00671 }
00672
00673 public function removeStores($stores)
00674 {
00675 $this->_deleteTable($stores);
00676 return $this;
00677 }
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687 public function move($categoryId, $prevParentId, $parentId)
00688 {
00689 $_staticFields = array(
00690 'parent_id',
00691 'path',
00692 'level',
00693 'position',
00694 'children_count',
00695 'updated_at'
00696 );
00697 $prevParent = Mage::getModel('catalog/category')->load($prevParentId);
00698 $parent = Mage::getModel('catalog/category')->load($parentId);
00699 if ($prevParent->getStore()->getWebsiteId() != $parent->getStore()->getWebsiteId()) {
00700 foreach ($prevParent->getStoreIds() as $storeId) {
00701 $this->_getWriteAdapter()->delete(
00702 $this->getMainStoreTable($storeId),
00703 $this->_getReadAdapter()->quoteInto('entity_id = ?', $categoryId)
00704 );
00705 }
00706 $categoryPath = $this->_getReadAdapter()->fetchOne("
00707 SELECT
00708 path
00709 FROM
00710 {$this->getTable('catalog/category')}
00711 WHERE
00712 entity_id = '$categoryId'
00713 ");
00714 $select = $this->_getReadAdapter()->select()
00715 ->from($this->getTable('catalog/category'), 'entity_id')
00716 ->where('path LIKE ?', "$categoryPath/%")
00717 ->orWhere('path = ?', $categoryPath);
00718 $_categories = $this->_getReadAdapter()->fetchAll($select);
00719 foreach ($_categories as $_category) {
00720 foreach ($parent->getStoreIds() as $storeId) {
00721 $_tmpCategory = Mage::getModel('catalog/category')
00722 ->setStoreId($storeId)
00723 ->load($_category['entity_id']);
00724 $this->_synchronize($_tmpCategory);
00725 }
00726 }
00727 } else {
00728 foreach ($parent->getStoreIds() as $store) {
00729 $update = "UPDATE {$this->getMainStoreTable($store)}, {$this->getTable('catalog/category')} SET";
00730 foreach ($_staticFields as $field) {
00731 $update .= " {$this->getMainStoreTable($store)}.".$field."={$this->getTable('catalog/category')}.".$field.",";
00732 }
00733 $update = substr($update, 0, -1);
00734 $update .= " WHERE {$this->getMainStoreTable($store)}.entity_id = {$this->getTable('catalog/category')}.entity_id AND " .
00735 "({$this->getTable('catalog/category')}.path like '{$parent->getPath()}/%' OR " .
00736 "{$this->getTable('catalog/category')}.path like '{$prevParent->getPath()}/%')";
00737 $this->_getWriteAdapter()->query($update);
00738 }
00739 }
00740 $prevParent = null;
00741 $parent = null;
00742 $_tmpCategory = null;
00743
00744 return $this;
00745 }
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758 protected function _prepareDataForAllFields($category, $replaceFields = array())
00759 {
00760 $table = $this->_getReadAdapter()->describeTable($this->getMainStoreTable($category->getStoreId()));
00761 $data = array();
00762 foreach ($table as $column=>$columnData) {
00763 if (null !== $category->getData($column)) {
00764 if (key_exists($column, $replaceFields)) {
00765 $value = $category->getData($replaceFields[$column]);
00766 } else {
00767 $value = $category->getData($column);
00768 }
00769 if (is_array($value)) {
00770 $value = implode(',', $value);
00771 }
00772 $data[$column] = $value;
00773 }
00774 }
00775 return $data;
00776 }
00777
00778
00779
00780
00781
00782
00783
00784
00785 public function getChildrenAmount($category, $isActiveFlag = true)
00786 {
00787 $_table = $this->getMainStoreTable($category->getStoreId());
00788 $select = $this->_getReadAdapter()->select()
00789 ->from($_table, "COUNT({$_table}.entity_id)")
00790 ->where("{$_table}.path LIKE ?", $category->getPath() . '/%')
00791 ->where("{$_table}.is_active = ?", (int) $isActiveFlag);
00792 return (int) $this->_getReadAdapter()->fetchOne($select);
00793 }
00794
00795
00796
00797
00798
00799
00800
00801 public function getProductCount($category)
00802 {
00803 $select = $this->_getReadAdapter()->select()
00804 ->from($this->getTable('catalog/category_product'), "COUNT({$this->getTable('catalog/category_product')}.product_id)")
00805 ->where("{$this->getTable('catalog/category_product')}.category_id = ?", $category->getId())
00806 ->group("{$this->getTable('catalog/category_product')}.category_id");
00807 return (int) $this->_getReadAdapter()->fetchOne($select);
00808 }
00809
00810
00811
00812
00813
00814
00815
00816 public function getParentCategories($category, $isActive = true)
00817 {
00818 $categories = array();
00819 $select = $this->_getReadAdapter()->select()
00820 ->from(array('main_table' => $this->getMainStoreTable($category->getStoreId())), array('main_table.entity_id', 'main_table.name'))
00821 ->joinLeft(
00822 array('url_rewrite'=>$this->getTable('core/url_rewrite')),
00823 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND url_rewrite.product_id IS NULL AND url_rewrite.store_id="'.$category->getStoreId().'" AND url_rewrite.id_path LIKE "category/%"',
00824 array('request_path' => 'url_rewrite.request_path'))
00825 ->where('main_table.entity_id IN (?)', array_reverse(explode(',', $category->getPathInStore())));
00826 if ($isActive) {
00827 $select->where('main_table.is_active = ?', '1');
00828 }
00829 $select->order('main_table.path ASC');
00830 $result = $this->_getReadAdapter()->fetchAll($select);
00831 foreach ($result as $row) {
00832 $row['id'] = $row['entity_id'];
00833 $categories[$row['entity_id']] = Mage::getModel('catalog/category')->setData($row);
00834 }
00835 return $categories;
00836 }
00837
00838
00839
00840
00841
00842
00843
00844 public function getChildrenCategories($category)
00845 {
00846
00847
00848
00849
00850 $categories = $this->_loadNodes($category, 1, $category->getStoreId());
00851 return $categories;
00852 }
00853
00854
00855
00856
00857
00858
00859
00860 public function isInRootCategoryList($category)
00861 {
00862 $innerSelect = $this->_getReadAdapter()->select()
00863 ->from($this->getMainStoreTable($category->getStoreId()), new Zend_Db_Expr("CONCAT(path, '/%')"))
00864 ->where('entity_id = ?', Mage::app()->getStore()->getRootCategoryId());
00865 $select = $this->_getReadAdapter()->select()
00866 ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00867 ->where('entity_id = ?', $category->getId())
00868 ->where(new Zend_Db_Expr("path LIKE ({$innerSelect->__toString()})"));
00869 return (bool) $this->_getReadAdapter()->fetchOne($select);
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00879 public function getChildren($category, $recursive = true, $isActive = true)
00880 {
00881 $select = $this->_getReadAdapter()->select()
00882 ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00883 ->where('path LIKE ?', "{$category->getPath()}/%");
00884 if (!$recursive) {
00885 $select->where('level <= ?', $category->getLevel() + 1);
00886 }
00887 if ($isActive) {
00888 $select->where('is_active = ?', '1');
00889 }
00890 $_categories = $this->_getReadAdapter()->fetchAll($select);
00891 $categoriesIds = array();
00892 foreach ($_categories as $_category) {
00893 $categoriesIds[] = $_category['entity_id'];
00894 }
00895 return $categoriesIds;
00896 }
00897
00898
00899
00900
00901
00902
00903
00904 public function getAllChildren($category)
00905 {
00906 $categoriesIds = $this->getChildren($category);
00907 $myId = array($category->getId());
00908 $categoriesIds = array_merge($myId, $categoriesIds);
00909
00910 return $categoriesIds;
00911 }
00912
00913
00914
00915
00916
00917
00918
00919 public function checkId($id)
00920 {
00921 $select = $this->_getReadAdapter()->select()
00922 ->from($this->getMainStoreTable(), 'entity_id')
00923 ->where('entity_id=?', $id);
00924 return $this->_getReadAdapter()->fetchOne($select);
00925 }
00926
00927
00928
00929
00930
00931
00932
00933 public function getDesignUpdateData($category)
00934 {
00935 $categories = array();
00936 $pathIds = array();
00937 foreach (array_reverse($category->getParentIds()) as $pathId) {
00938 if ($pathId == Mage::app()->getStore()->getRootCategoryId()) {
00939 $pathIds[] = $pathId;
00940 break;
00941 }
00942 $pathIds[] = $pathId;
00943 }
00944 $select = $this->_getReadAdapter()->select()
00945 ->from(
00946 array('main_table' => $this->getMainStoreTable($category->getStoreId())),
00947 array(
00948 'main_table.entity_id',
00949 'main_table.custom_design',
00950 'main_table.custom_design_apply',
00951 'main_table.custom_design_from',
00952 'main_table.custom_design_to',
00953 )
00954 )
00955 ->where('main_table.entity_id IN (?)', $pathIds)
00956 ->where('main_table.is_active = ?', '1')
00957 ->order('main_table.path DESC');
00958 $result = $this->_getReadAdapter()->fetchAll($select);
00959 foreach ($result as $row) {
00960 $row['id'] = $row['entity_id'];
00961 $categories[$row['entity_id']] = Mage::getModel('catalog/category')->setData($row);
00962 }
00963 return $categories;
00964 }
00965
00966
00967
00968
00969
00970
00971
00972
00973 public function getAnchorsAbove(array $filterIds, $storeId = 0)
00974 {
00975 $select = $this->_getReadAdapter()->select()
00976 ->from(array('e' => $this->getMainStoreTable($storeId)), 'entity_id')
00977 ->where('is_anchor = ?', 1)
00978 ->where('entity_id IN (?)', $filterIds);
00979
00980 return $this->_getReadAdapter()->fetchCol($select);
00981 }
00982 }