public function export() { $category = Category::getInstanceByID($this->getRequestCategory()); $category->load(); $filter = select(gte(f('Category.lft'), $category->lft->get()), lte(f('Category.rgt'), $category->rgt->get())); $categories = ActiveRecordModel::getRecordsetArray('Category', $filter); $this->categories = array(); foreach (array_merge($category->getPathNodeArray(), $categories) as $cat) { $this->categories[$cat['ID']] = $cat; } return parent::export(); }
public static function loadCategoryPathsForArray(&$productArray) { foreach ($productArray as $product) { $cond = lte('Category.lft', $product['Category']['lft']); $cond->addAnd(gte('Category.rgt', $product['Category']['rgt'])); $conditions[] = $cond; } $filter = select(Condition::mergeFromArray($conditions, true)); $filter->setOrder(f('Category.lft')); $categories = ActiveRecord::getRecordSetArray('Category', $filter); foreach ($productArray as &$product) { $product['Categories'] = array(); $names = array(); foreach ($categories as &$category) { if ($category['lft'] <= $product['Category']['lft'] && $category['rgt'] >= $product['Category']['rgt']) { $product['Categories'][] =& $category; $names[] = $category['name_lang']; } } array_shift($names); $product['category_path'] = implode(' > ', $names); $product['category_path_slash'] = implode(' / ', $names); $product['category_name'] = count($names) > 0 ? array_pop($names) : ''; } }
public function removeUnnecessaryCategoriesFromSnapshot(ClonedStore $store) { $conds = array(); foreach ($store->getRelatedRecordSet('ClonedStoreCategory', select(), 'Category') as $storeCat) { $cat = $storeCat->category->get(); if (1 == $cat->getID()) { continue; } $cat->load(); $cond = lte(f('Category.lft'), $cat->lft->get()); $cond->addAND(gte(f('Category.rgt'), $cat->rgt->get())); $conds[] = $cond; } $this->flush(__LINE__); if (!$conds) { return; } $this->flush(__LINE__); $cond = new OrChainCondition($conds); $ids = array(); foreach (ActiveRecordModel::getFieldValues('Category', select($cond), array('ID')) as $id) { $ids[] = array_shift($id); } $idstr = 'IN (' . implode(', ', $ids) . ')'; // delete unused extra category references // ActiveRecord::executeUpdate('DELETE FROM ' . $this->importDatabase . '.ProductCategory WHERE categoryID NOT ' . $idstr); $this->flush(__LINE__); //var_dump('before: ' . array_shift(array_shift(ActiveRecord::getDataBySQL('SELECT COUNT(*) FROM ' . $this->importDatabase . '.Product')))); ActiveRecord::executeUpdate('DELETE FROM ' . $this->importDatabase . '.Product WHERE (categoryID NOT ' . $idstr . ') AND ((SELECT COUNT(*) FROM ' . $this->importDatabase . '.ProductCategory WHERE productID=Product.ID AND (ProductCategory.categoryID ' . $idstr . ')) = 0)'); //echo 'DELETE FROM ' . $this->importDatabase . '.Product WHERE (categoryID NOT ' . $idstr . ') AND ((SELECT COUNT(*) FROM ' . $this->importDatabase . '.ProductCategory WHERE productID=Product.ID AND (ProductCategory.categoryID ' . $idstr . ')) = 0)'; //var_dump('AFTER: ' . array_shift(array_shift(ActiveRecord::getDataBySQL('SELECT COUNT(*) FROM ' . $this->importDatabase . '.Product')))); //die('test'); $this->flush(__LINE__); // fix main category ActiveRecord::executeUpdate('UPDATE ' . $this->importDatabase . '.Product SET categoryID=(SELECT categoryID FROM ' . $this->importDatabase . '.ProductCategory WHERE productID=Product.ID AND ProductCategory.categoryID IN (' . implode(',', $ids) . ') LIMIT 1) WHERE (parentID IS NULL) AND (categoryID NOT ' . $idstr . ')'); $this->flush(__LINE__); // remove redundant extra categories ActiveRecord::executeUpdate('DELETE ' . $this->importDatabase . '.ProductCategory FROM ' . $this->importDatabase . '.ProductCategory LEFT JOIN ' . $this->importDatabase . '.Product ON ProductCategory.productID=Product.ID WHERE ProductCategory.categoryID=Product.categoryID'); $this->flush(__LINE__); // delete unused products ActiveRecord::executeUpdate('DELETE FROM ' . $this->importDatabase . '.Product WHERE (categoryID IS NULL) AND (parentID IS NULL)'); $this->flush(__LINE__); // get all remaining categories $sql = 'SELECT lft, rgt FROM ' . $this->importDatabase . '.Category WHERE ID IN (SELECT categoryID FROM ' . $this->importDatabase . '.ProductCategory)'; $conds = array(); foreach (ActiveRecordModel::getDataBySQL($sql) as $row) { $cond = lte(f('Category.lft'), $row['lft']); $cond->addAND(gte(f('Category.rgt'), $row['rgt'])); $conds[] = $cond; } $this->flush(__LINE__); $cond = new OrChainCondition($conds); $ids = array(); foreach (ActiveRecordModel::getFieldValues('Category', select($cond), array('ID')) as $id) { $ids[] = array_shift($id); } $this->flush(__LINE__); $allidstr = 'IN (' . implode(', ', $ids) . ')'; // delete unused categories ActiveRecord::executeUpdate('DELETE FROM ' . $this->importDatabase . '.Category WHERE (ID<>1) AND (ID NOT ' . $idstr . ') AND (ID NOT ' . $allidstr . ')'); $this->flush(__LINE__); // disable/enable existing products according to category selection $store->addQueuedQuery('UPDATE Category SET isEnabled=0 WHERE (ID NOT ' . $idstr . ') AND (ID NOT ' . $allidstr . ')'); $store->addQueuedQuery('UPDATE Category SET isEnabled=1 WHERE ((ID ' . $idstr . ') OR (ID ' . $allidstr . ')) AND (COALESCE(LOCATE("|isEnabled|", protectedFields), 0) = 0)'); $this->flush(__LINE__); $store->addQueuedQuery('UPDATE Product SET isEnabled=0 WHERE (parentID IS NULL) AND (categoryID NOT ' . $idstr . ') AND (categoryID NOT ' . $allidstr . ')'); //$store->addQueuedQuery('UPDATE Product SET isEnabled=1 WHERE (parentID IS NULL) AND ((categoryID ' . $idstr . ') OR (categoryID ' . $allidstr . ')) AND (COALESCE(LOCATE("|isEnabled|", protectedFields), 0) = 0)'); $this->flush(__LINE__); }