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->activkey = UserModule::encrypting($newEmail . $user->password . microtime());
64 
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 
71  $subject = UserModule::t("Email address changed.");
72  $message = $this->renderMail('/mail_templates/approve_email', $subject, array('activation_url' => $activation_url));
73  $notification = new Notification($subject, $message, array(new EmailRecipient($newEmail, Yii::app()->getModule('user')->activationEmail, '', true)));
74  Yii::app()->notificationManager->notifyAbout($notification);
75  Yii::app()->user->setFlash(
76  'profileMessage',
77  UserModule::t("Changes is saved. To activate your new email please confirm it in mail which was sent to {email}", array('{email}' => $newEmail))
78  );
79  } else {
80  Yii::app()->user->setFlash('profileMessage', UserModule::t("Changes is saved."));
81  }
82  $user->update(array('status', 'username', 'activkey'));
83  } else {
84  Yii::app()->user->setState('User', array('attributes' => $user->attributes, 'errors' => $user->getErrors()));
85  Yii::app()->user->setState('Profile', array('attributes' => $profile->attributes, 'errors' => $profile->getErrors()));
86  }
87  }
88  /* if is set returl_url - we heed to redirect user there */
89  if (isset($_REQUEST['return_url'])) {
90  $this->redirect($_REQUEST['return_url']);
91  }
92  $this->render('edit');
93  }
94 
95  /**
96  * Change password
97  */
98  public function actionChangepassword()
99  {
100  $model = new UserChangePassword;
101  if (Yii::app()->user->id) {
102 
103  // ajax validator
104  if (isset($_POST['ajax']) && $_POST['ajax'] === 'changepassword-form') {
105  echo UActiveForm::validate($model);
106  Yii::app()->end();
107  }
108 
109  if (isset($_POST['UserChangePassword'])) {
110  $model->attributes = $_POST['UserChangePassword'];
111  if ($model->validate()) {
112  $new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
113  $new_password->password = UserModule::encrypting($model->password);
114  $new_password->activkey = UserModule::encrypting(microtime() . $model->password);
115  $new_password->save();
116  Yii::app()->user->setFlash('passwordChangeMessage', UserModule::t("New password is saved."));
117  if (isset($_GET['return_url'])) {
118  $this->redirect($_GET['return_url']);
119  }
120  $this->redirect(array("profile"));
121  }
122  }
123  $this->render('changepassword', array('model' => $model));
124  }
125  }
126 
127  public function actionApproveEmail($email, $key)
128  {
129  if (!User::model()->exists("email=:email", array('email' => $email))) {
130  $user = User::model()->findByAttributes(array('activkey' => $key));
131  if ($user) {
132  $user->email = $email;
133  if ($user->save()) {
134  $this->render('email_approved', array('email' => $email));
135  return;
136  }
137  }
138  }
139  $this->render('email_not_approved', array('email' => $email));
140  }
141 
142  /**
143  * Returns the data model based on the primary key given in the GET variable.
144  * If the data model is not found, an HTTP exception will be raised.
145  * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
146  */
147  public function loadUser()
148  {
149  if ($this->_model === null) {
150  if (Yii::app()->user->id) {
151  $this->_model = RegistrationForm::model()->findByPk(Yii::app()->user->id);
152  }
153  if ($this->_model === null) {
154  $this->redirect(Yii::app()->controller->module->loginUrl);
155  }
156  }
157  return $this->_model;
158  }
159 }