public function linkRefactoring(Application $app, Request $request) { $data = $request->get('refactoringBlocks'); try { /** @var \Model\Url $oldUrl */ $oldUrl = $app['em']->find('\\Model\\Url', $request->get('oldUrl')); } catch (Expcetion $e) { $app['monolog']->addError($e->getMessage()); return new Response($app['admin.message_composer']->exceptionMessage($e), 500); } if (!isset($oldUrl) || is_null($oldUrl)) { return new Response($app['admin.message_composer']->failureMessage("Url to be refactored does not exist"), 400); } $newUrl = new Url(); $newUrl->setUrl($request->get('newUrl')); $blockIds = array(); foreach ($data as $blockId => $val) { $blockIds[] = $blockId; } $blocks = $app['em']->getRepository('\\Model\\ContentBlock')->findBy(array('id' => $blockIds)); $lass = new LinkAssistant(); try { $updatedBlocks = $lass->refactorLinks($blocks, $oldUrl, $newUrl); //Saving blocks $app['em']->beginTransaction(); foreach ($updatedBlocks as $block) { $app['em']->merge($block); } //Update url row with new url refactored $oldUrl->setUrl($newUrl->getUrl()); $app['em']->merge($oldUrl); $app['em']->flush(); $app['em']->commit(); } catch (\Exception $e) { $app['em']->rollback(); $app['monolog']->addError($e->getMessage()); return new Response($app['admin.message_composer']->exceptionMessage($e), 500); } return new Response($app['admin.message_composer']->successMessage(), 200); }
/** * @param $em \Doctrine\ORM\EntityManager * @param $data array * @param null $pageid int * @return int Id of processed page */ protected function insertUpdateProcessing($em, $data, $pageid = null) { $update = !is_null($pageid); if ($update) { $page = $em->find('Model\\Page', $pageid); } else { $page = new Page(); } if ($update) { $deleteurl = $em->createQueryBuilder()->delete()->from('Model\\Url', "url")->where("url.page=" . $page->getId())->getQuery(); $deletepageblock = $em->createQueryBuilder()->delete()->from('Model\\PageBlock', "pb")->where("pb.page=" . $page->getId())->getQuery(); $deleteurl->execute(); $deletepageblock->execute(); } $page->setName($data['name']); $page->setDescription($data['description']); $page->setTitle($data['title']); $page->setPublished(!is_null($data['published'])); $page->setPublic(!is_null($data['public'])); $language = $em->find('Model\\Language', $data['language']); $page->setLanguage($language); //Url insertion foreach ($data['url'] as $urlstring) { if (strlen($urlstring) > 2) { $url = new Url(); $url->setUrl($urlstring); $page->addUrl($url); } } if (!$update) { $em->persist($page); $em->flush(); } //Processing Blocks for ($i = 0; $i < count($data['block']['id']); $i++) { $blockid = $data['block']['id'][$i]; //If block is new and content is empty, skip the block //Iterates over remaining blocks if (!($blockid == 0 && strlen($data['block']['content'][$i]) == 0)) { $isContentBlock = isset($data['block']['content'][$i]) && strlen($data['block']['content'][$i]) > 0; if ($blockid == 0) { //Insert new block $block = new \Model\ContentBlock(); } else { //Update existing block if ($isContentBlock) { $block = $em->find('Model\\ContentBlock', $blockid); } else { $block = $em->find('Model\\Block', $blockid); } } if (is_null($block)) { print "IS CONTENT BLOCK ->" . $isContentBlock . "\n"; print $data['block']['content'][$i]; print "BLOCK ID -> " . $blockid; } //Sets block properties $block->setName($data['block']['name'][$i]); $block->setDescription($data['block']['description'][$i]); $block->setBlockStyleClassName($data['block']['style'][$i]); $block->setBgurl($data['block']['bckurl'][$i]); $block->setBgred(intval($data['block']['bckred'][$i])); $block->setBggreen(intval($data['block']['bckgreen'][$i])); $block->setBgblue(intval($data['block']['bckblue'][$i])); $block->setBgopacity(floatval($data['block']['bckopacity'][$i])); $bgrepeatx = !(strlen($data['block']['bckrepeatx'][$i]) <= 0 || $data['block']['bckrepeatx'][$i] == 'false'); $bgrepeaty = !(strlen($data['block']['bckrepeaty'][$i]) <= 0 || $data['block']['bckrepeaty'][$i] == 'false'); $block->setBgrepeatx($bgrepeatx); $block->setBgrepeaty($bgrepeaty); $block->setBgsize($data['block']['bcksize'][$i]); // If is set content field, this block is a content block // If content is not set, I can work directly with block object if ($isContentBlock) { $block->setContent($data['block']['content'][$i]); } if ($blockid == 0) { $em->persist($block); } else { $em->merge($block); } $em->flush(); //Adding block to page $page->addBlock($block, $i); } } if ($update) { $em->merge($page); } else { $em->persist($page); } $em->flush(); return $page->getId(); }