START); $media = $em->getRepository('MediaCoreBundle:Media')->find($id); // 商品, ページ拡張フィールドのものを削除 $fields = $this->getProductAttributeList() + $this->getPageAttributeList(); $image_fields = array(); $image_fields_keys = array(); foreach ($fields as $k=>$v) { if ($v == "\Media\AdminBundle\Form\MediaType") { array_push($image_fields_keys, $k); } } $content_attribute_ids = array(); $products = $em->getRepository('EcCoreBundle:Product')->findAll(); foreach($products as $product) { $attributes = $product->getAttributes(); foreach ($attributes as $attribute) { if (in_array($attribute->getAttrKey(), $image_fields_keys)) { $media_id = unserialize($attribute->getAttrValue()); if ($media_id == $id) { // ProductAttributeMap $sql = "delete from ProductAttributeMap where attr_id = ".$attribute->getId()." AND product_id =" . $product->getId(); $stmt = $em->getConnection()->prepare($sql); $stmt->execute(); array_push($content_attribute_ids, $attribute->getId()); } } } } $pages = $em->getRepository('PageCoreBundle:Page')->findAll(); foreach($pages as $page) { $attributes = $page->getAttributes(); foreach ($attributes as $attribute) { if (in_array($attribute->getAttrKey(), $image_fields_keys)) { $media_id = unserialize($attribute->getAttrValue()); if ($media_id == $id) { // PageAttributeMap $sql = "delete from PageAttributeMap where attr_id = ".$attribute->getId()." AND page_id =" . $page->getId(); $stmt = $em->getConnection()->prepare($sql); $stmt->execute(); array_push($content_attribute_ids, $attribute->getId()); } } } } if (count(array_unique($content_attribute_ids)) > 0) { foreach (array_unique($content_attribute_ids) as $key => $id) { $ca = $em->getRepository('ContentAttributeBundle:ContentAttribute')->find($id); $em->remove($ca); } } $em->remove($media); $em->flush(); $path = $this->get('kernel')->getRootDir().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$this->container->getParameter('lc_media_upload_path'); $image_path = $path.DIRECTORY_SEPARATOR.$media->getPath(); if (file_exists($image_path)) { @unlink($image_path); } $this->get('ec.message.flash')->setMessage('system_success'); return $this->redirect($this->generateUrl('media')); } /** * @Route("/batch_delete", name="media_batch_delete") * */ public function batchDeleteAction() { // request $request = $this->get('request'); // em $entityManager = $this->get('doctrine')->getManager(); $idsHdn = $request->request->get('idsHdn'); if ($idsHdn == '') { throw new NotFoundHttpException('The media ids do NOT exist.'); } $ids = explode(',', $idsHdn); foreach ($ids as $id) { if (!is_numeric($id)) { throw new NotFoundHttpException('The media id is NOT correct.'); } $media = $entityManager->find('MediaCoreBundle:Media', $id); if( !$media ) { throw new NotFoundHttpException('The media does NOT exist.'); } $media->setDeletedAt(new \DateTime()); $path = $this->get('kernel')->getRootDir().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$this->container->getParameter('lc_media_upload_path'); $image_path = $path.DIRECTORY_SEPARATOR.$media->getPath(); @unlink($image_path); $entityManager->persist($media); $entityManager->flush(); } $this->get('ec.message.flash')->setMessage('system_success'); return $this->redirect($this->generateUrl('media')); } /** * @Route("/add", name="media_add") * @Template() * */ public function addAction() { $media = new Media(); $form = $this->createForm(new MediaType(true), $media); $request = $this->get('request'); if ($request->getMethod() === 'POST') { $form->bind($request); if ($form->isValid()) { $entityManager = $this->get('doctrine') ->getManager(); $entityManager->persist($media); $entityManager->flush(); $this->uploadToAWS($media); $this->get('ec.messaEND