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;
 }