diff --git a/www/protected/controllers/EmpresaController.php b/www/protected/controllers/EmpresaController.php index c705f07..7362575 100644 --- a/www/protected/controllers/EmpresaController.php +++ b/www/protected/controllers/EmpresaController.php @@ -57,7 +57,16 @@ class EmpresaController extends Controller { if (isset($_POST['Empresa'])) { $empresa->attributes = $_POST['Empresa']; + $ficheroLogotipo = CUploadedFile::getInstance($empresa, 'ficheroLogotipo'); + $quitarLogotipo = Yii::app()->request->getParam('quitar_logotipo', '0'); + 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')); $this->redirect(array('modificar', 'id' => $empresa->id)); } diff --git a/www/protected/models/Empresa.php b/www/protected/models/Empresa.php index e6fd05e..9e3ef3f 100644 --- a/www/protected/models/Empresa.php +++ b/www/protected/models/Empresa.php @@ -12,12 +12,17 @@ * @property integer $empleados * @property string $direccion * @property string $descripcion + * @property string $ficheroLogotipo + * @property FotografiaPerfil $logotipo * * The followings are the available model relations: * @property Usuario $usuarios */ class Empresa extends CActiveRecord { + public $ficheroLogotipo; + public $logotipo; + /** * Returns the static model of the specified AR class. * @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('pagina_web', 'url', 'defaultScheme' => 'http'), 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'), ); } @@ -67,6 +78,7 @@ class Empresa extends CActiveRecord { public function attributeLabels() { return array( 'id' => 'ID', + 'ficheroLogotipo' => 'Logotipo', 'cif' => 'CIF', 'nombre' => 'Nombre', 'email2' => 'Email', @@ -100,5 +112,65 @@ class Empresa extends CActiveRecord { '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; + } + + } \ No newline at end of file diff --git a/www/protected/models/UsuarioFotografia.php b/www/protected/models/FotografiaPerfil.php similarity index 69% rename from www/protected/models/UsuarioFotografia.php rename to www/protected/models/FotografiaPerfil.php index 0335660..4c01ee5 100644 --- a/www/protected/models/UsuarioFotografia.php +++ b/www/protected/models/FotografiaPerfil.php @@ -4,17 +4,17 @@ * Modelo para la fotografía del usuario * */ -class UsuarioFotografia { +class FotografiaPerfil { - public $usuario; + public $modelo; /* * 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->modelo) + throw new CException(Yii::t('profind', 'Modelo no asignado.')); if ($this->tieneFotografia()) { $fichero = $this->getRutaCompletaFicheroFotografia(); @@ -31,8 +31,8 @@ class UsuarioFotografia { * @return boolean */ public function tieneFotografia() { - if (!$this->usuario) - throw new CException(Yii::t('profind', 'Usuario no asignado.')); + if (!$this->modelo) + throw new CException(Yii::t('profind', 'Modelo no asignado.')); $fichero = $this->getRutaCompletaFicheroFotografia(); return file_exists($fichero); @@ -43,10 +43,10 @@ class UsuarioFotografia { * @return string ruta completa del fichero */ private function getRutaCompletaFicheroFotografia() { - if (!$this->usuario) - throw new CException(Yii::t('profind', 'Usuario no asignado.')); + if (!$this->modelo) + throw new CException(Yii::t('profind', 'Modelo no asignado.')); - $upload = $this->usuario->getUploadPath(); + $upload = $this->modelo->getUploadPath(); $nombre = $this->getNombreFicheroFotografia(); return $upload . $nombre; @@ -57,10 +57,10 @@ class UsuarioFotografia { * @return string nombre del fichero */ private function getNombreFicheroFotografia() { - if (!$this->usuario) - throw new CException(Yii::t('profind', 'Usuario no asignado.')); + if (!$this->modelo) + 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 */ public function guardarFotografia($fichero) { - if (!$this->usuario) - throw new CException(Yii::t('profind', 'Usuario no asignado.')); + if (!$this->modelo) + throw new CException(Yii::t('profind', 'Modelo no asignado.')); if ($fichero) { $nombre = $this->getRutaCompletaFicheroFotografia(); @@ -84,8 +84,8 @@ class UsuarioFotografia { * return bool */ public function eliminarFotografia() { - if (!$this->usuario) - throw new CException(Yii::t('profind', 'Usuario no asignado.')); + if (!$this->modelo) + throw new CException(Yii::t('profind', 'Modelo no asignado.')); $fichero = $this->getRutaCompletaFicheroFotografia(); return unlink($fichero); diff --git a/www/protected/models/Usuario.php b/www/protected/models/Usuario.php index 18457cc..169cb9b 100644 --- a/www/protected/models/Usuario.php +++ b/www/protected/models/Usuario.php @@ -19,7 +19,7 @@ * @property string $clave_seguridad * @property string $descripcion * @property string $ficheroFotografia - * @property UsuarioFotografia $fotografia + * @property FotografiaPerfil $fotografia * * Relaciones * @property Empresa $empresa @@ -67,7 +67,7 @@ class Usuario extends CActiveRecord { array('estado', 'length', 'max' => 1), array('email', 'email'), array('email', 'unique'), - array('descripcion', 'safe'), + array('descripcion, ficheroFotografia', 'safe'), array('tipo', 'default', 'value' => self::TIPO_USUARIO_COORDINADOR), array('email, nombre, apellidos, password, tipo, titulo, localidad, telefono', 'length', 'max' => 255), array('ficheroFotografia', 'file', @@ -151,14 +151,14 @@ class Usuario extends CActiveRecord { protected function afterFind() { parent::afterFind(); - $this->fotografia = new UsuarioFotografia(); - $this->fotografia->usuario = $this; + $this->fotografia = new FotografiaPerfil(); + $this->fotografia->modelo = $this; } protected function afterConstruct() { parent::afterConstruct(); - $this->fotografia = new UsuarioFotografia(); - $this->fotografia->usuario = $this; + $this->fotografia = new FotografiaPerfil(); + $this->fotografia->modelo = $this; } protected function afterSave() { @@ -178,7 +178,7 @@ class Usuario extends CActiveRecord { * @return string ruta */ 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; } /* diff --git a/www/themes/profind/views/empresa/_form.php b/www/themes/profind/views/empresa/_form.php index b9ecc09..af79af1 100644 --- a/www/themes/profind/views/empresa/_form.php +++ b/www/themes/profind/views/empresa/_form.php @@ -1,4 +1,5 @@ clientScript->registerCssFile(Yii::app()->theme->baseUrl . '/lib/uniform/css/uniform.default.css'); ?> +clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-fileupload.js'); ?> clientScript->registerScriptFile(Yii::app()->theme->baseUrl . '/lib/uniform/jquery.uniform.min.js', CClientScript::POS_END); ?> clientScript->registerScript( @@ -9,6 +10,18 @@ ", CClientScript::POS_END); ?> +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); +?> +

@@ -104,10 +117,34 @@
- - endWidget(); ?> +
+ +
+ labelEx($model, 'ficheroLogotipo', array('class' => 'control-label')); ?> +
+
+
+ logotipo->getThumbnail()); ?> +
+
+ + + + fileField($model, 'ficheroLogotipo'); ?> + + + +
+
+
+
+ + + + endWidget(); ?> +