Version: current

      1 <?php
      2
      3 /**
      4  * Create a form that a user can use to unsubscribe from a mailing list
      5  *
      6  * @package newsletter
      7  */
      8 class Unsubscribe_Controller extends Page_Controller {
      9
     10     public static $done_message;
     11
     12     function __construct($data = null) {
     13         parent::__construct($data);
     14     }
     15
     16     function RelativeLink($action = null) {
     17         return "unsubscribe/$action";
     18     }
     19
     20     function index() {
     21         Session::clear("loggedInAs");
     22         Requirements::themedCSS("form");
     23
     24         // if the email address is given
     25         $emailAddress = Convert::raw2sql($this->urlParams['Email']);
     26         $mailingListID = (int) $this->urlParams['MailingList'];
     27
     28         if ($mailingListID) {
     29             $mailingList = DataObject::get_by_id("NewsletterType", $mailingListID);
     30         }
     31
     32         // try to find the member with the email specified
     33         if ($emailAddress) {
     34             $member = DataObject::get_one('Member', "`Email` = '$emailAddress'");
     35         } else {
     36             $member = false;
     37         }
     38
     39         // if the email address and mailing list is given in the URL and both are valid,
     40         // then unsubscribe the user
     41         if ($member && $mailingList && $member->inGroup($mailingList->GroupID)) {
     42             $this->unsubscribeFromList($member, $mailingList);
     43             $url = 'done/' . $member->Email . '/' . $mailingList->ID;
     44             Director::redirect(Director::absoluteBaseURL() . $this->RelativeLink() . $url);
     45             return;
     46         } elseif ($member) {
     47             $form = $this->MailingListForm($member, $this->urlParams['Email']);
     48         } else {
     49             $form = $this->EmailAddressForm();
     50         }
     51
     52         return $this->customise(array(
     53             'Title' => _t('Unsubscribe.UNSUBSCRIBE', 'Unsubscribe'),
     54             'Content' => $form->forTemplate()
     55         ))->renderWith('Page');
     56     }
     57
     58     function done() {
     59         $message = self::$done_message ? self::$done_message : _t('Unsubscribe.SUCCESS', 'Thank you. You have been removed from the selected groups');
     60
     61         return $this->customise(array(
     62             'Title' => _t('Unsubscribe.UNSUBSCRIBED', 'Unsubscribed'),
     63             'Content' => $message
     64         ))->renderWith('Page');
     65     }
     66
     67     /**
     68      * Show the lists for the user with the given email address
     69      * (action of Unsubscribe_EmailAddressForm)
     70      */
     71     function showlists($data, $form) {
     72         $email = Convert::raw2sql($data['Email']);
     73         $member = DataObject::get_one('Member', "`Email`='{$email}'");
     74
     75         if ($member) {
     76             $form = $this->MailingListForm($member, $data['Email']);
     77         } else {
     78             $form->addErrorMessage('Email', 'Для этого адреса нет подписки на рассылки сайта', 'error');
     79             $form->setupFormErrors();
     80         }
     81
     82         return $this->customise(array(
     83             'Content' => $form->forTemplate()
     84         ))->renderWith('Page');
     85     }
     86
     87     /**
     88      * Unsubscribe the user from the given lists.
     89      * (action of Unsubscribe_MailingListForm)
     90      */
     91     function unsubscribe($data, $form) {
     92         $email = Convert::raw2sql(@$data['email']);
     93         $memberID = Session::get('UnsubscribeMemberID');
     94         $member = DataObject::get_by_id('Member', $memberID);
     95         if (!$member) {
     96             $member = DataObject::get_one('Member', "`EmailAddress` = '$email'");
     97         }
     98
     99         if ($data['MailingLists']) {
    100             foreach (array_keys($data['MailingLists']) as $listID) {
    101
    102                 $nlType = DataObject::get_by_id('NewsletterType', $listID);
    103                 $nlTypeTitles[] = $nlType->Title;
    104                 $this->unsubscribeFromList($member, DataObject::get_by_id('NewsletterType', $listID));
    105             }
    106
    107             $sORp = (sizeof($nlTypeTitles) > 1) ? "newsletters " : "newsletter ";
    108             //means single or plural
    109             $nlTypeTitles = $sORp . implode(", ", $nlTypeTitles);
    110             $url = "unsubscribe/done/" . $member->Email . "/" . $nlTypeTitles;
    111             Director::redirect($url);
    112         } else {
    113             $form->addErrorMessage('MailingLists', _t('Unsubscribe.NOMLSELECTED', 'You need to select at least one mailing list to unsubscribe from.'), 'bad');
    114             Director::redirectBack();
    115         }
    116     }
    117
    118     function MailingListForm($member=null) {
    119         if ($member && is_a($member, 'Member')) {
    120             Session::set('UnsubscribeMemberID', $member->ID);
    121         } else {
    122             $memberID = Session::get('UnsubscribeMemberID');
    123             $member = DataObject::get_by_id('Member', $memberID);
    124         }
    125
    126         if ($member)
    127             $email = $member->Email;
    128         return new Unsubscribe_MailingListForm($this, 'MailingListForm', $member, $email);
    129     }
    130
    131     function EmailAddressForm() {
    132         $url = $this->requestParams['url'];
    133         $email = (preg_match('!/unsubscribe/index/([^/]+)/?!', $url, $m)) ? $m[1] : '';
    134         return new Form(
    135                 $this,
    136                 'EmailAddressForm',
    137                 new FieldSet(new EmailField('Email', _t('Unsubscribe.EMAILADDR', 'Email address'), $email)),
    138                 new FieldSet(new FormAction('showlists', _t('Unsubscribe.SHOWLISTS', 'Next'))),
    139                 new RequiredFields('Email')
    140         );
    141     }
    142
    143     protected function unsubscribeFromList($member, $list) {
    144         // track unsubscriptions
    145         $member->Groups()->remove($list->GroupID);
    146         $unsubscribeRecord = new UnsubscribeRecord();
    147         $unsubscribeRecord->unsubscribe($member, $list);
    148     }
    149
    150 }
    151
    152 /**
    153  * 2nd step form for the Unsubcribe page.
    154  * The form will list all the mailing lists that the user is subscribed to.
    155  *
    156  * @package newsletter
    157  */
    158 class Unsubscribe_MailingListForm extends Form {
    159
    160     function __construct($controller, $name, $member, $email) {
    161         $fields = new FieldSet(new HiddenField('email', 'email', $member->Email));
    162         $actions = new FieldSet();
    163
    164         if ($member) {
    165             // get all the mailing lists for this user
    166             $lists = DataObject::get('NewsletterType', "`MemberID`='{$member->ID}'", null, "LEFT JOIN `Group_Members` USING(`GroupID`)");
    167         } else {
    168             $lists = false;
    169         }
    170
    171         if ($lists) {
    172             $fields->push(new HeaderField('SubscribedToLabel', _t('Unsubscribe.SUBSCRIBEDTO', 'You are subscribed to the following lists:'),4));
    173
    174             foreach ($lists as $list) {
    175                 $fields->push(new CheckboxField("MailingLists[{$list->ID}]", $list->Title));
    176             }
    177
    178             $actions->push(new FormAction('unsubscribe', _t('Unsubscribe.UNSUBSCRIBE', 'Unsubscribe')));
    179         } else {
    180             $fields->push(new HeaderField('NotSubscribedToLabel', _t('Unsubscribe.NOTSUBSCRIBED', 'I\'m sorry, but doesn\'t appear to be in any of our mailing lists.'),4));
    181         }
    182
    183         parent::__construct($controller, $name, $fields, $actions);
    184     }
    185
    186 }
    187
    188

[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. -