array('index', 'view', 'create', 'update', 'delete', 'admin', 'upload'), 'users' => array('@'), ), array('allow', // deny all users 'users' => array('*'), ), ); } /** * Displays a particular model. * @param integer $id the ID of the model to be displayed */ public function actionView($id) { $this->render('view', array( 'model' => $this->loadModel($id), )); } /** * Guarda el documento en el sistema * @param CandidatoDocumento $documento el documento * @param CUploadedFile $file el fichero asociado al documento a guardar * @return boolean true cuando el fichero se guarda correctamente */ private function guardarDocumento($documento, $file) { if (!is_null($file)) { $folder = $this->darRutaDocumentos() . $documento->candidato_id . '/'; if (!is_dir($folder)) mkdir($folder, 0755, true); $file->saveAs($folder . $documento->nombre_fichero); } } /** * Genera un nombre de fichero para guardar el documento. Se comprueba * que no exista ningĂșn otro fichero con ese mismo nombre. * @param CandidatoDocumento $model Documento * @return string */ private function generarNombreDocumento($model) { $cid = $model->candidato_id; $old_filename = FileHelper::sanitizeFileName(pathinfo($model->nombre_fichero, PATHINFO_FILENAME)); $ext = pathinfo($model->nombre_fichero, PATHINFO_EXTENSION); $folder = $this->darRutaDocumentos() . $cid . '/'; $contador = 1; $filename = $old_filename . '.' . $ext; // existe el directorio? if (is_dir($folder)) { // ya existe el fichero? while (file_exists($folder . $filename)) { $filename = $old_filename . '_' . $contador . '.' . $ext; $contador++; } } return $filename; } /** * Da la ruta donde se guardan los documentos * @return string ruta de los documentos */ private function darRutaDocumentos() { return Yii::app()->params['curriculumPath']; } /** * Borrar el documento del sistema * @param CandidatoDocumento $documento el documento a borrar * @return boolean true cuando el fichero se borra correctamente */ private function borrarDocumento($documento) { $cid = $documento->candidato_id; $folder = $this->darRutaDocumentos() . $cid . '/'; if (file_exists($folder . $documento->nombre_fichero)) unlink($folder . $documento->nombre_fichero); } /** * Descarga el documento * @param integer $id el ID del documento */ public function actionDownload($id) { $model = $this->loadModel($id); $cid = $model->candidato_id; $folder = $this->darRutaDocumentos() . $model->candidato_id . '/' . $model->nombre_fichero; GDownloadHelper::send($folder); } /** * Creates a new model. * @param integer $cid el ID del candidato */ public function actionCreate($cid) { $model = new CandidatoDocumento; $model->candidato_id = $cid; $candidato = Candidato::model()->findByPk($cid); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['CandidatoDocumento'])) { $model->attributes = $_POST['CandidatoDocumento']; $documento = CUploadedFile::getInstance($model, 'file'); $model->nombre_fichero = $documento->name; // Me aseguro que el nombre del fichero no tiene "cosas raras" $model->nombre_fichero = $this->generarNombreDocumento($model); if ($model->save()) { $this->guardarDocumento($model, $documento); Yii::app()->user->setFlash('success', Yii::t('intranet', 'CurrĂ­culum guardado correctamente.')); $url = $this->createUrl('index', array('cid' => $model->candidato_id)); $this->redirect($url); } } $this->render('create', array( 'model' => $model, 'candidato' => $candidato, )); } /** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['CandidatoDocumento'])) { $model->attributes = $_POST['CandidatoDocumento']; if ($model->save()) $this->redirect(array('view', 'id' => $model->id)); } $this->render('update', array( 'model' => $model, )); } /** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { if (Yii::app()->request->isPostRequest) { // we only allow deletion via POST request $model = $this->loadModel($id); if ($model->delete()) { $this->borrarDocumento($model); } // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!isset($_GET['ajax'])) $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } else throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.'); } /** * Lists all models. */ public function actionIndex($cid) { $this->layout = '//layouts/candidato'; $candidato = Candidato::model()->findByPk($cid); $dataProvider = new CActiveDataProvider('CandidatoDocumento', array( 'criteria' => array( 'condition' => 'candidato_id=' . $cid, ), )); $this->render('index', array( 'dataProvider' => $dataProvider, 'candidato' => $candidato, )); } /** * Manages all models. */ public function actionAdmin() { $model = new CandidatoDocumento('search'); $model->unsetAttributes(); // clear any default values if (isset($_GET['CandidatoDocumento'])) $model->attributes = $_GET['CandidatoDocumento']; $this->render('admin', array( 'model' => $model, )); } /** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = CandidatoDocumento::model()->findByPk($id); if ($model === null) throw new CHttpException(404, 'The requested page does not exist.'); return $model; } /** * Performs the AJAX validation. * @param CModel the model to be validated */ protected function performAjaxValidation($model) { if (isset($_POST['ajax']) && $_POST['ajax'] === 'candidato-documento-form') { echo CActiveForm::validate($model); Yii::app()->end(); } } } ?>