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); } } ?>