private function createCategory(Category $parent, $name) { $cat = Category::getNewInstance($parent); $cat->setValueByLang('name', null, $name); $cat->isEnabled->set(true); $cat->save(); return $cat; }
/** * Creates a new category record * * @role !category.create * * @return ActionRedirectResponse */ public function create() { $parent = Category::getRequestInstance($this->request, 'parent'); $categoryNode = Category::getNewInstance($parent); $categoryNode->loadRequestModel($this->request); $categoryNode->save(); return new JSONResponse($this->getCategoryJson($categoryNode->toArray()), 'success', $this->translate('_new_category_was_successfully_created')); }
/** * Creates a new category record * * @role !category.create * * @return ActionRedirectResponse */ public function create() { $parent = Category::getInstanceByID((int) $this->request->get("id")); $categoryNode = Category::getNewInstance($parent); $categoryNode->setValueByLang("name", $this->application->getDefaultLanguageCode(), 'dump'); $categoryNode->save(); $categoryNode->setValueByLang("name", $this->application->getDefaultLanguageCode(), $this->translate("_new_category") . " " . $categoryNode->getID()); $categoryNode->save(); return new JSONResponse($categoryNode->toArray(), 'success'); }
public function create() { $parser = $this->getParser(); $request = $this->application->getRequest(); $parser->loadDataInRequest($request); $parentId = $request->get(Category::PARENT_NODE_FIELD_NAME); $parentCategory = $parentId > 0 ? $this->_getCategoryById($parentId, true) : $this->root; $category = Category::getNewInstance($parentCategory); $category->loadRequestData($request); $category->save(); return $this->statusResponse($category->getID(), 'created'); }
public function setUp() { parent::setUp(); Category::recalculateProductsCount(); $this->root = Category::getNewInstance(Category::getRootNode()); $this->root->save(); for ($k = 1; $k <= 2; $k++) { $cat = Category::getNewInstance($this->root); $cat->save(); $this->categories[$k] = $cat; } $this->product = Product::getNewInstance($this->categories[1]); $this->product->save(); $this->secondCategory = ProductCategory::getNewInstance($this->product, $this->categories[2]); $this->secondCategory->save(); }
public function testProductRatingTypes() { $subCategory = Category::getNewInstance(Category::getRootNode()); $subCategory->save(); $product = Product::getNewInstance($subCategory, 'test'); $product->save(); $rootType = ProductRatingType::getNewInstance(Category::getRootNode()); $rootType->save(); $subType = ProductRatingType::getNewInstance($subCategory); $subType->save(); $types = ProductRatingType::getProductRatingTypes($product); $this->assertEqual($types->size(), 2); // parent category types should go first $this->assertSame($types->get(0), $rootType); $this->assertSame($types->get(1), $subType); }
private function _testStringSerialize($string, $equals = true) { error_reporting(E_WARNING); $root = Category::getInstanceByID(1); $new = Category::getNewInstance($root); $new->setValueByLang('name', 'en', $string); $new->save(); ActiveRecordModel::clearPool(); $restored = Category::getInstanceByID($new->getID(), Category::LOAD_DATA); if ($equals) { $this->assertEqual($string, $restored->getValueByLang('name', 'en')); } else { $this->assertNotEquals($string, $restored->getValueByLang('name', 'en')); } error_reporting(E_ALL); }
public function testDelete() { $category = Category::getNewInstance(Category::getRootNode()); $category->save(); $field = SpecField::getNewInstance($category, SpecField::DATATYPE_TEXT, SpecField::TYPE_TEXT_SIMPLE); $field->handle->set('randomhandle'); $field->save(); $this->request->set('id', $field->getID()); $response = $this->controller->delete(); $this->assertIsA($response, 'JSONResponse'); $value = $response->getValue(); $this->assertEqual($value['status'], 'success'); // already deleted $response = $this->controller->delete(); $value = $response->getValue(); $this->assertEqual($value['status'], 'failure'); }
public function getNextCategory() { if (is_null($this->categoryMap)) { $join = $langs = array(); foreach ($this->languages as $id => $code) { list($join[], $langs[]) = $this->joinCategoryFields($id, $code); } // get all categories $sql = 'SELECT ' . $this->getTablePrefix() . 'categories.* ' . ($langs ? ',' : '') . implode(', ', $langs) . ' FROM ' . $this->getTablePrefix() . 'categories ' . implode(' ', $join) . ' ORDER BY order_by ASC'; foreach ($this->getDataBySQL($sql) as $category) { $this->categoryMap[$category['categoryid']] = $category; } // get level for each category foreach ($this->categoryMap as $id => &$category) { $level = 0; while ($id != 0 && $level < 100) { $level++; if (isset($this->categoryMap[$id]['parentid'])) { $id = $this->categoryMap[$id]['parentid']; } else { if ($this->categoryMap[$id]['parentid'] != 0) { unset($this->categoryMap[$id]); $level = 101; } } } // circular reference if ($level >= 100) { unset($this->categoryMap[$category['categoryid']]); } else { $category['level'] = $level; } } usort($this->categoryMap, array($this, 'sortCategories')); } // root level categories first if ($data = array_shift($this->categoryMap)) { $parentNode = 0 == $data['parentid'] ? Category::getRootNode() : Category::getInstanceById($this->getRealId('Category', $data['parentid'])); $rec = Category::getNewInstance($parentNode); } else { return null; } $rec->setID($data['categoryid']); $rec->isEnabled->set('Y' == $data['avail']); $rec->keywords->set($data['meta_keywords']); $rec->setValueByLang('name', $this->defLang, $data['category']); $rec->setValueByLang('description', $this->defLang, $data['description']); foreach ($this->languages as $code => $id) { $rec->setValueByLang('name', $code, $data['category_' . $code]); $rec->setValueByLang('description', $code, $data['description_' . $code]); } //images $images = $this->getDataBySQL('SELECT * FROM ' . $this->getTablePrefix() . 'images_c WHERE id=' . $data['categoryid'] . ' ORDER BY orderby ASC'); foreach ($images as $image) { $this->importCategoryImage($rec, $this->path . '/' . $image['image_path']); } $rec->rawData = $data; return $rec; }
public function getNextCategory() { if (is_null($this->categoryMap)) { $join = $langs = array(); foreach ($this->languages as $id => $code) { list($join[], $langs[]) = $this->joinCategoryFields($id, $code); } // get all categories $q = 'SELECT *,' . $this->getTablePrefix() . 'categories.categories_id AS categories_id,' . implode(', ', $langs) . ' FROM ' . $this->getTablePrefix() . 'categories ' . implode(' ', $join) . ' ORDER BY sort_order ASC'; foreach ($this->getDataBySQL($q) as $category) { $this->categoryMap[$category['categories_id']] = $category; } // get level for each category foreach ($this->categoryMap as $id => &$category) { $level = 0; while ($id != 0 && $level < 100) { $level++; if (isset($this->categoryMap[$id]['parent_id'])) { $id = $this->categoryMap[$id]['parent_id']; } else { if ($this->categoryMap[$id]['parent_id'] != 0) { unset($this->categoryMap[$id]); $level = 101; } } } // circular reference if ($level >= 100) { unset($this->categoryMap[$category['categories_id']]); } else { $category['level'] = $level; } } usort($this->categoryMap, array($this, 'sortCategories')); } // root level categories first if ($data = array_shift($this->categoryMap)) { $parentNode = 0 == $data['parent_id'] ? Category::getRootNode() : Category::getInstanceById($this->getRealId('Category', $data['parent_id'])); $rec = Category::getNewInstance($parentNode); } else { return null; } $rec->setID($data['categories_id']); $rec->isEnabled->set(true); foreach ($this->languages as $code) { $rec->setValueByLang('name', $code, $data['name_' . $code]); } //product image if ($data['categories_image']) { $this->importCategoryImage($rec, $this->path . '/images/' . $data['categories_image']); } $rec->rawData = $data; return $rec; }
private function getCategoryByPath($profile, $names) { if (!is_array($names)) { $names = explode(' / ', $names); } $hash = ''; $hashRoot = $this->getRoot($profile)->getID(); $cat = null; foreach ($names as $name) { $hash .= "\n" . $name; if (!isset($this->categories[$hash])) { $f = Category::getInstanceByID($hashRoot)->getSubcategoryFilter(); $f->mergeCondition(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('Category', 'name'), $this->application->getDefaultLanguageCode()), $name)); $cat = ActiveRecordModel::getRecordSet('Category', $f)->get(0); if (!$cat) { $cat = Category::getNewInstance(Category::getInstanceByID($hashRoot)); $cat->isEnabled->set(true); $cat->setValueByLang('name', $this->application->getDefaultLanguageCode(), $name); $cat->save(); } $this->categories[$hash] = $cat->getID(); } $hashRoot = $this->categories[$hash]; $cat = Category::getInstanceByID($hashRoot, true); } return $cat; }
public function testActiveProductCountWhenChangingCategoryAvailability() { $this->root->reload(); $rootActiveProductCount = $this->root->activeProductCount->get(); $rootAotalProductCount = $this->root->totalProductCount->get(); $rootAvailableProductCount = $this->root->availableProductCount->get(); $subCategory = Category::getNewInstance($this->root); $subCategory->setValueByLang("name", 'en', "New Category 1"); $subCategory->isEnabled->set(false); $subCategory->activeProductCount->set(1); $subCategory->totalProductCount->set(1); $subCategory->availableProductCount->set(1); $subCategory->save(); $this->root->reload(); $this->assertEqual($rootActiveProductCount, $this->root->activeProductCount->get()); $subCategory->isEnabled->set(true); $subCategory->save(); $this->root->reload(); $this->assertEqual($rootActiveProductCount + 1, $this->root->activeProductCount->get()); }
/** * Enabled product, with some stock - the numbers should increase by one */ public function testCategoryCountsWhenEnabledProductWithNoStockIsAdded() { $secondCategory = Category::getNewInstance($this->productCategory); $secondCategory->save(); $product = Product::getNewInstance($secondCategory); $product->isEnabled->set(1); $product->stockCount->set(0); $product->save(); ActiveRecordModel::removeFromPool($secondCategory); $sameCategory = Category::getInstanceByID($secondCategory->getID(), true); $this->assertEqual((int) $secondCategory->activeProductCount->get() + 1, (int) $sameCategory->activeProductCount->get()); $this->assertEqual((int) $secondCategory->availableProductCount->get(), (int) $sameCategory->availableProductCount->get()); // now add some stock, so available product count should increase by one $product->stockCount->set(5); $product->save(); ActiveRecordModel::removeFromPool($sameCategory); $sameCategory = Category::getInstanceByID($secondCategory->getID(), true); $this->assertEqual((int) $secondCategory->availableProductCount->get() + 1, (int) $sameCategory->availableProductCount->get()); }
/** * @todo: doesn't work */ public function testAdditionalCategories() { $customCategory = Category::getNewInstance(Category::getRootNode()); $customCategory->save(); $product = $this->product1; $this->order->addProduct($product, 1, true); $this->order->save(); $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->conditionClass->set('RuleConditionContainsProduct'); $condition->save(); DiscountConditionRecord::getNewInstance($condition, $customCategory)->save(); $this->assertEquals(0, count($this->order->getDiscountConditions(true))); ProductCategory::getNewInstance($product, $customCategory)->save(); $condition->loadAll(); //$this->assertEquals(1, count($this->order->getDiscountConditions(true))); }
public function testDiscountByAdditionalCategories() { $product = $this->products[1]; $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->save(); $actionCondition = DiscountCondition::getNewInstance(); $actionCondition->isEnabled->set(true); $actionCondition->conditionClass->set('RuleConditionContainsProduct'); $actionCondition->save(); $action = DiscountAction::getNewInstance($condition); $action->actionCondition->set($actionCondition); $action->isEnabled->set(true); $action->type->set(DiscountAction::TYPE_ITEM_DISCOUNT); $action->amount->set(10); $action->actionClass->set('RuleActionPercentageDiscount'); $action->save(); $randomCategory = Category::getNewInstance(Category::getRootNode()); $randomCategory->save(); DiscountConditionRecord::getNewInstance($actionCondition, $randomCategory)->save(); $this->order->addProduct($product, 1, true); $this->order->save(); Category::recalculateProductsCount(); $product->reload(); $this->assertFalse(RuleCondition::create($actionCondition)->isProductMatching($product)); $customCategory = Category::getNewInstance(Category::getRootNode()); $customCategory->save(); ProductCategory::getNewInstance($product, $customCategory)->save(); DiscountConditionRecord::getNewInstance($actionCondition, $customCategory)->save(); Category::recalculateProductsCount(); $product->reload(); $actionCondition->loadAll(); $this->assertTrue(RuleCondition::create($actionCondition)->isProductMatching($product)); $this->assertEquals(count($this->order->getDiscountActions(true)), 1); $this->assertEquals($this->products[1]->getPrice($this->usd) * 0.9, $this->order->getTotal(true)); ActiveRecordModel::clearPool(); $order = CustomerOrder::getInstanceByID($this->order->getID()); $order->loadAll(); $this->assertEquals($this->products[1]->getPrice($this->usd) * 0.9, $this->order->getTotal(true)); }
public function getNextCategory() { if (is_null($this->categoryMap)) { // get all categories $sql = 'SELECT * FROM ' . $this->getTablePrefix() . 'categories WHERE categoryID > 1'; foreach ($this->getDataBySQL($sql) as $category) { $this->categoryMap[$category['categoryID']] = $category; } // get level for each category foreach ($this->categoryMap as $id => &$category) { if (1 == $category['parent']) { $category['parent'] = 0; } $level = 0; while ($id != 0 && $level < 100) { $level++; if (isset($this->categoryMap[$id]['parent'])) { $id = $this->categoryMap[$id]['parent']; } else { if (!isset($this->categoryMap[$id]) || $this->categoryMap[$id]['parent'] != 0) { unset($this->categoryMap[$id]); $level = 101; } } } // circular reference if ($level >= 100) { unset($this->categoryMap[$category['categoryID']]); } else { $category['level'] = $level; } } $this->categoryIds = array_keys($this->categoryMap); usort($this->categoryMap, array($this, 'sortCategories')); } // root level categories first if ($data = array_shift($this->categoryMap)) { $parentNode = 0 == $data['parent'] ? Category::getRootNode() : Category::getInstanceById($this->getRealId('Category', $data['parent'])); $rec = Category::getNewInstance($parentNode); } else { return null; } $rec->setID($data['categoryID']); $rec->isEnabled->set(true); $rec->keywords->set($data['meta_keywords']); $rec->setValueByLang('name', $this->defLang, $data['name']); $rec->setValueByLang('description', $this->defLang, $data['description']); //images if ($data['picture']) { //$this->importCategoryImage($rec, $this->path . '/' . $data['picture']); } $rec->rawData = $data; return $rec; }