public function edit(Application $app, Request $request, $id = null)
 {
     /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
     $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
     $softDeleteFilter->setExcludes(array('Eccube\\Entity\\ProductClass'));
     $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];
             $ProductStock = $ProductClasses[0]->getProductStock();
         }
     }
     $builder = $app['form.factory']->createBuilder('admin_product', $Product);
     // 規格あり商品の場合、規格関連情報をFormから除外
     if ($has_class) {
         $builder->remove('class');
     }
     $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);
     if ('POST' === $request->getMethod()) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $Product = $form->getData();
             if (!$has_class) {
                 $ProductClass = $form['class']->getData();
                 $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();
             $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');
         }
     }
     // 検索結果の保持
     $searchForm = $app['form.factory']->createBuilder('admin_search_product')->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));
 }
Esempio n. 2
0
 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 $arrCategory カテゴリ の配列
  * @return void
  */
 function createProductsCategories($arrCategory)
 {
     $count = 0;
     foreach ($arrCategory as $category) {
         foreach ($this->arrProduct as $product) {
             $ProductCategory = new Eccube\Entity\ProductCategory();
             $ProductCategory->setProductId($product->getId())->setProduct($product)->setCategoryId($category->getId())->setCategory($category)->setRank($count);
             $this->saveEntity($ProductCategory);
             print "\$";
         }
     }
     print "\n";
 }