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 class Mage_Catalog_Model_Resource_Eav_Mysql4_Category extends Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_tree;
00042
00043
00044
00045
00046
00047
00048 protected $_categoryProductTable;
00049
00050
00051
00052
00053
00054
00055 protected $_isActiveAttributeId = null;
00056
00057
00058
00059
00060
00061
00062 protected $_storeId = null;
00063
00064
00065
00066
00067 public function __construct()
00068 {
00069 $resource = Mage::getSingleton('core/resource');
00070 $this->setType('catalog_category')
00071 ->setConnection(
00072 $resource->getConnection('catalog_read'),
00073 $resource->getConnection('catalog_write')
00074 );
00075 $this->_categoryProductTable = $this->getTable('catalog/category_product');
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 public function setStoreId($storeId)
00085 {
00086 $this->_storeId = $storeId;
00087 return $this;
00088 }
00089
00090
00091
00092
00093
00094
00095 public function getStoreId()
00096 {
00097 if (is_null($this->_storeId)) {
00098 return Mage::app()->getStore()->getId();
00099 }
00100 return $this->_storeId;
00101 }
00102
00103
00104
00105
00106
00107
00108 protected function _getTree()
00109 {
00110 if (!$this->_tree) {
00111 $this->_tree = Mage::getResourceModel('catalog/category_tree')
00112 ->load();
00113 }
00114 return $this->_tree;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 protected function _beforeDelete(Varien_Object $object)
00126 {
00127 parent::_beforeDelete($object);
00128
00129
00130
00131
00132 $parentIds = $object->getParentIds();
00133 $childDecrease = $object->getChildrenCount() + 1;
00134 $this->_getWriteAdapter()->update(
00135 $this->getEntityTable(),
00136 array('children_count'=>new Zend_Db_Expr('`children_count`-'.$childDecrease)),
00137 $this->_getWriteAdapter()->quoteInto('entity_id IN(?)', $parentIds)
00138 );
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 $select = $this->_getWriteAdapter()->select()
00151 ->from($this->getEntityTable(), array('entity_id'))
00152 ->where($this->_getWriteAdapter()->quoteInto('`path` LIKE ?', $object->getPath().'/%'));
00153
00154 $childrenIds = $this->_getWriteAdapter()->fetchCol($select);
00155
00156 if (!empty($childrenIds)) {
00157 $this->_getWriteAdapter()->delete(
00158 $this->getEntityTable(),
00159 $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $childrenIds)
00160 );
00161 }
00162
00163
00164
00165
00166
00167 $object->setDeletedChildrenIds($childrenIds);
00168 return $this;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178 protected function _beforeSave(Varien_Object $object)
00179 {
00180 parent::_beforeSave($object);
00181
00182 if (!$object->getId()) {
00183 $object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
00184 $path = explode('/', $object->getPath());
00185 $level = count($path);
00186 $object->setLevel($level);
00187 if ($level) {
00188 $object->setParentId($path[$level - 1]);
00189 }
00190 $object->setPath($object->getPath() . '/');
00191
00192 $toUpdateChild = explode('/',$object->getPath());
00193
00194 $this->_getWriteAdapter()->update(
00195 $this->getEntityTable(),
00196 array('children_count'=>new Zend_Db_Expr('`children_count`+1')),
00197 $this->_getWriteAdapter()->quoteInto('entity_id IN(?)', $toUpdateChild)
00198 );
00199
00200 }
00201 return $this;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 protected function _afterSave(Varien_Object $object)
00212 {
00213
00214
00215
00216 if (substr($object->getPath(), -1) == '/') {
00217 $object->setPath($object->getPath() . $object->getId());
00218 $this->_savePath($object);
00219 }
00220
00221 $this->_saveCategoryProducts($object);
00222 return parent::_afterSave($object);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 protected function _savePath($object)
00232 {
00233 if ($object->getId()) {
00234 $this->_getWriteAdapter()->update(
00235 $this->getEntityTable(),
00236 array('path'=>$object->getPath()),
00237 $this->_getWriteAdapter()->quoteInto('entity_id=?', $object->getId())
00238 );
00239 }
00240 return $this;
00241 }
00242
00243 protected function _getMaxPosition($path)
00244 {
00245 $select = $this->getReadConnection()->select();
00246 $select->from($this->getTable('catalog/category'), 'MAX(position)');
00247 $select->where('path ?', new Zend_Db_Expr("regexp '{$path}/[0-9]+\$'"));
00248
00249 $result = 0;
00250 try {
00251 $result = (int) $this->getReadConnection()->fetchOne($select);
00252 } catch (Exception $e) {
00253
00254 }
00255 return $result;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 protected function _saveCategoryProducts($category)
00265 {
00266 $category->setIsChangedProductList(false);
00267
00268
00269
00270
00271 $products = $category->getPostedProducts();
00272
00273
00274
00275 if (is_null($products)) {
00276 return $this;
00277 }
00278
00279
00280
00281
00282 $oldProducts = $category->getProductsPosition();
00283
00284 $insert = array_diff_key($products, $oldProducts);
00285 $delete = array_diff_key($oldProducts, $products);
00286
00287
00288
00289 $update = array_intersect_key($products, $oldProducts);
00290
00291
00292
00293 $update = array_diff_assoc($update, $oldProducts);
00294
00295 $productTable = $this->getTable('catalog/product');
00296 $productUpdateSql = sprintf('UPDATE `%s` AS `e` SET `category_ids`=(SELECT
00297 GROUP_CONCAT(`category_id`) FROM `%s` AS `cp` WHERE `cp`.`product_id`=`e`.`entity_id`)
00298 WHERE `e`.`entity_id` IN(?)', $productTable, $this->_categoryProductTable);
00299
00300
00301
00302
00303
00304 if (!empty($delete)) {
00305 $deleteIds = array_keys($delete);
00306 $this->_getWriteAdapter()->delete($this->_categoryProductTable,
00307 $this->_getWriteAdapter()->quoteInto('product_id in(?)', $deleteIds) .
00308 $this->_getWriteAdapter()->quoteInto(' AND category_id=?', $category->getId())
00309 );
00310
00311 $sql = $this->_getWriteAdapter()->quoteInto($productUpdateSql, $deleteIds);
00312 $this->_getWriteAdapter()->query($sql);
00313 }
00314
00315
00316
00317
00318
00319 if (!empty($insert)) {
00320 $insertSql = array();
00321 foreach ($insert as $k => $v) {
00322 $insertSql[] = '('.(int)$category->getId().','.(int)$k.','.(int)$v.')';
00323 }
00324 $sql = sprintf(
00325 'INSERT INTO `%s` (`category_id`,`product_id`,`position`) VALUES%s',
00326 $this->_categoryProductTable,
00327 join(',', $insertSql)
00328 );
00329 $this->_getWriteAdapter()->query($sql);
00330
00331 $insertIds = array_keys($insert);
00332 $sql = $this->_getWriteAdapter()->quoteInto($productUpdateSql, $insertIds);
00333 $this->_getWriteAdapter()->query($sql);
00334 }
00335
00336
00337
00338
00339
00340 if (!empty($update)) {
00341 foreach ($update as $k => $v) {
00342 $cond = array(
00343 $this->_getWriteAdapter()->quoteInto('category_id=?', (int)$category->getId()),
00344 $this->_getWriteAdapter()->quoteInto('product_id=?', (int)$k)
00345 );
00346 $where = join(' AND ', $cond);
00347 $bind = array(
00348 'position' => (int)$v
00349 );
00350 $this->_getWriteAdapter()->update($this->_categoryProductTable, $bind, $where);
00351 }
00352 }
00353
00354 if (!empty($insert) || !empty($delete)) {
00355 $productIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
00356 Mage::dispatchEvent('catalog_category_change_products', array(
00357 'category' => $category,
00358 'product_ids' => $productIds
00359 ));
00360 }
00361
00362 if (!empty($insert) || !empty($update) || !empty($delete)) {
00363 $category->setIsChangedProductList(true);
00364 $categoryIds = explode('/', $category->getPath());
00365 $this->refreshProductIndex($categoryIds);
00366 }
00367
00368 return $this;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377 public function getStoreIds($category)
00378 {
00379 if (!$category->getId()) {
00380 return array();
00381 }
00382
00383 $nodePath = $this->_getTree()
00384 ->getNodeById($category->getId())
00385 ->getPath();
00386
00387 $nodes = array();
00388 foreach ($nodePath as $node) {
00389 $nodes[] = $node->getId();
00390 }
00391
00392 $stores = array();
00393 $storeCollection = Mage::getModel('core/store')->getCollection()->loadByCategoryIds($nodes);
00394 foreach ($storeCollection as $store) {
00395 $stores[$store->getId()] = $store->getId();
00396 }
00397
00398 $entityStoreId = $category->getStoreId();
00399 if (!in_array($entityStoreId, $stores)) {
00400 array_unshift($stores, $entityStoreId);
00401 }
00402 if (!in_array(0, $stores)) {
00403 array_unshift($stores, 0);
00404 }
00405 return $stores;
00406 }
00407
00408
00409
00410
00411
00412
00413
00414 public function getProductsPosition($category)
00415 {
00416 $select = $this->_getWriteAdapter()->select()
00417 ->from($this->_categoryProductTable, array('product_id', 'position'))
00418 ->where('category_id=?', $category->getId());
00419 $positions = $this->_getWriteAdapter()->fetchPairs($select);
00420 return $positions;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429 public function getChildrenCount($categoryId)
00430 {
00431 $select = $this->_getReadAdapter()->select()
00432 ->from($this->getEntityTable(), 'children_count')
00433 ->where('entity_id=?', $categoryId);
00434
00435 $child = $this->_getReadAdapter()->fetchOne($select);
00436
00437 return $child;
00438 }
00439
00440
00441
00442
00443
00444
00445
00446
00447 public function move($categoryId, $newParentId)
00448 {
00449 $category = Mage::getModel('catalog/category')->load($categoryId);
00450 $oldParent = $category->getParentCategory();
00451 $newParent = Mage::getModel('catalog/category')->load($newParentId);
00452
00453 $childrenCount = $this->getChildrenCount($category->getId()) + 1;
00454
00455
00456 $parentIds = explode('/', $newParent->getPath());
00457 $this->_getWriteAdapter()->update(
00458 $this->getEntityTable(),
00459 array('children_count' => new Zend_Db_Expr("`children_count` + {$childrenCount}")),
00460 $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $parentIds)
00461 );
00462
00463
00464 $parentIds = explode('/', $oldParent->getPath());
00465 $this->_getWriteAdapter()->update(
00466 $this->getEntityTable(),
00467 array('children_count' => new Zend_Db_Expr("`children_count` - {$childrenCount}")),
00468 $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $parentIds)
00469 );
00470
00471
00472 $this->_getWriteAdapter()->query("UPDATE
00473 {$this->getEntityTable()} SET parent_id = {$newParent->getId()}
00474 WHERE entity_id = {$categoryId}");
00475
00476 return $this;
00477 }
00478
00479
00480
00481
00482
00483
00484
00485 public function checkId($id)
00486 {
00487 $select = $this->_getReadAdapter()->select()
00488 ->from($this->getEntityTable(), 'entity_id')
00489 ->where('entity_id=?', $id);
00490 return $this->_getReadAdapter()->fetchOne($select);
00491 }
00492
00493
00494
00495
00496
00497
00498
00499 public function verifyIds(array $ids)
00500 {
00501 $validIds = array();
00502 $select = $this->_getWriteAdapter()->select()
00503 ->from($this->getEntityTable(), 'entity_id')
00504 ->where('entity_id IN(?)', $ids);
00505 $query = $this->_getWriteAdapter()->query($select);
00506 while ($row = $query->fetch()) {
00507 $validIds[] = $row['entity_id'];
00508 }
00509 return $validIds;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519 public function getChildrenAmount($category, $isActiveFlag = true)
00520 {
00521 $storeId = Mage::app()->getStore()->getId();
00522 $attributeId = $this->_getIsActiveAttributeId();
00523 $table = Mage::getSingleton('core/resource')->getTableName('catalog/category') . '_int';
00524
00525 $select = $this->_getReadAdapter()->select()
00526 ->from(array('m'=>$this->getEntityTable()), array('COUNT(m.entity_id)'))
00527 ->joinLeft(
00528 array('d'=>$table),
00529 "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id",
00530 array()
00531 )
00532 ->joinLeft(
00533 array('c'=>$table),
00534 "c.attribute_id = '{$attributeId}' AND c.store_id = '{$storeId}' AND c.entity_id = m.entity_id",
00535 array()
00536 )
00537 ->where('m.path like ?', $category->getPath() . '/%')
00538 ->where('(IFNULL(c.value, d.value) = ?)', $isActiveFlag);
00539
00540 return $this->_getReadAdapter()->fetchOne($select);
00541 }
00542
00543
00544
00545
00546
00547
00548 protected function _getIsActiveAttributeId()
00549 {
00550 if (is_null($this->_isActiveAttributeId)) {
00551 $select = $this->_getReadAdapter()->select()
00552 ->from(array('a'=>$this->getTable('eav/attribute')), array('attribute_id'))
00553 ->join(array('t'=>$this->getTable('eav/entity_type')), 'a.entity_type_id = t.entity_type_id')
00554 ->where('entity_type_code = ?', 'catalog_category')
00555 ->where('attribute_code = ?', 'is_active');
00556
00557 $this->_isActiveAttributeId = $this->_getReadAdapter()->fetchOne($select);
00558 }
00559 return $this->_isActiveAttributeId;
00560 }
00561
00562
00563
00564
00565
00566
00567
00568 public function refreshProductIndex($categoryIds = array(), $productIds = array(), $storeIds = array())
00569 {
00570
00571
00572
00573 $statusAttribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'status');
00574 $visibilityAttribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'visibility');
00575 $statusAttributeId = $statusAttribute->getId();
00576 $visibilityAttributeId = $visibilityAttribute->getId();
00577 $statusTable = $statusAttribute->getBackend()->getTable();
00578 $visibilityTable = $visibilityAttribute->getBackend()->getTable();
00579
00580
00581
00582
00583 $select = $this->_getReadAdapter()->select()
00584 ->from($this->getTable('catalog/category'))
00585 ->order('level')
00586 ->order('path');
00587
00588 if (is_array($categoryIds) && !empty($categoryIds)) {
00589 $select->where('entity_id IN (?)', $categoryIds);
00590 } elseif (is_numeric($categoryIds)) {
00591 $select->where('entity_id=?', $categoryIds);
00592 }
00593
00594 $categories = $this->_getWriteAdapter()->fetchAll($select);
00595
00596 $storesCondition = '';
00597 if (!empty($storeIds)) {
00598 $storesCondition = $this->_getWriteAdapter()->quoteInto(
00599 ' AND s.store_id IN (?)', $storeIds
00600 );
00601 }
00602
00603
00604
00605
00606 $stores = $this->_getWriteAdapter()->fetchAll("
00607 SELECT
00608 s.store_id, s.website_id, c.path AS root_path
00609 FROM
00610 {$this->getTable('core/store')} AS s,
00611 {$this->getTable('core/store_group')} AS sg,
00612 {$this->getTable('catalog/category')} AS c
00613 WHERE
00614 sg.group_id=s.group_id
00615 AND c.entity_id=sg.root_category_id
00616 {$storesCondition}
00617 ");
00618
00619 $indexTable = $this->getTable('catalog/category_product_index');
00620
00621 foreach ($stores as $storeData) {
00622 $storeId = $storeData['store_id'];
00623 $websiteId = $storeData['website_id'];
00624 $rootPath = $storeData['root_path'];
00625
00626 $productCondition = '';
00627 if (!empty($productIds)) {
00628 $productCondition = $this->_getWriteAdapter()->quoteInto(
00629 ' AND product_id IN (?)', $productIds
00630 );
00631 }
00632 $insProductCondition = str_replace('product_id', 'cp.product_id', $productCondition);
00633
00634 foreach ($categories as $category) {
00635 $categoryId = $category['entity_id'];
00636 $path = $category['path'];
00637
00638 $this->_getWriteAdapter()->delete(
00639 $indexTable,
00640 'category_id='.$categoryId. ' AND store_id='.$storeId.$productCondition
00641 );
00642
00643 if (strpos($path.'/', $rootPath.'/') === false) {
00644 continue;
00645 }
00646
00647 $query = "INSERT INTO {$indexTable}
00648 (`category_id`, `product_id`, `position`, `is_parent`, `store_id`, `visibility`)
00649 SELECT
00650 {$categoryId},
00651 cp.product_id,
00652 cp.position,
00653 MAX({$categoryId}=cp.category_id) as is_parent,
00654 {$storeId},
00655 IFNULL(t_v.value, t_v_default.value)
00656 FROM
00657 {$this->getTable('catalog/category_product')} AS cp
00658 INNER JOIN {$this->getTable('catalog/product_website')} AS pw
00659 ON pw.product_id=cp.product_id AND pw.website_id={$websiteId}
00660 INNER JOIN {$visibilityTable} AS `t_v_default`
00661 ON (t_v_default.entity_id = cp.product_id)
00662 AND (t_v_default.attribute_id='{$visibilityAttributeId}')
00663 AND t_v_default.store_id=0
00664 LEFT JOIN {$visibilityTable} AS `t_v`
00665 ON (t_v.entity_id = cp.product_id)
00666 AND (t_v.attribute_id='{$visibilityAttributeId}')
00667 AND (t_v.store_id='{$storeId}')
00668 INNER JOIN {$statusTable} AS `t_s_default`
00669 ON (t_s_default.entity_id = cp.product_id)
00670 AND (t_s_default.attribute_id='{$statusAttributeId}')
00671 AND t_s_default.store_id=0
00672 LEFT JOIN {$statusTable} AS `t_s`
00673 ON (t_s.entity_id = cp.product_id)
00674 AND (t_s.attribute_id='{$statusAttributeId}')
00675 AND (t_s.store_id='{$storeId}')
00676 WHERE category_id IN(
00677 SELECT entity_id FROM {$this->getTable('catalog/category')}
00678 WHERE entity_id = {$category['entity_id']} OR path LIKE '{$path}/%')
00679 AND (IFNULL(t_s.value, t_s_default.value)=".Mage_Catalog_Model_Product_Status::STATUS_ENABLED.")
00680 {$insProductCondition}
00681 GROUP BY product_id
00682 ORDER BY is_parent desc";
00683
00684 $this->_getWriteAdapter()->query($query);
00685 }
00686 }
00687 return $this;
00688 }
00689
00690 public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue)
00691 {
00692 $select = $this->_getReadAdapter()->select()
00693 ->from($attribute->getBackend()->getTable(), array('entity_id'))
00694 ->where('attribute_id = ?', $attribute->getId())
00695 ->where('value = ?', $expectedValue)
00696 ->where('entity_id in (?)', $entityIdsFilter);
00697
00698 return $this->_getReadAdapter()->fetchCol($select);
00699 }
00700
00701
00702
00703
00704
00705
00706
00707 public function getProductCount($category)
00708 {
00709 $productTable =Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
00710
00711 $select = $this->getReadConnection()->select();
00712 $select->from(
00713 array('main_table'=>$productTable),
00714 array(new Zend_Db_Expr('COUNT(main_table.product_id)'))
00715 )
00716 ->where('main_table.category_id = ?', $category->getId())
00717 ->group('main_table.category_id');
00718
00719 $counts =$this->getReadConnection()->fetchOne($select);
00720
00721 return intval($counts);
00722 }
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 protected function _saveCountChidren($object)
00738 {
00739 $chidren = $object->getChildren();
00740 if (strlen($chidren)>0) {
00741 $chidrenCount = count(explode(',', $chidren));
00742 } else {
00743 $chidrenCount = 0;
00744 }
00745 $this->_getWriteAdapter()->update($this->getEntityTable(),
00746 array('children_count'=>$chidrenCount),
00747 $this->_getWriteAdapter()->quoteInto('entity_id=?', $object->getId())
00748 );
00749
00750 return $this;
00751 }
00752
00753
00754
00755
00756
00757
00758
00759 protected function _saveInStores(Varien_Object $object)
00760 {
00761 if (!$object->getMultistoreSaveFlag()) {
00762 $stores = $object->getStoreIds();
00763 foreach ($stores as $storeId) {
00764 if ($object->getStoreId() != $storeId) {
00765 $newObject = clone $object;
00766 $newObject->setStoreId($storeId)
00767 ->setMultistoreSaveFlag(true)
00768 ->save();
00769 }
00770 }
00771 }
00772 return $this;
00773 }
00774
00775
00776
00777
00778 protected function _updateCategoryPath($category, $path)
00779 {
00780 return $this;
00781 if ($category->getNotUpdateDepends()) {
00782 return $this;
00783 }
00784 foreach ($path as $pathItem) {
00785 if ($pathItem->getId()>1 && $category->getId() != $pathItem->getId()) {
00786 $category = Mage::getModel('catalog/category')
00787 ->load($pathItem->getId())
00788 ->save();
00789 }
00790 }
00791 return $this;
00792 }
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 public function getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
00805 {
00806 $tree = Mage::getResourceModel('catalog/category_tree');
00807
00808 $nodes = $tree->loadNode($parent)
00809 ->loadChildren($recursionLevel)
00810 ->getChildren();
00811
00812 $tree->addCollectionData(null, $sorted, $parent, $toLoad, true);
00813
00814 if ($asCollection) {
00815 return $tree->getCollection();
00816 }
00817 return $nodes;
00818 }
00819
00820
00821
00822
00823
00824
00825
00826 public function getParentCategories($category)
00827 {
00828 $pathIds = array_reverse(explode(',', $category->getPathInStore()));
00829 $categories = Mage::getResourceModel('catalog/category_collection')
00830 ->setStore(Mage::app()->getStore())
00831 ->addAttributeToSelect('name')
00832 ->addAttributeToSelect('url_key')
00833 ->addFieldToFilter('entity_id', array('in'=>$pathIds))
00834 ->addFieldToFilter('is_active', 1)
00835 ->load()
00836 ->getItems();
00837 return $categories;
00838 }
00839
00840
00841
00842
00843
00844
00845
00846 public function getChildrenCategories($category)
00847 {
00848 $collection = $category->getCollection();
00849
00850 $collection->addAttributeToSelect('url_key')
00851 ->addAttributeToSelect('name')
00852 ->addAttributeToSelect('all_children')
00853 ->addAttributeToSelect('is_anchor')
00854 ->addAttributeToFilter('is_active', 1)
00855 ->addIdFilter($category->getChildren())
00856 ->setOrder('position', 'ASC')
00857 ->joinUrlRewrite()
00858 ->load();
00859 return $collection;
00860 }
00861
00862
00863
00864
00865
00866
00867
00868
00869 public function getChildren($category, $recursive = true)
00870 {
00871 $attributeId = $this->_getIsActiveAttributeId();
00872 $select = $this->_getReadAdapter()->select()
00873 ->from(array('m' => $this->getEntityTable()), 'entity_id')
00874 ->joinLeft(
00875 array('d' => $this->getEntityTable() . '_int'),
00876 "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id",
00877 array()
00878 )
00879 ->joinLeft(
00880 array('c' => $this->getEntityTable() . '_int'),
00881 "c.attribute_id = '{$attributeId}' AND c.store_id = '{$category->getStoreId()}' AND c.entity_id = m.entity_id",
00882 array()
00883 )
00884 ->where('(IFNULL(c.value, d.value) = ?)', '1')
00885 ->where('path LIKE ?', "{$category->getPath()}/%");
00886 if (!$recursive) {
00887 $select->where('level <= ?', $category->getLevel() + 1);
00888 }
00889 $_categories = $this->_getReadAdapter()->fetchAll($select);
00890 $categoriesIds = array();
00891 foreach ($_categories as $_category) {
00892 $categoriesIds[] = $_category['entity_id'];
00893 }
00894
00895 return $categoriesIds;
00896
00897
00898
00899 }
00900
00901
00902
00903
00904
00905
00906
00907 public function getAllChildren($category)
00908 {
00909 $children = $this->getChildren($category);
00910 $myId = array($category->getId());
00911 $children = array_merge($myId, $children);
00912
00913 return $children;
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926 }
00927
00928
00929
00930
00931
00932
00933
00934 public function isInRootCategoryList($category)
00935 {
00936 $innerSelect = $this->_getReadAdapter()->select()
00937 ->from($this->getEntityTable(), new Zend_Db_Expr("CONCAT(path, '/%')"))
00938 ->where('entity_id = ?', Mage::app()->getStore()->getRootCategoryId());
00939 $select = $this->_getReadAdapter()->select()
00940 ->from($this->getEntityTable(), 'entity_id')
00941 ->where('entity_id = ?', $category->getId())
00942 ->where(new Zend_Db_Expr("path LIKE ({$innerSelect->__toString()})"));
00943 return (bool) $this->_getReadAdapter()->fetchOne($select);
00944
00945
00946
00947
00948
00949
00950
00951
00952 }
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962 public function isForbiddenToDelete($categoryId)
00963 {
00964 $select = $this->_getReadAdapter()->select()
00965 ->from($this->getTable('core/store_group'), array('group_id'))
00966 ->where('root_category_id = ?', $categoryId);
00967 if ($this->_getReadAdapter()->fetchOne($select)) {
00968 return true;
00969 }
00970 return false;
00971 }
00972 }