Guest User

Untitled

a guest
Dec 9th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. <?php
  2. class ProfileController extends Controller
  3. {
  4. public $defaultAction = 'profile';
  5.  
  6. /**
  7. * @var CActiveRecord the currently loaded data model instance.
  8. */
  9. private $_model;
  10. /**
  11. * Shows a particular model.
  12. */
  13. public function actionProfile()
  14. {
  15. $model = $this->loadUser();
  16. $this->render('profile',array(
  17. 'model'=>$model,
  18. 'profile'=>$model->profile,
  19. ));
  20. }
  21.  
  22.  
  23. /**
  24. * Updates a particular model.
  25. * If update is successful, the browser will be redirected to the 'view' page.
  26. */
  27. public function actionEdit()
  28. {
  29. $model = $this->loadUser();
  30. $profile=$model->profile;
  31.  
  32. // ajax validator
  33. if(isset($_POST['ajax']) && $_POST['ajax']==='profile-form')
  34. {
  35. echo UActiveForm::validate(array($model,$profile));
  36. Yii::app()->end();
  37. }
  38.  
  39. if(isset($_POST['User']))
  40. {
  41. $model->attributes=$_POST['User'];
  42. $profile->attributes=$_POST['Profile'];
  43.  
  44. //specification des scenario à utiliser
  45. if($profile->speciality=='Autre')
  46. {
  47. $profile->scenario='add_speciality';
  48. }
  49. if($profile->city=='Autre')
  50. {
  51. $profile->scenario='add_city';
  52. }
  53. if($profile->speciality=='Autre'&&$profile->city=='Autre')
  54. {
  55. $profile->scenario='add_speciality_and_city';
  56. }
  57. //fin de specification des scenario à utiliser
  58.  
  59.  
  60. if($model->validate()&&$profile->validate()) {
  61. /*ajoute la nouvelle spacialité ou ville à la BD
  62. affecte à la variable speciality la valeur de la nouvelle variable add_speciality (pour la mettre dans le profile de l'utilisateur)
  63. de meme pour les variable city et add_city*/
  64. if($profile->speciality=='Autre')
  65. {
  66. //debut de la recuperation de la liste des specialité
  67. $sql='SELECT {{profiles_fields}}.range FROM {{profiles_fields}} WHERE varname="speciality"';
  68. $connection=Yii::app()->db;
  69. $command=$connection->createCommand($sql);
  70. $rows=$command->query();
  71. $speciality_array=$rows->readAll();
  72. //fin de la recuperation de la liste des specialité
  73.  
  74. //update de la liste des spacialité dans la DB
  75.  
  76. $speciality_range=$speciality_array[0]['range'].';'.$profile->add_speciality;
  77. $profile_field_speciality=ProfileField::model()->findByAttributes(array('varname'=>'speciality'));
  78. $profile_field_speciality->range=$speciality_range;
  79. $profile_field_speciality->save();
  80. $profile->speciality=$profile->add_speciality;
  81. $profile->add_speciality=null;
  82. /*$profile->setAttributes(array('speciality'=>$profile->add_speciality));
  83. $profile->setAttributes(array('add_speciality'=>null));
  84. echo'<pre>';
  85. print_r($profile->Attributes);
  86. echo'<pre>'*/;
  87.  
  88. }
  89. // permet d'ajouter des villes;
  90. if($profile->city=='Autre')
  91. {
  92.  
  93. //debut de la recuperation de la liste des villes
  94. $sql='SELECT {{profiles_fields}}.range FROM {{profiles_fields}} WHERE varname="city"';
  95. $connection=Yii::app()->db;
  96. $command=$connection->createCommand($sql);
  97. $rows=$command->query();
  98. $city_array=$rows->readAll();
  99. //fin de la recuperation de la liste des villes
  100.  
  101. //update de la liste des villes dans la DB
  102.  
  103. $city_range=$city_array[0]['range'].';'.$profile->add_city;
  104. $profile_field_city=ProfileField::model()->findByAttributes(array('varname'=>'city'));
  105. $profile_field_city->range=$city_range;
  106. $profile_field_city->save();
  107. $profile->city=$profile->add_city;
  108. $profile->add_city=null;
  109.  
  110. /*$profile->setAttributes(array('city'=>$profile->add_city));
  111. $profile->setAttributes(array('add_city'=>null));*/
  112. }
  113. /*fin de :
  114. ajoute la nouvelle spacialité ou ville à la BD
  115. affecte à la variable speciality la valeur de la nouvelle variable add_speciality (pour la mettre dans le profile de l'utilisateur)
  116. de meme pour les variable city et add_city*/
  117.  
  118.  
  119. $model->save();
  120. $profile->save();
  121. Yii::app()->user->setFlash('profileMessage',UserModule::t("Changes is saved."));
  122. $this->redirect(array('/user/profile'));
  123. } else $profile->validate();
  124. }
  125.  
  126. $this->render('edit',array(
  127. 'model'=>$model,
  128. 'profile'=>$profile,
  129. ));
  130. }
  131.  
  132. /**
  133. * Change password
  134. */
  135. public function actionChangepassword() {
  136. $model = new UserChangePassword;
  137. if (Yii::app()->user->id) {
  138.  
  139. // ajax validator
  140. if(isset($_POST['ajax']) && $_POST['ajax']==='changepassword-form')
  141. {
  142. echo UActiveForm::validate($model);
  143. Yii::app()->end();
  144. }
  145.  
  146. if(isset($_POST['UserChangePassword'])) {
  147. $model->attributes=$_POST['UserChangePassword'];
  148. if($model->validate()) {
  149. $new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
  150. $new_password->password = UserModule::encrypting($model->password);
  151. $new_password->activkey=UserModule::encrypting(microtime().$model->password);
  152. $new_password->save();
  153. Yii::app()->user->setFlash('profileMessage',UserModule::t("New password is saved."));
  154. $this->redirect(array("profile"));
  155. }
  156. }
  157. $this->render('changepassword',array('model'=>$model));
  158. }
  159. }
  160.  
  161. /**
  162. * Returns the data model based on the primary key given in the GET variable.
  163. * If the data model is not found, an HTTP exception will be raised.
  164. * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
  165. */
  166. public function loadUser()
  167. {
  168. if($this->_model===null)
  169. {
  170. if(Yii::app()->user->id)
  171. $this->_model=Yii::app()->controller->module->user();
  172. if($this->_model===null)
  173. $this->redirect(Yii::app()->controller->module->loginUrl);
  174. }
  175. return $this->_model;
  176. }
  177. }
Add Comment
Please, Sign In to add comment