/**
  * Checks whether a user is allowed to respond to this alert.
  *
  * A user is allowed to respond to an alert if they are listed as
  * a "confirmed" member in one of the teams associated with this
  * alert.
  *
  * @todo
  * Currently, an alert dynamically looks up who is on the
  * teams associated with it. This should be changed so it
  * keeps a snapshotted list of the confirmed team members
  * at the time the alert was created. This will prevent a
  * user from being added to a team (and thus granted access
  * to an alert) *after* the alert has been sent out.
  *
  * @uses WP_Buoy_Team::get_confirmed_members()
  *
  * @param int $user_id
  *
  * @return bool
  */
 public function can_respond($user_id)
 {
     foreach ($this->get_teams() as $team_id) {
         $team = new WP_Buoy_Team($team_id);
         if (in_array($user_id, $team->get_confirmed_members())) {
             return true;
         }
     }
     return false;
 }
 /**
  * Add the column content for custom columns in the "My Teams" UI.
  *
  * @link https://developer.wordpress.org/reference/hooks/manage_post-post_type_posts_custom_column/
  *
  * @param string $column_name
  * @param int $post_id
  *
  * @return void
  */
 public static function renderTeamPostsColumn($column_name, $post_id)
 {
     $team = new WP_Buoy_Team($post_id);
     switch ($column_name) {
         case 'num_members':
             print esc_html(count($team->get_member_ids()));
             break;
         case 'confirmed_members':
             print esc_html(count($team->get_confirmed_members()));
             break;
         case 'default_team':
             if ($team->is_default()) {
                 print '<strong>' . esc_html__('Yes', 'buoy') . '</strong>';
             }
             break;
     }
 }
    /**
     * Returns the HTML for a Bootstrap Panel of the user's teams.
     *
     * @return string
     */
    public function renderChooseTeamsPanelHtml()
    {
        $teams = $this->get_teams();
        ?>
<div class="panel panel-default">
    <div class="panel-heading" role="tab" id="">
        <h3 class="panel-title">
            <?php 
        esc_html_e('Choose teams', 'buoy');
        ?>
        </h3>
    </div>
    <div class="panel-body">
        <table class="table" summary="<?php 
        esc_attr_e('Your teams with responders', 'buoy');
        ?>
">
            <thead>
                <tr>
                    <th></th>
                    <th><?php 
        esc_html_e('Team name', 'buoy');
        ?>
</th>
                    <th><?php 
        esc_html_e('Responders', 'buoy');
        ?>
</th>
                </tr>
            </thead>
            <tbody>
<?php 
        foreach ($teams as $team_id) {
            $team = new WP_Buoy_Team($team_id);
            if (!$team->has_responder()) {
                continue;
            }
            ?>
                <tr>
                    <td>
                        <input type="checkbox"
                            id="<?php 
            print esc_attr(self::$prefix);
            ?>
_team-<?php 
            print esc_attr($team_id);
            ?>
"
                            name="<?php 
            print esc_attr(self::$prefix);
            ?>
_teams[]"
                            <?php 
            checked($team->is_default());
            ?>
                            value="<?php 
            print esc_attr($team_id);
            ?>
"
                        />
                    </td>
                    <td>
                        <label for="<?php 
            print esc_attr(self::$prefix);
            ?>
_team-<?php 
            print esc_attr($team_id);
            ?>
">
                            <?php 
            print esc_html($team->wp_post->post_title);
            ?>
                        </label>
                    </td>
                    <td>
                        <?php 
            print esc_html(count($team->get_confirmed_members()));
            ?>
                    </td>
                </tr>
<?php 
        }
        ?>
            </tbody>
        </table>
    </div>
</div>
<?php 
    }
 /**
  * Runs whenever an alert is published. Sends notifications to an
  * alerter's response team informing them of the alert.
  *
  * @param int $post_id
  * @param WP_Post $post
  *
  * @return void
  */
 public static function publishAlert($post_id, $post)
 {
     $alert = new WP_Buoy_Alert($post_id);
     $responder_link = admin_url('?page=' . self::$prefix . '_review_alert' . '&' . self::$prefix . '_hash=' . $alert->get_hash());
     $responder_short_link = home_url('?' . self::$prefix . '_alert=' . substr($alert->get_hash(), 0, 8));
     $subject = $post->post_title;
     $alerter = get_userdata($post->post_author);
     $headers = array("From: \"{$alerter->display_name}\" <{$alerter->user_email}>");
     foreach ($alert->get_teams() as $team_id) {
         $team = new WP_Buoy_Team($team_id);
         foreach ($team->get_confirmed_members() as $user_id) {
             $responder = new WP_Buoy_User($user_id);
             // TODO: Write a more descriptive message.
             wp_mail($responder->wp_user->user_email, $subject, $responder_link, $headers);
             $smsemail = $responder->get_sms_email();
             if (!empty($smsemail)) {
                 $sms_max_length = 160;
                 // We need to ensure that SMS notifications fit within the 160 character
                 // limit of SMS transmissions. Since we're using email-to-SMS gateways,
                 // a subject will be wrapped inside of parentheses, making it two chars
                 // longer than whatever its original contents are. Then a space is
                 // inserted between the subject and the message body. The total length
                 // of strlen($subject) + 2 + 1 + strlen($message) must be less than 160.
                 $extra_length = 3;
                 // two parenthesis and a space
                 // but in practice, there seems to be another 7 chars eaten up somewhere?
                 $extra_length += 7;
                 $url_length = strlen($responder_short_link);
                 $full_length = strlen($subject) + $extra_length + $url_length;
                 if ($full_length > $sms_max_length) {
                     // truncate the $subject since the link must be fully included
                     $subject = substr($subject, 0, $sms_max_length - $url_length - $extra_length);
                 }
                 wp_mail($smsemail, $subject, $responder_short_link, $headers);
             }
         }
     }
 }