/** * @param EiVersion $copie * @param Doctrine_Connection $conn * @param null $ei_scenario_id * @param array $allowedTypes * @return EiVersion * @throws Exception */ public function createStructureCopy(EiVersion $copie, Doctrine_Connection $conn = null, $ei_scenario_id = null, array $allowedTypes = array()) { /** @var sfLogger $logger */ $logger = sfContext::getInstance()->getLogger(); // Déclaration de la table EiVersionStructure. /** @var EiVersionStructureTable $tableVersionStr */ $tableVersionStr = Doctrine_Core::getTable('EiVersionStructure'); // Tableau des correspondances. $correspondances = array(); $correspondancesF = array(); $version_id = $this->old_version_id == false ? $this->getId() : $this->old_version_id; $allowedTypes = count($allowedTypes) == 0 ? EiVersionStructure::getAllTypes() : $allowedTypes; $ignored = 0; //*******************************************************// //********** COPIE STRUCTURE **********// //*******************************************************// $logger->info("--------------------------------------------"); $logger->info("---------- COPIE SCENARIO ----------"); $logger->info("--------------------------------------------"); // Création d'une collection de structure de version. /** @var Doctrine_Collection $nouvelleStructure */ $nouvelleStructure = new Doctrine_Collection('EiVersionStructure'); // Variable temporaire contenant le noeud père. /** @var EiVersionStructure $ei_version_str_prec */ $ei_version_str_prec = null; // Variable temporaire contenant le niveau où l'on se situait dans l'arbre à la boucle précédente. $level_prec = 0; // Variables temporaires. /** @var EiVersionStructure $parent */ $root_id = $parent = null; // On récupère la structure complète de la version actuelle. $currentVersionTree = $tableVersionStr->getEiVersionTree($version_id, $allowedTypes); // On parcourt chaque élément. /** @var EiVersionStructure $ei_version_str */ foreach ($currentVersionTree as $i => $ei_version_str) { $logger->info("---------- PARCOURS ELT STRUCTURE N° " . $ei_version_str->getId() . " ----------"); if (in_array($ei_version_str->getType(), $allowedTypes)) { //*************************************************************// //********** DETERMINATION DU PERE **********// //*************************************************************// // Si le niveau précédent est inférieur au niveau parcouru, alors, le parent est la dernière version structure. if ($ei_version_str->getLevel() > $level_prec) { $parent = $ei_version_str_prec; } elseif ($ei_version_str->getLevel() < $level_prec) { // On réalise la différence entre les deux niveau. $j = $level_prec - $ei_version_str->getLevel(); // On remonte d'autant de fois afin de récupérer le père. while ($j > 0) { $parent = $parent->getNode()->getParent(); $j--; } } //*******************************************************************************// //********** ENREGISTREMENT DES INFORMATIONS DE BASE **********// //*******************************************************************************// $strElementCopie = new EiVersionStructure(); $strElementCopie->setRootId($root_id); $strElementCopie->setLft($ei_version_str->getLft()); $strElementCopie->setRgt($ei_version_str->getRgt()); $strElementCopie->setLevel($ei_version_str->getLevel()); $strElementCopie->setName($ei_version_str->getName()); $strElementCopie->setDescription($ei_version_str->getDescription()); $strElementCopie->setSlug($ei_version_str->getSlug()); $strElementCopie->setEiVersion($copie); $strElementCopie->setType($ei_version_str->getType()); // Si l'élément possède un père, on le précise. if ($parent) { $strElementCopie->setEiVersionStructureParentId($parent->getId()); } // On sauvegarde l'élément. $strElementCopie->save($conn); // Mise à jour du tableau des correspondances. $correspondances[$ei_version_str->getId()] = $strElementCopie->getId(); //**********************************************************// //********** CREATION FONCTION ***********// //**********************************************************// // S'il s'agit d'une fonction, on crée la copie. if ($ei_version_str->isEiFonction()) { $logger->info("---------- CREATION FONCTION ----------"); $fct = $ei_version_str->getEiFonction()->createCopie($strElementCopie, $conn); $strElementCopie->setEiFonctionId($fct->getId()); $correspondancesF[$ei_version_str->getEiFonctionId()] = $fct->getId(); } elseif ($ei_version_str->isEiBlock()) { $logger->info("---------- CREATION BLOCK ----------"); $ei_version_str_prec = $strElementCopie; } $level_prec = $ei_version_str->getLevel(); if ($i == 0) { $root_id = $strElementCopie->getId(); $strElementCopie->setRootId($root_id); $strElementCopie->save($conn); } $nouvelleStructure->add($strElementCopie); } } foreach ($currentVersionTree as $i => $ei_version_str) { //**********************************************************// //********** CAS BLOCK FOREACH **********// //**********************************************************// if ($ei_version_str->getType() == EiVersionStructure::$TYPE_FOREACH) { $logger->info("---------- CREATION FOREACH ----------"); /** @var EiMappingStructureSyncIn $oldMapping */ $oldMapping = $ei_version_str->getEiVersionStructureDataSetMapping()->getFirst(); $mapping = new EiMappingStructureSyncIn(); $mapping->setEiDatasetStructureId($oldMapping->getEiDatasetStructureId()); $mapping->setEiVersionStructureId($correspondances[$ei_version_str->getId()]); $mapping->save($conn); } elseif ($ei_version_str->isEiBlockParam()) { $logger->info("---------- CREATION BLOCK PARAM ----------"); // Copie des mappings IN et OUT. /** @var EiBlockDataSetMapping $oldMappingIn */ $oldMappingIn = $ei_version_str->getMappingDataSet(EiBlockDataSetMapping::$TYPE_IN); /** @var EiBlockDataSetMapping $oldMappingOut */ $oldMappingOut = $ei_version_str->getMappingDataSet(EiBlockDataSetMapping::$TYPE_OUT); if ($oldMappingIn != null) { $logger->info("---------- MAPPING IN : " . $oldMappingIn->getEiDatasetStructureId() . " / " . $correspondances[$ei_version_str->getId()] . " ----------"); $newMappingIn = new EiMappingStructureSyncIn(); $newMappingIn->setEiDatasetStructureId($oldMappingIn->getEiDatasetStructureId()); $newMappingIn->setEiVersionStructureId($correspondances[$ei_version_str->getId()]); $newMappingIn->save($conn); } if ($oldMappingOut != null) { $logger->info("---------- MAPPING OUT : " . $oldMappingOut->getEiDatasetStructureId() . " / " . $correspondances[$ei_version_str->getId()] . " ----------"); $newMappingOut = new EiMappingStructureSyncOut(); $newMappingOut->setEiDatasetStructureId($oldMappingOut->getEiDatasetStructureId()); $newMappingOut->setEiVersionStructureId($correspondances[$ei_version_str->getId()]); $newMappingOut->save($conn); } } elseif ($ei_version_str->isEiFonction() && in_array($ei_version_str->getType(), $allowedTypes)) { $logger->info("---------- CREATION PARAM FONCTION ----------"); // Récupération de l'ensemble des paramètres OUT. /** @var EiParamBlockFunctionMapping[] $outParams */ $outParams = $ei_version_str->getEiFonction()->getEiFunctionMapping(); $logger->info("---------- " . count($outParams) . " PARAMS."); // Copie de chaque paramètre de mapping OUT. /** @var EiParamBlockFunctionMapping $oldMapping */ foreach ($outParams as $oldMapping) { if ($oldMapping->getEiFunctionId() != "" && $oldMapping->getEiParamFunctionId() != "") { /** @var EiParamBlockFunctionMapping $mapping */ $mapping = new EiParamBlockFunctionMapping(); $mapping->setEiParamBlockId($oldMapping->getEiParamBlockId() == "" ? null : $correspondances[$oldMapping->getEiParamBlockId()]); $mapping->setEiFunctionId($correspondancesF[$oldMapping->getEiFunctionId()]); $mapping->setEiParamFunctionId($oldMapping->getEiParamFunctionId()); $mapping->save($conn); } } } elseif (!in_array($ei_version_str->getType(), $allowedTypes)) { $ignored++; } } // On affecte à la copie la structure complète. $copie->setEiVersionStructures($nouvelleStructure); // Sauvegarde de la copie de version. $copie->save($conn); //*****************************************************// //********** VERIFICATIONS **********// //*****************************************************// // On récupère l'arbre de la structure de la copie. $arbreNouvelleVersion = $tableVersionStr->getEiVersionTree($copie->getId()); // On vérifie que le nombre d'éléments est le même de chaque côté sinon on lève une exception. if (count($currentVersionTree) != count($arbreNouvelleVersion) + $ignored) { throw new Exception('Version copie failed'); } return $copie; }
/** * @param Doctrine_Connection $conn */ public function createAutoMapping(Doctrine_Connection $conn = null) { /** @var EiBlockForeach $block */ $block = $this; /** @var EiDataSetStructure $node */ $node = $this->getIteratorMapping()->getEiDataSetStructureMapping(); // Si le block est nouveau, on crée automatiquement les paramètres et le mapping avec le jeu de données. if ($block->isEiBlock() && count($block->getParams()) == 0) { // Récupération des paramètres de la structure mappée. $nodeParams = $node->getEiDataSetStructures(); /** @var EiDataSetStructure $param => Pour chaque noeud, on crée le paramètre pour le block. */ foreach ($nodeParams as $param) { if ($param->isLeaf() && $node->getNode()->getLevel() == $param->getNode()->getLevel() - 1) { // Création du paramètre pour le block. $paramBlock = new EiBlockParam(); $paramBlock->setName($param->getName()); $paramBlock->setEiVersionId($block->getEiVersionId()); $paramBlock->setEiVersionStructureParent($block); // Création du mapping IN. $paramBlockSyncIn = new EiMappingStructureSyncIn(); $paramBlockSyncIn->setEiDataSetStructureMapping($param); $paramBlockSyncIn->setEiVersionStructureMapping($paramBlock); // Création du mapping OUT. $paramBlockSyncOut = new EiMappingStructureSyncOut(); $paramBlockSyncOut->setEiDataSetStructureMapping($param); $paramBlockSyncOut->setEiVersionStructureMapping($paramBlock); // Persistence. $block->getNode()->addChild($paramBlock); $paramBlockSyncIn->save($conn); $paramBlockSyncOut->save($conn); } } } }