diff --git a/www/protected/controllers/CandidatoController.php b/www/protected/controllers/CandidatoController.php index f6c9f5c..a681147 100644 --- a/www/protected/controllers/CandidatoController.php +++ b/www/protected/controllers/CandidatoController.php @@ -25,7 +25,7 @@ class CandidatoController extends Controller { public function accessRules() { return array( array('allow', - 'actions' => array('index', 'view', 'create', 'update', 'delete', 'admin', 'updateEstado'), + 'actions' => array('index', 'view', 'create', 'update', 'delete', 'admin', 'updateEstado', 'download'), 'users' => array('@'), ), array('deny', // deny all users @@ -126,6 +126,17 @@ class CandidatoController extends Controller { throw new CHttpException(400, Yii::t('profind', 'Petición no válida')); } + /** + * Descarga el documento + * @param integer $id el ID del documento + */ + public function actionDownload($id) { + $documento = CandidatoDocumento::model()->findByPk($id); + if ($documento === null) + throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.')); + + return $documento->download(); + } /** * @brief Lista todos los candidatos diff --git a/www/protected/models/CandidatoDocumento.php b/www/protected/models/CandidatoDocumento.php index b02eedd..e3ccb42 100644 --- a/www/protected/models/CandidatoDocumento.php +++ b/www/protected/models/CandidatoDocumento.php @@ -133,7 +133,8 @@ class CandidatoDocumento extends CActiveRecord { } protected function afterSave() { - $this->guardarFicheroDocumento(); + if ($this->isNewRecord) + $this->guardarFicheroDocumento(); return parent::afterSave(); } @@ -150,7 +151,6 @@ class CandidatoDocumento extends CActiveRecord { protected function beforeValidate() { if ($this->ficheroDocumento) { - $upload = $this->candidato->getUploadPath(); $this->nombre_fichero = $this->generarNombreFicheroDocumento(); Yii::trace('Nombre para el documento: ' . $this->nombre_fichero, 'application.models.CandidatoDocumento'); } @@ -182,7 +182,17 @@ class CandidatoDocumento extends CActiveRecord { } } return $filename; - } + } + + public function download() { + $nombre = $this->darRutaCompletaFichero(); + return GDownloadHelper::send($nombre); + } + + public function darRutaCompletaFichero() { + $upload = $this->candidato->getUploadPath(); + return $upload . $this->nombre_fichero; + } /* * Guarda un documento subido por el usuario @@ -197,8 +207,7 @@ class CandidatoDocumento extends CActiveRecord { if (!$this->ficheroDocumento) throw new CException(Yii::t('profind', 'Fichero de documento no asignado.')); - $upload = $this->candidato->getUploadPath(); - $nombre = $upload . $this->nombre_fichero; + $nombre = $this->darRutaCompletaFichero(); return $this->ficheroDocumento->saveAs($nombre); } @@ -211,8 +220,7 @@ class CandidatoDocumento extends CActiveRecord { if (!$this->candidato) throw new CException(Yii::t('profind', 'Candidato no asignado.')); - $upload = $this->candidato->getUploadPath(); - $nombre = $upload . $this->nombre_fichero; + $nombre = $this->darRutaCompletaFichero(); return unlink($nombre); } } \ No newline at end of file diff --git a/www/themes/profind/views/candidato/__documentos.php b/www/themes/profind/views/candidato/__documentos.php index 675dce6..8ed7bff 100644 --- a/www/themes/profind/views/candidato/__documentos.php +++ b/www/themes/profind/views/candidato/__documentos.php @@ -58,7 +58,10 @@ Yii::app()->clientScript->registerScript('js_datepicker', $js_datepicker, CClien 'to_remove')); ?> hasErrors()) : ?> - + 'icon-download'), ''), + $this->createUrl('download', array("id"=>$documento->id)), + array('class' => 'btn btn-small download') + ); ?>