/**
  * Update or create the Criteria of a StepCondition.
  *
  * @param StepCondition $condition The condition to Update
  * @param array         $criteria  The list of criteria of the StepCondition
  *
  * @return array
  */
 public function updateCriteriaGroups(StepCondition $condition, $level = 0, Criteriagroup $parent = null, array $criteria = [])
 {
     $currentOrder = 0;
     $processedGroups = [];
     $existingGroups = $condition->getCriteriagroups();
     foreach ($criteria as $groupStructure) {
         if (empty($group->cgid) || !$existingGroups->containsKey($group->cgid)) {
             // Current CriteriaGroup has never been published or has been deleted => create it
             $criteriaGroup = $this->criteriaManager->createGroup($condition, $level, $parent, $currentOrder, $groupStructure);
         } else {
             // CriteriaGroup already exists => update it
             $criteriaGroup = $existingGroups->get($group->cgid);
             $criteriaGroup = $this->criteriaManager->editGroup($condition, $level, $parent, $currentOrder, $groupStructure, $criteriaGroup);
         }
         // Store CriteriaGroup to know it doesn't have to be deleted when we will clean the StepCondition
         $processedGroups[] = $criteriaGroup;
         // Process children of current group
         if (!empty($groupStructure->criteriagroup)) {
             $childrenLevel = $level + 1;
             $childrenGroups = $this->updateCriteriaGroups($condition, $childrenLevel, $criteriaGroup, $groupStructure->criteriagroup);
             // Store children groups
             $processedGroups = array_merge($processedGroups, $childrenGroups);
         }
         ++$currentOrder;
     }
     return $processedGroups;
 }
 /**
  * Update criteriagroups for a condition
  *
  * @param StepCondition $conditionDB
  * @param int $level
  * @param array $criteriagroupsJS
  * @return array
  */
 protected function publishCriteriagroups(StepCondition $conditionDB, $level = 0, Criteriagroup $parentCG = null, array $criteriagroupsJS = array())
 {
     $processedCriteriagroups = array();
     $currentOrder = 0;
     // Retrieve existing criteriagroups for this condition
     $existingCriteriagroups = $conditionDB->getCriteriagroups();
     foreach ($criteriagroupsJS as $criteriagroupJS) {
         //echo "criteriagroupid ".$criteriagroupJS->id."<br>\n";
         // Current criteriagroup has never been published or criteriagroup entity has been deleted => create it
         if (empty($criteriagroupJS->cgid) || !$existingCriteriagroups->containsKey($criteriagroupJS->cgid)) {
             //echo "create group <br>\n";
             $criteriagroupDB = $this->stepConditionsManager->createCriteriagroup($level, $currentOrder, $parentCG, $conditionDB);
             $uniqId = "_CG" . uniqid();
             $this->uniqId2cg[$uniqId] = $criteriagroupDB;
             // Update json structure with new resource ID
             $criteriagroupJS->cgid = $uniqId;
         } else {
             //echo "edit group <br>\n";
             //retrieve CG
             $criteriagroupDB = $existingCriteriagroups->get($criteriagroupJS->cgid);
             //edit CG in DB
             $criteriagroupDB = $this->stepConditionsManager->editCriteriagroup($level, $currentOrder, $parentCG, $conditionDB, $criteriagroupDB);
         }
         //echo "Manage criteria <br>\n";
         // Manage criteria
         $existingCriteria = $criteriagroupDB->getCriteria();
         $publishedCriteria = $this->publishCriteria($criteriagroupJS, $criteriagroupDB);
         //echo "Clean criteria to remove <br>\n";
         // Clean criteria to remove
         $this->cleanCriteria($publishedCriteria, $existingCriteria->toArray(), $criteriagroupDB);
         // Store criteriagroup to know it doesn't have to be deleted when we will clean the condition
         $processedCriteriagroups[] = $criteriagroupDB;
         //Check children criteriagroup
         if (!empty($criteriagroupJS->criteriagroup)) {
             $childrenLevel = $level + 1;
             $childrenCriteriagroups = $this->publishCriteriagroups($conditionDB, $childrenLevel, $criteriagroupDB, $criteriagroupJS->criteriagroup);
             // Store children criteriagroup
             $processedCriteriagroups = array_merge($processedCriteriagroups, $childrenCriteriagroups);
         }
         $currentOrder++;
     }
     return $processedCriteriagroups;
 }