/**
  * prepares the error messages and CSS for a main database submission
  *
  * @return array indexed array of error messages
  */
 public function get_validation_errors()
 {
     // check for errors
     if (!$this->errors_exist()) {
         return array();
     }
     $output = '';
     $error_messages = array();
     $this->error_CSS = array();
     foreach ($this->errors as $field => $error) {
         $field_atts = Participants_Db::get_field_atts($field);
         switch ($field_atts->form_element) {
             case 'rich-text':
             case 'text-area':
             case 'textarea':
                 $element = 'textarea';
                 break;
             case 'link':
             case 'captcha':
                 $field_atts->name .= '[]';
             case 'text':
             case 'text-line':
             case 'date':
                 $element = 'input';
                 break;
             case 'image-upload':
             case 'file-upload':
                 $element = 'input';
                 break;
             default:
                 $element = false;
         }
         $this->error_CSS[] = '[class*="' . Participants_Db::$prefix . '"] [name="' . $field_atts->name . '"]';
         if (isset($this->error_messages[$error])) {
             $error_messages[] = $error == 'nonmatching' ? sprintf($this->error_messages[$error], $field_atts->title, Participants_Db::column_title($field_atts->validation)) : sprintf($this->error_messages[$error], $field_atts->title);
             $this->error_class = Participants_Db::$prefix . 'error';
         } else {
             $error_messages[] = $error;
             $this->error_class = empty($field) ? Participants_Db::$prefix . 'message' : Participants_Db::$prefix . 'error';
         }
     }
     // $this->errors
     return $error_messages;
 }
 /**
  * prints a table header row
  */
 public function print_header_row($head_pattern)
 {
     // print the top header row
     foreach ($this->display_columns as $column) {
         $title = stripslashes(Participants_Db::column_title($column));
         printf($head_pattern, str_replace(array('"', "'"), array('"', '''), $title), $column);
     }
 }
    /**
     * prints the sorting and filtering forms
     *
     * @param string $mode determines whether to print filter, sort, both or 
     *                     none of the two functions
     */
    private static function _sort_filter_forms()
    {
        global $post;
        $filter_count = intval(self::$filter['list_filter_count']);
        //build the list of columns available for filtering
        $filter_columns = array();
        foreach (Participants_db::get_column_atts('backend') as $column) {
            // add the field name if a field with the same title is already in the list
            $title = Participants_Db::set_filter('translate_string', $column->title);
            $select_title = isset($filter_columns[$column->title]) || strlen($column->title) === 0 ? $title . ' (' . $column->name . ')' : $title;
            $filter_columns[$select_title] = $column->name;
        }
        $record_id_field = Participants_Db::$fields['id'];
        $filter_columns += array(Participants_Db::set_filter('translate_string', $record_id_field->title) => 'id');
        ?>
          <div class="pdb-searchform">
            <form method="post" id="sort_filter_form" action="<?php 
        echo self::prepare_page_link($_SERVER['REQUEST_URI']);
        ?>
" >
              <input type="hidden" name="action" value="admin_list_filter">
              <table class="form-table">
                <tbody><tr><td>
                        <?php 
        for ($i = 0; $i <= $filter_count - 1; $i++) {
            $filter_set = self::get_filter_set($i);
            ?>
                        <fieldset class="widefat inline-controls">
                          <?php 
            if ($i === 0) {
                ?>
                            <legend><?php 
                _e('Show only records with', 'participants-database');
                ?>
:</legend>
                            <?php 
            }
            $element = array('type' => 'dropdown', 'name' => 'search_field[' . $i . ']', 'value' => $filter_set['search_field'], 'options' => array('' => 'none') + $filter_columns);
            PDb_FormElement::print_element($element);
            _ex('that', 'joins two search terms, such as in "Show only records with last name that is Smith"', 'participants-database');
            $element = array('type' => 'dropdown', 'name' => 'operator[' . $i . ']', 'value' => $filter_set['operator'], 'options' => array('null_select' => false, __('is', 'participants-database') => '=', __('is not', 'participants-database') => '!=', __('contains', 'participants-database') => 'LIKE', __('doesn&#39;t contain', 'participants-database') => 'NOT LIKE', __('is greater than', 'participants-database') => 'gt', __('is less than', 'participants-database') => 'lt'));
            PDb_FormElement::print_element($element);
            ?>
                            <input id="participant_search_term_<?php 
            echo $i;
            ?>
" type="text" name="value[<?php 
            echo $i;
            ?>
]" value="<?php 
            echo htmlspecialchars(esc_attr($filter_set['value']));
            ?>
">
                          <?php 
            if ($i < $filter_count - 1) {
                echo '<br />';
                $element = array('type' => 'radio', 'name' => 'logic[' . $i . ']', 'value' => $filter_set['logic'], 'options' => array(__('and', 'participants-database') => 'AND', __('or', 'participants-database') => 'OR'));
            } else {
                $element = array('type' => 'hidden', 'name' => 'logic[' . $i . ']', 'value' => $filter_set['logic']);
            }
            PDb_FormElement::print_element($element);
            ?>

                        </fieldset>
                        <?php 
        }
        ?>
                      <fieldset class="widefat inline-controls">
            <input class="button button-default" name="submit-button" type="submit" value="<?php 
        echo self::$i18n['filter'];
        ?>
">
            <input class="button button-default" name="submit-button" type="submit" value="<?php 
        echo self::$i18n['clear'];
        ?>
">
                      <div class="widefat inline-controls filter-count">
                        <label for="list_filter_count"><?php 
        _e('Number of filters to use: ', 'participants-database');
        ?>
<input id="list_filter_count" name="list_filter_count" class="number-entry single-digit" type="number" max="5" min="1" value="<?php 
        echo $filter_count;
        ?>
"  /></label>
                      </div>
          </fieldset>
                        </td></tr><tr><td>
          <fieldset class="widefat inline-controls">
            <legend><?php 
        _e('Sort by', 'participants-database');
        ?>
:</legend>
            <?php 
        $element = array('type' => 'dropdown', 'name' => 'sortBy', 'value' => self::$filter['sortBy'], 'options' => self::$sortables);
        PDb_FormElement::print_element($element);
        $element = array('type' => 'radio', 'name' => 'ascdesc', 'value' => strtolower(self::$filter['ascdesc']), 'options' => array(__('Ascending', 'participants-database') => 'asc', __('Descending', 'participants-database') => 'desc'));
        PDb_FormElement::print_element($element);
        ?>
            <input class="button button-default"  name="submit-button" type="submit" value="<?php 
        echo self::$i18n['sort'];
        ?>
">
          </fieldset>
                        </td></tr></tbody></table>
        </form>
          </div>

          <h3><?php 
        printf(_n('%s record found, sorted by: %s.', '%s records found, sorted by: %s.', self::$num_records, 'participants-database'), self::$num_records, Participants_Db::column_title(self::$filter['sortBy']));
        ?>
</h3>
            <?php 
    }
    /**
     * top section for admin listing
     */
    private static function _admin_top()
    {
        ?>
    <script type="text/javascript" language="javascript">

      var L10n = {
        "record": "<?php 
        _e("Do you really want to delete the selected record?", 'participants-database');
        ?>
",
        "records": "<?php 
        _e("Do you really want to delete the selected records?", 'participants-database');
        ?>
"
      },
      check_state = false;

      window.onload = function() {
        armDelbutton(false)
      };

      function delete_confirm() {
        var plural = (document.getElementById('select_count').value > 1) ? true : false;
        var x = window.confirm(plural ? L10n.records : L10n.record);
        armDelbutton(x);
        check_state = !x;
        return x;
      }

      function checkedAll() {
        var form = document.getElementById('list_form');
        if (check_state == false) {
          check_state = true
        } else {
          check_state = false;
          armDelbutton(false);
        }
        for (var i = 0; i < form.elements.length; i++) {
          if (form.elements[i].type == 'checkbox' && form.elements[i].name != 'checkall' && form.elements[i].checked != check_state) {
            form.elements[i].checked = check_state;
            addSelects(check_state);
          }
        }
      }

      function addSelects(selected) {
        var count_element = document.getElementById('select_count');
        var count = count_element.value;
        if (selected === true)
          count++;
        else {
          count--;
          document.getElementById('checkall').checked = false;
        }
        if (count < 0)
          count = 0;
        armDelbutton(count > 0);
        count_element.value = count;
      }

      function armDelbutton(state) {
        var delbutton = document.getElementById('delete_button');
        delbutton.setAttribute('class', state ? delbutton.getAttribute('class').replace('unarmed', 'armed') : delbutton.getAttribute('class').replace('armed', 'unarmed'));
        delbutton.disabled = state ? false : true;
      }


      function checkEnter(e) {
        e = e || event;
        return (e.keyCode || event.which || event.charCode || 0) !== 13;
      }

    </script>
    <div  class="wrap participants_db">
    <a id="pdb-list-admin" name="pdb-list-admin"></a>
      <?php 
        Participants_Db::admin_page_heading();
        ?>
    <div id="poststuff">
      <div class="post-body">
          <h2><?php 
        _e('List Participants', 'participants-database');
        ?>
</h2>
          <h3><?php 
        printf(_n('%s record found, sorted by:', '%s records found, sorted by:', self::$num_records, 'participants-database'), "\n" . self::$num_records);
        ?>
 
          <?php 
        echo Participants_Db::column_title(self::$filter['sortBy']);
        ?>
.</h3>
    <?php 
    }
 /**
  * prepares the error messages and CSS for a main database submission
  *
  * @return array indexed array of error messages
  */
 public function get_validation_errors()
 {
     // check for errors
     if (!$this->errors_exist()) {
         return array();
     }
     $output = '';
     $error_messages = array();
     $this->error_CSS = array();
     foreach ($this->errors as $field => $error) {
         if ($field !== '') {
             $field_atts = clone Participants_Db::$fields[$field];
             $this->error_CSS[] = '[class*="' . Participants_Db::$prefix . '"] [name="' . $field_atts->name . '"]';
             if (isset($this->error_messages[$error])) {
                 $error_messages[] = $error == 'nonmatching' ? sprintf($this->error_messages[$error], $field_atts->title, Participants_Db::column_title($field_atts->validation)) : sprintf($this->error_messages[$error], $field_atts->title);
                 $this->error_class = Participants_Db::$prefix . 'error';
             } else {
                 $error_messages[] = $error;
                 $this->error_class = empty($field) ? Participants_Db::$prefix . 'message' : Participants_Db::$prefix . 'error';
             }
         } else {
             $error_messages[] = $error;
             $this->error_class = Participants_Db::$prefix . 'message';
         }
     }
     // $this->errors
     return $error_messages;
 }