BanModel::setBanned PHP Method

setBanned() public static method

Set the banned mask value for a reason and return the new value.
public static setBanned ( integer $banned, boolean $value, integer $reason ) : integer
$banned integer The current banned value.
$value boolean The new ban value for the given reason.
$reason integer The reason for the banning. This should be one of the `BanModel::BAN_*` constants.
return integer Returns the new banned value.
    public static function setBanned($banned, $value, $reason)
    {
        if ($value) {
            $banned = $banned | $reason;
        } else {
            $banned = $banned & ~$reason;
        }
        return $banned;
    }

Usage Example

Beispiel #1
0
 /**
  * Unban a user.
  *
  * @param int $UserID The user to unban.
  * @param array $Options Options for the unban.
  * @since 2.1
  */
 public function unBan($UserID, $Options = [])
 {
     $User = $this->getID($UserID, DATASET_TYPE_ARRAY);
     if (!$User) {
         throw notFoundException();
     }
     $Banned = $User['Banned'];
     if (!BanModel::isBanned($Banned, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL)) {
         throw new Gdn_UserException(t("The user isn't banned.", "The user isn't banned or is banned by some other function."));
     }
     // Unban the user.
     $NewBanned = BanModel::setBanned($Banned, false, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL);
     $this->setField($UserID, 'Banned', $NewBanned);
     // Restore the user's content.
     if (val('RestoreContent', $Options)) {
         $BanLogID = $this->getAttribute($UserID, 'BanLogID');
         if ($BanLogID) {
             $LogModel = new LogModel();
             try {
                 $LogModel->restore($BanLogID);
             } catch (Exception $Ex) {
                 if ($Ex->getCode() != 404) {
                     throw $Ex;
                 }
             }
             $this->saveAttribute($UserID, 'BanLogID', null);
         }
     }
     // Add an activity for the unbanning.
     if (val('AddActivity', $Options, true)) {
         $ActivityModel = new ActivityModel();
         $Story = val('Story', $Options, null);
         // Notify the moderators of the unban.
         $Activity = ['ActivityType' => 'Ban', 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::session()->UserID, 'HeadlineFormat' => t('HeadlineFormat.Unban', '{RegardingUserID,You} unbanned {ActivityUserID,you}.'), 'Story' => $Story, 'Data' => ['Unban' => true]];
         $ActivityModel->queue($Activity);
         // Notify the user of the unban.
         $Activity['NotifyUserID'] = $UserID;
         $Activity['Emailed'] = ActivityModel::SENT_PENDING;
         $Activity['HeadlineFormat'] = t('HeadlineFormat.Unban.Notification', "You've been unbanned.");
         $ActivityModel->queue($Activity, false, ['Force' => true]);
         $ActivityModel->saveQueue();
     }
 }