Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ProfileController.php
1 <?php
2 
4 {
5  public $defaultAction = 'profile';
6 
7  /**
8  * @var CActiveRecord the currently loaded data model instance.
9  */
10  private $_model;
11 
12  /**
13  * Shows a particular model.
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  * Updates a particular model.
27  * If update is successful, the browser will be redirected to the 'view' page.
28  */
29  public function actionEdit()
30  {
31  $user = $this->loadUser();
32  if (!$user) {
33  throw new CHttpException(400);
34  }
35  $profile = $user->profile;
36 
37  // ajax validator
38  if (isset($_POST['ajax']) && $_POST['ajax'] === 'profile-form') {
39  echo UActiveForm::validate(array($user, $profile));
40  Yii::app()->end();
41  }
42 
43  if (isset($_POST['User'], $_POST['Profile'])) {
44  //save old email in case it was changed, for future validation
45  $oldEmail = $user->email;
46  $user->attributes = $_POST['User'];
47  $profile->attributes = $_POST['Profile'];
48  $uv = $user->validate();
49  $pv = $profile->validate();
50  if ($uv && $pv) {
51  $profile->save();
52  if(isset($_POST['Profile']['firstname'])){
53  Yii::app()->user->firstname = $_POST['Profile']['firstname'];
54  }
55  if(isset($_POST['Profile']['lastname'])){
56  Yii::app()->user->lastname = $_POST['Profile']['lastname'];
57  }
58  if(isset($_POST['User']['email'])){
59  Yii::app()->user->email = $_POST['User']['email'];
60  }
61  if ($oldEmail != $user->email) {
62  $newEmail = $user->email;
63  $user->email = $oldEmail;
64  $user->activkey = UserModule::encrypting($newEmail . $user->password . microtime());
65  if (Yii::app()->getModule('user')->profileEditUrl[Yii::app()->language] !== false) {
66  $activation_url = Yii::app()->createAbsoluteUrl(Yii::app()->getModule('user')->profileEditUrl[Yii::app()->language])."?approveEmail=1&amp;email={$newEmail}&amp;key={$user->activkey}";
67  } else {
68  $activation_url = Yii::app()->createAbsoluteUrl('/user/profile/approveEmail', array('email' => $newEmail, 'key' => $user->activkey));
69  }
70  $subject = UserModule::t("Email address changed.");
71  $message = $this->renderMail('/mail_templates/approve_email', $subject, array('activation_url' => $activation_url));
72  $notification = new Notification($subject, $message, array(new EmailRecipient($newEmail, Yii::app()->getModule('user')->activationEmail, '', true)));
73  Yii::app()->notificationManager->notifyAbout($notification);
74  Yii::app()->user->setFlash(
75  'profileMessage',
76  UserModule::t("Changes is saved. To activate your new email please confirm it in mail which was sent to {email}", array('{email}' => $newEmail))
77  );
78  } else {
79  Yii::app()->user->setFlash('profileMessage', UserModule::t("Changes is saved."));
80  }
81  $user->update(array('status', 'username', 'activkey'));
82  } else {
83  $user->password = null;
84  $user->verifyPassword = null;
85  Yii::app()->user->setState('User', array('attributes' => $user->attributes, 'errors' => $user->getErrors()));
86  Yii::app()->user->setState('Profile', array('attributes' => $profile->attributes, 'errors' => $profile->getErrors()));
87  }
88  }
89  /* if is set returl_url - we heed to redirect user there */
90  if (isset($_REQUEST['return_url'])) {
91  $this->redirect($_REQUEST['return_url']);
92  }
93  $this->render('edit');
94  }
95 
96  /**
97  * Change password
98  */
99  public function actionChangepassword()
100  {
101  $model = new UserChangePassword;
102  if (Yii::app()->user->id) {
103 
104  // ajax validator
105  if (isset($_POST['ajax']) && $_POST['ajax'] === 'changepassword-form') {
106  echo UActiveForm::validate($model);
107  Yii::app()->end();
108  }
109 
110  if (isset($_POST['UserChangePassword'])) {
111  $model->attributes = $_POST['UserChangePassword'];
112  if ($model->validate()) {
113  $new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
114  $new_password->password = UserModule::encrypting($model->password);
115  $new_password->activkey = UserModule::encrypting(microtime() . $model->password);
116  $new_password->save();
117  Yii::app()->user->setFlash('passwordChangeMessage', UserModule::t("New password is saved."));
118  if (isset($_GET['return_url'])) {
119  $this->redirect($_GET['return_url']);
120  }
121  $this->redirect(array("profile"));
122  }
123  }
124  $this->render('changepassword', array('model' => $model));
125  }
126  }
127 
128  public function actionApproveEmail($email, $key)
129  {
130  if (!User::model()->exists("email=:email", array('email' => $email))) {
131  $user = User::model()->findByAttributes(array('activkey' => $key));
132  if ($user) {
133  $user->email = $email;
134  if ($user->save()) {
135  $this->render('email_approved', array('email' => $email));
136  return;
137  }
138  }
139  }
140  $this->render('email_not_approved', array('email' => $email));
141  }
142 
143  /**
144  * Returns the data model based on the primary key given in the GET variable.
145  * If the data model is not found, an HTTP exception will be raised.
146  * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
147  */
148  public function loadUser()
149  {
150  if ($this->_model === null) {
151  if (Yii::app()->user->id) {
152  $this->_model = RegistrationForm::model()->findByPk(Yii::app()->user->id);
153  }
154  if ($this->_model === null) {
155  $this->redirect(Yii::app()->controller->module->loginUrl);
156  }
157  }
158  return $this->_model;
159  }
160 }