/** * Add the settings from the transformed import data to the formData and the model * * @param \ArrayObject $import * @param \MUtil_Model_ModelAbstract $model */ public function addExistingRoundsToModel(\ArrayObject $import, \MUtil_Model_ModelAbstract $model) { $currentRounds = $this->trackEngine->getRounds(); if (!$currentRounds) { return; } $importRounds = array(); $newImportRounds = array(); $tracker = $this->loader->getTracker(); foreach ($import['rounds'] as $lineNr => $roundData) { if (isset($roundData['survey_export_code'], $import['surveyCodes'][$roundData['survey_export_code']])) { $roundData['gro_id_survey'] = $import['surveyCodes'][$roundData['survey_export_code']]; $round = $tracker->createTrackClass('Round', $roundData); $importRounds[$round->getRoundOrder()] = $round->getFullDescription(); $newImportRounds[$round->getRoundOrder()] = sprintf($this->_('Set round to round %s'), $round->getFullDescription()); $import['roundOrderToLine'][$round->getRoundOrder()] = $lineNr; } } // Filter for rounds not in current track foreach ($currentRounds as $roundId => $round) { if ($round instanceof Round) { $order = $round->getRoundOrder(); if (isset($newImportRounds[$order])) { unset($newImportRounds[$order]); } } } $except = array(self::ROUND_LEAVE, self::ROUND_DEACTIVATE); $notEqualTo = array(); // Make sure no round is imported twice foreach ($currentRounds as $roundId => $round) { if ($round instanceof Round) { $name = "round_{$roundId}"; $order = $round->getRoundOrder(); $model->set($name, 'existingRound', true, 'required', true, 'roundId', $roundId); if (isset($importRounds[$order])) { if ($round->getFullDescription() == $importRounds[$order]) { $options = array(self::ROUND_LEAVE => $this->_('Leave current round'), $order => $this->_('Replace with import round')); } else { $options = array(self::ROUND_LEAVE => $this->_('Leave current round'), $order => sprintf($this->_('Replace with import round %s'), $importRounds[$order])); } $model->set($name, 'label', sprintf($this->_('Matching round %s'), $round->getFullDescription()), 'elementClass', 'Radio', 'multiOptions', $options); $value = $order; } else { $model->set($name, 'label', sprintf($this->_('Round not in import: %s'), $round->getFullDescription()), 'elementClass', 'Select', 'multiOptions', array(self::ROUND_LEAVE => sprintf($this->_('Leave current round %d unchanged'), $order), self::ROUND_DEACTIVATE => sprintf($this->_('Deactivate current round %d'), $order)) + $newImportRounds, 'size', 3 + count($newImportRounds)); $value = null; if ($notEqualTo) { $notEqualVal = new NotEqualExcept($notEqualTo, $except); $model->set($name, 'validators[notequal]', $notEqualVal); } $notEqualTo[] = $name; } if (!array_key_exists($name, $this->formData)) { $this->formData[$name] = $value; } } } }