Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ProfileController extends Controller
- {
- public $defaultAction = 'profile';
- /**
- * @var CActiveRecord the currently loaded data model instance.
- */
- private $_model;
- /**
- * Shows a particular model.
- */
- public function actionProfile()
- {
- $model = $this->loadUser();
- $this->render('profile',array(
- 'model'=>$model,
- 'profile'=>$model->profile,
- ));
- }
- /**
- * Updates a particular model.
- * If update is successful, the browser will be redirected to the 'view' page.
- */
- public function actionEdit()
- {
- $model = $this->loadUser();
- $profile=$model->profile;
- // ajax validator
- if(isset($_POST['ajax']) && $_POST['ajax']==='profile-form')
- {
- echo UActiveForm::validate(array($model,$profile));
- Yii::app()->end();
- }
- if(isset($_POST['User']))
- {
- $model->attributes=$_POST['User'];
- $profile->attributes=$_POST['Profile'];
- //specification des scenario à utiliser
- if($profile->speciality=='Autre')
- {
- $profile->scenario='add_speciality';
- }
- if($profile->city=='Autre')
- {
- $profile->scenario='add_city';
- }
- if($profile->speciality=='Autre'&&$profile->city=='Autre')
- {
- $profile->scenario='add_speciality_and_city';
- }
- //fin de specification des scenario à utiliser
- if($model->validate()&&$profile->validate()) {
- /*ajoute la nouvelle spacialité ou ville à la BD
- affecte à la variable speciality la valeur de la nouvelle variable add_speciality (pour la mettre dans le profile de l'utilisateur)
- de meme pour les variable city et add_city*/
- if($profile->speciality=='Autre')
- {
- //debut de la recuperation de la liste des specialité
- $sql='SELECT {{profiles_fields}}.range FROM {{profiles_fields}} WHERE varname="speciality"';
- $connection=Yii::app()->db;
- $command=$connection->createCommand($sql);
- $rows=$command->query();
- $speciality_array=$rows->readAll();
- //fin de la recuperation de la liste des specialité
- //update de la liste des spacialité dans la DB
- $speciality_range=$speciality_array[0]['range'].';'.$profile->add_speciality;
- $profile_field_speciality=ProfileField::model()->findByAttributes(array('varname'=>'speciality'));
- $profile_field_speciality->range=$speciality_range;
- $profile_field_speciality->save();
- $profile->speciality=$profile->add_speciality;
- $profile->add_speciality=null;
- /*$profile->setAttributes(array('speciality'=>$profile->add_speciality));
- $profile->setAttributes(array('add_speciality'=>null));
- echo'<pre>';
- print_r($profile->Attributes);
- echo'<pre>'*/;
- }
- // permet d'ajouter des villes;
- if($profile->city=='Autre')
- {
- //debut de la recuperation de la liste des villes
- $sql='SELECT {{profiles_fields}}.range FROM {{profiles_fields}} WHERE varname="city"';
- $connection=Yii::app()->db;
- $command=$connection->createCommand($sql);
- $rows=$command->query();
- $city_array=$rows->readAll();
- //fin de la recuperation de la liste des villes
- //update de la liste des villes dans la DB
- $city_range=$city_array[0]['range'].';'.$profile->add_city;
- $profile_field_city=ProfileField::model()->findByAttributes(array('varname'=>'city'));
- $profile_field_city->range=$city_range;
- $profile_field_city->save();
- $profile->city=$profile->add_city;
- $profile->add_city=null;
- /*$profile->setAttributes(array('city'=>$profile->add_city));
- $profile->setAttributes(array('add_city'=>null));*/
- }
- /*fin de :
- ajoute la nouvelle spacialité ou ville à la BD
- affecte à la variable speciality la valeur de la nouvelle variable add_speciality (pour la mettre dans le profile de l'utilisateur)
- de meme pour les variable city et add_city*/
- $model->save();
- $profile->save();
- Yii::app()->user->setFlash('profileMessage',UserModule::t("Changes is saved."));
- $this->redirect(array('/user/profile'));
- } else $profile->validate();
- }
- $this->render('edit',array(
- 'model'=>$model,
- 'profile'=>$profile,
- ));
- }
- /**
- * Change password
- */
- public function actionChangepassword() {
- $model = new UserChangePassword;
- if (Yii::app()->user->id) {
- // ajax validator
- if(isset($_POST['ajax']) && $_POST['ajax']==='changepassword-form')
- {
- echo UActiveForm::validate($model);
- Yii::app()->end();
- }
- if(isset($_POST['UserChangePassword'])) {
- $model->attributes=$_POST['UserChangePassword'];
- if($model->validate()) {
- $new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
- $new_password->password = UserModule::encrypting($model->password);
- $new_password->activkey=UserModule::encrypting(microtime().$model->password);
- $new_password->save();
- Yii::app()->user->setFlash('profileMessage',UserModule::t("New password is saved."));
- $this->redirect(array("profile"));
- }
- }
- $this->render('changepassword',array('model'=>$model));
- }
- }
- /**
- * 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 primary key value. Defaults to null, meaning using the 'id' GET variable
- */
- public function loadUser()
- {
- if($this->_model===null)
- {
- if(Yii::app()->user->id)
- $this->_model=Yii::app()->controller->module->user();
- if($this->_model===null)
- $this->redirect(Yii::app()->controller->module->loginUrl);
- }
- return $this->_model;
- }
- }
Add Comment
Please, Sign In to add comment