public function downloadAction()
 {
     $updateId = (int) $this->_getParam('id');
     $version = $this->_session->versions[$updateId];
     $updateFile = new UpdateFile();
     $uploadDir = $updateFile->getUploadDir();
     $error = false;
     if (!is_dir($uploadDir)) {
         $error = $uploadDir . ' directory does not exists';
         trigger_error($error, E_USER_NOTICE);
     } else {
         if (!is_writable($uploadDir)) {
             $error = $uploadDir . ' directory is not writable';
             trigger_error($error, E_USER_NOTICE);
         }
     }
     if ($error !== false) {
         $ret = $error;
     } else {
         if (($ret = $this->_fetch('download', array('updateFileId' => $updateId))) === false) {
             $ret = __('There was an error connecting to HealthCloud');
             trigger_error($ret, E_USER_NOTICE);
         } else {
             try {
                 $filename = tempnam(sys_get_temp_dir(), 'uf_');
                 file_put_contents($filename, $ret);
                 $updateFile->active = 1;
                 $updateFile->dateTime = date('Y-m-d H:i:s');
                 try {
                     $updateFile->populateWithArray($this->_session->versions[$updateId]);
                     if (substr($updateFile->name, -3) == '.gz') {
                         $updateFile->name = substr($updateFile->name, 0, -3);
                     }
                     $updateFile->version = $version['version'];
                     $updateFile->persist();
                     $contents = $updateFile->verify($filename);
                     unset($this->_session->versions[$updateId]);
                     $ret = true;
                     list($index, $next) = each($this->_session->versions);
                     if ($next !== null) {
                         $ret = array('next' => $next);
                     }
                 } catch (Exception $e) {
                     $error = __('Invalid signature');
                     $msg = $error . ': ' . $e->getMessage();
                     try {
                         $xml = new SimpleXMLElement($ret);
                         if ($xml->error) {
                             $msg = (string) $xml->error->errorMsg;
                         }
                     } catch (Exception $ex) {
                     }
                     $ret = $msg;
                     $updateFile->setPersistMode(WebVista_Model_ORM::DELETE);
                     trigger_error($ret, E_USER_NOTICE);
                 }
                 $updateFile->persist();
             } catch (Exception $e) {
                 $error = __('There was an error with the downloaded file');
                 $ret = $error . ': ' . $e->getMessage();
                 $error .= ' Error code: ' . $e->getCode() . ' Error Message: ' . $e->getMessage();
                 trigger_error($error, E_USER_NOTICE);
             }
         }
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct($ret);
 }