コード例 #1
0
ファイル: Element.php プロジェクト: novuscom/cmfbundle
 public function updateSections($element, $newSections, $oldSections = false)
 {
     if (is_object($element)) {
         if (!$oldSections) {
             $oldSections = $this->getSections($element);
         }
         /*
          * Готовим массив разделов, в которые надо добавить элемент
          */
         $addSections = new ArrayCollection();
         foreach ($newSections as $n) {
             if (!$oldSections->contains($n)) {
                 $addSections->add($n);
             }
         }
         $countAddSections = count($addSections);
         /*
          * Добавляем элемент в разделы
          */
         if ($countAddSections > 0) {
             foreach ($addSections as $obj) {
                 $ElementSection = new ElementSection();
                 $ElementSection->setElement($element);
                 $ElementSection->setSection($obj);
                 $this->em->persist($ElementSection);
             }
         }
         /*
          * Готовим массив ИД разделов из которых элемент должен быть удален
          */
         $deleteSectionsId = array();
         foreach ($oldSections as $o) {
             // Раздел к которому был привязан элемент отсутствует в списке разделов к которым нужно привязать элемент
             //echo 'Удаляем из разделов: <pre>[' . print_r($o->getName(), true) . ']</pre>';
             if (!$newSections->contains($o)) {
                 $deleteSectionsId[] = $o->getId();
             }
         }
         $countDeleteSections = count($deleteSectionsId);
         /*
          * Удаляем элемент из разделов
          */
         //echo 'ИД разделов, из котрых элемент должен быть удален: <pre>' . print_r($deleteSectionsId, true) . '</pre>';
         $eSection = $element->getSection();
         $countESection = count($eSection);
         foreach ($eSection as $obj) {
             //$obj->getSection();
             //echo '<pre>' . print_r($obj->getSection(), true) . '</pre>';
             if ($obj->getSection()) {
                 if (in_array($obj->getSection()->getId(), $deleteSectionsId)) {
                     //echo '<pre>' . print_r('Удаляем '.$obj->getId(), true) . '</pre>';
                     $this->em->remove($obj);
                 }
             } else {
                 //echo '<pre>' . print_r('Элемент прикреплен к разделу NULL', true) . '</pre>';
                 if ($countAddSections > 0 && $countESection > 0 || $countAddSections == 0 && $countDeleteSections == 0 && $countESection > 1) {
                     $this->em->remove($obj);
                 }
             }
         }
         /*
          * Создаем нулевую запись, если элемент не прикрепелен к разделам
          */
         if ($countAddSections == 0 && $countESection == $countDeleteSections) {
             $ElementSection = new ElementSection();
             $ElementSection->setElement($element);
             $ElementSection->setSection(null);
             $this->em->persist($ElementSection);
         }
     } else {
         throw new \Exception('$element must be Element object');
     }
 }
コード例 #2
0
 /**
  *
  * Обновляет разделы элемента
  *
  * @param Element $entity Элемент для которого надо изменить разделы
  * @param Doctrine\Common\Collections\ArrayCollection $newSections Новые разделы элемента
  * @return bool
  */
 private function sectionsUpdate($entity, $newSections)
 {
     $em = $this->getDoctrine()->getManager();
     $result = false;
     $originalSections = $this->getElementSections($entity);
     foreach ($originalSections as $section) {
         //if ($section)
         //    echo '<pre>' . print_r($section->getName(), true) . '</pre>';
     }
     foreach ($newSections as $section) {
         //if ($section)
         //    echo '<pre>' . print_r($section->getName(), true) . '</pre>';
     }
     /**
      * Готовим массив ИД разделов из которых элемент должен быть удален
      */
     $deleteSectionsId = array();
     foreach ($originalSections as $o) {
         // Раздел к которому был привязан элемент отсутствует в списке раздлов к которым нужно привязать элемент
         //echo '<pre>' . print_r($o->getName(), true) . '</pre>';
         if (!$newSections->contains($o)) {
             $deleteSectionsId[] = $o->getSection()->getId();
         }
     }
     /**
      * Готовим массив новых разделов к которым необходимо привязать элемент
      */
     $addSections = new ArrayCollection();
     foreach ($newSections as $n) {
         // Элемент не был привязан к данному разделу - добавляем его в списко новых
         if (!$originalSections->contains($n)) {
             $addSections->add($n);
         }
     }
     //echo '<pre>' . print_r('ИД удаляемых разделов', true) . '</pre>';
     //echo '<pre>' . print_r($deleteSectionsId, true) . '</pre>';
     /**
      * Удаляем элементы из разделов
      */
     /*
     if ($deleteSections) {
         // Проходим по всем привязкам к разделам
         foreach ($entity->getSection() as $s) {
             // Если раздел есть в списке удаляемых - удаляем
             if ($deleteSections->contains($s)) {
                 $em->remove($s);
             }
         }
     }
     */
     //exit;
     $deleteSectionsId = array();
     if ($originalSections->isEmpty()) {
     } else {
         /*
          * У элемета были разделы
          */
         //echo '<pre>' . print_r('Оригнальные разделы:', true) . '</pre>';
         foreach ($originalSections as $o) {
             if (!$newSections->contains($o)) {
                 /*
                  * Добавляем раздел в список удаляемых, т.к. он не содержится в новом списке разделов
                  */
                 $deleteSectionsId[] = $o->getId();
             }
         }
     }
     /*
      * Создаем массив новых разделов
      */
     $addSections = new ArrayCollection();
     foreach ($newSections as $n) {
         if (!$originalSections->contains($n)) {
             $addSections->add($n);
         }
     }
     /*
      * Удаляем разделы
      */
     if ($deleteSectionsId) {
         foreach ($entity->getSection() as $obj) {
             if (in_array($obj->getId(), $deleteSectionsId)) {
                 $em->remove($obj);
             }
         }
     }
     $countAddSections = count($addSections);
     if ($countAddSections > 0) {
         /*
          * Добавляем элемент в разделы
          */
         foreach ($addSections as $obj) {
             $ElementSection = new ElementSection();
             $ElementSection->setElement($entity);
             $ElementSection->setSection($obj);
             $em->persist($ElementSection);
         }
     }
     if ($countEntitySections == 0 && $countAddSections == 0) {
         /*
          * У элемента не было разделов вообще (не было связи с разделдами, даже NULL)
          * Новых раздлов у элемента также нет
          * Создаем связь с NULL разделом
          */
         $ElementSection = new ElementSection();
         $ElementSection->setElement($entity);
         $ElementSection->setSection(null);
         $em->persist($ElementSection);
     }
     return $result;
 }