/** * Copy a track and all it's related data (rounds/fields etc) * * @param inte $oldTrackId The id of the track to copy * @return int The id of the copied track */ public function copyTrack($oldTrackId) { $trackModel = $this->tracker->getTrackModel(); $roundModel = $this->getRoundModel(true, 'rounds'); $fieldModel = $this->getFieldsMaintenanceModel(); // First load the track $trackModel->applyParameters(array('id' => $oldTrackId)); $track = $trackModel->loadFirst(); // Create an empty track $newTrack = $trackModel->loadNew(); unset($track['gtr_id_track'], $track['gtr_changed'], $track['gtr_changed_by'], $track['gtr_created'], $track['gtr_created_by']); $track['gtr_track_name'] .= $this->_(' - Copy'); $newTrack = $track + $newTrack; // Now save (not done yet) $savedValues = $trackModel->save($newTrack); $newTrackId = $savedValues['gtr_id_track']; // Now copy the fields $fieldModel->applyParameters(array('id' => $oldTrackId)); $fields = $fieldModel->load(); if ($fields) { $oldIds = array(); $numFields = count($fields); $newFields = $fieldModel->loadNew($numFields); foreach ($newFields as $idx => $newField) { $field = $fields[$idx]; $oldIds[$idx] = $field['gtf_id_field']; unset($field['gtf_id_field'], $field['gtf_changed'], $field['gtf_changed_by'], $field['gtf_created'], $field['gtf_created_by']); $field['gtf_id_track'] = $newTrackId; $newFields[$idx] = $field + $newFields[$idx]; } // Now save (not done yet) $savedValues = $fieldModel->saveAll($newFields); foreach ($savedValues as $idx => $field) { $oldNewFieldMap[$oldIds[$idx]] = $field['gtf_id_field']; } } else { $numFields = 0; } // Now copy the rounds and map the gro_id_relation to the right field $roundModel->applyParameters(array('id' => $oldTrackId)); $rounds = $roundModel->load(); if ($rounds) { $numRounds = count($rounds); $newRounds = $roundModel->loadNew($numRounds); foreach ($newRounds as $idx => $newRound) { $round = $rounds[$idx]; unset($round['gro_id_round'], $round['gro_changed'], $round['gro_changed_by'], $round['gro_created'], $round['gro_created_by']); $round['gro_id_track'] = $newTrackId; if (array_key_exists('gro_id_relationfield', $round) && $round['gro_id_relationfield'] > 0) { $round['gro_id_relationfield'] = $oldNewFieldMap[$round['gro_id_relationfield']]; } $newRounds[$idx] = $round + $newRounds[$idx]; } // Now save (not done yet) $savedValues = $roundModel->saveAll($newRounds); } else { $numRounds = 0; } //MUtil_Echo::track($track, $copy); //MUtil_Echo::track($rounds, $newRounds); //MUtil_Echo::track($fields, $newFields); Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage(sprintf($this->_('Copied track, including %s round(s) and %s field(s).'), $numRounds, $numFields)); return $newTrackId; }
/** * * @return string Internal code of the track */ public function getCode() { static $track = false; if (!$track) { $track = $this->tracker->getTrackModel()->loadFirst(array('gtr_id_track' => $this->_respTrackData['gr2t_id_track'])); } if (is_array($track)) { return $track['gtr_code']; } else { return false; } }