コード例 #1
1
ファイル: import.php プロジェクト: ClaudioThomas/shopware-4
 /**
  * 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();
 }
コード例 #2
0
ファイル: CategoryTest.php プロジェクト: GerDner/luck-docker
 /**
  * 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;
 }
コード例 #3
0
 /**
  * @param string $categorypaths
  * @return array
  */
 public function createCategoriesByCategoryPaths($categorypaths)
 {
     $categoryIds = array();
     $categorypaths = explode("\n", $categorypaths);
     foreach ($categorypaths as $categorypath) {
         $categorypath = trim($categorypath);
         if (empty($categorypath)) {
             continue;
         }
         $categories = explode('|', $categorypath);
         $categoryId = 1;
         foreach ($categories as $categoryName) {
             $categoryName = trim($categoryName);
             if (empty($categoryName)) {
                 break;
             }
             $categoryModel = $this->getCategoryRepository()->findOneBy(array('name' => $categoryName, 'parentId' => $categoryId));
             if (!$categoryModel) {
                 $parent = $this->getCategoryRepository()->find($categoryId);
                 if (!$parent) {
                     throw new \Exception(sprintf('Could not find %s '));
                 }
                 $categoryModel = new \Shopware\Models\Category\Category();
                 $categoryModel->setParent($parent);
                 $categoryModel->setName($categoryName);
                 $this->getManager()->persist($categoryModel);
                 $this->getManager()->flush();
                 $this->getManager()->clear();
             }
             $categoryId = $categoryModel->getId();
             if (empty($categoryId)) {
                 continue;
             }
             if (!in_array($categoryId, $categoryIds)) {
                 $categoryIds[] = $categoryId;
             }
         }
     }
     return $categoryIds;
 }