Logotipo empresa

git-svn-id: https://192.168.0.254/svn/Proyectos.Incam_PROFIND_Web/trunk@44 3fe1ab16-cfe0-e34b-8c9f-7d8c168d430d
This commit is contained in:
roberto 2012-10-01 15:15:00 +00:00
parent b04f5c6c22
commit 553faae59d
5 changed files with 145 additions and 27 deletions

View File

@ -57,7 +57,16 @@ class EmpresaController extends Controller {
if (isset($_POST['Empresa'])) { if (isset($_POST['Empresa'])) {
$empresa->attributes = $_POST['Empresa']; $empresa->attributes = $_POST['Empresa'];
$ficheroLogotipo = CUploadedFile::getInstance($empresa, 'ficheroLogotipo');
$quitarLogotipo = Yii::app()->request->getParam('quitar_logotipo', '0');
if ($empresa->save()) { if ($empresa->save()) {
if (($quitarLogotipo == '1') && ($empresa->logotipo->tieneFotografia()))
$empresa->logotipo->eliminarFotografia();
if ($ficheroLogotipo)
$empresa->logotipo->guardarFotografia($ficheroLogotipo);
Yii::app()->user->setFlash('success', Yii::t('profind', 'Se ha actualizado los datos de la empresa')); Yii::app()->user->setFlash('success', Yii::t('profind', 'Se ha actualizado los datos de la empresa'));
$this->redirect(array('modificar', 'id' => $empresa->id)); $this->redirect(array('modificar', 'id' => $empresa->id));
} }

View File

@ -12,12 +12,17 @@
* @property integer $empleados * @property integer $empleados
* @property string $direccion * @property string $direccion
* @property string $descripcion * @property string $descripcion
* @property string $ficheroLogotipo
* @property FotografiaPerfil $logotipo
* *
* The followings are the available model relations: * The followings are the available model relations:
* @property Usuario $usuarios * @property Usuario $usuarios
*/ */
class Empresa extends CActiveRecord { class Empresa extends CActiveRecord {
public $ficheroLogotipo;
public $logotipo;
/** /**
* Returns the static model of the specified AR class. * Returns the static model of the specified AR class.
* @param string $className active record class name. * @param string $className active record class name.
@ -44,8 +49,14 @@ class Empresa extends CActiveRecord {
array('empleados, cif, nombre, email, pagina_web, direccion', 'length', 'max' => 255), array('empleados, cif, nombre, email, pagina_web, direccion', 'length', 'max' => 255),
array('pagina_web', 'url', 'defaultScheme' => 'http'), array('pagina_web', 'url', 'defaultScheme' => 'http'),
array('email', 'email'), array('email', 'email'),
array('descripcion', 'safe'), array('descripcion, ficheroLogotipo', 'safe'),
array('ficheroLogotipo', 'file',
'types' => 'jpg',
'maxSize' => 1024 * 1024 * 1, // 1MB como máximo
'tooLarge' => Yii::t('profind', 'La imagen es demasiado pesada. Elija otra más pequeña.'),
'wrongType' => Yii::t('profind', 'Sólo se permiten imágenes en formato JPG.'),
'allowEmpty' => 'true',
),
array('id, cif, nombre, email, pagina_web, empleados, direccion, descripcion', 'safe', 'on' => 'search'), array('id, cif, nombre, email, pagina_web, empleados, direccion, descripcion', 'safe', 'on' => 'search'),
); );
} }
@ -67,6 +78,7 @@ class Empresa extends CActiveRecord {
public function attributeLabels() { public function attributeLabels() {
return array( return array(
'id' => 'ID', 'id' => 'ID',
'ficheroLogotipo' => 'Logotipo',
'cif' => 'CIF', 'cif' => 'CIF',
'nombre' => 'Nombre', 'nombre' => 'Nombre',
'email2' => 'Email', 'email2' => 'Email',
@ -100,5 +112,65 @@ class Empresa extends CActiveRecord {
'criteria' => $criteria, 'criteria' => $criteria,
)); ));
} }
protected function afterFind() {
parent::afterFind();
$this->logotipo = new FotografiaPerfil();
$this->logotipo->modelo = $this;
}
protected function afterConstruct() {
parent::afterConstruct();
$this->logotipo = new FotografiaPerfil();
$this->logotipo->modelo = $this;
}
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord)
$this->createUploadDir();
}
protected function afterDelete() {
parent::afterDelete();
$this->deleteUploadDir();
}
/*
* Devuelve la ruta con los ficheros de la empresa.
* Incluye el separador de directorios al final de la ruta.
* @return string ruta
*/
public function getUploadPath() {
return Yii::getPathOfAlias('application.uploads.empresas') . DIRECTORY_SEPARATOR . $this->id . DIRECTORY_SEPARATOR;
}
/*
* Crea un directorio para almacenar ficheros de la empresa
* @return boolean
*/
private function createUploadDir() {
$upload = $this->getUploadPath();
if(!is_dir($upload)) {
return mkdir($upload);
}
else return false;
}
/*
* Elimina el directorio de la empresa 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;
}
} }

View File

@ -4,17 +4,17 @@
* Modelo para la fotografía del usuario * Modelo para la fotografía del usuario
* *
*/ */
class UsuarioFotografia { class FotografiaPerfil {
public $usuario; public $modelo;
/* /*
* Devuelve la fotografía del usuario * Devuelve la fotografía del usuario
* @return string * @return string
*/ */
public function getThumbnail() { public function getThumbnail() {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
if ($this->tieneFotografia()) { if ($this->tieneFotografia()) {
$fichero = $this->getRutaCompletaFicheroFotografia(); $fichero = $this->getRutaCompletaFicheroFotografia();
@ -31,8 +31,8 @@ class UsuarioFotografia {
* @return boolean * @return boolean
*/ */
public function tieneFotografia() { public function tieneFotografia() {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$fichero = $this->getRutaCompletaFicheroFotografia(); $fichero = $this->getRutaCompletaFicheroFotografia();
return file_exists($fichero); return file_exists($fichero);
@ -43,10 +43,10 @@ class UsuarioFotografia {
* @return string ruta completa del fichero * @return string ruta completa del fichero
*/ */
private function getRutaCompletaFicheroFotografia() { private function getRutaCompletaFicheroFotografia() {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$upload = $this->usuario->getUploadPath(); $upload = $this->modelo->getUploadPath();
$nombre = $this->getNombreFicheroFotografia(); $nombre = $this->getNombreFicheroFotografia();
return $upload . $nombre; return $upload . $nombre;
@ -57,10 +57,10 @@ class UsuarioFotografia {
* @return string nombre del fichero * @return string nombre del fichero
*/ */
private function getNombreFicheroFotografia() { private function getNombreFicheroFotografia() {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
return $this->usuario->id . '.jpg'; return $this->modelo->id . '.jpg';
} }
/* /*
@ -68,8 +68,8 @@ class UsuarioFotografia {
* return CUploadedFile fichero subido * return CUploadedFile fichero subido
*/ */
public function guardarFotografia($fichero) { public function guardarFotografia($fichero) {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
if ($fichero) { if ($fichero) {
$nombre = $this->getRutaCompletaFicheroFotografia(); $nombre = $this->getRutaCompletaFicheroFotografia();
@ -84,8 +84,8 @@ class UsuarioFotografia {
* return bool * return bool
*/ */
public function eliminarFotografia() { public function eliminarFotografia() {
if (!$this->usuario) if (!$this->modelo)
throw new CException(Yii::t('profind', 'Usuario no asignado.')); throw new CException(Yii::t('profind', 'Modelo no asignado.'));
$fichero = $this->getRutaCompletaFicheroFotografia(); $fichero = $this->getRutaCompletaFicheroFotografia();
return unlink($fichero); return unlink($fichero);

View File

@ -19,7 +19,7 @@
* @property string $clave_seguridad * @property string $clave_seguridad
* @property string $descripcion * @property string $descripcion
* @property string $ficheroFotografia * @property string $ficheroFotografia
* @property UsuarioFotografia $fotografia * @property FotografiaPerfil $fotografia
* *
* Relaciones * Relaciones
* @property Empresa $empresa * @property Empresa $empresa
@ -67,7 +67,7 @@ class Usuario extends CActiveRecord {
array('estado', 'length', 'max' => 1), array('estado', 'length', 'max' => 1),
array('email', 'email'), array('email', 'email'),
array('email', 'unique'), array('email', 'unique'),
array('descripcion', 'safe'), array('descripcion, ficheroFotografia', 'safe'),
array('tipo', 'default', 'value' => self::TIPO_USUARIO_COORDINADOR), array('tipo', 'default', 'value' => self::TIPO_USUARIO_COORDINADOR),
array('email, nombre, apellidos, password, tipo, titulo, localidad, telefono', 'length', 'max' => 255), array('email, nombre, apellidos, password, tipo, titulo, localidad, telefono', 'length', 'max' => 255),
array('ficheroFotografia', 'file', array('ficheroFotografia', 'file',
@ -151,14 +151,14 @@ class Usuario extends CActiveRecord {
protected function afterFind() { protected function afterFind() {
parent::afterFind(); parent::afterFind();
$this->fotografia = new UsuarioFotografia(); $this->fotografia = new FotografiaPerfil();
$this->fotografia->usuario = $this; $this->fotografia->modelo = $this;
} }
protected function afterConstruct() { protected function afterConstruct() {
parent::afterConstruct(); parent::afterConstruct();
$this->fotografia = new UsuarioFotografia(); $this->fotografia = new FotografiaPerfil();
$this->fotografia->usuario = $this; $this->fotografia->modelo = $this;
} }
protected function afterSave() { protected function afterSave() {
@ -178,7 +178,7 @@ class Usuario extends CActiveRecord {
* @return string ruta * @return string ruta
*/ */
public function getUploadPath() { public function getUploadPath() {
return Yii::getPathOfAlias('application.uploads') . DIRECTORY_SEPARATOR . $this->id . DIRECTORY_SEPARATOR; return Yii::getPathOfAlias('application.uploads.usuarios') . DIRECTORY_SEPARATOR . $this->id . DIRECTORY_SEPARATOR;
} }
/* /*

View File

@ -1,4 +1,5 @@
<?php Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl . '/lib/uniform/css/uniform.default.css'); ?> <?php Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl . '/lib/uniform/css/uniform.default.css'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-fileupload.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/lib/uniform/jquery.uniform.min.js', CClientScript::POS_END); ?> <?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/lib/uniform/jquery.uniform.min.js', CClientScript::POS_END); ?>
<?php Yii::app()->clientScript->registerScript( <?php Yii::app()->clientScript->registerScript(
@ -9,6 +10,18 @@
", CClientScript::POS_END); ", CClientScript::POS_END);
?> ?>
<?php Yii::app()->clientScript->registerScript(
'quitar_logotipo', "
$(document).ready(function(){
$(document).find(':file').bind('change', function(event, param) {
if (param == 'clear') {
$('#quitar_logotipo').val('1');
}
});
});
", CClientScript::POS_END);
?>
<div class="row-fluid"> <div class="row-fluid">
<div class="span12"> <div class="span12">
<h3 class="heading"><?php echo Yii::t('profind', 'Empresa'); ?></h3> <h3 class="heading"><?php echo Yii::t('profind', 'Empresa'); ?></h3>
@ -104,10 +117,34 @@
<button type="submit" class="btn btn-primary"><?php echo $model->isNewRecord ? Yii::t('profind', 'Crear') : Yii::t('profind', 'Guardar cambios'); ?></button> <button type="submit" class="btn btn-primary"><?php echo $model->isNewRecord ? Yii::t('profind', 'Crear') : Yii::t('profind', 'Guardar cambios'); ?></button>
<button class="btn"><?php echo Yii::t('profind', 'Cancelar'); ?></button> <button class="btn"><?php echo Yii::t('profind', 'Cancelar'); ?></button>
</div> </div>
</fieldset>
<?php $this->endWidget(); ?>
</div> </div>
<div class="span4"> <div class="span4">
<div class="control-group">
<?php echo $form->labelEx($model, 'ficheroLogotipo', array('class' => 'control-label')); ?>
<div class="controls">
<div class="fileupload <?php echo ($model->logotipo->tieneFotografia()) ? 'fileupload-exists' : 'fileupload-new'; ?>" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;">
<?php echo CHtml::image($model->logotipo->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, 'ficheroLogotipo'); ?>
<input type="hidden" id="quitar_logotipo" name="quitar_logotipo" value="0">
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><?php echo Yii::t('profind', 'Quitar'); ?></a>
</div>
</div>
</div>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</div> </div>
</div> </div>
</div> </div>