/** * * @Route("/admin/blog/edit/{id}", name="admin_blog_edit") */ public function editAction(Request $request, $id) { $blog = $this->getDoctrineRepo('AppBundle:Blog')->find($id); if (!$blog) { throw $this->createNotFoundException('No blog post found for id ' . $id); } $form = $this->createFormBuilder($blog, array('constraints' => array(new Callback(array($this, 'validateSlug')))))->add('id', 'hidden')->add('title', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 128)))))->add('slug', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 128)), new Regex(array('pattern' => '/^[a-z][-a-z0-9]*$/', 'htmlPattern' => '/^[a-z][-a-z0-9]*$/', 'message' => 'This is not a valid slug')))))->add('content', 'textarea', array('required' => false, 'constraints' => array(new NotBlank())))->add('position', 'integer', array('required' => false, 'constraints' => array(new Type(array('type' => 'integer')))))->getForm(); //when the form is posted this method prefills entity with data from form $form->handleRequest($request); if ($form->isValid()) { //$blog->setModificationDate(new \DateTime()); //check if there is file $file = $request->files->get('upl'); $file2 = $request->files->get('upl_big'); $em = $this->getDoctrine()->getManager(); if ($file != null && $file->isValid()) { //remove old Image (both file from filesystem and entity from db) $this->getDoctrineRepo('AppBundle:Image')->removeImage($blog->getImage(), $this->getParameter('image_storage_dir')); $blog->setImage(null); // save file $uuid = Utils::getUuid(); $image_storage_dir = $this->getParameter('image_storage_dir'); //$destDir = sprintf("%sblog\\",$image_storage_dir); $destDir = $image_storage_dir . DIRECTORY_SEPARATOR . 'blog' . DIRECTORY_SEPARATOR; $destFilename = sprintf("%s.%s", $uuid, $file->getClientOriginalExtension()); $file->move($destDir, $destFilename); // create object $img = new Image(); $img->setUuid($uuid); $img->setName($file->getClientOriginalName()); $img->setExtension($file->getClientOriginalExtension()); $img->setPath('blog'); $em->persist($img); $em->flush(); $blog->setImage($img); } if ($file2 != null && $file2->isValid()) { //remove old Image (both file from filesystem and entity from db) $this->getDoctrineRepo('AppBundle:Image')->removeImage($blog->getBigImage(), $this->getParameter('image_storage_dir')); $blog->setImage(null); // save file $uuid = Utils::getUuid(); $image_storage_dir = $this->getParameter('image_storage_dir'); //$destDir = sprintf("%sblog\\",$image_storage_dir); $destDir = $image_storage_dir . DIRECTORY_SEPARATOR . 'blog' . DIRECTORY_SEPARATOR; $destFilename = sprintf("%s.%s", $uuid, $file2->getClientOriginalExtension()); $file2->move($destDir, $destFilename); // create object $img = new Image(); $img->setUuid($uuid); $img->setName($file2->getClientOriginalName()); $img->setExtension($file2->getClientOriginalExtension()); $img->setPath('blog'); $em->persist($img); $em->flush(); $blog->setBigImage($img); } // save to db $em->persist($blog); $em->flush(); return $this->redirectToRoute("admin_blog_list"); } return $this->render('admin/blog/edit.html.twig', array('form' => $form->createView(), 'blog' => $blog)); }
/** * * @Route("/admin/testimonials/edit/{id}", name="admin_testimonials_edit") */ public function editAction(Request $request, $id) { $testimonial = $this->getDoctrineRepo('AppBundle:Testimonial')->find($id); if (!$testimonial) { throw $this->createNotFoundException('No $testimonial found for id ' . $id); } $form = $this->createFormBuilder($testimonial)->add('name', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 100)))))->add('id', 'hidden')->add('description', 'textarea', array('constraints' => array(new NotBlank(), new Length(array('max' => 500)))))->add('place', 'text', array('constraints' => array(new NotBlank())))->add('age', 'integer', array('required' => true, 'constraints' => array(new Type(array('type' => 'integer')))))->add('position', 'integer', array('required' => false, 'constraints' => array(new Type(array('type' => 'integer')))))->getForm(); //when the form is posted this method prefills entity with data from form $form->handleRequest($request); if ($form->isValid()) { //check if there is file $file = $request->files->get('upl'); $em = $this->getDoctrine()->getManager(); if ($file != null && $file->isValid()) { //remove old Image (both file from filesystem and entity from db) $this->getDoctrineRepo('AppBundle:Image')->removeImage($testimonial, $this->getParameter('image_storage_dir')); // save file $uuid = Utils::getUuid(); $image_storage_dir = $this->getParameter('image_storage_dir'); //$destDir = sprintf("%sblog\\",$image_storage_dir); $destDir = $image_storage_dir . DIRECTORY_SEPARATOR . 'testimonials' . DIRECTORY_SEPARATOR; $destFilename = sprintf("%s.%s", $uuid, $file->getClientOriginalExtension()); $file->move($destDir, $destFilename); // create object $img = new Image(); $img->setUuid($uuid); $img->setName($file->getClientOriginalName()); $img->setExtension($file->getClientOriginalExtension()); $img->setPath('testimonials'); $em->persist($img); $em->flush(); $testimonial->setImage($img); } $em->persist($testimonial); $em->flush(); return $this->redirectToRoute("admin_testimonials_list"); } return $this->render('admin/testimonials/edit.html.twig', array('form' => $form->createView(), 'testimonial' => $testimonial)); }
private function handleImages($eqFiles, $eq, $em) { foreach ($eqFiles as $file) { // store the original, and image itself $origFullPath = $this->getParameter('image_storage_dir') . DIRECTORY_SEPARATOR . 'equipment' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . $file[0] . '.' . $file[2]; $imgFullPath = $this->getParameter('image_storage_dir') . DIRECTORY_SEPARATOR . 'equipment' . DIRECTORY_SEPARATOR . $file[0] . '.' . $file[2]; rename($file[3], $origFullPath); // check image size $imgInfo = getimagesize($origFullPath); $ow = $imgInfo[0]; // original width $oh = $imgInfo[1]; // original height $r = $ow / $oh; // ratio $nw = $ow; // new width $nh = $oh; // new height $scale = False; if ($r > 1) { if ($ow > 1024) { $nw = 1024; $m = $nw / $ow; // multiplier $nh = $oh * $m; $scale = True; } } else { if ($oh > 768) { $nh = 768; $m = $nh / $oh; // multiplier $nw = $ow * $m; $scale = True; } } // scale the image if ($scale) { if ($file[2] == 'png') { $img = imagecreatefrompng($origFullPath); } else { $img = imagecreatefromjpeg($origFullPath); } $sc = imagescale($img, intval(round($nw)), intval(round($nh)), IMG_BICUBIC_FIXED); if ($file[2] == 'png') { imagepng($sc, $imgFullPath); } else { imagejpeg($sc, $imgFullPath); } } else { copy($origFullPath, $imgFullPath); } // store entry in database $img = new Image(); $img->setUuid($file[0]); $img->setName($file[1]); $img->setExtension($file[2]); $img->setPath('equipment'); $img->setOriginalPath('equipment' . DIRECTORY_SEPARATOR . 'original'); $em->persist($img); $em->flush(); $eq->addImage($img); $em->flush(); } }
/** * * @Route("/admin/subcategory/edit/{id}", name="admin_subcategory_edit") */ public function editAction(Request $request, $id) { $subcategory = $this->getDoctrineRepo('AppBundle:Subcategory')->find($id); if (!$subcategory) { throw $this->createNotFoundException('No subcategory found for id ' . $id); } $category = $subcategory->getCategory(); $form = $this->createFormBuilder($subcategory)->add('id', 'hidden')->add('category', 'entity', array('class' => 'AppBundle:Category', 'property' => 'name', 'data' => $category))->add('name', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 256)))))->add('slug', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 256)), new Regex(array('pattern' => '/^[a-z][-a-z0-9]*$/', 'htmlPattern' => '/^[a-z][-a-z0-9]*$/', 'message' => 'This is not a valid slug')))))->add('emailBody', 'textarea', array('required' => false, 'constraints' => array(new NotBlank())))->add('position', 'integer', array('required' => false, 'constraints' => array(new Type(array('type' => 'integer')))))->getForm(); //when the form is posted this method prefills entity with data from form $form->handleRequest($request); if ($form->isValid()) { //check if there is file $file = $request->files->get('upl'); $em = $this->getDoctrine()->getManager(); if ($file != null && $file->isValid()) { //remove old Image (both file from filesystem and entity from db) $this->getDoctrineRepo('AppBundle:Image')->removeImage($subcategory->getImage(), $this->getParameter('image_storage_dir')); $subcategory->setImage(null); // save file $uuid = Utils::getUuid(); $image_storage_dir = $this->getParameter('image_storage_dir'); //$destDir = sprintf("%scategory\\",$image_storage_dir); $destDir = $image_storage_dir . DIRECTORY_SEPARATOR . 'subcategory' . DIRECTORY_SEPARATOR; $destFilename = sprintf("%s.%s", $uuid, $file->getClientOriginalExtension()); $file->move($destDir, $destFilename); // create object $img = new Image(); $img->setUuid($uuid); $img->setName($destFilename); $img->setExtension($file->getClientOriginalExtension()); $img->setOriginalPath($file->getClientOriginalName()); $img->setPath('category'); $em->persist($img); $em->flush(); $subcategory->setImage($img); } // save to db $em->persist($subcategory); $em->flush(); return $this->redirectToRoute("admin_subcategory_list", array('categoryID' => $category->getId())); } return $this->render('admin/subcategory/edit.html.twig', array('form' => $form->createView(), 'category' => $category, 'subcategory' => $subcategory)); }