public function edit(Application $app, Request $request, $id = null) { $has_class = false; if (is_null($id)) { $Product = new \Eccube\Entity\Product(); $ProductClass = new \Eccube\Entity\ProductClass(); $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); $Product->setDelFlg(Constant::DISABLED)->addProductClass($ProductClass)->setStatus($Disp); $ProductClass->setDelFlg(Constant::DISABLED)->setStockUnlimited(true)->setProduct($Product); $ProductStock = new \Eccube\Entity\ProductStock(); $ProductClass->setProductStock($ProductStock); $ProductStock->setProductClass($ProductClass); } else { $Product = $app['eccube.repository.product']->find($id); if (!$Product) { throw new NotFoundHttpException(); } // 規格あり商品か $has_class = $Product->hasProductClass(); if (!$has_class) { $ProductClasses = $Product->getProductClasses(); $ProductClass = $ProductClasses[0]; $BaseInfo = $app['eccube.repository.base_info']->get(); if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); } $ProductStock = $ProductClasses[0]->getProductStock(); } } $builder = $app['form.factory']->createBuilder('admin_product', $Product); // 規格あり商品の場合、規格関連情報をFormから除外 if ($has_class) { $builder->remove('class'); } $event = new EventArgs(array('builder' => $builder, 'Product' => $Product), $request); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); $form = $builder->getForm(); if (!$has_class) { $ProductClass->setStockUnlimited((bool) $ProductClass->getStockUnlimited()); $form['class']->setData($ProductClass); } // ファイルの登録 $images = array(); $ProductImages = $Product->getProductImage(); foreach ($ProductImages as $ProductImage) { $images[] = $ProductImage->getFileName(); } $form['images']->setData($images); $categories = array(); $ProductCategories = $Product->getProductCategories(); foreach ($ProductCategories as $ProductCategory) { /* @var $ProductCategory \Eccube\Entity\ProductCategory */ $categories[] = $ProductCategory->getCategory(); } $form['Category']->setData($categories); $Tags = array(); $ProductTags = $Product->getProductTag(); foreach ($ProductTags as $ProductTag) { $Tags[] = $ProductTag->getTag(); } $form['Tag']->setData($Tags); if ('POST' === $request->getMethod()) { $form->handleRequest($request); if ($form->isValid()) { $Product = $form->getData(); if (!$has_class) { $ProductClass = $form['class']->getData(); // 個別消費税 $BaseInfo = $app['eccube.repository.base_info']->get(); if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { if ($ProductClass->getTaxRate()) { if ($ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); } else { $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); $taxrule->setTaxRate($ProductClass->getTaxRate()); $taxrule->setApplyDate(new \DateTime()); $taxrule->setProduct($Product); $taxrule->setProductClass($ProductClass); $ProductClass->setTaxRule($taxrule); } } else { if ($ProductClass->getTaxRule()) { $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); } } } $app['orm.em']->persist($ProductClass); // 在庫情報を作成 if (!$ProductClass->getStockUnlimited()) { $ProductStock->setStock($ProductClass->getStock()); } else { // 在庫無制限時はnullを設定 $ProductStock->setStock(null); } $app['orm.em']->persist($ProductStock); } // カテゴリの登録 // 一度クリア /* @var $Product \Eccube\Entity\Product */ foreach ($Product->getProductCategories() as $ProductCategory) { $Product->removeProductCategory($ProductCategory); $app['orm.em']->remove($ProductCategory); } $app['orm.em']->persist($Product); $app['orm.em']->flush(); $count = 1; $Categories = $form->get('Category')->getData(); foreach ($Categories as $Category) { $ProductCategory = new \Eccube\Entity\ProductCategory(); $ProductCategory->setProduct($Product)->setProductId($Product->getId())->setCategory($Category)->setCategoryId($Category->getId())->setRank($count); $app['orm.em']->persist($ProductCategory); $count++; /* @var $Product \Eccube\Entity\Product */ $Product->addProductCategory($ProductCategory); } // 画像の登録 $add_images = $form->get('add_images')->getData(); foreach ($add_images as $add_image) { $ProductImage = new \Eccube\Entity\ProductImage(); $ProductImage->setFileName($add_image)->setProduct($Product)->setRank(1); $Product->addProductImage($ProductImage); $app['orm.em']->persist($ProductImage); // 移動 $file = new File($app['config']['image_temp_realdir'] . '/' . $add_image); $file->move($app['config']['image_save_realdir']); } // 画像の削除 $delete_images = $form->get('delete_images')->getData(); foreach ($delete_images as $delete_image) { $ProductImage = $app['eccube.repository.product_image']->findOneBy(array('file_name' => $delete_image)); // 追加してすぐに削除した画像は、Entityに追加されない if ($ProductImage instanceof \Eccube\Entity\ProductImage) { $Product->removeProductImage($ProductImage); $app['orm.em']->remove($ProductImage); } $app['orm.em']->persist($Product); // 削除 $fs = new Filesystem(); $fs->remove($app['config']['image_save_realdir'] . '/' . $delete_image); } $app['orm.em']->persist($Product); $app['orm.em']->flush(); $ranks = $request->get('rank_images'); if ($ranks) { foreach ($ranks as $rank) { list($filename, $rank_val) = explode('//', $rank); $ProductImage = $app['eccube.repository.product_image']->findOneBy(array('file_name' => $filename, 'Product' => $Product)); $ProductImage->setRank($rank_val); $app['orm.em']->persist($ProductImage); } } $app['orm.em']->flush(); // 商品タグの登録 // 商品タグを一度クリア $ProductTags = $Product->getProductTag(); foreach ($ProductTags as $ProductTag) { $Product->removeProductTag($ProductTag); $app['orm.em']->remove($ProductTag); } // 商品タグの登録 $Tags = $form->get('Tag')->getData(); foreach ($Tags as $Tag) { $ProductTag = new ProductTag(); $ProductTag->setProduct($Product)->setTag($Tag); $Product->addProductTag($ProductTag); $app['orm.em']->persist($ProductTag); } $app['orm.em']->flush(); $event = new EventArgs(array('form' => $form, 'Product' => $Product), $request); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); $app->addSuccess('admin.register.complete', 'admin'); return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); } else { $app->addError('admin.register.failed', 'admin'); } } // 検索結果の保持 $builder = $app['form.factory']->createBuilder('admin_search_product'); $event = new EventArgs(array('builder' => $builder, 'Product' => $Product), $request); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); $searchForm = $builder->getForm(); if ('POST' === $request->getMethod()) { $searchForm->handleRequest($request); } return $app->render('Product/product.twig', array('Product' => $Product, 'form' => $form->createView(), 'searchForm' => $searchForm->createView(), 'has_class' => $has_class, 'id' => $id)); }
/** * タグの登録 * * @param array $row * @param Product $Product * @param Application $app * @param CsvImportService $data */ protected function createProductTag($row, Product $Product, $app, $data) { // タグの削除 $ProductTags = $Product->getProductTag(); foreach ($ProductTags as $ProductTags) { $Product->removeProductTag($ProductTags); $this->em->remove($ProductTags); } if ($row['タグ(ID)'] == '') { return; } // タグの登録 $tags = explode(',', $row['タグ(ID)']); foreach ($tags as $tag_id) { $Tag = null; if (preg_match('/^\\d+$/', $tag_id)) { $Tag = $app['eccube.repository.master.tag']->find($tag_id); if ($Tag) { $ProductTags = new ProductTag(); $ProductTags->setProduct($Product)->setTag($Tag); $Product->addProductTag($ProductTags); $this->em->persist($ProductTags); } } if (!$Tag) { $this->addErrors($data->key() + 1 . '行目のタグ(ID)「' . $tag_id . '」が存在しません。'); } } }
/** * EC-CUBE本体のエンティティとjoinし, 検索するテスト */ public function testWhereWithJoinEntity() { // `新商品`のTagが登録されたProductを生成 $Tag = $this->app['eccube.repository.master.tag']->find(1); $Member = $this->app['eccube.repository.member']->find(2); $Product = $this->app['eccube.repository.product']->find(reset($this->expectedIds)); $ProductTag = new ProductTag(); $ProductTag->setCreator($Member); $ProductTag->setProduct($Product); $ProductTag->setTag($Tag); $Product->addProductTag($ProductTag); $this->app['orm.em']->persist($ProductTag); $this->app['orm.em']->flush(array($Product, $ProductTag)); $qb = $this->app['eccube.repository.product']->getQueryBuilderBySearchData(array()); // 商品タグとjoinして検索 $qb->innerJoin('p.ProductTag', 'ptag')->innerJoin('ptag.Tag', 'tag')->andWhere($qb->expr()->in('ptag.Tag', ':Tag'))->setParameter(':Tag', $Tag); $expectedIds = array(); $results = $qb->getQuery()->getResult(); foreach ($results as $result) { $expectedIds[] = $result->getId(); } $pagination = $this->app['paginator']()->paginate($qb, 1, 30, array('wrap-queries' => true)); $actualIds = array(); foreach ($pagination as $result) { $actualIds[] = $result->getId(); } $this->expected = $expectedIds; $this->actual = $actualIds; // tagが登録されたProductは1件のみ. $this->assertTrue(count($this->actual) === 1); $this->verify(); }