Beispiel #1
0
 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()));
 }
 /**
  * @param FormBuilderInterface $builder
  * @param array $options
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     //echo '<pre>' . print_r('build ElementPropertyType', true) . '</pre>'; exit;
     //$request = $this->request;
     //$params = $request->get('_route_params');
     //$block = $this->em->getRepository('NovuscomCMFBundle:Block')->find($params['id']);
     //return true;
     //echo '<pre>' . print_r(array_keys($options['data']['VALUES']), true) . '</pre>';
     //exit;
     //$builder->add('value');
     //echo '<pre>' . print_r(count($options['data']['BLOCK_PROPERTIES']), true) . '</pre>'; exit;
     foreach ($options['data']['BLOCK_PROPERTIES'] as $p) {
         //$value = $p->getValue();
         //echo '<pre>' . print_r($p->getType(), true) . '</pre>';
         //echo '<pre>' . print_r($p->getInfo(), true) . '</pre>';
         //$info = $p->getInfo();
         $info = json_decode($p->getInfo(), true);
         $is_multiple = is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true;
         $required = is_array($info) && array_key_exists('REQUIRED', $info) && $info['REQUIRED'] == true;
         //echo '<pre>' . print_r($p->getType(), true) . '</pre>';
         $property_reference = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\Property', $p->getId());
         switch ($p->getType()) {
             /**
              * Поле типа "Список"
              */
             case 'LIST':
                 /**
                  * Получаем значения свойства
                  */
                 $PropertyList = $this->em->getRepository('NovuscomCMFBundle:PropertyList')->findBy(array('property' => $property_reference));
                 $choices = array();
                 foreach ($PropertyList as $pl) {
                     $choices[$pl->getId()] = $pl->getValue();
                 }
                 /**
                  * Получаем значение элемента
                  */
                 $values = $this->getPropertyValues($p->getId());
                 $data = false;
                 if ($values) {
                     $data = $values[0];
                 }
                 /**
                  * Формируем поле
                  */
                 $choiceOptions = array('choices' => $choices, 'required' => false, 'multiple' => false, 'label' => $p->getName(), 'mapped' => false, 'data' => $data);
                 if (isset($this->data['VALUES'][$p->getId()]) && is_numeric($this->data['VALUES'][$p->getId()])) {
                     $choiceOptions['data'] = $this->data['VALUES'][$p->getId()];
                 }
                 $builder->add($p->getId(), ChoiceType::class, $choiceOptions);
                 break;
             case 'E':
                 $elements = $this->em->getRepository('NovuscomCMFBundle:Element')->findBy(array('block' => $info['BLOCK_ID']));
                 $choices = array();
                 foreach ($elements as $e) {
                     $choices[$e->getName()] = $e->getId();
                 }
                 //echo '<pre>' . print_r($choices, true) . '</pre>';
                 if (is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true) {
                     //$FPECollection = new \Doctrine\Common\Collections\ArrayCollection();
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     /*if (isset($this->data['VALUES']) && is_array($this->data['VALUES']) && array_key_exists($p->getId(), $this->data['VALUES'])) {
                     			foreach ($this->data['VALUES'][$p->getId()] as $v) {
                     				$FPE = new ElementProperty();
                     				$FPE->setValue($v);
                     				$FPECollection->add($FPE);
                     			}
                     		}*/
                     /*$builder->add($p->getId(), ChoiceType::class,
                     			array(
                     				//'type' => new ElementPropertyEMultipleType(
                     				//	$choices, array('PROPERTY' => $p)
                     				//),
                     				'entry_type' => Element::class,
                     				'mapped' => false,
                     				'allow_add' => true,
                     				'label' => $p->getName(),
                     				'allow_delete' => true,
                     				//'cascade_validation' => true,
                     				'by_reference' => false,
                     				'data' => $FPECollection
                     			)
                     		);*/
                     /*$builder->addEventListener(
                     			FormEvents::BIND,
                     			function (FormEvent $event) {
                     				echo '<pre>' . print_r('BIND', true) . '</pre>';
                     				$data = $event->getData();
                     				$lineups = $data->getLineups();
                     				foreach ($lineups as &$lineup) {
                     					$lineup->setMatch($data);
                     				}
                     				$event->setData($data);
                     			}
                     		);*/
                     $choiceOptions = array('choices' => $choices, 'required' => false, 'multiple' => true, 'label' => $p->getName(), 'mapped' => false);
                     //echo '<pre>'.print_r($this->data['VALUES'], true).'</pre>';
                     if (isset($options['data']['VALUES'][$p->getId()]) && $options['data']['VALUES'][$p->getId()]) {
                         //$choiceOptions['data'] = $this->data['VALUES'][$p->getId()];
                         //echo '<pre>'.print_r($options['data']['VALUES'][$p->getId()], true).'</pre>';
                         $choiceOptions['data'] = $options['data']['VALUES'][$p->getId()];
                     }
                     $builder->add($p->getId(), ChoiceType::class, $choiceOptions);
                 } else {
                     $choiceOptions = array('choices' => $choices, 'required' => false, 'multiple' => false, 'label' => $p->getName(), 'mapped' => false);
                     //Utils::msg($options['data']['VALUES'][$p->getId()][0]);
                     if (isset($options['data']['VALUES'][$p->getId()]) && is_numeric($options['data']['VALUES'][$p->getId()][0])) {
                         $choiceOptions['data'] = $options['data']['VALUES'][$p->getId()][0];
                     }
                     $builder->add($p->getId(), ChoiceType::class, $choiceOptions);
                 }
                 break;
             case "SECTION":
                 $sections = new ArrayCollection();
                 $builderData = $builder->getData();
                 $element = $builderData['ELEMENT_ENTITY'];
                 if ($element->getId()) {
                     $ElementPropertySection = $this->em->getRepository('NovuscomCMFBundle:ElementPropertySection')->findBy(array('element' => $element, 'property' => $property_reference));
                     $sectionsId = array();
                     foreach ($ElementPropertySection as $es) {
                         $sectionsId[] = $es->getSection()->getId();
                     }
                     if ($sectionsId) {
                         $sectionsEntity = $this->em->getRepository('NovuscomCMFBundle:Section')->findBy(array('id' => $sectionsId));
                         foreach ($sectionsEntity as $s) {
                             $sections->add($s);
                         }
                     }
                 }
                 $builder->add($p->getId(), EntityType::class, array('label' => 'Раздел', 'class' => 'NovuscomCMFBundle:Section', 'choice_label' => 'indentedTitle', 'multiple' => false, 'required' => false, 'mapped' => true, 'attr' => array('size' => '20'), 'data' => $sections[0], 'query_builder' => function ($er) use($info) {
                     return $er->createQueryBuilder('s')->where("s.block = :block")->orderBy('s.root, s.lft', 'ASC')->setParameters(array('block' => $info['BLOCK_ID']));
                 }));
                 break;
             case 'F':
                 if ($is_multiple) {
                     $dataAtr = array();
                     if (isset($options['data']['PROPERTY_FILE_VALUES'][$p->getId()]) && is_array($options['data']['PROPERTY_FILE_VALUES']) && array_key_exists($p->getId(), $options['data']['PROPERTY_FILE_VALUES'])) {
                         $filesId = $options['data']['PROPERTY_FILE_VALUES'][$p->getId()];
                         $files = $this->em->getRepository('NovuscomCMFBundle:File')->findBy(array('id' => $filesId));
                         if (count($files) > 0) {
                             $liip = $options['data']['LIIP'];
                             $dataAtr['files'] = array();
                             foreach ($files as $file) {
                                 $fileInfo = array();
                                 $originalPath = '/upload/etc/' . $file->getName();
                                 $path = $liip->getBrowserPath($originalPath, 'my_thumb');
                                 $fileInfo['path'] = $path;
                                 $fileInfo['original_path'] = $originalPath;
                                 $fileInfo['file_id'] = $file->getId();
                                 $fileInfo['property_id'] = $p->getId();
                                 $dataAtr['files'][] = $fileInfo;
                             }
                             //Utils::msg($dataAtr);
                         }
                     }
                     $jsonData = json_encode($dataAtr);
                     $builder->add($p->getId(), CollectionType::class, array('entry_type' => ElementPropertyFMultipleType::class, 'mapped' => false, 'allow_add' => true, 'label' => $p->getName(), 'allow_delete' => true, 'by_reference' => false, 'label_attr' => array('class' => 'files-property', 'data-type' => 'files', 'data-data' => $jsonData, 'data-id' => $p->getId())));
                 }
                 break;
                 /**
                  * Дата/время
                  */
             /**
              * Дата/время
              */
             case 'DATE_TIME':
                 $field_options = array('label' => $p->getName(), 'mapped' => false, 'required' => false);
                 if ($info && is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true) {
                 } else {
                     if (isset($options['data']['VALUES'][$p->getId()][0])) {
                         //echo '<pre>'.print_r($options['data']['VALUES'][$p->getId()][0], true).'</pre>';
                         if ($options['data']['VALUES'][$p->getId()][0] instanceof \DateTime) {
                             $field_options['data'] = $options['data']['VALUES'][$p->getId()][0];
                         }
                     }
                 }
                 $builder->add($p->getId(), DateTimeType::class, $field_options);
                 break;
             case 'TEXT':
                 $field_options = array('label' => $p->getName(), 'mapped' => false, 'required' => false);
                 if (isset($this->data['VALUES'][$p->getId()])) {
                     $field_options['data'] = $this->data['VALUES'][$p->getId()][0];
                 }
                 $field_options['attr'] = array('style' => 'height: 150px;');
                 $builder->add($p->getId(), TextareaType::class, $field_options);
                 break;
             case 'HTML':
                 $field_options = array('label' => $p->getName(), 'mapped' => false, 'required' => false);
                 //echo '<pre>' . print_r($options['data']['VALUES'], true) . '</pre>';
                 if (isset($options['data']['VALUES']) && is_array($options['data']['VALUES']) && array_key_exists($p->getId(), $options['data']['VALUES'])) {
                     $field_options['data'] = $options['data']['VALUES'][$p->getId()][0];
                 }
                 $field_options['attr'] = array('style' => 'height: 300px;', 'class' => 'tinymce');
                 $builder->add($p->getId(), TextareaType::class, $field_options);
                 break;
             case 'N':
                 $field_options = array('label' => $p->getName(), 'mapped' => false, 'required' => false);
                 if (isset($options['data']['VALUES'][$p->getId()])) {
                     $field_options['data'] = $options['data']['VALUES'][$p->getId()][0];
                 }
                 $builder->add($p->getId(), TextType::class, $field_options);
                 break;
             case 'U':
                 $users = $this->em->getRepository('NovuscomCMFUserBundle:User')->findAll();
                 $users_array = array();
                 foreach ($users as $u) {
                     $users_array[$u->getUsername()] = $u->getId();
                 }
                 $field_options = array('choices' => $users_array, 'required' => false, 'label' => $p->getName(), 'mapped' => false, 'attr' => array('class' => 'form-control'));
                 if (isset($this->data['VALUES'][$p->getId()])) {
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     $field_options['data'] = $this->data['VALUES'][$p->getId()][0];
                 }
                 $builder->add($p->getId(), ChoiceType::class, $field_options);
                 break;
             default:
                 //echo '<pre>' . print_r($p->getInfo(), true) . '</pre>';
                 //$info = json_decode($p->getInfo(), true);
                 //echo '<pre>' . print_r($info, true) . '</pre>';
                 if ($info && is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true) {
                     $data = array();
                     /*
                     						$element_reference = $this->em->getReference('Novuscom\CMFBundle\Entity\Element', 251);
                     						$property_reference = $this->em->getReference('Novuscom\CMFBundle\Entity\Property', $p->getId());
                     						$ElementProperty = $this->em->getRepository('NovuscomCMFBundle:ElementProperty')->findBy(array(
                     							'element'=>$element_reference,
                     							'property'=>$property_reference
                     						));*/
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     $collection = new \Doctrine\Common\Collections\ArrayCollection();
                     foreach ($this->getPropertyValues($p->getId()) as $v) {
                         $ep = new ElementProperty();
                         $ep->setValue($v);
                         $collection->add($ep);
                     }
                     $builder->add($p->getId(), CollectionType::class, array('entry_type' => ElementPropertySMultipleType::class, 'mapped' => false, 'allow_add' => true, 'label' => $p->getName(), 'allow_delete' => true, 'by_reference' => false, 'label_attr' => array()));
                 } else {
                     $optionsArray = array('mapped' => false, 'label' => $p->getName(), 'required' => $required, 'attr' => array('class' => 'form-control'));
                     if (isset($options['data']['VALUES']) && is_array($options['data']['VALUES']) && array_key_exists($p->getId(), $options['data']['VALUES'])) {
                         $optionsArray['data'] = $options['data']['VALUES'][$p->getId()][0];
                     }
                     $builder->add($p->getId(), TextType::class, $optionsArray);
                 }
         }
         /*$builder->add('replaced_files', CollectionType::class,
         			array(
         				'type' => new ElementPropertyFMultipleType(false, false, array('REPLACED' => true)),
         				'mapped' => false,
         				'allow_add' => true,
         				'allow_delete' => true,
         				'cascade_validation' => true,
         				'by_reference' => false,
         				'label_attr' => array(
         					'class' => 'files-property',
         					'data-type' => 'files',
         				)
         			)
         		);
         		$builder->add('deleted_files', CollectionType::class,
         			array(
         				'type' => new ElementPropertyFMultipleType(false, false, array('DELETED' => true)),
         				'mapped' => false,
         				'allow_add' => true,
         				'allow_delete' => true,
         				'cascade_validation' => true,
         				'by_reference' => false,
         				'label_attr' => array(
         					'class' => 'files-property',
         					'data-type' => 'files',
         				)
         			)
         		);*/
         //$builder->add('tags', 'collection', array('type' => new ElementPropertyStringType($this->options)));
         //$propertyStringForm = new ElementPropertyStringType($this->options);
         //$builder->add($p->getCode(), $propertyStringForm, array('mapped' => false, 'label' => 'Текстовые свойства'));
     }
     //$propertyStringForm = new ElementPropertyStringType($this->options);
     //$builder->add('value', $propertyStringForm, array('mapped' => false, 'label' => 'Текстовые свойства'));
     //$builder->add('tags', 'collection', array('type' => new ElementPropertyStringType()));
     /*$builder->addEventListener(
     			FormEvents::PRE_SET_DATA,
     			function (FormEvent $event) {
     				$form = $event->getForm();
     				$data = $event->getData();
     				echo '<pre>' . print_r($form->get('11')->getData(), true) . '</pre>';
     
     			}
     		);*/
 }
 /**
  * @param FormBuilderInterface $builder
  * @param array $options
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     //return true;
     //$builder->add('value');
     foreach ($this->options as $p) {
         //$value = $p->getValue();
         //echo '<pre>' . print_r($p->getType(), true) . '</pre>';
         //echo '<pre>' . print_r($p->getInfo(), true) . '</pre>';
         //$info = $p->getInfo();
         $info = json_decode($p->getInfo(), true);
         $is_multiple = is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true;
         $required = is_array($info) && array_key_exists('REQUIRED', $info) && $info['REQUIRED'] == true;
         switch ($p->getType()) {
             /**
              * Поле типа "Список"
              */
             case 'LIST':
                 /**
                  * Получаем значения свойства
                  */
                 $property_reference = $this->em->getReference('Novuscom\\CMFBundle\\Entity\\Property', $p->getId());
                 $PropertyList = $this->em->getRepository('NovuscomCMFBundle:PropertyList')->findBy(array('property' => $property_reference));
                 $choices = array();
                 foreach ($PropertyList as $pl) {
                     $choices[$pl->getId()] = $pl->getValue();
                 }
                 /**
                  * Получаем значение элемента
                  */
                 $values = $this->getPropertyValues($p->getId());
                 $data = false;
                 if ($values) {
                     $data = $values[0];
                 }
                 /**
                  * Формируем поле
                  */
                 $choiceOptions = array('choices' => $choices, 'required' => false, 'multiple' => false, 'label' => $p->getName(), 'mapped' => false, 'data' => $data);
                 if (isset($this->data['VALUES'][$p->getId()]) && is_numeric($this->data['VALUES'][$p->getId()])) {
                     $choiceOptions['data'] = $this->data['VALUES'][$p->getId()];
                 }
                 $builder->add($p->getId(), 'choice', $choiceOptions);
                 break;
             case 'E':
                 $elements = $this->em->getRepository('NovuscomCMFBundle:Element')->findBy(array('block' => $info['BLOCK_ID']));
                 $choices = array();
                 foreach ($elements as $e) {
                     $choices[$e->getId()] = $e->getName();
                 }
                 //echo '<pre>' . print_r($choices, true) . '</pre>';
                 if (array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true) {
                     $FPECollection = new \Doctrine\Common\Collections\ArrayCollection();
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     if (isset($this->data['VALUES']) && is_array($this->data['VALUES']) && array_key_exists($p->getId(), $this->data['VALUES'])) {
                         foreach ($this->data['VALUES'][$p->getId()] as $v) {
                             $FPE = new ElementProperty();
                             $FPE->setValue($v);
                             $FPECollection->add($FPE);
                         }
                     }
                     $builder->add($p->getId(), 'collection', array('type' => new ElementPropertyEMultipleType($choices, array('PROPERTY' => $p)), 'mapped' => false, 'allow_add' => true, 'label' => $p->getName(), 'allow_delete' => true, 'cascade_validation' => true, 'by_reference' => false, 'data' => $FPECollection));
                     /*$builder->addEventListener(
                           FormEvents::BIND,
                           function (FormEvent $event) {
                               echo '<pre>' . print_r('BIND', true) . '</pre>';
                               $data = $event->getData();
                               $lineups = $data->getLineups();
                               foreach ($lineups as &$lineup) {
                                   $lineup->setMatch($data);
                               }
                               $event->setData($data);
                           }
                       );*/
                 } else {
                     $choiceOptions = array('choices' => $choices, 'required' => false, 'multiple' => false, 'label' => $p->getName(), 'mapped' => false);
                     if (isset($this->data['VALUES'][$p->getId()]) && is_numeric($this->data['VALUES'][$p->getId()])) {
                         $choiceOptions['data'] = $this->data['VALUES'][$p->getId()];
                     }
                     $builder->add($p->getId(), 'choice', $choiceOptions);
                 }
                 break;
             case 'F':
                 if ($is_multiple) {
                     $dataAtr = array();
                     //echo '<pre>' . print_r($this->data['VALUES'], true) . '</pre>';
                     if (isset($this->data['PROPERTY_FILE_VALUES'][$p->getId()]) && is_array($this->data['PROPERTY_FILE_VALUES']) && array_key_exists($p->getId(), $this->data['PROPERTY_FILE_VALUES'])) {
                         //$choiceOptions['data'] = $this->data['VALUES'][$p->getId()];
                         $filesId = $this->data['PROPERTY_FILE_VALUES'][$p->getId()];
                         //echo '<pre>' . print_r($filesId, true) . '</pre>';
                         $files = $this->em->getRepository('NovuscomCMFBundle:File')->findBy(array('id' => $filesId));
                         if (count($files) > 0) {
                             $liip = $this->data['LIIP'];
                             $dataAtr['files'] = array();
                             foreach ($files as $file) {
                                 $fileInfo = array();
                                 $originalPath = '/upload/images/' . $file->getName();
                                 $path = $liip->getBrowserPath($originalPath, 'my_thumb');
                                 $fileInfo['path'] = $path;
                                 $fileInfo['original_path'] = $originalPath;
                                 $fileInfo['file_id'] = $file->getId();
                                 $fileInfo['property_id'] = $p->getId();
                                 $dataAtr['files'][] = $fileInfo;
                             }
                         }
                     }
                     $jsonData = json_encode($dataAtr);
                     $builder->add($p->getId(), 'collection', array('type' => new ElementPropertyFMultipleType(), 'mapped' => false, 'allow_add' => true, 'label' => $p->getName(), 'allow_delete' => true, 'cascade_validation' => true, 'by_reference' => false, 'label_attr' => array('class' => 'files-property', 'data-type' => 'files', 'data-data' => $jsonData, 'data-id' => $p->getId())));
                 }
                 break;
                 /**
                  * Дата/время
                  */
             /**
              * Дата/время
              */
             case 'D':
                 $field_options = array('label' => $p->getName(), 'mapped' => false, 'required' => false);
                 if (isset($this->data['VALUES'][$p->getId()])) {
                     if (is_a($this->data['VALUES'][$p->getId()][0], 'DateTime')) {
                         $field_options['data'] = $this->data['VALUES'][$p->getId()][0];
                     }
                 }
                 $builder->add($p->getId(), 'datetime', $field_options);
                 break;
             case 'U':
                 $users = $this->em->getRepository('NovuscomCMFUserBundle:User')->findAll();
                 $users_array = array();
                 foreach ($users as $u) {
                     $users_array[$u->getId()] = $u->getUsername();
                 }
                 $field_options = array('choices' => $users_array, 'required' => false, 'label' => $p->getName(), 'mapped' => false, 'attr' => array('class' => 'form-control'));
                 if (isset($this->data['VALUES'][$p->getId()])) {
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     $field_options['data'] = $this->data['VALUES'][$p->getId()][0];
                 }
                 $builder->add($p->getId(), 'choice', $field_options);
                 break;
             default:
                 //echo '<pre>' . print_r($p->getInfo(), true) . '</pre>';
                 //$info = json_decode($p->getInfo(), true);
                 //echo '<pre>' . print_r($info, true) . '</pre>';
                 if ($info && is_array($info) && array_key_exists('MULTIPLE', $info) && $info['MULTIPLE'] == true) {
                     $data = array();
                     /*
                                             $element_reference = $this->em->getReference('Novuscom\CMFBundle\Entity\Element', 251);
                                             $property_reference = $this->em->getReference('Novuscom\CMFBundle\Entity\Property', $p->getId());
                                             $ElementProperty = $this->em->getRepository('NovuscomCMFBundle:ElementProperty')->findBy(array(
                                                 'element'=>$element_reference,
                                                 'property'=>$property_reference
                                             ));*/
                     //echo '<pre>' . print_r($this->data['VALUES'][$p->getId()], true) . '</pre>';
                     $collection = new \Doctrine\Common\Collections\ArrayCollection();
                     foreach ($this->getPropertyValues($p->getId()) as $v) {
                         $ep = new ElementProperty();
                         $ep->setValue($v);
                         $collection->add($ep);
                     }
                     $builder->add($p->getId(), 'collection', array('type' => new ElementPropertySMultipleType($data), 'mapped' => false, 'allow_add' => true, 'label' => $p->getName(), 'allow_delete' => true, 'cascade_validation' => true, 'by_reference' => false, 'data' => $collection));
                 } else {
                     $optionsArray = array('mapped' => false, 'label' => $p->getName(), 'required' => $required, 'attr' => array('class' => 'form-control'));
                     if (isset($this->data['VALUES']) && is_array($this->data['VALUES']) && array_key_exists($p->getId(), $this->data['VALUES'])) {
                         $optionsArray['data'] = $this->data['VALUES'][$p->getId()][0];
                     }
                     $builder->add($p->getId(), 'text', $optionsArray);
                 }
         }
         $builder->add('replaced_files', 'collection', array('type' => new ElementPropertyFMultipleType(false, false, array('REPLACED' => true)), 'mapped' => false, 'allow_add' => true, 'allow_delete' => true, 'cascade_validation' => true, 'by_reference' => false, 'label_attr' => array('class' => 'files-property', 'data-type' => 'files')));
         $builder->add('deleted_files', 'collection', array('type' => new ElementPropertyFMultipleType(false, false, array('DELETED' => true)), 'mapped' => false, 'allow_add' => true, 'allow_delete' => true, 'cascade_validation' => true, 'by_reference' => false, 'label_attr' => array('class' => 'files-property', 'data-type' => 'files')));
         //$builder->add('tags', 'collection', array('type' => new ElementPropertyStringType($this->options)));
         //$propertyStringForm = new ElementPropertyStringType($this->options);
         //$builder->add($p->getCode(), $propertyStringForm, array('mapped' => false, 'label' => 'Текстовые свойства'));
     }
     //$propertyStringForm = new ElementPropertyStringType($this->options);
     //$builder->add('value', $propertyStringForm, array('mapped' => false, 'label' => 'Текстовые свойства'));
     //$builder->add('tags', 'collection', array('type' => new ElementPropertyStringType()));
     /*$builder->addEventListener(
                 FormEvents::PRE_SET_DATA,
                 function (FormEvent $event) {
                     $form = $event->getForm();
                     $data = $event->getData();
                     echo '<pre>' . print_r($form->get('11')->getData(), true) . '</pre>';
     
                 }
             );*/
 }