Tarea #1085 -> Falta la foto en el perfil del agente

git-svn-id: https://192.168.0.254/svn/Proyectos.Incam_PROFIND_Web/trunk@31 3fe1ab16-cfe0-e34b-8c9f-7d8c168d430d
This commit is contained in:
roberto 2012-09-26 11:07:00 +00:00
parent 034fd83959
commit 736f2b3e42
8 changed files with 433 additions and 50 deletions

BIN
www/images/user_photo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -39,21 +39,30 @@ class UsuarioController extends Controller {
if ($id != Yii::app()->user->id)
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
$model = $this->loadModel($id);
$usuario = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
// $this->performAjaxValidation($usuario);
if (isset($_POST['Usuario'])) {
$model->attributes = $_POST['Usuario'];
if ($model->save()) {
$usuario->attributes = $_POST['Usuario'];
$ficheroFotografia = CUploadedFile::getInstance($usuario, 'ficheroFotografia');
$quitarFotografia = Yii::app()->request->getParam('quitar_fotografia', '0');
if ($usuario->save()) {
if (($quitarFotografia == '1') && ($usuario->fotografia->tieneFotografia()))
$usuario->fotografia->eliminarFotografia();
if ($ficheroFotografia)
$usuario->fotografia->guardarFotografia($ficheroFotografia);
Yii::app()->user->setFlash('success', Yii::t('profind', 'Se ha actualizado el perfil'));
$this->redirect(array('modificar', 'id' => $model->id));
$this->redirect(array('modificar', 'id' => $usuario->id));
}
}
$this->render('modificar', array(
'model' => $model,
'model' => $usuario,
));
}
@ -66,6 +75,7 @@ class UsuarioController extends Controller {
$model = Usuario::model()->findByPk($id);
if ($model === null)
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
return $model;
}

View File

@ -0,0 +1,46 @@
<?php
/*
* Borrado recursivo de un directorio y su contenido
*
* @param string directorio
* @param boolean Si es TRUE vacía el directorio pero no elimina
* @return boolean
*/
function recursive_remove_directory($directory, $empty=FALSE)
{
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory))
{
return FALSE;
}elseif(is_readable($directory))
{
$handle = opendir($directory);
while (FALSE !== ($item = readdir($handle)))
{
if($item != '.' && $item != '..')
{
$path = $directory.'/'.$item;
if(is_dir($path))
{
recursive_remove_directory($path);
}else{
unlink($path);
}
}
}
closedir($handle);
if($empty == FALSE)
{
if(!rmdir($directory))
{
return FALSE;
}
}
}
return TRUE;
}
?>

View File

@ -1,9 +1,9 @@
<?php
/**
* This is the model class for table "tbl_usuarios".
* Modelo del usuario
*
* The followings are the available columns in table 'tbl_usuarios':
* Propiedades
* @property integer $id
* @property integer $id_empresa
* @property string $estado
@ -18,8 +18,10 @@
* @property string $last_login_time
* @property string $clave_seguridad
* @property string $descripcion
* @property string $ficheroFotografia
* @property UsuarioFotografia $fotografia
*
* The followings are the available model relations:
* Relaciones
* @property Empresa $empresa
*/
class Usuario extends CActiveRecord {
@ -28,13 +30,13 @@ class Usuario extends CActiveRecord {
const ESTADO_ACTIVO = 1;
const ESTADO_DENEGADO = 2;
//public $password_repeat;
//public $empresa;
public $ficheroFotografia;
public $fotografia;
public function getEstaActivo() {
return ($this->estado == self::ESTADO_ACTIVO);
}
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
@ -43,7 +45,7 @@ class Usuario extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
@ -54,22 +56,27 @@ class Usuario extends CActiveRecord {
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('email, password', 'required'),
array('estado', 'length', 'max'=>1),
array('estado', 'length', 'max' => 1),
array('email', 'email'),
array('email', 'unique'),
array('descripcion', 'safe'),
array('email, nombre, apellidos, password, tipo, titulo, localidad, telefono', 'length', 'max'=>255),
array('id, id_empresa, estado, email, nombre, apellidos, tipo, titulo, localidad, telefono, descripcion', 'safe', 'on'=>'search'),
array('email, nombre, apellidos, password, tipo, titulo, localidad, telefono', 'length', 'max' => 255),
array('ficheroFotografia', 'file',
'types' => 'jpg',
'maxSize' => 1024 * 1024 * 1, // 1MB como máximo
'tooLarge' => Yii::t('profind', 'La imagen es demasiado pesada. Elija otra fotografía más pequeña.'),
'wrongType' => Yii::t('profind', 'Sólo se permiten imágenes en formato JPG.'),
'allowEmpty' => 'true',
),
array('id, id_empresa, estado, email, nombre, apellidos, tipo, titulo, localidad, telefono, descripcion', 'safe', 'on' => 'search'),
);
}
}
/**
* @return array relational rules.
*/
@ -77,16 +84,17 @@ class Usuario extends CActiveRecord {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'empresa' => array(self::HAS_ONE, 'Empresa', 'id'),
'empresa' => array(self::HAS_ONE, 'Empresa', 'id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'ficheroFotografia' => 'Fotografía',
'estado' => 'Estado',
'email' => 'Email',
'nombre' => 'Nombre',
@ -97,37 +105,98 @@ class Usuario extends CActiveRecord {
'localidad' => 'Localidad',
'telefono' => 'Teléfono',
'descripcion' => 'Descripción',
);
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
$criteria=new CDbCriteria;
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('id_empresa',$this->id_empresa);
$criteria->compare('estado',$this->estado,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('nombre',$this->nombre,true);
$criteria->compare('apellidos',$this->apellidos,true);
$criteria->compare('tipo',$this->tipo,true);
$criteria->compare('titulo',$this->titulo,true);
$criteria->compare('localidad',$this->localidad,true);
$criteria->compare('telefono',$this->telefono,true);
$criteria->compare('last_login_time',$this->last_login_time,true);
$criteria->compare('descripcion',$this->descripcion,true);
$criteria->compare('id', $this->id);
$criteria->compare('id_empresa', $this->id_empresa);
$criteria->compare('estado', $this->estado, true);
$criteria->compare('email', $this->email, true);
$criteria->compare('nombre', $this->nombre, true);
$criteria->compare('apellidos', $this->apellidos, true);
$criteria->compare('tipo', $this->tipo, true);
$criteria->compare('titulo', $this->titulo, true);
$criteria->compare('localidad', $this->localidad, true);
$criteria->compare('telefono', $this->telefono, true);
$criteria->compare('last_login_time', $this->last_login_time, true);
$criteria->compare('descripcion', $this->descripcion, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
'criteria' => $criteria,
));
}
protected function afterFind() {
parent::afterFind();
$this->fotografia = new UsuarioFotografia();
$this->fotografia->usuario = $this;
}
protected function afterConstruct() {
parent::afterConstruct();
$this->fotografia = new UsuarioFotografia();
$this->fotografia->usuario = $this;
}
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord)
$this->createUploadDir();
}
protected function afterDelete() {
parent::afterDelete();
$this->deleteUploadDir();
}
/*
* Devuelve la ruta con los ficheros del usuario.
* Incluye el separador de directorios al final de la ruta.
* @return string ruta
*/
public function getUploadPath() {
return Yii::getPathOfAlias('application.uploads') . DIRECTORY_SEPARATOR . $this->id . DIRECTORY_SEPARATOR;
}
/*
* Crea un directorio para almacenar ficheros del usuario
* @return boolean
*/
private function createUploadDir() {
$upload = $this->getUploadPath();
if(!is_dir($upload)) {
return mkdir($upload);
}
else return false;
}
/*
* Elimina el directorio del usuario y todos sus ficheros
* @return boolean
*/
private function deleteUploadDir() {
$upload = $this->getUploadPath();
if(is_dir($upload)) {
require_once( Yii::getPathOfAlias('application.helpers') . DIRECTORY_SEPARATOR . 'recursive_remove_directory.php');
return recursive_remove_directory($upload);
}
else return false;
}
/*
* Función para cifrar la contraseña del usuario (temporal)
* @return string contraseña cifrada
*/
public function encrypt($value) {
return md5($value);
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* Modelo para la fotografía del usuario
*
*/
class UsuarioFotografia {
public $usuario;
/*
* Devuelve la fotografía del usuario
* @return string
*/
public function getThumbnail() {
if (!$this->usuario)
throw new CException(Yii::t('profind', 'Usuario 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->usuario)
throw new CException(Yii::t('profind', 'Usuario 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->usuario)
throw new CException(Yii::t('profind', 'Usuario no asignado.'));
$upload = $this->usuario->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->usuario)
throw new CException(Yii::t('profind', 'Usuario no asignado.'));
return $this->usuario->id . '.jpg';
}
/*
* Guarda una fotografía subida por el usuario
* return CUploadedFile fichero subido
*/
public function guardarFotografia($fichero) {
if (!$this->usuario)
throw new CException(Yii::t('profind', 'Usuario 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->usuario)
throw new CException(Yii::t('profind', 'Usuario no asignado.'));
$fichero = $this->getRutaCompletaFicheroFotografia();
return unlink($fichero);
}
}
?>

View File

@ -0,0 +1,126 @@
/* ===========================================================
* bootstrap-fileupload.js j1a
* http://jasny.github.com/bootstrap/javascript.html#fileupload
* ===========================================================
* Copyright 2012 Jasny BV, Netherlands.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================== */
!function ($) {
"use strict"; // jshint ;_
/* INPUTMASK PUBLIC CLASS DEFINITION
* ================================= */
var Fileupload = function (element, options) {
this.$element = $(element)
this.type = this.$element.data('uploadtype') || (this.$element.find('.thumbnail').length > 0 ? "image" : "file")
this.$input = this.$element.find(':file')
if (this.$input.length === 0) return
this.name = this.$input.attr('name') || options.name
this.$hidden = this.$element.find(':hidden[name="'+this.name+'"]')
if (this.$hidden.length === 0) {
this.$hidden = $('<input type="hidden" />')
this.$element.prepend(this.$hidden)
}
this.$preview = this.$element.find('.fileupload-preview')
var height = this.$preview.css('height')
if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height)
this.$remove = this.$element.find('[data-dismiss="fileupload"]')
this.listen()
}
Fileupload.prototype = {
listen: function() {
this.$input.on('change.fileupload', $.proxy(this.change, this))
if (this.$remove) this.$remove.on('click.fileupload', $.proxy(this.clear, this))
},
change: function(e, invoked) {
var file = e.target.files !== undefined ? e.target.files[0] : { name: e.target.value.replace(/^.+\\/, '') }
if (!file || invoked === 'clear') return
this.$hidden.val('')
this.$hidden.attr('name', '')
this.$input.attr('name', this.name)
if (this.type === "image" && this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match('\\.(gif|png|jpe?g)$')) && typeof FileReader !== "undefined") {
var reader = new FileReader()
var preview = this.$preview
var element = this.$element
reader.onload = function(e) {
preview.html('<img src="' + e.target.result + '" ' + (preview.css('max-height') != 'none' ? 'style="max-height: ' + preview.css('max-height') + ';"' : '') + ' />')
element.addClass('fileupload-exists').removeClass('fileupload-new')
}
reader.readAsDataURL(file)
} else {
this.$preview.text(file.name)
this.$element.addClass('fileupload-exists').removeClass('fileupload-new')
}
},
clear: function(e) {
this.$hidden.val('')
this.$hidden.attr('name', this.name)
this.$input.attr('name', '')
this.$preview.html('')
this.$element.addClass('fileupload-new').removeClass('fileupload-exists')
this.$input.trigger('change', [ 'clear' ])
e.preventDefault()
return false
}
}
/* INPUTMASK PLUGIN DEFINITION
* =========================== */
$.fn.fileupload = function (options) {
return this.each(function () {
var $this = $(this)
, data = $this.data('fileupload')
if (!data) $this.data('fileupload', (data = new Fileupload(this, options)))
})
}
$.fn.fileupload.Constructor = Fileupload
/* INPUTMASK DATA-API
* ================== */
$(function () {
$('body').on('click.fileupload.data-api', '[data-provides="fileupload"]', function (e) {
var $this = $(this)
if ($this.data('fileupload')) return
$this.fileupload($this.data())
if ($(e.target).data('dismiss') == 'fileupload') $(e.target).trigger('click.fileupload')
})
})
}(window.jQuery)

View File

@ -35,7 +35,7 @@
<a class="brand" href="<?php echo Yii::app()->params['frontpage']; ?>"><i class="icon-home icon-white"></i> <?php echo CHtml::encode(Yii::app()->name); ?></a>
<ul class="nav user_menu pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo Yii::app()->user->name; ?> <b class="caret"></b></a>
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo CHtml::encode(Yii::app()->user->name); ?> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><?php echo CHtml::link(Yii::t('profind', 'Cambiar la password'), $this->createUrl('seguridadUsuario/cambiarPassword', array('id' => Yii::app()->user->id))); ?></li>
<li class="divider"></li>

View File

@ -1,16 +1,30 @@
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/js/bootstrap-inputmask.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-inputmask.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-fileupload.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/lib/chosen/chosen.jquery.min.js'); ?>
<?php Yii::app()->clientScript->registerScript(
'chosen', "
$(document).ready(function(){
$('#Usuario_localidad').chosen({
allow_single_deselect: true
});
allow_single_deselect: true
});
});
");
?>
<?php Yii::app()->clientScript->registerScript(
'quitar_fotografia', "
$(document).ready(function(){
$(document).find(':file').bind('change', function(event, param) {
if (param == 'clear') {
$('#quitar_fotografia').val('1');
}
});
});
");
?>
<div class="row-fluid">
<div class="span12">
<h3 class="heading"><?php echo Yii::t('profind', 'Perfil del agente'); ?></h3>
@ -30,11 +44,32 @@
'id' => 'usuario-form',
'enableAjaxValidation' => false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
'class' => 'form-horizontal'
)
));
));
?>
<fieldset>
<div class="control-group formSep">
<?php echo $form->labelEx($model, 'ficheroFotografia', array('class' => 'control-label')); ?>
<div class="controls">
<div class="fileupload <?php echo ($model->fotografia->tieneFotografia()) ? 'fileupload-exists' : 'fileupload-new'; ?>" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;">
<?php echo CHtml::image($model->fotografia->getThumbnail()); ?>
</div>
<div>
<span class="btn btn-file">
<span class="fileupload-new"><?php echo Yii::t('profind', 'Seleccionar imagen'); ?></span>
<span class="fileupload-exists"><?php echo Yii::t('profind', 'Cambiar'); ?></span>
<?php echo $form->fileField($model, 'ficheroFotografia'); ?>
<input type="hidden" id="quitar_fotografia" name="quitar_fotografia" value="0">
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><?php echo Yii::t('profind', 'Quitar'); ?></a>
</div>
</div>
</div>
</div>
<div class="control-group formSep">
<div class="sepH_b">
<?php echo $form->labelEx($model, 'nombre', array('class' => 'control-label')); ?>
@ -61,7 +96,7 @@
<div class="control-group formSep">
<?php echo $form->labelEx($model, 'password', array('class' => 'control-label')); ?>
<div class="controls text_line">
<?php echo CHtml::link(Yii::t('profind', 'Cambiar la password'), $this->createUrl('seguridadUsuario/cambiarPassword', array('id' => Yii::app()->user->id))); ?>
<?php echo CHtml::link(Yii::t('profind', 'Cambiar la password'), $this->createUrl('seguridadUsuario/cambiarPassword', array('id' => Yii::app()->user->id))); ?>
</div>
</div>
@ -83,7 +118,7 @@
$lista,
array(
'empty' => '',
'data-placeholder'=>'Elije localidad...',
'data-placeholder' => Yii::t('profind', 'Elegir la localidad...'),
'class'=>'chzn_a input-xlarge',
));
?>