RainLab\User\Models\MailBlocker::setPreferences PHP Method

setPreferences() public static method

MailBlocker::setPreferences($user, [acme.blog::post.new_reply => 0]) MailBlocker::setPreferences($user, [acme.blog::post.new_reply => 0], [fillable => [acme.blog::post.new_reply]]) MailBlocker::setPreferences($user, [template_alias => 0], [aliases => [template_alias => acme.blog::post.new_reply]]) Supported options: - aliases: Alias definitions, with alias as key and template as value. - fillable: An array of expected templates, undefined templates are ignored. - verify: Only allow mail templates that are registered in the system.
public static setPreferences ( RainLab\User\Models\User $user, array $templates, array $options = [] ) : void
$user RainLab\User\Models\User
$templates array Template name as key and boolean as value. If false, template is blocked.
$options array
return void
    public static function setPreferences($user, $templates, $options = [])
    {
        $templates = (array) $templates;
        if (!$user) {
            throw new Exception('A user must be provided for MailBlocker::setPreferences');
        }
        extract(array_merge(['aliases' => [], 'fillable' => [], 'verify' => false], $options));
        if ($aliases) {
            $fillable = array_merge($fillable, array_values($aliases));
            $templates = array_build($templates, function ($key, $value) use($aliases) {
                return [array_get($aliases, $key, $key), $value];
            });
        }
        if ($fillable) {
            $templates = array_intersect_key($templates, array_flip($fillable));
        }
        if ($verify) {
            $existing = MailTemplate::listAllTemplates();
            $templates = array_intersect_key($templates, $existing);
        }
        $currentBlocks = array_flip(static::checkAllForUser($user));
        foreach ($templates as $template => $value) {
            // User wants to receive mail and is blocking
            if ($value && isset($currentBlocks[$template])) {
                static::removeBlock($template, $user);
            } elseif (!$value && !isset($currentBlocks[$template])) {
                static::addBlock($template, $user);
            }
        }
    }

Usage Example

Beispiel #1
0
 public function onUpdate()
 {
     try {
         if (!$this->canEdit()) {
             throw new ApplicationException('Permission denied.');
         }
         $member = $this->getMember();
         if (!$member) {
             return;
         }
         /*
          * Process mail preferences
          */
         if ($member->user) {
             MailBlocker::setPreferences($member->user, post('MailPreferences'), ['aliases' => $this->getMailTemplates()]);
         }
         /*
          * Save member
          */
         $data = array_except(post(), 'MailPreferences');
         $member->fill($data);
         $member->save();
         Flash::success(post('flash', 'Settings successfully saved!'));
         return $this->redirectToSelf();
     } catch (Exception $ex) {
         Flash::error($ex->getMessage());
     }
 }