/**
  * Table constructor with optional contest parameter.
  * @param cf_Contest $contest
  * @return cf_Table_Participants
  */
 function __construct($contest = '')
 {
     $this->per_page = 30;
     $this->contest = $contest;
     if (!is_object($contest)) {
         $this->contest = '';
     }
     if ($this->contest != '') {
         $this->current_winner_num = $this->contest->get_current_winner_num();
     }
     $this->extra_nav = array();
     $contests = cf_Contest::get_all();
     foreach ($contests as $contest) {
         $this->extra_nav[$contest->ID] = $contest->cf_headline;
     }
     parent::__construct();
 }
 /** 
  * Generates custom content for dashboard tab.
  */
 protected function generate_dashboard()
 {
     $contest_status = '';
     $isstarted = $this->contest->is_started();
     $isexpired = $this->contest->is_expired();
     $contest_status = __('Expired', 'contestfriend');
     if (!$isstarted) {
         $contest_status = __('Not active yet', 'contestfriend');
     } else {
         if ($isexpired && $this->contest->cf_status == 'expired') {
             $contest_status = __('Expired (forced)', 'contestfriend');
         } else {
             if ($isexpired && $this->contest->cf_status == 'active') {
                 $contest_status = __('Expired (automatic)', 'contestfriend');
             } else {
                 if ($isexpired && $this->contest->cf_status == 'winners_picked') {
                     $contest_status = __('Expired (winners picked)', 'contestfriend');
                 } else {
                     if ($isstarted && !$isexpired) {
                         $contest_status = __('Active', 'contestfriend');
                     }
                 }
             }
         }
     }
     $part_num = cf_Participant::get_num($this->contest->ID);
     $part_num_all = cf_Participant::get_num($this->contest->ID, 'all');
     $participant_num = $part_num;
     if ($part_num_all != $part_num) {
         $participant_num .= ' (' . $part_num_all . ')';
     }
     echo '<div style="margin-top: 5px;">';
     // Dashboard buttons
     if (!$this->contest->is_expired() || $this->contest->cf_status == 'expired') {
         // display stop / resume contest
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="setexpired" value="1" />';
         $text = __('Stop Contest', 'contestfriend');
         if ($this->contest->cf_status == 'expired') {
             $text = __('Resume Contest', 'contestfriend');
         }
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     if ($this->contest->is_expired() && $this->contest->cf_status == 'winners_picked') {
         // display reset contest button
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="resetcontest" value="1" />';
         $text = __('Reset Contest', 'contestfriend');
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     // if not all winners were picked, show pick buttons
     $winners_num = 1;
     $current_winners = $this->contest->get_current_winner_num();
     if (isset($this->contest->cf_winners_num) && is_numeric($this->contest->cf_winners_num) && $this->contest->cf_winners_num > 0) {
         $winners_num = $this->contest->cf_winners_num;
     }
     if ($current_winners < $winners_num) {
         $confirm_class = '';
         if ($current_winners == 0 && $this->contest->cf_status != 'winners_picked') {
             $confirm_class = 'confirm_setwinner';
         }
         $text = __('Pick Random Winners', 'contestfriend');
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form class="' . $confirm_class . '" action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="pickwinners" value="1" />';
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
         $text = __('Manually Pick Winners', 'contestfriend');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px">';
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     // if there are some winners picked, show clear winners button
     if ($current_winners > 0) {
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         $text = __('Clear All Winners', 'contestfriend');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="clearwinners" value="1" />';
         submit_button($text, 'button-secondary action', false, false);
         echo '</a></form>';
     }
     echo '</div>
     <div id="poststuff" style="clear: both;">
     <div class="postbox">
     <h3>' . __('Contest Status', 'contestfriend') . '</h3>
     <div class="inside">    
     <table class="form-table">
     <tbody><tr valign="top"><th class="cf_label">' . __('Status', 'contestfriend') . '</th>
     <td>' . $contest_status . '</td>
     </tr>
     <tr valign="top"><th class="cf_label" style="vertical-align: middle">' . __('Number of participants', 'contestfriend') . '</th>
     <td style="vertical-align: middle">' . $participant_num . ' <a href="' . admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest']) . '" class="button">' . __('View all participants', 'contestfriend') . '</a></td>
     </tr>
     </table>
     </div></div>';
     if ($current_winners > 0) {
         $winners = cf_Participant::get_all($this->contest->ID, '', '', '', '', 'winner');
         echo '<div class="postbox">
         <h3>' . __('Winners', 'contestfriend') . '</h3>
         <div class="inside">
         <table class="form-table">
         <tbody>';
         foreach ($winners as $winner) {
             $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
             $text = __('Remove Winner', 'contestfriend');
             echo '<tr valign="top"><th class="cf_label">' . $winner->email . ', ' . $winner->first_name . ' ' . $winner->last_name . '</th><td><form action="' . $url . '" method="post"><input type="hidden" name="removewinner" value="' . esc_attr($winner->id) . '" />';
             submit_button($text, 'button-secondary action', false, false);
             echo '</td></tr>';
         }
         echo '
         </table>    
         </div>
         </div>
         ';
     }
     echo '</div>';
 }
 /**
  * Init method, called when accessing the page. Handles processing of participant and contest $_POST and $_GET requests.
  */
 function init()
 {
     $contest = '';
     if (isset($_GET['contest'])) {
         $contest = $_GET['contest'];
     }
     if (isset($_GET['export'])) {
         cf_Table_Participants::export_csv($contest);
     }
     if (isset($_GET['togglevalid'])) {
         $participant = new cf_Participant($_GET['togglevalid']);
         if ($participant->_valid) {
             if ($participant->status == 'not_valid') {
                 $participant->set_status('');
             } else {
                 if ($participant->status != 'not_confirmed') {
                     $participant->set_status('not_valid');
                 }
             }
         }
         $url = 'admin.php?page=' . self::page_id;
         if (isset($_GET['contest'])) {
             $url .= '&contest=' . $_GET['contest'];
         }
         wp_safe_redirect(admin_url($url));
         die;
     }
     if (isset($_GET['del'])) {
         $participant = new cf_Participant($_GET['del']);
         if ($participant->_valid) {
             $participant->delete();
         }
         $url = 'admin.php?page=' . self::page_id;
         if (isset($_GET['contest'])) {
             $url .= '&contest=' . $_GET['contest'];
         }
         wp_safe_redirect(admin_url($url));
         die;
     }
     if (isset($_GET['contest'])) {
         $contest = new cf_Contest($_GET['contest']);
         if ($contest->_valid != true) {
             wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Participants::page_id));
             die;
         }
         if (isset($_POST['resetcontest'])) {
             if ($contest->cf_status == 'winners_picked') {
                 $contest->set_status('active');
                 $contest->clear_winners();
             }
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
         if (isset($_POST['setexpired'])) {
             if ($contest->cf_status == 'expired') {
                 $contest->set_status('active');
             } else {
                 if ($contest->cf_status == 'active' || $contest->cf_status == '') {
                     $contest->set_status('expired');
                 }
             }
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
         if (isset($_POST['pickwinners'])) {
             $winners_num = 1;
             if (isset($contest->cf_winners_num) && is_numeric($contest->cf_winners_num) && $contest->cf_winners_num > 0) {
                 $winners_num = $contest->cf_winners_num;
             }
             $current_winners = $contest->get_current_winner_num();
             $pick_num = intval($winners_num) - intval($current_winners);
             if ($pick_num > 0) {
                 $contest->pick_winners($pick_num);
             }
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
         if (isset($_POST['clearwinners'])) {
             $contest->clear_winners();
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
         if (isset($_POST['pickwinner'])) {
             $winners_num = 1;
             if (isset($contest->cf_winners_num) && is_numeric($contest->cf_winners_num) && $contest->cf_winners_num > 0) {
                 $winners_num = $contest->cf_winners_num;
             }
             if ($contest->get_current_winner_num() < $winners_num) {
                 $participant = new cf_Participant($_POST['pickwinner']);
                 if ($participant->_valid) {
                     $participant->set_status('winner');
                     $contest->set_status('winners_picked');
                 }
             }
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
         if (isset($_POST['removewinner'])) {
             $participant = new cf_Participant($_POST['removewinner']);
             if ($participant->_valid) {
                 if (substr($participant->status, 0, 6) == 'winner') {
                     $participant->set_status('');
                 }
             }
             if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') {
                 wp_safe_redirect(admin_url('admin.php?page=' . cf_Page_Contest::page_id . '&contest=' . $contest->ID . '&cf_page=dashboard'));
             } else {
                 wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID));
             }
             die;
         }
     }
     add_action('admin_footer', array(&$this, 'list_scripts'));
 }