Incam_PROFIND_Web/www/protected/models/FotografiaPerfil.php
2012-10-01 15:15:00 +00:00

96 lines
2.8 KiB
PHP

<?php
/**
* Modelo para la fotografía del usuario
*
*/
class FotografiaPerfil {
public $modelo;
/*
* Devuelve la fotografía del usuario
* @return string
*/
public function getThumbnail() {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
if ($this->tieneFotografia()) {
$fichero = $this->getRutaCompletaFicheroFotografia();
} else {
$fichero = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
$fichero .= 'user_photo.jpg';
}
$imgdata = base64_encode(file_get_contents($fichero));
return 'data:image/jpeg;base64,' . $imgdata;
}
/*
* Comprueba si el usuario ha subido su fotografía
* @return boolean
*/
public function tieneFotografia() {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$fichero = $this->getRutaCompletaFicheroFotografia();
return file_exists($fichero);
}
/*
* Devuelve la ruta completa (ruta y nombre) del fichero con la fotografía del usuario.
* @return string ruta completa del fichero
*/
private function getRutaCompletaFicheroFotografia() {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$upload = $this->modelo->getUploadPath();
$nombre = $this->getNombreFicheroFotografia();
return $upload . $nombre;
}
/*
* Devuelve el nombre del fichero con la fotografía del usuario.
* @return string nombre del fichero
*/
private function getNombreFicheroFotografia() {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
return $this->modelo->id . '.jpg';
}
/*
* Guarda una fotografía subida por el usuario
* return CUploadedFile fichero subido
*/
public function guardarFotografia($fichero) {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
if ($fichero) {
$nombre = $this->getRutaCompletaFicheroFotografia();
return $fichero->saveAs($nombre);
}
else
return false;
}
/*
* Elimina la fotografía del usuario
* return bool
*/
public function eliminarFotografia() {
if (!$this->modelo)
throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$fichero = $this->getRutaCompletaFicheroFotografia();
return unlink($fichero);
}
}
?>