Incam_PROFIND_Web/www/protected/controllers/EmpresaController.php

144 lines
4.8 KiB
PHP

<?php
class EmpresaController extends Controller {
public $defaultAction = 'modificar';
/**
* @return array action filters
*/
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
return array(
array('allow',
'actions' => array('modificar'),
'users' => array('@'),
'expression' => 'Yii::app()->user->esCoordinador',
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
* @param string $provider
*/
public function actionModificar($id, $provider = '') {
if ($id != Yii::app()->user->id_empresa)
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
if (($provider != '') && (!isset($_POST['Empresa']))) {
switch ($provider) {
case 'Twitter':
case 'Facebook':
case 'LinkedIn':
$empresa = $this->loadModelwithSocialData($id, $provider);
break;
default:
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
}
}
else
$empresa = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Empresa'])) {
$empresa->attributes = $_POST['Empresa'];
$ficheroLogotipo = CUploadedFile::getInstance($empresa, 'ficheroLogotipo');
Yii::log(CVarDumper::dumpAsString($_POST['Empresa']), CLogger::LEVEL_ERROR);
Yii::log(CVarDumper::dumpAsString($empresa->attributes), CLogger::LEVEL_ERROR);
Yii::log(CVarDumper::dumpAsString($ficheroLogotipo), CLogger::LEVEL_ERROR);
$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));
}
}
$this->render('modificar', array(
'model' => $empresa,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id) {
$model = Empresa::model()->findByPk($id);
if ($model === null)
throw new CHttpException(404, Yii::t('profind', 'La página solicitada no existe.'));
return $model;
}
public function loadModelwithSocialData($id, $provider) {
$empresa = $this->loadModel($id);
if (!Yii::app()->socialConnect->loadUserProfile($provider)) {
throw new CHttpException(
Yii::app()->socialConnect->errorCode,
Yii::t('profind', Yii::app()->socialConnect->errorMessage)
);
}
$profile = Yii::app()->socialConnect->userProfile;
Yii::log(CVarDumper::dumpAsString($profile));
$empresa->nombre = $profile->displayName;
$empresa->pagina_web = $profile->webSiteURL;
$empresa->email = $profile->email;
$empresa->descripcion = $profile->description;
switch ($provider) {
case 'Twitter':
$empresa->direccion = $profile->region;
break;
case 'Facebook':
case 'LinkedIn':
$empresa->direccion = $profile->city;
break;
}
return $empresa;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model) {
if (isset($_POST['ajax']) && $_POST['ajax'] === 'empresa-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}