1 <?php
2
3 class ProfileController extends Controller
4 {
5 public $defaultAction = 'profile';
6 public $layout='//layouts/column2';
7
8 9 10
11 private $_model;
12 13 14
15 public function actionProfile()
16 {
17 $model = $this->loadUser();
18 $this->render('profile',array(
19 'model'=>$model,
20 'profile'=>$model->profile,
21 ));
22 }
23
24
25 26 27 28
29 public function actionEdit()
30 {
31 $model = $this->loadUser();
32 $profile=$model->profile;
33
34
35 if(isset($_POST['ajax']) && $_POST['ajax']==='profile-form')
36 {
37 echo UActiveForm::validate(array($model,$profile));
38 Yii::app()->end();
39 }
40
41 if(isset($_POST['User']))
42 {
43 $model->attributes=$_POST['User'];
44 $profile->attributes=$_POST['Profile'];
45
46 if($model->validate()&&$profile->validate()) {
47 $model->save();
48 $profile->save();
49 Yii::app()->user->setFlash('profileMessage',UserModule::t("Changes is saved."));
50 $this->redirect(array('/user/profile'));
51 } else $profile->validate();
52 }
53
54 $this->render('edit',array(
55 'model'=>$model,
56 'profile'=>$profile,
57 ));
58 }
59
60 61 62
63 public function actionChangepassword() {
64 $model = new UserChangePassword;
65 if (Yii::app()->user->id) {
66
67
68 if(isset($_POST['ajax']) && $_POST['ajax']==='changepassword-form')
69 {
70 echo UActiveForm::validate($model);
71 Yii::app()->end();
72 }
73
74 if(isset($_POST['UserChangePassword'])) {
75 $model->attributes=$_POST['UserChangePassword'];
76 if($model->validate()) {
77 $new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
78 $new_password->password = UserModule::encrypting($model->password);
79 $new_password->activkey=UserModule::encrypting(microtime().$model->password);
80 $new_password->save();
81 Yii::app()->user->setFlash('profileMessage',UserModule::t("New password is saved."));
82 $this->redirect(array("profile"));
83 }
84 }
85 $this->render('changepassword',array('model'=>$model));
86 }
87 }
88
89 90 91 92 93
94 public function loadUser()
95 {
96 if($this->_model===null)
97 {
98 if(Yii::app()->user->id)
99 $this->_model=Yii::app()->controller->module->user();
100 if($this->_model===null)
101 $this->redirect(Yii::app()->controller->module->loginUrl);
102 }
103 return $this->_model;
104 }
105 }