forked from SmartMobileHouse/customregistration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomRegistrationPlugin.php
106 lines (87 loc) · 4.06 KB
/
CustomRegistrationPlugin.php
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
<?php
namespace Craft;
class CustomRegistrationPlugin extends BasePlugin
{
//http://buildwithcraft.com/classreference/services/UsersService#activateUser-detail
//http://craftcms.stackexchange.com/questions/5216/user-registration-keep-as-inactive-until-verified-by-admin
public function init()
{
parent::init();
craft()->on('users.saveUser', function(Event $event)
{
$user = $event->params['user'];
$isNewUser = $event->params['isNewUser'];
if ($isNewUser) {
// assign to group
craft()->userGroups->assignUserToGroups($user->id, 1);
// Get all users from UserGroup
$user_criteria = craft()->elements->getCriteria(ElementType::User);
$user_criteria->groupId = '3';
$users = $user_criteria->find();
// Send mail to each Administrator
foreach ($users as $admin) {
$email = new EmailModel();
$email->toEmail = $admin->email;
$email->subject = $this->parseMessage($this->getSettings()->adminMessageSubject, $user->firstname, $user->lastname, $user->business, $user->reason);
$email->body = $this->parseMessage($this->getSettings()->adminMessage, $user->firstname, $user->lastname, $user->business, $user->reason);
craft()->email->sendEmail($email);
}
$email = new EmailModel();
$email->toEmail = $user->email;
$email->subject = $this->parseMessage($this->getSettings()->registrationMessageSubject, $user->firstname, $user->lastname, $user->business, $user->reason);
$email->body = $this->parseMessage($this->getSettings()->registrationMessage, $user->firstname, $user->lastname, $user->business, $user->reason);
craft()->email->sendEmail($email);
}
});
craft()->on('users.activateUser', function(Event $event)
{
$user = $event->params['user'];
$email = new EmailModel();
$email->toEmail = $user->email;
$email->subject = $this->parseMessage($this->getSettings()->activationMessageSubject, $user->firstname, $user->lastname, $user->business, $user->reason);
$email->body = $this->parseMessage($this->getSettings()->activationMessage, $user->firstname, $user->lastname, $user->business, $user->reason);
craft()->email->sendEmail($email);
});
}
function getName()
{
return Craft::t('Custom Registration');
}
function getVersion()
{
return '1.0.0';
}
function getDeveloper()
{
return 'Mateusz Madej';
}
function getDeveloperUrl()
{
return 'http://www.smartmobilehouse.pl';
}
protected function defineSettings()
{
return array(
'registrationMessage' => array(AttributeType::String, 'required' => false),
'registrationMessageSubject' => array(AttributeType::String, 'required' => false),
'adminMessage' => array(AttributeType::String, 'required' => false),
'adminMessageSubject' => array(AttributeType::String, 'required' => false),
'activationMessage' => array(AttributeType::String, 'required' => false),
'activationMessageSubject' => array(AttributeType::String, 'required' => false)
);
}
public function getSettingsHtml()
{
return craft()->templates->render('customRegistration/_settings', array(
'settings' => $this->getSettings()
));
}
public function parseMessage($message, $firstname, $lastname, $business, $reason)
{
$message = str_replace("{{firstName}}", $firstname, $message);
$message = str_replace("{{lastName}}", $lastname, $message);
$message = str_replace("{{business}}", $business, $message);
$message = str_replace("{{reason}}", $reason, $message);
return $message;
}
}