public function SetPropertyValues($element, array $properties = array()) { if (!$properties) { return null; } $currentValues = $this->getPropertiesValues($element, array_keys($properties)); $currentSectionValues = $this->getPropertiesSectionValues($element, array_keys($properties)); $removed = array(); $currentById = array(); foreach ($currentValues as $prop) { if (array_key_exists($prop['property_id'], $properties)) { $newValue = $properties[$prop['property_id']]; if (is_array($newValue)) { if (in_array($prop['value'], $newValue) == false) { $removed[] = $prop; } $currentById[$prop['property_id']][] = $prop['value']; } else { $currentById[$prop['property_id']] = $prop['value']; } } } $added = array(); $Section = new SectionEntity(); $currentSectionV = array(); foreach ($currentSectionValues as $info) { $ref = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\ElementPropertySection', $info['id']); $prop = $properties[$info['property_id']]; if ($info['section_id'] != $prop->getId()) { //Utils::msg('изменяем запись'); //Utils::msg($info); $ref->setSection($prop); $this->em->persist($prop); $currentSectionV[] = $prop->getId(); } else { $currentSectionV[] = $info['section_id']; } } //Utils::msg($currentSectionV); //exit; foreach ($properties as $key => $value) { $propertyReference = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\Property', $key); if (is_array($value)) { if (isset($currentById[$key])) { $currentVal = $currentById[$key]; } else { $currentVal = array(); } $diff = array_diff($value, $currentVal); foreach ($diff as $d) { $added[] = array('value' => $d, 'description' => null, 'element_id' => $element->getId(), 'property_id' => $key); } } //Utils::msg(gettype($value)); // Надо пернести ниже? if (is_object($value)) { if ($value instanceof $Section) { Utils::msg($value->getId()); Utils::msg($currentSectionV); //exit; if (in_array($value->getId(), $currentSectionV) == false) { $ElementPropertySection = new ElementPropertySection(); $ElementPropertySection->setProperty($propertyReference); $ElementPropertySection->setElement($element); $ElementPropertySection->setSection($value); $this->em->persist($ElementPropertySection); } } else { if ($value instanceof \DateTime) { //Utils::msg($value); $ElementPropertyDT = new ElementPropertyDT(); $ElementPropertyDT->setElement($element); $ElementPropertyDT->setProperty($propertyReference); $ElementPropertyDT->setValue($value); $this->em->persist($ElementPropertyDT); } else { Utils::msg('Class: ' . get_class($value)); } } } /*if (is_array($value)) { foreach ($value as $v) { Utils::msg($v); } }*/ } //Utils::msg('-------added--------'); //Utils::msg($added); //Utils::msg('-------removed--------'); //Utils::msg($removed); //exit; foreach ($added as $addArray) { $propertyReference = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\Property', $addArray['property_id']); $value = $addArray['value']; if (isset($value['file']) && $value['file'] instanceof UploadedFile) { //$serviceFile = $elementFile = new ElementPropertyF(); //Utils::msg($value); //exit; $nFile = new File($value['file']); $elementFile->setDescription($value['description']); $elementFile->setElement($element); $elementFile->setProperty($propertyReference); $elementFile->setFile($nFile); //Utils::msg('аплоадим файл'); //exit; $this->em->persist($nFile); $this->em->persist($elementFile); $this->file->uploadFile($nFile); } else { $ep = new ElementProperty(); $ep->setDescription($addArray['description']); $ep->setElement($element); $ep->setProperty($propertyReference); $ep->setValue($value); $this->em->persist($ep); } } foreach ($removed as $removeArray) { $ref = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\ElementProperty', $removeArray['id']); $this->em->remove($ref); } //exit; $this->em->flush(); //exit; }
/** * 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>'; }*/ if ($editForm->has('properties')) { //echo '<pre>' . print_r('У формы есть свойства', true) . '</pre>'; exit; /** * Собираем массив свойств */ $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; /** * Получаем свойства элемента */ $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()]; //echo '<pre>' . print_r($val, true) . '</pre>'; 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>'; foreach ($propArray[$ep->getProperty()->getId()] as $k => $v) { //echo '<pre>' . print_r($v->getValue(), true) . '</pre><hr/>'; $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; } //echo '<pre>' . print_r($val, true) . '</pre>'; if ($val->isEmpty()) { //echo '<pre>' . print_r('Удаляем коллекецию, т.к. она уже пустая', true) . '</pre>'; unset($propArray[$ep->getProperty()->getId()]); } } } } //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(); 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); } } } $ElementProperty = new ElementProperty(); if ($pv instanceof $ElementProperty) { //echo '<pre>' . print_r('это свойство', true) . '</pre>'; $ElementProperty->setValue($pv->getValue()); $ElementProperty->setElement($entity); $ElementProperty->setProperty($property); $em->persist($ElementProperty); } } } 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) { //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 ($propArray[$property_id]->isEmpty()) { //echo '<pre>' . print_r('Удаляем коллекецию, т.к. она уже пустая', true) . '</pre>'; unset($propArray[$property_id]); } } } } } } //echo '<pre>' . print_r($propArray, true) . '</pre>'; //exit; //echo '<hr/>'; /** * Обновление разделов */ $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 { //echo '<pre>' . print_r('Нет детейл пикчи', true) . '</pre>'; } $this->downloadFile($editForm['detail_picture_src']->getData(), $entity, 'detail'); if ($detail_picture = $entity->getDetailPicture()) { //echo '<pre>' . print_r('есть detailPicture', true) . '</pre>'; //exit; $detail_picture->setDescription($editForm['detail_picture_alt']->getData()); $em->persist($detail_picture); } $entity->setLastModified(new \DateTime('now')); //exit; $em->flush(); /** * Очищаем кэш */ $this->clearElementsListCache($request, $block_id); $this->clearElementCache($request, $block_id); /** * Редирект */ $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 { //echo '<pre>' . print_r('форма не валидна', true) . '</pre>'; //echo $editForm->getErrorsAsString(); } return $this->render('NovuscomCMFBundle:Element:edit.html.twig', array('entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView())); }