Incam_PROFIND_Web/www/protected/controllers/CandidatoController.php
2012-10-23 19:02:18 +00:00

259 lines
9.2 KiB
PHP

<?php
/**
* @class CandidatoController
* @brief Controlador del modelo del candidato
*
* @package application.controllers
*/
class CandidatoController extends Controller {
/**
* @return array action filters
*/
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
return array(
array('allow',
'actions' => array('index', 'view', 'create', 'update', 'delete', 'admin', 'updateEstado'),
'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),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate() {
$candidato = new Candidato;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Candidato'])) {
$candidato->attributes = $_POST['Candidato'];
if ($candidato->save())
$this->redirect(array('index'));
}
$this->render('create', array(
'candidato' => $candidato,
));
}
/**
* @brief Modificar un candidato.
* @param integer $id el ID del candidato a modificar
*/
public function actionUpdate($id) {
$candidato = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($candidato);
if (isset($_POST['Candidato'])) {
Yii::trace(CVarDumper::dumpAsString($_POST));
// Candidato
$candidato->attributes = $_POST['Candidato'];
$ficheroFotografia = CUploadedFile::getInstance($candidato, 'ficheroFotografia');
$quitarFotografia = Yii::app()->request->getParam('quitar_fotografia', '0');
// Idiomas del candidato
$listaIdiomas = array();
$listaIdiomasBorrar = array();
foreach ($_POST['CandidatoIdioma'] as $key => $idioma) {
if ($idioma['id'])
$candidatoIdioma = CandidatoIdioma::model()->findByPk($idioma['id']);
else
$candidatoIdioma = new CandidatoIdioma();
$candidatoIdioma->attributes = $idioma;
$candidatoIdioma->candidato_id = $candidato->id;
if ($idioma['_borrar'])
$listaIdiomasBorrar[] = $candidatoIdioma;
else
$listaIdiomas[] = $candidatoIdioma;
Yii::trace('Idioma ' . $key);
Yii::trace(CVarDumper::dumpAsString($candidatoIdioma->attributes));
}
$candidato->idiomas = $listaIdiomas;
Yii::trace('Idiomas a borrar');
Yii::trace(CVarDumper::dumpAsString($listaIdiomasBorrar));
// Guardar los datos
$transaccion = Yii::app()->db->beginTransaction();
try {
Yii::trace('Guardando el candidato', 'application.controllers.CandidatoController');
if (!$candidato->save())
throw new CException('Error al guardar el candidato');
if (($quitarFotografia == '1') && ($candidato->fotografia->tieneFotografia())) {
Yii::trace('Eliminando la fotografía del candidato', 'application.controllers.CandidatoController');
$candidato->fotografia->eliminarFotografia();
}
if ($ficheroFotografia) {
Yii::trace('Guardando la fotografía del candidato', 'application.controllers.CandidatoController');
$candidato->fotografia->guardarFotografia($ficheroFotografia);
}
if (!empty($listaIdiomasBorrar)) {
Yii::trace('Eliminando idiomas marcados para borrar', 'application.controllers.CandidatoController');
foreach ($listaIdiomasBorrar as $candidatoIdioma) {
if (!$candidatoIdioma->delete())
throw new CException('Error al eliminar un idioma del candidato');
}
}
if (!empty($candidato->idiomas)) {
Yii::trace('Guardando la lista de idiomas', 'application.controllers.CandidatoController');
foreach ($candidato->idiomas as $candidatoIdioma) {
if (!$candidatoIdioma->save())
throw new CException('Error al guardar un idioma del candidato');
}
}
$transaccion->commit();
Yii::trace('Candidato guardado', 'application.controllers.CandidatoController');
Yii::app()->user->setFlash('success', Yii::t('profind', 'Se ha actualizado el candidato'));
$this->redirect(array('index'));
} catch (Exception $e) {
Yii::trace($e->getMessage(), 'application.controllers.CandidatoController');
$transaccion->rollBack();
$errores = array_merge($candidato->getErrors() /*, $nueva_empresa->getErrors(), $nueva_subscripcion->getErrors()*/);
foreach ($errores as $campo => $mensaje) $resultado[$campo] = $mensaje;
Yii::trace(CVarDumper::dumpAsString($resultado), 'application.controllers.CandidatoController');
Yii::app()->user->setFlash('error', Yii::t('profind', 'Se ha producido un error al actualizar el candidato'));
}
}
$this->render('update', array(
'candidato' => $candidato,
));
}
public function actionUpdateEstado($id) {
$model = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if (isset($_GET['ajax']))
var_dump($_GET['ajax']);
if (isset($_POST['Candidato'])) {
$model->id_estado = $_POST['Candidato']['id_estado'];
if ($model->save()) {
} else {
}
} else {
echo $this->renderPartial('_form_estado', array('model' => $model, false, true));
}
}
/**
* 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
$this->loadModel($id)->delete();
$this->borrarFoto($id);
Yii::app()->user->setFlash('success', "Candidato eliminardo correctamente.");
// 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('index'));
}
else
throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
}
/**
* @brief Lista todos los candidatos
*
*/
public function actionIndex() {
$candidatos = new Candidato('search');
$candidatos->unsetAttributes(); // clear any default values
if (isset($_GET['Candidato']))
$candidatos->attributes = $_GET['Candidato'];
$this->render('index', array(
'candidatos' => $candidatos));
}
public function actionHistorial($id) {
$this->layout = '//layouts/candidato';
$this->render('historial', array(
'model' => $this->loadModel($id),
));
}
/**
* Manages all models.
*/
public function actionAdmin() {
$model = new Candidato('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Candidato']))
$model->attributes = $_GET['Candidato'];
$this->render('admin', array(
'model' => $model,
));
}
/**
* @brief Devuelve los datos del candidato con id $id.
* Si el candidato no existe, se lanza una excepción HTTP 404
* @param integer $id el ID del candidato a recuperar
*/
public function loadModel($id) {
Yii::trace('Cargar el modelo', 'application.controllers.CandidatoController');
$model = Candidato::model()->findByPk($id);
if ($model === null)
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
return $model;
}
}
?>