/**
  * TODO: A vérifier
  * ADD block to stack.
  *
  * @param EiTestSet $testSet
  * @param EiVersionStructure $block
  * @param EiTestSetBlockStack $previousStack
  * @param EiTestSetBlockStack $parentStack
  * @param int $position
  * @param Doctrine_Connection $conn
  * @return EiTestSetBlockStack
  */
 public function addToStack(EiTestSet $testSet, EiVersionStructure $block, EiTestSetBlockStack $previousStack = null, EiTestSetBlockStack $parentStack = null, $position = 1, Doctrine_Connection $conn = null)
 {
     $conn = $conn == null ? Doctrine_Manager::connection() : $conn;
     $parStrId = $parentStack != null ? $parentStack->getEiVersionStructureId() : "";
     $prevStrId = $previousStack != null ? $previousStack->getEiVersionStructureId() : "";
     $strId = $block->getId();
     $dataSetStr = null;
     //        var_dump($block->toArray(false));exit;
     // On définit l'index du précédent ainsi que sa fragmentation
     if ($previousStack != null) {
         $prevIndex = $previousStack->getRepetitionIndex();
         $prevPartsCount = $previousStack->getPartsCount();
         $prevPartsIndex = $previousStack->getPartIndex();
     } else {
         $prevIndex = $prevPartsCount = $prevPartsIndex = 0;
     }
     // On définit l'index du père ainsi que sa fragmentation
     if ($parentStack != null) {
         $parIndex = $parentStack->getRepetitionIndex();
         $parPartsCount = $parentStack->getPartsCount();
         $parPartsIndex = $parentStack->getPartIndex();
     } else {
         $parIndex = $parPartsCount = $parPartsIndex = 0;
     }
     //***********************************************************//
     //**********     GESTION ASSOCIATION STACK/JDD     **********//
     //***********************************************************//
     if ($parentStack != null) {
         $dataSetStr = $this->getDataFromDataSet($testSet, $block, $previousStack, $parentStack);
     } else {
         $dataSetStr = $this->getDataFromDataSet($testSet, $block);
     }
     //*********************************************************//
     //**********     AJOUT ELEMENT DANS LA STACK     **********//
     //*********************************************************//
     // On crée le nouvel élément de la pile.
     $stack = new EiTestSetBlockStack();
     $stack->setEiTestSet($testSet);
     $stack->setEiVersionStructure($block);
     $stack->setEiTestSetDataSet($dataSetStr);
     $stack->setPosition($position);
     if ($parentStack != null && $parentStack->getEiVersionStructure()->isEiLoop() && $block->getId() == $parStrId) {
         $stack->setEiTestSetBlockStackParent($parentStack->getEiTestSetBlockStackParent());
     } else {
         $stack->setEiTestSetBlockStackParent($parentStack);
     }
     // Vérification de la fragmentation du block.
     $stack->setPartsCount($this->getFragmentsCount($stack));
     //        var_dump($strId . " VS " . $parStrId);
     //        var_dump($parPartsIndex . " VS " . $parPartsCount);
     // Récupération de l'index.
     // Cas où il y a un précédent de même type que le suivant et que le précédent est terminé.
     if ($previousStack != null && $strId == $prevStrId && !($prevPartsIndex < $prevPartsCount)) {
         //            var_dump("CAS INDEX PART 1");
         // On incrémente alors son index car cela signifie que l'on passe à la boucle suivante.
         $stack->setRepetitionIndex($prevIndex + 1);
     } elseif ($previousStack != null && $strId == $prevStrId && $prevPartsIndex < $prevPartsCount) {
         //            var_dump("CAS INDEX PART 2");
         // On n'incrémente pas l'index car cela signifie que nous sommes dans la même boucle.
         $stack->setRepetitionIndex($prevIndex);
         // Et on incrémente la partie.
         $stack->setPartIndex($prevPartsIndex + 1);
     } elseif ($parentStack != null && $strId == $parStrId && !($parPartsIndex < $parPartsCount)) {
         //            var_dump("CAS INDEX PART 3");
         $stack->setRepetitionIndex($parIndex + 1);
     } elseif ($parentStack != null && $strId == $parStrId && $parPartsIndex < $parPartsCount) {
         //            var_dump("CAS INDEX PART 4");
         $stack->setRepetitionIndex($parIndex);
         $stack->setPartIndex($parPartsIndex + 1);
     } else {
         //            var_dump("CAS INDEX PART 5");
     }
     $stack->save($conn);
     return $stack;
 }