Version: current

      1 <?php
      2 /**
      3  * Регистрация пользователей
      4  * Можно определять в меню, где будет находиться страница регистрации
      5  *
      6  * @package auth
      7  * @author inxo, dvp
      8  */
      9 class RegistrationPage extends Page {
     10
     11     static $icon = "auth/img/register";
     12     static $allowed_children = 'none';
     13
     14     static $db = array(
     15         "ThanksTitle" => "HTMLVarchar",
     16         "ThanksContent" => "HTMLText",
     17         "ConfirmTitle" => "HTMLVarchar",
     18         "ConfirmContent" => "HTMLText",
     19     );
     20
     21     static $default = array(
     22         'URLSegment' => 'register',
     23         'AutoChild' => 0,
     24     );
     25
     26     static function find_link($action = '') {
     27         if (!$page = DataObject::get_one('RegistrationPage')) {
     28             return false;
     29             user_error('Создайте страницу регистрации', E_USER_ERROR);
     30         }
     31
     32         return $page->Link($action);
     33     }
     34
     35     function getCMSFields() {
     36         $fields = parent::getCMSFields();
     37         $fields->removeByName('AutoChild'); // Удалим переход к нижним элементам
     38
     39         $fields->addFieldToTab('Root.Content', new Tab("Thanks", _t('RegistrationPage.tab_Thanks', 'Thanks Page')), 'Metadata');
     40         $fields->addFieldsToTab("Root.Content.Thanks", array(
     41             new TextField("ThanksTitle", $this->fieldLabel('Title')),
     42             new HTMLEditorField("ThanksContent", $this->fieldLabel('Content')),
     43         ));
     44         $fields->addFieldToTab('Root.Content', new Tab("Confirm", _t('RegistrationPage.tab_Confirm', 'Confirm Page')), 'Metadata');
     45         $fields->addFieldsToTab("Root.Content.Confirm", array(
     46             new TextField("ConfirmTitle", $this->fieldLabel('Title')),
     47             new HTMLEditorField("ConfirmContent", $this->fieldLabel('Content')),
     48         ));
     49
     50         return $fields;
     51     }
     52
     53     function onBeforeWrite() {
     54         $this->AutoChild = 0;
     55         parent::onBeforeWrite();
     56     }
     57
     58     function requireDefaultRecords() {
     59         parent::requireDefaultRecords();
     60
     61         $code = "ACCESS_CLOSESIDE";
     62
     63         if (!$Group = DataObject::get_one("Group", "`Group`.`Code` = 'site-members'")) {
     64             $group = new Group();
     65             $group->Code = 'site-members';
     66             $group->Title = "Пользователи";
     67             $group->write();
     68
     69             Permission::grant($group->ID, $code);
     70             if (method_exists('DB', 'alteration_message'))
     71                 DB::alteration_message(_t('Registration.GROUPCREATED', 'Members group created'), "created");
     72         }
     73         if (!$Group = DataObject::get_one("Group", "`Group`.`Code` = 'unconfirm-site-members'")) {
     74             $group = new Group();
     75             $group->Code = 'unconfirm-site-members';
     76             $group->Title = "Пользователи неподтвержденные";
     77             $group->write();
     78
     79             Permission::grant($group->ID, 'DENY_CLOSESIDE');
     80             if (method_exists('DB', 'alteration_message'))
     81                 DB::alteration_message(_t('Registration.GROUPCREATED', 'Members group created'), "created");
     82         }
     83     }
     84
     85     function canCreate($member = false) {
     86         if (DataObject::get_one('RegistrationPage')) return false;
     87         if (!Director::isDev()) return false;
     88         return parent::canCreate($member);
     89     }
     90
     91     function canDelete() {
     92         return Director::isDev();
     93     }
     94
     95 }
     96
     97 class RegistrationPage_Controller extends Page_Controller {
     98
     99     static $allowed_actions = array('RegistrationForm', 'thanks', 'confirm');
    100
    101     function init() {
    102         parent::init();
    103         if (Member::currentUserID() && $this->getAction() == '') {
    104             $this->data()->Content = '<p>Вы уже зарегистрированы</p>';
    105         }
    106     }
    107     /**
    108      * Обертка для шаблона
    109      */
    110     function Form() {
    111         if ($form = $this->RegistrationForm()) {
    112             $form->fields()->dataFieldByName('Password')->setValue('');
    113         }
    114         return $form;
    115     }
    116
    117     /**
    118      * Форма регистрации
    119      *
    120      * Получает список полей от Member
    121      *
    122      * @return Form|NULL
    123      */
    124     function RegistrationForm() {
    125         if (Member::currentUserID()) return false;
    126
    127         $member = singleton('Member');
    128         $form = new Form(
    129             $this,
    130             'RegistrationForm',
    131             $member->getMemberFormFields(),
    132             new FieldSet(new FormAction('register', _t('RegistrationPage.REGISTERBUTTON', 'Register'))),
    133             $member->getValidator()
    134         );
    135         if (class_exists('SpamProtectorManager')) {
    136             SpamProtectorManager::update_form($form, null, array(), _t('RegistrationPage.Captcha', 'Captcha'));
    137         }
    138         $siteConfig = SiteConfig::current_site_config();
    139         if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
    140             $rulesField->setForm($form);
    141             $form->Fields()->push($rulesField);
    142             $form->getValidator()->addRequiredField($rulesField->Name());
    143         }
    144         $this->extend('updateRegistrationForm', $form);
    145         return $form;
    146     }
    147
    148     /**
    149      * Создает нового пользователя
    150      *
    151      * Вызывается из формы RegistrationForm
    152      * Создает пользователя, отправляет ему письмо и редеректит на страницу благодарностей
    153      *
    154      * @param array $data
    155      * @param Form $form
    156      */
    157     function register($data, $form) {
    158         $member = new Member();
    159         $form->saveInto($member);
    160         $member->Email = trim($member->Email);
    161         $member->Locale = i18n::get_locale();
    162         $password = $member->Password;
    163
    164         $id = $member->write();
    165
    166         if ($this->SiteConfig()->AuthActivationRequired) {
    167             $group = DataObject::get_one('Group', "Code = 'unconfirm-site-members'");
    168             if ($group)
    169                 $group->Members()->add($member);
    170             $member->ConfirmLink = Director::absoluteURL(RegistrationPage::find_link('confirm/' . $member->ActivationKey));
    171         } else {
    172             $Group = DataObject::get_one('Group', "Code = 'site-members'");
    173             if ($Group)
    174                 $Group->Members()->add($member);
    175             $member->login();
    176         }
    177
    178         $member->CleanPassword = $password;
    179         $conf = SiteConfig::current_site_config();
    180         $mail = new Email();
    181         $mail->setFrom($conf->AuthSenderEmail());
    182         $mail->setTo($member->Email);
    183         $mail->populateTemplate($member);
    184         $mail->setSubject($conf->AuthNewUserSubject());
    185         $mail->setTemplate('RegistrationComplite_Email');
    186         $mail->send();
    187
    188         if ($conf->NotifyAdminForNewUser) {
    189             $mail = new Email($conf->AuthSenderEmail(), $conf->AuthSenderEmail(), ($conf->NewUserLetterTheme ? $conf->NewUserLetterTheme : _t('SiteConfig.DefaultNewUserLetterTheme')));
    190             $mail->populateTemplate($member);
    191             $mail->setTemplate('NewRegistration_Email');
    192             $mail->send();
    193         }
    194         Director::redirect($this->Link() . 'thanks');
    195         return;
    196     }
    197
    198     /**
    199      * Страница благодарностей после регистрации
    200      */
    201     function thanks() {
    202         if (!$this->SiteConfig()->AuthActivationRequired && ($backURL = Session::get('BackURL'))) {
    203             Session::clear('BackURL');
    204             return Director::redirect($backURL);
    205         }
    206
    207         return array(
    208             'Title' => $this->ThanksTitle,
    209             'Content' => $this->ThanksContent,
    210             'Form' => false,
    211         );
    212     }
    213
    214     /**
    215      * Подтверждение регистрации
    216      */
    217     function confirm() {
    218         if (Director::urlParams()) {
    219             $id = Director::urlParam('ID');
    220             $member = MemberActivation::get_by_activation_key($id);
    221             if ($member) {
    222                 $group = DataObject::get_one('Group', "Code = 'site-members'");
    223                 $group->Members()->add($member);
    224                 $group = DataObject::get_one('Group', "Code = 'unconfirm-site-members'");
    225                 $group->Members()->remove($member);
    226                 $member->login();
    227                 return array(
    228                     'Title' => $this->ConfirmTitle,
    229                     'Content' => $this->ConfirmContent,
    230                     'Form' => false,
    231                 );
    232             }
    233         }
    234         Director::redirect('');
    235     }
    236
    237 }
    238
    239

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -