/
registration.txt
163 lines (120 loc) · 4.7 KB
/
registration.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
How to implement your own registration process.
In this example we want our user to only register with a email address
and requires him to add some information about him in a text area.
We also do not want him to enter a password. The password should be
automatically generated and send to him by Email.
This differs from the example registration logic provided by
yii-user-management. We are going to extend the provided classes and change
the places that are necessary. In Yum all classes are prefixed with Yum for
exactly this purpose.
Lets assume you have the profile and the registration submodule of the
yii-user-management installed. You will have the following directory
structure:
modules/
profile/
controllers/
YumProfileController
YumFieldController
models/
YumProfile
YumProfileField
registration/
controllers/
YumRegistrationController
views/
registration/
registration.php
1.) Overriding YumRegistrationController
Create a file RegistrationController.php in your project-specific
controllers/ directory containing some code like this:
Yii::import(
'user.registration.controllers.YumRegistrationController');
class RegistrationController extends YumRegistrationController {
}
This class extends the YumRegistrationController and has all the abilities
that the yum registration class has. We will adjust the behavior of this
controller later.
2.) Adjusting the registration view files
Now we create the view directory for our new created controller and copy
our example view files over there:
$ cd /path/to/my/webapp/protected
$ mkdir views/registration
$ cp modules/registration/views/registration/* views/registration
Our new Controller now uses this view file for its rendering.
We change the registration.php to look something like this:
<h2> My Registration Page </h2>
<p> Hi there, please enter your E-Mail address and drop a note about you </p>
<?php $this->breadcrumbs = array(Yum::t('Registration')); ?>
<div class="form">
<?php $activeform = $this->beginWidget('CActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>false,
'focus'=>array($profile,'email'),
));
?>
<?php echo Yum::requiredFieldNote(); ?>
<?php echo CHtml::errorSummary($profile); ?>
<div class="row"> <?php
echo $activeform->labelEx($profile,'email');
echo $activeform->textArea($profile,'email');
?> </div>
<div class="row"> <?php
echo $activeform->labelEx($profile,'about');
echo $activeform->textArea($profile,'about');
?> </div>
<div class="row submit">
<?php echo CHtml::submitButton(Yum::t('Registration')); ?>
</div>
<?php $this->endWidget(); ?>
3.) Changing the Controller Behavior
override the actionRegistration() of your Controller like this:
public function actionRegistration() {
Yii::import('user.profile.models.*');
$profile = new YumProfile;
if (isset($_POST['Profile'])) {
$profile->attributes = $_POST['YumProfile'];
if($profile->save()) {
$user = new YumUser;
$password = YumUser::generatePassword();
$user->register(md5($profile->email), $password, $profile, $user->generateSalt());
$this->sendRegistrationEmail($user, $password);
Yum::setFlash('Thank you for your registration. Please check your email.');
$this->redirect(Yum::module()->loginUrl);
}
$this->render('registration', array(
'profile' => $profile,
)
);
}
}
Now, we also need to override the sendRegistrationEmail method, because we want to include the clear-text password in the email:
public function sendRegistrationEmail($user, $password) {
if (!isset($user->profile->email)) {
throw new CException(Yum::t('Email is not set when trying to send Registration Email'));
}
$activation_url = $user->getActivationUrl();
if (is_object($content)) {
$body = strtr('Hi, {email}, your new password is {password}. Please activate your account by clicking this link: {activation_url}', array(
'{email}' => $user->profile->email,
'{password}' => $password,
'{activation_url}' => $activation_url));
$mail = array(
'from' => Yum::module('registration')->registrationEmail,
'to' => $user->profile->email,
'subject' => 'Your registration on my example Website',
'body' => $body,
);
$sent = YumMailer::send($mail);
}
return $sent;
}
4.) Making the "about" field required.
create a file models/Profile.php that extends the YumProfile:
Yii::import('user.profile.models.YumProfile');
class Profile extends YumProfile {
function rules() {
$rules = parent::rules();
$rules[] = array('about', 'required');
return $rules;
}
}