/** * New method to create categories * @param array $category */ public function sCategory($category = array()) { // In order to be compatible with the old API syntax but to also be able to use ->fromArray(), // we map from the old keys to doctrine keys $mappings = array('description' => 'name', 'cmsheadline' => 'cmsHeadline', 'metakeywords' => 'metaKeywords', 'metadescription' => 'metaDescription'); foreach ($mappings as $original => $new) { if (isset($category[$original])) { $category[$new] = $category[$original]; unset($category[$original]); } } $model = null; $categoryRepository = $this->getCategoryRepository(); // If user wants to update the category if ($category['updateID']) { $model = $categoryRepository->find((int) $category['updateID']); if ($model === null) { $this->sAPI->sSetError("Category {$category['updateID']} not found", 10405); return false; } $model->fromArray($category); Shopware()->Models()->persist($model); Shopware()->Models()->flush(); } // Create a new category if no model was set, yet if (!$model) { // try to find a existing category by name and parent if (isset($category['parent']) && isset($category['name'])) { $model = $categoryRepository->findOneBy(array('parent' => $category['parent'], 'name' => $category['name'])); } if (!$model) { $model = new \Shopware\Models\Category\Category(); } if (isset($category['parent'])) { $parentModel = $categoryRepository->find((int) $category['parent']); if ($parentModel === null) { $this->sAPI->sSetError("Parent category {$category['parent']} not found", 10406); return false; } } $model->fromArray($category); $model->setParent($parentModel); Shopware()->Models()->persist($model); Shopware()->Models()->flush(); } // set category attributes $upset = array(); for ($i = 1; $i <= 6; $i++) { if (isset($category['ac_attr' . $i])) { $upset['attribute' . $i] = (string) $category['ac_attr' . $i]; } elseif (isset($category['attr'][$i])) { $upset['attribute' . $i] = (string) $category['attr'][$i]; } } if (!empty($upset)) { $attributeID = Shopware()->Db()->fetchOne("SELECT id FROM s_categories_attributes WHERE categoryID=?", array($model->getId())); if ($attributeID === false) { $upset['categoryID'] = $model->getId(); Shopware()->Db()->insert('s_categories_attributes', $upset); } else { Shopware()->Db()->update('s_categories_attributes', $upset, array('categoryID = ?' => $model->getId())); } } return $model->getId(); }
/** * Creates the dummy data * * @return \Shopware\Models\Category\Category */ private function getDummyData() { $dummyModel = new \Shopware\Models\Category\Category(); $dummyData = $this->dummyData; $dummyModel->fromArray($dummyData); //set category parent $parent = $this->repository->find($dummyData['parentId']); $dummyModel->setParent($parent); return $dummyModel; }