Version: current

      1 <?php
      2
      3 /**
      4  * NewsletterRole provides extensions to the {@link Member}
      5  * class, with new database fields and functions specific
      6  * to the newsletter module.
      7  *
      8  * @package newsletter
      9  */
     10 class NewsletterRole extends DataObjectDecorator {
     11
     12     function extraStatics() {
     13         return array(
     14             'db' => array(
     15                 'BlacklistedEmail' => 'Boolean'
     16             ),
     17             'has_many' => array(
     18                 'UnsubscribedRecords' => 'UnsubscribeRecord'
     19             )
     20         );
     21     }
     22
     23     /**
     24      * Update the CMS fields specifically for Member
     25      * decorated by this NewsletterRole decorator.
     26      *
     27      * @param FieldSet $fields CMS fields to update
     28      */
     29     function updateCMSFields($fields) {
     30         $fields->removeByName('UnsubscribedRecords');
     31         $fields->removeByName('BlacklistedEmail');
     32     }
     33
     34     /**
     35      * Update the frontend fields specifically for Member
     36      * decorated by this NewsletterRole decorator.
     37      *
     38      * @param FieldSet $fields Frontend fields to update
     39      */
     40     function updateMemberFormFields($fields) {
     41         $fields->removeByName('BlacklistedEmail');
     42     }
     43
     44     /**
     45      * Factory method for the member validator.
     46      *
     47      * @TODO It's unclear where this is used.
     48      *
     49      * @return Member_Validator Returns an instance of a
     50      *                          {@link Member_Validator} object.
     51      */
     52     function getNewsletterSubscriptions() {
     53         $groups = $this->owner->Groups()->toDropDownMap('ID', 'ID');
     54         return $groups;
     55     }
     56
     57     /**
     58      * Unsubscribe the current {@link Member} from
     59      * a newsletter.
     60      *
     61      * @TODO It's unclear where this is used.
     62      *
     63      * @param NewsletterType $newsletterType Newsletter type to unsubscribe from
     64      */
     65     function unsubscribeFromNewsletter($newsletterType) {
     66         $unsubscribeRecord = new UnsubscribeRecord();
     67         $unsubscribeRecord->unsubscribe($this->owner, $newsletterType);
     68         $this->owner->Groups()->remove($newsletterType->GroupID);
     69     }
     70
     71     /**
     72      * This does some cunning and automatically save the newsletter subscriptions
     73      * by adding and removing the member from the appropriate
     74      * groups based on a checkboxset field.
     75      * This function is called by the form handler
     76      * whenever form->saveInto($member); is called with an
     77      * checkboxsetfield in the data with the name
     78      * "newsletterSubscriptions"
     79      */
     80     function saveNewsletterSubscriptions($groups) {
     81         $checkboxsetfield = new CheckboxSetField(
     82             "NewsletterSubscriptions",
     83             "",
     84             $sourceitems = DataObject::get("NewsletterType")->toDropDownMap("GroupID","Title"),
     85             $selectedgroups = $groups
     86         );
     87         return $this->owner->Groups()->setByCheckboxSetField($checkboxsetfield);
     88     }
     89
     90     function removeAllNewsletterSubscriptions(){
     91         $groups = $this->owner->Groups();
     92         $groupIDs = $groups->getIDList();
     93         $newsletterTypes = DataObject::get("NewsletterType");
     94         if($newsletterTypes&&$newsletterTypes->count()){
     95             foreach($newsletterTypes as $type){
     96                 $newsletterGroupIDs[] = $type->GroupID;
     97             }
     98         }
     99         if($newsletterGroupIDs) {
    100             foreach($newsletterGroupIDs as $newsletterGroupID){
    101                 if($groupIDs&&in_array($newsletterGroupID, $groupIDs)){
    102                     $groups->remove($newsletterGroupID);
    103                 }
    104             }
    105         }
    106     }
    107
    108     /**
    109      * Add the members email address to the blacklist
    110      *
    111      * With this method the blacklisted email table is updated to ensure that
    112      * no promotional material is sent to the member (newsletters).
    113      * Standard system messages are still sent such as receipts.
    114      *
    115      * @param bool $val Set to TRUE if the address should be added to the
    116      *                  blacklist, otherwise to FALSE.
    117      */
    118     function setBlacklistedEmail($val) {
    119         if($val && $this->owner->Email) {
    120             $blacklisting = new NewsletterEmailBlacklist();
    121             $blacklisting->BlockedEmail = $this->owner->Email;
    122             $blacklisting->MemberID = $this->owner->ID;
    123             $blacklisting->write();
    124         }
    125
    126         $this->owner->setField("BlacklistedEmail", $val);
    127         // Save the BlacklistedEmail field to the Member table
    128         $this->owner->write();
    129     }
    130
    131 }
    132
    133 ?>

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