public function getFullCode($entity) { $sections = $this->getSections($entity); $routes = $this->routeService->getCurrentSiteRoutes(); foreach ($routes as $r) { Utils::msg($r->getCode()); } foreach ($sections as $s) { Utils::msg($this->sectionService->getFullCode($s->getId())); } }
/** * Edits an existing Element entity. * */ public function updateAction(Request $request, $id, $block_id, $section_id = false) { //echo '<pre>' . print_r('updateAction', true) . '</pre>'; $em = $this->getDoctrine()->getManager(); $Element = $this->get('Element'); $entity = $em->getRepository('NovuscomCMFBundle:Element')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Element entity.'); } /** * Получаем разделы к которым принадлежал элемент до отправки формы */ $oldSections = $Element->getSections($entity); $deleteForm = $this->createDeleteForm($id); $editForm = $this->createEditForm($entity); $editForm->handleRequest($request); //echo '<pre>' . print_r('Начинаем проверять форму', true) . '</pre>'; exit; if ($editForm->isValid()) { ///echo '<pre>' . print_r('Форма валидна', true) . '</pre>'; exit; $newSections = new ArrayCollection(); if ($editForm->has('section')) { foreach ($editForm->get('section')->getData() as $s) { $newSections->add($s); } } //mail('*****@*****.**', '$data', print_r($editForm->get('properties'), true)); //echo '<pre>' . print_r($editForm->getData(), true) . '</pre>'; /*foreach ($editForm->get('properties') as $property) { echo '<pre>' . print_r($property->getName(), true) . '</pre>'; echo '<pre>' . print_r($property->get('code')->getData(), true) . '</pre>'; }*/ $FormService = $this->get('cmf.forms'); $props = $FormService->getElementProperties($editForm->get('properties')); $Element->SetPropertyValues($entity, $props); if ($editForm->has('properties')) { //echo '<pre>' . print_r('У формы есть свойства', true) . '</pre>'; exit; //Utils::msg($editForm->get('properties')); /** * Собираем массив свойств */ $propArray = array(); foreach ($editForm->get('properties') as $p) { if ($p->getData()) { $propArray[$p->getName()] = $p->getData(); //echo '<pre>' . print_r($p->getData(), true) . '</pre>'; } } //$keys = array_keys($propArray); //echo '<pre>' . print_r($keys, true) . '</pre>'; exit; //Utils::msg($propArray); exit; /** * Получаем свойства элемента */ $ElementProperty = $em->getRepository('NovuscomCMFBundle:ElementProperty')->findBy(array('element' => $entity)); $countEP = count($ElementProperty); /** * Тестовые свойства уже существуют - обрабатываем их */ if ($countEP > 0) { $updatedId = array(); foreach ($ElementProperty as $key => $ep) { //echo '<hr/><pre>' . print_r('$ep->getProperty()->getId() - ' . $ep->getProperty()->getId(), true) . '</pre>'; //echo '<pre>' . print_r('$ep->getValue() - '.$ep->getValue(), true) . '</pre><hr/>'; //echo '<pre>$key: ' . print_r($key, true) . '</pre>'; //echo '<pre>' . print_r($ep->getValue(), true) . '</pre>'; //echo '<pre>[id=' . print_r($ep->getId() . ', value=' . $ep->getValue() . ', property_id=' . $ep->getProperty()->getId(), true) . ']</pre>'; if (array_key_exists($ep->getProperty()->getId(), $propArray)) { //echo '<pre>' . print_r('propArray[' . $ep->getProperty()->getId() . '] существует', true) . '</pre>'; $val = $propArray[$ep->getProperty()->getId()]; //Utils::msg($val); if (!is_object($val)) { //echo '<pre>' . print_r('Не объект', true) . '</pre>'; //echo '<pre>' . print_r($val, true) . '</pre>'; if (is_array($val)) { //echo '<pre>' . print_r('Массив, $ep->getId()=' . $ep->getId(), true) . '</pre>'; //echo '<pre>' . print_r($val, true) . '</pre>'; /*foreach ($val as $keyVal=>$valueVal) { //echo '<pre>' . print_r('['.$ep->getProperty()->getId().']', true) . '</pre>'; $file = new \CMF\MediaBundle\Entity\File(); if ($valueVal instanceof $file) { } //echo '<pre>' . print_r($propArray[$ep->getProperty()->getId()], true) . '</pre>'; //unset($propArray[$ep->getProperty()->getId()]); //break; }*/ $updatedId[] = $ep->getId(); } else { //echo '<pre>' . print_r('Не массив', true) . '</pre>'; //echo '<pre>' . print_r($val, true) . '</pre>'; $ep->setValue($val); $updatedId[] = $ep->getId(); //echo '<pre>' . print_r('Обновляем ' . $key, true) . '</pre>'; //$updatedId[] = $key; $em->persist($ep); unset($propArray[$ep->getProperty()->getId()]); } } else { //echo '<pre>' . print_r('Объект', true) . '</pre>'; //echo '<pre>' . print_r($val, true) . '</pre>'; //exit; if ($propArray[$ep->getProperty()->getId()] instanceof \DateTime) { } else { foreach ($propArray[$ep->getProperty()->getId()] as $k => $v) { $ep->setValue($v->getValue()); $updatedId[] = $ep->getId(); //echo '<pre>' . print_r('Обновляем ' . $key, true) . '</pre>'; //$updatedId[] = $key; $em->persist($ep); //$val->remove($k); $propArray[$ep->getProperty()->getId()]->remove($k); break; } if ($val->isEmpty()) { //echo '<pre>' . print_r('Удаляем коллекецию, т.к. она уже пустая', true) . '</pre>'; unset($propArray[$ep->getProperty()->getId()]); } } //echo '<pre>' . print_r($val, true) . '</pre>'; } } } //exit; //echo 'Обновленные свойства: <pre>' . print_r($updatedId, true) . '</pre>'; //echo '<pre>' . print_r($propArray, true) . '</pre>'; foreach ($ElementProperty as $key => $ep) { if (!in_array($ep->getId(), $updatedId)) { //if (!in_array($key, $updatedId)) { //echo '<pre>' . print_r('Удаляем $key=' . $key . ', $ep->getId()=' . $ep->getId() . ', ' . $ep->getValue(), true) . '</pre>'; $em->remove($ep); } } //exit; } // echo '<pre>' . print_r('Текстовые свойства обработаны', true) . '</pre>'; exit; /** * Обрабатываем дату/время */ $ElementPropertyDT = $em->getRepository('NovuscomCMFBundle:ElementPropertyDT')->findBy(array('element' => $entity)); $updatedId = array(); foreach ($ElementPropertyDT as $ep) { if (array_key_exists($ep->getProperty()->getId(), $propArray)) { $val = $propArray[$ep->getProperty()->getId()]; $ep->setValue($val); $updatedId[] = $ep->getId(); $em->persist($ep); unset($propArray[$ep->getProperty()->getId()]); break; } } foreach ($ElementPropertyDT as $ep) { if (!in_array($ep->getId(), $updatedId)) { $em->remove($ep); } } //exit; if ($propArray) { //echo '<pre>' . print_r('Свойства остались', true) . '</pre>'; //echo '<pre>' . print_r($propArray, true) . '</pre>'; foreach ($propArray as $property_id => $property_value) { $property = false; if (is_numeric($property_id)) { $property = $em->getRepository('NovuscomCMFBundle:Property')->find($property_id); } //echo '<hr/>$property_value:<pre>' . print_r($property_value, true) . '</pre><hr/>'; if (!is_object($property_value)) { //echo '<hr/>Это не объект:<pre>' . print_r($property_value, true) . '</pre><hr/>'; if (is_array($property_value)) { //echo '<hr/>Это массив:<pre>' . print_r($property_value, true) . '</pre><hr/>'; foreach ($property_value as $pv) { echo '<pre>' . print_r($pv, true) . '</pre>'; $file = new \Novuscom\CMFBundle\Entity\FormPropertyFile(); $ElementProperty = new ElementProperty(); if ($pv instanceof $file) { //exit; //echo '<pre>' . print_r($pv->getName(), true) . '</pre>'; //$this->createPreviewPicture() //$mediaController = new \CMF\MediaBundle\Controller\DefaultController(); //$em->flush(); //$em->clear(); /** * Заменяем файл */ $ElementPropertyF = $em->getRepository('NovuscomCMFBundle:ElementPropertyF')->findBy(array('element' => $entity)); //if ($pv->getDeleteFileId()){ if (false) { $this->deleteElementFiles($entity, $pv->getDeleteFileId()); foreach ($ElementPropertyF as $key => $ep) { if ($ep->getFile()->getId() == $pv->getDeleteFileId()) { $em->remove($ep); } } } else { if ($pv->getReplaceFileId()) { //echo '<pre> $property_id=' . print_r($property_id, true) . '</pre>'; //echo '<pre>' . print_r('Заменяем файл', true) . '</pre>'; foreach ($ElementPropertyF as $key => $ep) { //echo '<pre>' . print_r('fil_id: ' . $ep->getFile()->getId() . ', property:' . $ep->getProperty()->getId(), true) . '</pre>'; //echo '<pre>' . print_r('$pv->getReplaceFileId()=' . $pv->getReplaceFileId(), true) . '</pre>'; if ($ep->getFile()->getId() == $pv->getReplaceFileId()) { $newFile = $this->createFile($pv->getFile()); $em->persist($newFile); $ep->setFile($newFile); $em->persist($ep); $oldFile = $em->getRepository('NovuscomCMFBundle:File')->find($pv->getReplaceFileId()); $em->remove($oldFile); $fileName = $_SERVER['DOCUMENT_ROOT'] . '/upload/images/' . $oldFile->getName(); if (is_file($fileName)) { unlink($fileName); } } } //exit; } else { /* * Создаем значение свойства */ $newFile = $this->createFile($pv->getFile()); $em->persist($newFile); //echo '<pre>' . print_r('создаем новый файл', true) . '</pre>'; //echo '<pre>' . print_r($pv, true) . '</pre>'; $ElementPropertyF = new ElementPropertyF(); $ElementPropertyF->setFile($newFile); $ElementPropertyF->setElement($entity); $ElementPropertyF->setProperty($property); $ElementPropertyF->setDescription($pv->getDescription()); $em->persist($ElementPropertyF); } } } else { if ($pv instanceof $ElementProperty) { //echo '<pre>' . print_r('это свойство', true) . '</pre>'; $ElementProperty->setValue($pv->getValue()); $ElementProperty->setElement($entity); $ElementProperty->setProperty($property); $em->persist($ElementProperty); } else { Utils::msg('Просто какое-то свойство'); } } } } else { /** * Создание свойства */ $ElementProperty = new ElementProperty(); $ElementProperty->setValue($property_value); $ElementProperty->setElement($entity); $ElementProperty->setProperty($property); $em->persist($ElementProperty); } } else { /** * Добавления свойства типа "дата/время" */ if (is_a($property_value, 'DateTime')) { //echo '<pre>' . print_r('это дата/время', true) . '</pre>'; $ElementProperty = new ElementPropertyDT(); $ElementProperty->setElement($entity); $ElementProperty->setProperty($property); $ElementProperty->setValue($property_value); $em->persist($ElementProperty); } else { foreach ($propArray[$property_id] as $k => $v) { if ((is_object($v) && method_exists($v, 'getValue')) == false) { continue; } //echo '<pre>' . print_r($v, true) . '</pre>'; $ElementProperty = new ElementProperty(); $ElementProperty->setValue($v->getValue()); $ElementProperty->setElement($entity); $ElementProperty->setProperty($property); $em->persist($ElementProperty); //$val->remove($k); $propArray[$property_id]->remove($k); //break; } if (method_exists($propArray[$property_id], 'isEmpty') && $propArray[$property_id]->isEmpty()) { //echo '<pre>' . print_r('Удаляем коллекецию, т.к. она уже пустая', true) . '</pre>'; unset($propArray[$property_id]); } } } } } } /** * Обновление разделов */ $Element->updateSections($entity, $newSections, $oldSections); /** * Превью пикча */ $file = $editForm['preview_picture']->getData(); if ($file) { //echo '<pre>' . print_r($file, true) . '</pre>'; $this->deletePreviewPicture($entity); $this->createPreviewPicture($entity, $file, $editForm['preview_picture_alt']->getData()); } else { //echo '<pre>' . print_r('Нет превью пикчи', true) . '</pre>'; } $this->downloadFile($editForm['preview_picture_src']->getData(), $entity, 'preview'); /* * Обновление описания картинки */ if ($preview_picture = $entity->getPreviewPicture()) { $preview_picture->setDescription($editForm['preview_picture_alt']->getData()); } //$em->persist($preview_picture); /** * Детейл пикча */ $file = $editForm['detail_picture']->getData(); if ($file) { $this->deleteDetailPicture($entity); $this->createDetailPicture($entity, $file, $editForm['detail_picture_alt']->getData()); } else { } $this->downloadFile($editForm['detail_picture_src']->getData(), $entity, 'detail'); if ($detail_picture = $entity->getDetailPicture()) { $detail_picture->setDescription($editForm['detail_picture_alt']->getData()); $em->persist($detail_picture); } $entity->setLastModified(new \DateTime('now')); $em->flush(); /** * Редирект */ $redirect_url = $this->generateUrl('admin_element_edit', array('id' => $id, 'block_id' => $block_id)); if ($section_id) { $redirect_url = $this->generateUrl('admin_element_edit_in_section', array('id' => $id, 'block_id' => $block_id, 'section_id' => $section_id)); } return $this->redirect($redirect_url); } else { } return $this->render('NovuscomCMFBundle:Element:edit.html.twig', array('entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView())); }