/** * @test * @dataProvider customRulesProvider */ public function customRules($rule, $string, $result) { $slugify = new Slugify(); $slugify->activateRuleSet($rule); $this->assertSame($result, $slugify->slugify($string)); }
public function addProductAction(Request $request, $productID) { $slugify = new Slugify(); $slugify->activateRuleSet('polish'); $product = $this->getDoctrine()->getRepository('ShopBundle:Products')->findOneById($productID); if ($product) { $productToView = $product; $productForm = $this->makeProductForm($product); } else { $productToView = ''; $productForm = $this->makeProductForm(); } $categoriesToView = $this->getDoctrine()->getRepository('ShopBundle:ProductCategories')->findAll(); $productForm->handleRequest($request); if ($productForm->isSubmitted() && $productForm->isValid()) { $productData = $productForm->getData(); $productData->getName(); try { $em = $this->getDoctrine()->getManager(); $em->getConnection()->beginTransaction(); if ($productData->getId() != '') { $product = $this->getDoctrine()->getRepository('ShopBundle:Products')->findOneById($productData->getId()); } else { $product = new Products(); } $product->setName($productData->getName())->setPrice($productData->getPrice())->setPermalink($productData->getPermalink())->setDescription($productData->getDescription())->setIsActive($productData->getIsActive())->setIsNew($productData->getIsNew()); $categories = json_decode($productData->getCategoriesInput()); if (is_array($categories) && !empty($categories)) { foreach ($categories as $cat) { $slug = $slugify->slugify($cat->name); $catIsExistingCategory = $this->getDoctrine()->getRepository('ShopBundle:productCategories')->findOneByPermalink($slug); if ($catIsExistingCategory) { $this->addCategoryIfNotRelated($product, $catIsExistingCategory); if ($catIsExistingCategory->getParent()) { $parentCategory = $this->getDoctrine()->getRepository('ShopBundle:ProductCategories')->findOneByPermalink($catIsExistingCategory->getParent()); if ($parentCategory) { $this->addCategoryIfNotRelated($product, $parentCategory); if ($parentCategory->getParent()) { $product = $this->addParentsCategoriesToProduct($parentCategory, $product); } } } } else { $productCategory = (new ProductCategories())->setPermalink($slug)->setName($cat->name); if ($cat->parent) { $parentCategory = $this->getDoctrine()->getRepository('ShopBundle:ProductCategories')->findOneByPermalink($cat->parent); if ($parentCategory) { $productCategory->setParent($parentCategory); $this->addCategoryIfNotRelated($product, $parentCategory); if ($parentCategory->getParent()) { $product = $this->addParentsCategoriesToProduct($parentCategory, $product); } } } $this->addCategoryIfNotRelated($product, $productCategory); $em->persist($productCategory); } } } $em->persist($product); $variants = json_decode($productData->getVariantsInput()); if (is_array($variants) && !empty($variants)) { foreach ($variants as $value) { $slug = $slugify->slugify($value->name); // if (strpos($value->value, ';')) { $oneValue = explode(';', $value->value); foreach ($oneValue as $val) { if ($val != '') { $checkIfProductVariantExist = $this->getDoctrine()->getRepository('ShopBundle:productVariants')->findOneBy(['slug' => $slug, 'value' => $val]); if ($checkIfProductVariantExist) { $this->addVariantIfNotRelated($product, $checkIfProductVariantExist); $em->persist($checkIfProductVariantExist); } else { $productVariant = (new ProductVariants())->setSlug($slug)->setName($value->name)->setValue($val)->setProduct($product); $em->persist($productVariant); } } } // } // else { // $productVariant = (new ProductVariants) // ->setSlug($slug) // ->setName($value->name) // ->setValue($value->value); // // $this->addVariantIfNotRelated($product, $productVariant); // $em->persist($productVariant); // } } } $properties = json_decode($productData->getPropertiesInput()); if (is_array($properties) && !empty($properties)) { foreach ($properties as $value) { $slug = $slugify->slugify($value->name); $checkIfProductPropertyExist = $this->getDoctrine()->getRepository('ShopBundle:productProperties')->findOneBy(['slug' => $slug, 'value' => $value->value]); if ($checkIfProductPropertyExist) { $this->addPropertyIfNotRelated($product, $checkIfProductPropertyExist); $em->persist($checkIfProductPropertyExist); } else { $productProperties = (new ProductProperties())->setSlug($slug)->setName($value->name)->setValue($value->value)->setProduct($product); $em->persist($productProperties); } } } $em->flush(); $namePhotos = json_decode($productData->getImgCount()); $fileCount = 0; $photoDirectory = 'data/photo/' . $productData->getPermalink() . '/*'; if ($namePhotos && $productData->getPermalink() != '') { // rename('data/photo/och/01.jpg','data/photo/och/02.jpg'); // $photoDirectory = 'data/photo/'.$productData->getPermalink().'/*'; if (glob($photoDirectory) != false) { $flipped = array_flip($namePhotos); ksort($flipped); $row = 0; foreach ($flipped as $key => $value) { $fileCount .= $row . '/' . $key . '/' . $value . '|'; if ($row < $key) { $key = $row; } rename('data/photo/' . $productData->getPermalink() . '/' . str_pad($value, 2, "0", STR_PAD_LEFT) . '.jpg', 'data/photo/' . $productData->getPermalink() . '/' . str_pad($key, 2, "0", STR_PAD_LEFT) . 'a.jpg'); $row++; } foreach (glob($photoDirectory . '*') as $k) { rename($k, str_replace('a.jpg', '.jpg', $k)); } } } $files = $productData->getPhoto(); if (!empty($files) && is_array($files)) { $fileCount2 = 0; // $photoDirectory = 'data/photo/'.$productData->getPermalink().'/*'; if (glob($photoDirectory) != false) { $fileCount2 = count(glob($photoDirectory . '*')); } for ($i = 0; $i < count($files); $i++) { $a = str_pad($i + $fileCount2, 2, "0", STR_PAD_LEFT); $destFilename = $a . '.jpg'; $destPath = $this->get('kernel')->getDataDir() . '/photo/' . $product->getPermalink() . '/'; $destPathname = $this->get('kernel')->getDataDir() . '/photo/' . $product->getPermalink() . '/' . $destFilename; $file = $files[$i]; if (!empty($file)) { if ($file->getMimeType() != "image/jpeg") { $im = new Imagick($file->getPathname()); $im->setImageBackgroundColor('white'); $im = $im->flattenImages(); $im->setImageFormat('jpg'); $im->writeImage($destPathname); } else { $file->move($destPath, $destFilename); } } } $product->setImgCount(count(glob($photoDirectory . '*'))); $em->flush(); } $em->getConnection()->commit(); $this->addFlash('notice', 'Udało się zaktualizować produkt!'); return $this->redirectToRoute('add_products_action_manager_url', ['productID' => $product->getId()]); // return $this->render('ManagerBundle:Products:productsManager.html.twig', // [ // 'productForm' => $productForm->createView(), // 'categories' => $categoriesToView, // 'product' => $product // ]); } catch (\Exception $e) { $em->getConnection()->rollback(); $this->addFlash('error', 'Wyjątek Nie udało się dodać nowego produktu.' . $e->getMessage() . '|'); $response = 'Wyjątek Nie udało się dodać nowego produktu.' . $e->getMessage() . '|'; // return $this->render('ManagerBundle:Products:productsManager.html.twig', // [ // 'productForm' => $productForm->createView(), // // 'success' => $response, // 'categories' => $categoriesToView, // 'product' => $productToView // ]); } } return $this->render('ManagerBundle:Products:productsManager.html.twig', ['productForm' => $productForm->createView(), 'categories' => $categoriesToView, 'product' => $productToView]); }