function lists()
 {
     global $wpdb;
     // pager
     $pager = new mgm_pager();
     // data
     $data = array();
     // search fields
     $data['search_fields'] = array('' => __('Select', 'mgm'), 'id' => __('ID', 'mgm'), 'title' => __('Title', 'mgm'), 'filename' => __('Filename', 'mgm'), 'post_date' => __('Posted', 'mgm'), 'expire_dt' => __('Expires', 'mgm'));
     // sort fields
     $data['sort_fields'] = array('id' => __('ID', 'mgm'), 'title' => __('Title', 'mgm'), 'post_date' => __('Posted', 'mgm'), 'expire_dt' => __('Expires', 'mgm'));
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     // check
     if (isset($_POST['search_field_name'])) {
         // issue#: 219
         $search_field_name = $_POST['search_field_name'];
         // for sql
         $search_field_value = mgm_escape($_POST['search_field_value']);
         // for sql
         // view data
         $data['search_field_name'] = $_POST['search_field_name'];
         //issue #1281
         $data['search_field_value'] = htmlentities($_POST['search_field_value'], ENT_QUOTES, "UTF-8");
         // for display
         // by field
         switch ($search_field_name) {
             case 'id':
                 $sql_filter = " AND `id` = '" . (int) $search_field_value . "'";
                 break;
             case 'title':
                 $sql_filter = " AND `title` LIKE '%{$search_field_value}%'";
                 break;
             case 'filename':
                 $sql_filter = " AND (`filename` LIKE '%{$search_field_value}%' OR `real_filename` LIKE '%{$search_field_value}%')";
                 break;
             case 'post_date':
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 // set filter
                 $sql_filter = " AND DATE_FORMAT(`post_date`,'%Y-%m-%d') = '{$search_field_value}'";
                 break;
             case 'expire_dt':
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 // set filter
                 $sql_filter = " AND DATE_FORMAT(`expire_dt`,'%Y-%m-%d') = '{$search_field_value}'";
                 break;
         }
     }
     // default
     $sort_field = 'post_date';
     // type
     $sort_type = 'DESC';
     // sort field
     if (isset($_POST['sort_field'])) {
         $sort_field = $_POST['sort_field'];
     }
     // sort type
     if (isset($_POST['sort_type'])) {
         $sort_type = $_POST['sort_type'];
     }
     // set
     $data['sort_field'] = $sort_field;
     // set
     $data['sort_type'] = $sort_type;
     // set
     $sql_order_by = "ORDER BY `{$sort_field}` {$sort_type}";
     // page limit
     $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : 10;
     // 10
     // page no
     $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
     // limit
     $sql_limit = $pager->get_query_limit($data['page_limit']);
     //sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `" . TBL_MGM_DOWNLOAD . "` WHERE 1 {$sql_filter} {$sql_order_by} {$sql_limit}";
     // downloads
     $data['downloads'] = $wpdb->get_results($sql);
     // log
     // mgm_log($wpdb->last_query, __FUNCTION__);
     // page url
     $data['page_url'] = 'admin-ajax.php?action=mgm_admin_ajax_action&page=mgm/admin/downloads&method=lists';
     // get page links
     $data['page_links'] = $pager->get_pager_links($data['page_url']);
     // total pages
     $data['page_count'] = $pager->get_page_count();
     // total rows/results
     $data['row_count'] = $pager->get_row_count();
     // search term
     $search_term = '';
     // search provided
     if (!empty($data['search_field_value'])) {
         $search_term = sprintf('where <b>%s</b> is <b>%s</b>', isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value']);
     }
     // message
     $data['message'] = sprintf(__('%d %s matched %s', 'mgm'), $data['row_count'], $data['row_count'] > 1 ? __('downloads', 'mgm') : __('download', 'mgm'), $search_term);
     // load view
     $this->load->template('downloads/lists', array('data' => $data));
 }
 function payment_history_list()
 {
     global $wpdb;
     // pager
     $pager = new mgm_pager();
     // data
     $data = array();
     // search fields
     $data['search_fields'] = array('' => __('Select', 'mgm'), 'username' => __('Username', 'mgm'), 'id' => __('User ID', 'mgm'), 'email' => __('User Email', 'mgm'), 'first_name' => __('First Name', 'mgm'), 'last_name' => __('Last Name', 'mgm'), 'payment_type' => __('Payment Type', 'mgm'), 'membership_type' => __('Membership Type', 'mgm'));
     // sort fields
     $data['sort_fields'] = array('username' => __('Username', 'mgm'), 'id' => __('User ID', 'mgm'), 'email' => __('User Email', 'mgm'));
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     $payment_type = '';
     // check
     if (isset($_POST['search_field_name'])) {
         // issue#: 219
         $search_field_name = $_POST['search_field_name'];
         // for sql
         $search_field_value = mgm_escape($_POST['search_field_value']);
         // for sql
         $search_field_value = trim($search_field_value);
         // view data
         $data['search_field_name'] = $_POST['search_field_name'];
         //issue #1281
         $data['search_field_value'] = htmlentities($_POST['search_field_value'], ENT_QUOTES, "UTF-8");
         // for display
         // by field
         switch ($search_field_name) {
             case 'username':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND user.user_login LIKE '%{$search_field_value}%'";
                 break;
             case 'id':
                 $sql_filter = " AND user.ID = '" . (int) $search_field_value . "'";
                 break;
             case 'email':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND user.user_email LIKE '%{$search_field_value}%'";
                 break;
             case 'membership_type':
                 // members
                 $members = mgm_get_members_with('membership_type', $search_field_value);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND user.ID IN ({$members_in})";
                 break;
             case 'payment_type':
                 $payment_type = $search_field_value;
                 break;
             case 'first_name':
             case 'last_name':
                 // members
                 $members = mgm_get_members_with($search_field_name, $search_field_value);
                 //check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND user.ID IN ({$members_in})";
                 break;
         }
     }
     // page limit
     $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : 20;
     // 20
     // page no
     $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
     // limit
     $sql_limit = $pager->get_query_limit($data['page_limit']);
     // page url
     $data['page_url'] = 'admin-ajax.php?action=mgm_admin_ajax_action&page=mgm/admin/reports&method=payment_history_list';
     // search term
     $search_term = '';
     // search provided
     if (!empty($data['search_field_value'])) {
         $search_term = sprintf('where <b>%s</b> is <b>%s</b>', isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value']);
     }
     if (!empty($payment_type)) {
         $con = " AND transaction.payment_type =  '" . $payment_type . "' ";
     } else {
         $con = '';
     }
     //payment success check
     $pay_succ = " AND transaction.status_text =  'Last payment was successful'";
     $transaction_sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MGM_TRANSACTION . " transaction LEFT JOIN {$wpdb->users} user ON transaction.user_id = user.ID ";
     $transaction_sql .= "WHERE transaction.module IS NOT NULL";
     $transaction_sql .= $pay_succ;
     $transaction_sql .= $con;
     $transaction_sql .= $sql_filter;
     $transaction_sql .= " ORDER BY transaction.transaction_dt DESC {$sql_limit}";
     $data['transactions'] = $wpdb->get_results($transaction_sql);
     if (!empty($data['transactions'])) {
         // get page links
         $data['page_links'] = $pager->get_pager_links($data['page_url']);
         // total pages
         $data['page_count'] = $pager->get_page_count();
         // total rows/results
         $data['row_count'] = $pager->get_row_count();
         // message
         $data['message'] = sprintf(__('%d %s matched %s', 'mgm'), $data['row_count'], $data['row_count'] > 1 ? 'transactions' : 'transaction', $search_term);
     } else {
         // message
         $data['message'] = sprintf(__(' No transactions matched %s', 'mgm'), $search_term);
     }
     // load template view
     $this->load->template('reports/payment_history/list', array('data' => $data));
 }
 function purchase_lists()
 {
     global $wpdb;
     // pager
     $pager = new mgm_pager();
     // data
     $data = array();
     // search fields
     $data['search_fields'] = array('' => __('Select', 'mgm'), 'user_login' => __('Username', 'mgm'), 'purchase_dt' => __('Purchase Date', 'mgm'), 'addon_option' => __('Addon', 'mgm'));
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     // post
     $search_field_name = mgm_post_var('search_field_name');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         //searc value
         $search_field_value = esc_sql($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = esc_sql($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'user_login':
                 $sql_filter = " AND (`user_login` LIKE '%{$search_field_value}%')";
                 break;
             case 'addon_option':
                 $sql_filter = " AND (`option` LIKE '%{$search_field_value}%')";
                 break;
             case 'purchase_dt':
                 // date start
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date end
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two);
                 // set
                 $sql_filter = " AND (DATE_FORMAT(`purchase_dt`,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}')";
                 break;
         }
     }
     // oredr
     $sql_order = " ORDER BY `user_login`, `purchase_dt`";
     // page limit
     $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : 20;
     // 20
     // page no
     $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
     // limit
     $sql_limit = $pager->get_query_limit($data['page_limit']);
     // sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS A.id, A.purchase_dt, B.option AS addon_option, C.user_login\r\r\n\t\t        FROM `" . TBL_MGM_ADDON_PURCHASES . "` A \r\r\n\t\t\t\tLEFT JOIN " . TBL_MGM_ADDON_OPTION . " B ON (B.id = A.addon_option_id) \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->users . " C ON (C.ID = A.user_id) WHERE 1 \r\r\n\t\t\t\t{$sql_filter} {$sql_order} {$sql_limit}";
     // store
     $data['addon_purchases'] = $wpdb->get_results($sql);
     // page url
     $data['page_url'] = 'admin-ajax.php?action=mgm_admin_ajax_action&page=mgm/admin/addons&method=purchase_lists';
     // get page links
     $data['page_links'] = $pager->get_pager_links($data['page_url']);
     // total pages
     $data['page_count'] = $pager->get_page_count();
     // total rows/results
     $data['row_count'] = $pager->get_row_count();
     // search term
     $search_term = '';
     // search provided
     if (!empty($data['search_field_value'])) {
         // date range
         if (!empty($data['search_field_value_two'])) {
             // set
             $search_term = sprintf(__('where <b>%s</b> between <b>%s</b> and <b>%s</b> dates', 'mgm'), isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value'], $data['search_field_value_two']);
         } else {
             $search_term = sprintf(__('where <b>%s</b> is <b>%s</b>', 'mgm'), isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value']);
         }
     }
     // message
     $data['message'] = sprintf(__('%d %s matched %s', 'mgm'), $data['row_count'], $data['row_count'] > 1 ? 'purchases' : 'purchase', $search_term);
     // load template view
     $this->load->template('addons/purchases/lists', array('data' => $data));
 }
/**
 * generate members list - short code content
 */
function mgm_generate_member_list($args = array())
{
    global $wpdb, $post;
    //issue #1327
    $show_level = isset($args['show_level']) ? $args['show_level'] : null;
    $show_level_members = array();
    if (!empty($show_level) && $show_level != null) {
        $show_level = explode(',', $show_level);
        $show_level_count = count($show_level);
        for ($i = 0; $i < $show_level_count; $i++) {
            $level_match_members = mgm_get_members_with('membership_type', $show_level[$i]);
            if (!empty($level_match_members)) {
                $show_level_members = array_merge($show_level_members, $level_match_members);
            }
        }
        if (!empty($show_level_members)) {
            $show_level_members = array_unique($show_level_members);
        }
    }
    // current url
    $current_url = get_permalink($post->ID);
    // append ? why?
    // if( !strpos($current_url,'?') !== false) $current_url = ($current_url . '?');
    // echo $current_url;
    // init pager
    $pager = new mgm_pager();
    // init data
    $data = $custom_search_fields = $custom_sort_fields = $custom_user_list = $user_list = array();
    // css group
    $css_group = mgm_get_css_group();
    // html
    $html = '';
    // check
    if ($css_group != 'none') {
        // expand this if needed
        $css_link_format = '<link rel="stylesheet" href="%s" type="text/css" media="all" />';
        $css_file = MGM_ASSETS_URL . 'css/' . $css_group . '/mgm.pages.css';
        $html .= sprintf($css_link_format, $css_file);
    }
    // search fields
    $data['search_fields'] = array('' => __('Select', 'mgm'), 'username' => __('Username', 'mgm'), 'id' => __('User ID', 'mgm'), 'email' => __('User Email', 'mgm'), 'first_name' => __('First Name', 'mgm'), 'last_name' => __('Last Name', 'mgm'), 'membership_type' => __('Membership Type', 'mgm'), 'reg_date' => __('Registration Date', 'mgm'), 'last_payment' => __('Last Payment', 'mgm'), 'expire_date' => __('Expiration Date', 'mgm'), 'fee' => __('Fee', 'mgm'), 'status' => __('Status', 'mgm'));
    // sort fields
    $data['sort_fields'] = array('id' => __('User ID', 'mgm'), 'username' => __('Username', 'mgm'), 'email' => __('User Email', 'mgm'), 'reg_date' => __('Registration Date', 'mgm'));
    // order fields
    $data['order_fields'] = array('desc' => __('DESC', 'mgm'), 'asc' => __('ASC', 'mgm'));
    // custom_fields
    $custom_fields = mgm_get_class('member_custom_fields');
    // getting custom fileds and skip the search fields if allready exists.
    foreach ($custom_fields->custom_fields as $custom_field) {
        if (!array_key_exists($custom_field['name'], $data['search_fields'])) {
            $custom_search_fields[$custom_field['name']] = $custom_field['label'];
        }
    }
    // getting custom fileds and skip the sort fields if allready exists.
    foreach ($custom_fields->custom_fields as $custom_field) {
        if (!array_key_exists($custom_field['name'], $data['sort_fields'])) {
            if ($custom_field['name'] != 'status') {
                $custom_sort_fields[$custom_field['name']] = $custom_field['label'];
            }
        }
    }
    // filter
    $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
    // field value
    if (isset($_REQUEST['query'])) {
        $search_field_value = $_REQUEST['query'];
    } else {
        $search_field_value = '';
    }
    // field name
    if (isset($_REQUEST['by'])) {
        $search_field_name = $_REQUEST['by'];
    } else {
        $search_field_name = '';
    }
    // sort field
    if (isset($_REQUEST['sort_field'])) {
        $sort_field_name = $_REQUEST['sort_field'];
    } else {
        $sort_field_name = '';
    }
    // sort order type //order_type
    if (isset($_REQUEST['sort_order'])) {
        $sort_order_type = $_REQUEST['sort_order'];
    } else {
        $sort_order_type = '';
    }
    // members
    $active_members = mgm_get_members_with('status', MGM_STATUS_ACTIVE);
    // wrongly called,use constant
    // check
    if (!empty($search_field_name)) {
        // clean
        $search_field_value = mgm_escape($search_field_value);
        // for sql
        $search_field_name = mgm_escape($search_field_name);
        // for sql
        // view data
        $data['search_field_name'] = $search_field_name;
        $data['search_field_value'] = trim($search_field_value);
        // current date
        $curr_date = mgm_get_current_datetime();
        $current_date = $curr_date['timestamp'];
        // check
        if (array_key_exists($search_field_name, $custom_search_fields)) {
            // members
            $members = mgm_get_members_with_customfiled($search_field_name, $search_field_value);
            //issue #1327
            if (!empty($show_level_members)) {
                $members = array_intersect($show_level_members, $members);
            }
            //check
            $members_in = count($members) == 0 ? 0 : implode(',', $members);
            // set filter
            $sql_filter = " AND `ID` IN ({$members_in})";
        } else {
            // by field
            switch ($search_field_name) {
                case 'username':
                    // issue#: 347(LIKE SEARCH)
                    $filter = " AND `user_login` LIKE '%{$search_field_value}%'";
                    // matched
                    $matched_members = mgm_get_members_with_sql_filter($filter);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'id':
                    // filter
                    $filter = " AND `ID` = '" . (int) $search_field_value . "'";
                    // match
                    $matched_members = mgm_get_members_with_sql_filter($filter);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'email':
                    // issue#: 347(LIKE SEARCH)
                    $filter = " AND `user_email` LIKE '%{$search_field_value}%'";
                    // match
                    $matched_members = mgm_get_members_with_sql_filter($filter);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'membership_type':
                    // match
                    $matched_members = mgm_get_members_with('membership_type', $search_field_value);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'reg_date':
                    // check
                    if (empty($search_field_value)) {
                        $search_field_value = date('Y-m-d', $current_date);
                    }
                    // convert
                    $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                    // set filter
                    $filter = " AND DATE_FORMAT(`user_registered`,'%Y-%m-%d') = '{$search_field_value}'";
                    // match
                    $matched_members = mgm_get_members_with_sql_filter($filter);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'last_payment':
                    // check
                    if (empty($search_field_value)) {
                        $search_field_value = date('Y-m-d', $current_date);
                    }
                    // convert
                    $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                    // match
                    $matched_members = mgm_get_members_with('last_pay_date', $search_field_value);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'expire_date':
                    // check
                    if (empty($search_field_value)) {
                        $search_field_value = date('Y-m-d', $current_date);
                    }
                    // convert
                    $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                    // match
                    $matched_members = mgm_get_members_with('expire_date', $search_field_value);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                case 'fee':
                    // match
                    $matched_members = mgm_get_members_with('amount', $search_field_value);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                    /*				
                    case 'status':
                    	// members
                    	$members    = mgm_get_members_with('status', $search_field_value);
                    	// check
                    	$members_in = (count($members)==0) ? 0 : (implode(',', $members));
                    	// set filter
                    	$sql_filter = " AND `ID` IN ({$members_in})";
                    break;
                    */
                /*				
                case 'status':
                	// members
                	$members    = mgm_get_members_with('status', $search_field_value);
                	// check
                	$members_in = (count($members)==0) ? 0 : (implode(',', $members));
                	// set filter
                	$sql_filter = " AND `ID` IN ({$members_in})";
                break;
                */
                case 'first_name':
                case 'last_name':
                    // members
                    $matched_members = mgm_get_members_with($search_field_name, $search_field_value);
                    //issue #1327
                    if (!empty($show_level_members)) {
                        $matched_members = array_intersect($show_level_members, $matched_members);
                    }
                    // common
                    $members = array_intersect($active_members, $matched_members);
                    // check
                    $members_in = count($members) == 0 ? 0 : implode(',', $members);
                    // set filter
                    $sql_filter = " AND `ID` IN ({$members_in})";
                    break;
            }
        }
    }
    // filters via shortcode args
    $use_field = isset($args['use_field']) ? $args['use_field'] : null;
    $use_filter = isset($args['use_filter']) ? $args['use_filter'] : '';
    $sort_by = isset($args['sort_by']) ? $args['sort_by'] : null;
    $sort_type = isset($args['sort_type']) ? $args['sort_type'] : null;
    // use shortcode field
    if (!empty($use_field)) {
        $use_field = explode(',', $use_field);
    }
    // use shortcode flter
    if (!empty($use_filter)) {
        $use_filter = explode(',', $use_filter);
    }
    // use shortcode sort
    if (!empty($sort_by)) {
        $sort_by = explode(',', $sort_by);
    }
    // use shortcode sort order
    if (!empty($sort_type)) {
        $sort_type = explode(',', $sort_type);
    }
    //setting up the default list fiedls
    if (empty($use_field)) {
        $use_field = array('image', 'first_name', 'last_name', 'email');
    }
    // check length
    $use_field_len = count($use_field);
    //getting user fillter options from short code.
    $arr_filter_search = array();
    if (!empty($use_filter)) {
        $use_filter_len = count($use_filter);
        for ($k = 0; $k < $use_filter_len; $k++) {
            if (array_key_exists($use_filter[$k], $data['search_fields'])) {
                $arr_filter_search[$use_filter[$k]] = $data['search_fields'][$use_filter[$k]];
            } elseif (array_key_exists($use_filter[$k], $custom_search_fields)) {
                $arr_filter_search[$use_filter[$k]] = $custom_search_fields[$use_filter[$k]];
            }
        }
        $data['search_fields'] = $arr_filter_search;
    }
    //getting sort by options from short code.
    $arr_sort_search = array();
    if (!empty($sort_by)) {
        $use_sort_len = count($sort_by);
        for ($k = 0; $k < $use_sort_len; $k++) {
            if (array_key_exists($sort_by[$k], $data['sort_fields'])) {
                $arr_sort_search[$sort_by[$k]] = $data['sort_fields'][$sort_by[$k]];
            } elseif (array_key_exists($sort_by[$k], $custom_sort_fields)) {
                $arr_sort_search[$sort_by[$k]] = $custom_sort_fields[$sort_by[$k]];
            }
        }
        $data['sort_fields'] = $arr_sort_search;
    }
    //getting sort type options from short code.
    $arr_sort_type = array();
    if (!empty($sort_type)) {
        $use_sort_type_len = count($sort_by);
        for ($k = 0; $k < $use_sort_type_len; $k++) {
            if (array_key_exists($sort_type[$k], $data['order_fields'])) {
                $arr_sort_type[$sort_type[$k]] = $data['order_fields'][$sort_type[$k]];
            }
        }
        $data['order_fields'] = $arr_sort_type;
    }
    //issue #1301
    //setting default sort order field as sort fields first value
    if (empty($sort_field_name)) {
        $sort_field_name = array_shift(array_keys($data['sort_fields']));
    }
    //setting default sort order type as sort order fields first value
    if (empty($sort_order_type)) {
        $sort_order_type = array_shift(array_keys($data['order_fields']));
    }
    //setting page limit
    $page_limit = isset($args['page_limit']) ? (int) $args['page_limit'] : 20;
    // page limit
    $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : $page_limit;
    // page no
    $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
    // limit
    $sql_limit = $pager->get_query_limit($data['page_limit']);
    // order
    $sql_order = $data['sort_field'] = $data['sort_type'] = '';
    // sort
    $sort_field_name = mgm_escape($sort_field_name);
    // for sql
    $sort_order_type = mgm_escape($sort_order_type);
    // for sql
    // check
    if (isset($sort_field_name)) {
        // set
        $data['sort_field'] = $sort_field_name;
        $data['sort_type'] = $sort_order_type;
        // init
        $custom_sort = false;
        // check
        if (array_key_exists($sort_field_name, $custom_sort_fields)) {
            $show_level_member = array_intersect($active_members, $show_level_members);
            // members
            $sql_order_by = mgm_userlist_customfield_sort($sort_field_name, $sort_order_type, $sql_filter, $show_level_member);
            // limit
            $lim = str_replace('LIMIT', '', $sql_limit);
            $lim = explode(',', $lim);
            // init
            $temp_array = array();
            // loop
            for ($i = trim($lim[0]); $i < $lim[0] + $lim[1]; $i++) {
                if (!empty($sql_order_by[$i])) {
                    $temp_array[] = $sql_order_by[$i];
                }
            }
            //check for active members
            //$temp_array = array_intersect ($active_members,$temp_array);
            $in_order = count($temp_array) == 0 ? 0 : implode(',', $temp_array);
            // order
            //$in_order = implode(',',$temp_array);
            if (!empty($temp_array)) {
                // set
                $sql_order = " ORDER BY FIELD( ID, {$in_order} ) ";
            } else {
                $sql_order = '';
            }
            // sql
            $sql = "SELECT * FROM `{$wpdb->users}` WHERE ID != 1 AND `ID` IN ({$in_order}) {$sql_order}";
            //  list
            $custom_user_list = $wpdb->get_results($sql);
            // flag
            $custom_sort = true;
        } else {
            // by name
            switch ($sort_field_name) {
                case 'username':
                    $sql_order_by = "user_login";
                    break;
                case 'id':
                    $sql_order_by = "ID";
                    break;
                case 'email':
                    $sql_order_by = "user_email";
                    break;
                case 'membership_type':
                    break;
                case 'reg_date':
                    $sql_order_by = "user_registered";
                    break;
            }
            // set
            if (isset($sql_order_by)) {
                $sql_order = " ORDER BY {$sql_order_by} {$sort_order_type}";
            }
        }
    }
    // default
    if (!isset($sql_order_by)) {
        $sql_order = " ORDER BY ID desc";
    }
    //default active members ids
    if (empty($sql_filter)) {
        //issue #1327
        if (!empty($show_level_members)) {
            // common
            $members = array_intersect($active_members, $show_level_members);
            $members_in = count($members) == 0 ? 0 : implode(',', $members);
        } else {
            $members_in = count($active_members) == 0 ? 0 : implode(',', $active_members);
        }
        // set filter
        $sql_filter = " AND `ID` IN ({$members_in})";
    }
    // get members
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `{$wpdb->users}` WHERE ID != 1 {$sql_filter} {$sql_order} {$sql_limit}";
    // users
    $user_list = $wpdb->get_results($sql);
    // echo $wpdb->last_query;
    // set
    if ($custom_sort) {
        $data['users'] = $custom_user_list;
    } else {
        $data['users'] = $user_list;
    }
    // page url
    $data['page_url'] = add_query_arg(array('query' => $search_field_value, 'by' => $search_field_name, 'sort_field' => $sort_field_name, 'sort_order' => $sort_order_type), $current_url);
    //$url.'query='.$search_field_value.'&by='.$search_field_name.'&sort_field='.$sort_field_name.'&order_type='.$order_type;
    // get page links
    $data['page_links'] = $pager->get_pager_links($data['page_url']);
    // total pages
    $data['page_count'] = $pager->get_page_count();
    // total rows/results
    $data['row_count'] = $pager->get_row_count();
    //sort by filed
    $sort_field_html = sprintf('<select id="sort_field" name="sort_field" class="width100px">%s</select>', mgm_make_combo_options($data['sort_fields'], $data['sort_field'], MGM_KEY_VALUE));
    //order by asc/desc
    $sort_order_html = sprintf('<select id="sort_order" name="sort_order" class="width100px">%s</select>', mgm_make_combo_options($data['order_fields'], $data['sort_type'], MGM_KEY_VALUE));
    //search by
    $search_by_html = sprintf('<select id="by" name="by" class="width100px">%s</select>', mgm_make_combo_options($data['search_fields'], $data['search_field_name'], MGM_KEY_VALUE));
    //search box
    $html = '<div>
				<form method="get" action="' . $current_url . '">
					<h5>' . __('Search Our Members', 'mgm') . ':</h5>
					<input type="text" id="query" name="query" value="' . $data['search_field_value'] . '" /> 
					' . __('in', 'mgm') . ' ' . $search_by_html . ' ' . __('sort by', 'mgm') . ' ' . $sort_field_html . ' ' . $sort_order_html . '					
					<input class="button" type="submit" id="submit" value="' . __('Submit', 'mgm') . '" />
					<input type="hidden" name="search" id="search" value="search" />
				</form>
			</div>';
    $html .= '<div><table><tr>';
    for ($i = 0; $i < $use_field_len; $i++) {
        $html .= sprintf('<th class="th_div mgm_text_align_left mgm_column_%s" id="mgm_column_%s"><label><b>%s</b></label></th>', $use_field[$i], $use_field[$i], ucwords(str_replace('_', ' ', $use_field[$i])));
    }
    $html .= '</tr>';
    $enable_public_profile = mgm_get_class('system')->get_setting('enable_public_profile');
    if (!empty($data['users'])) {
        foreach ($data['users'] as $user) {
            // user object
            $user = get_userdata($user->ID);
            // mgm member object
            $member = mgm_get_member($user->ID);
            $html .= '<tr>';
            for ($i = 0; $i < $use_field_len; $i++) {
                $app_user_filed = 'user_' . $use_field[$i];
                if (isset($member->custom_fields->{$use_field}[$i]) || isset($user->{$use_field}[$i]) || isset($user->{$app_user_filed}) || $use_field[$i] == 'image') {
                    if ($use_field[$i] == 'image') {
                        //is_multisite,network_home_url
                        $html .= sprintf('<td align="left" valign="top" class="mgm_%s_value">', $use_field[$i]);
                        if (bool_from_yn($enable_public_profile)) {
                            //$profile_url = add_query_arg(array('username'=>$user->user_login), site_url('userprofile'));
                            $profile_url = network_site_url() . '/userprofile/?username='******'<a class="tern_wp_member_gravatar" href="%s">%s</a>', $profile_url, get_avatar($user->ID, 60));
                        } else {
                            $html .= sprintf('<a href="javascript://">%s</a>', get_avatar($user->ID, 60));
                        }
                        $html .= '</td>';
                    } else {
                        //getting data from user object
                        if (isset($user->{$use_field}[$i])) {
                            $member->custom_fields->{$use_field}[$i] = $user->{$use_field}[$i];
                        }
                        //getting data from user object
                        if ($use_field[$i] == 'email') {
                            $member->custom_fields->{$use_field}[$i] = $user->{$app_user_filed};
                        }
                        // val unserialize
                        $val = maybe_unserialize($member->custom_fields->{$use_field}[$i]);
                        // array to string
                        if (is_array($val)) {
                            $val = implode(', ', $val);
                        }
                        // set
                        $html .= sprintf('<td align="left" valign="top" class="mgm_%s_value"><b>%s</b></td>', $use_field[$i], $val);
                    }
                }
            }
            $html .= '</tr>';
        }
    } else {
        $html .= '<tr><td colspan="' . $use_field_len . '" align="center">' . __('No members found', 'mgm') . ' ...! </td></tr>';
    }
    $html .= '</table></div><br/>';
    $html .= '<div class="mgm_page_links_div">';
    if ($data['page_links']) {
        $html .= '<div class="pager-wrap">' . $data['page_links'] . '</div><div class="clearfix"></div>';
    }
    $html .= '</div><br/>';
    //issue #1635
    $users_list_html = '<div class="mgm_user_list_container">' . $html . '</div>';
    return $users_list_html;
}
 function member_list()
 {
     global $wpdb;
     // system
     $system_obj = mgm_get_class('system');
     // getting super admin ids - issue#1219
     $super_adminids = mgm_get_super_adminids();
     // pager
     $pager = new mgm_pager();
     // data
     $data = array();
     // search fields
     $data['search_fields'] = array('' => __('Select', 'mgm'), 'username' => __('Username', 'mgm'), 'id' => __('User ID', 'mgm'), 'email' => __('User Email', 'mgm'), 'first_name' => __('First Name', 'mgm'), 'last_name' => __('Last Name', 'mgm'), 'membership_type' => __('Membership Type', 'mgm'), 'reg_date' => __('Registration Date', 'mgm'), 'last_payment' => __('Last Payment', 'mgm'), 'expire_date' => __('Expiration Date', 'mgm'), 'join_date' => __('Join Date', 'mgm'), 'fee' => __('Fee', 'mgm'), 'status' => __('Status', 'mgm'), 'transaction_id' => __('Transaction ID', 'mgm'), 'payment_module' => __('Payment Module', 'mgm'), 'pack_id' => __('Pack ID', 'mgm'));
     // sort fields
     $data['sort_fields'] = array('id' => __('User ID', 'mgm'), 'username' => __('Username', 'mgm'), 'email' => __('User Email', 'mgm'), 'reg_date' => __('Registration Date', 'mgm'), 'last_pay_date' => __('Last Pay Date', 'mgm'));
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     $search_field_name = mgm_post_var('search_field_name');
     //issue #1311
     $sformat = mgm_get_date_format('date_format_short');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         // issue#: 219
         $search_field_value = mgm_escape($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = mgm_escape($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'username':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND `user_login` LIKE '%{$search_field_value}%'";
                 break;
             case 'id':
                 $sql_filter = " AND `ID` = '" . (int) $search_field_value . "'";
                 break;
             case 'email':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND `user_email` LIKE '%{$search_field_value}%'";
                 break;
             case 'membership_type':
                 // members
                 $members = mgm_get_members_with('membership_type', $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'reg_date':
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value, $sformat);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two, $sformat);
                 // set filter
                 // $sql_filter = " AND DATE_FORMAT(user_registered,'%Y-%m-%d') = '{$search_field_value}'";
                 $sql_filter = " AND DATE_FORMAT(user_registered,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}'";
                 // AND transaction_dt BETWEEN  '$start_date' AND  '$end_date'";
                 break;
             case 'last_payment':
                 // date1
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date2
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $date_one = mgm_format_inputdate_to_mysql($search_field_value, $sformat);
                 $date_two = mgm_format_inputdate_to_mysql($search_field_value_two, $sformat);
                 // members
                 $members = mgm_get_members_between_two_dates('last_pay_date', $date_one, $date_two);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // convert
                 // $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 // members
                 // $members = mgm_get_members_with('last_pay_date', $search_field_value);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'expire_date':
                 // date1
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date2
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $date_one = mgm_format_inputdate_to_mysql($search_field_value, $sformat);
                 $date_two = mgm_format_inputdate_to_mysql($search_field_value_two, $sformat);
                 // members
                 $members = mgm_get_members_between_two_dates('expire_date', $date_one, $date_two);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'join_date':
                 // date1
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date2
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $date_one = mgm_format_inputdate_to_mysql($search_field_value, $sformat);
                 $date_two = mgm_format_inputdate_to_mysql($search_field_value_two, $sformat);
                 // members
                 $members = mgm_get_members_between_two_dates('join_date', $date_one, $date_two);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'fee':
                 // members
                 $members = mgm_get_members_with('amount', $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'status':
                 // members
                 $members = mgm_get_members_with('status', $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'first_name':
             case 'last_name':
                 // members
                 $members = mgm_get_members_with($search_field_name, $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 //check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'transaction_id':
                 // members
                 $members = mgm_get_members_with('transaction_id', $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
             case 'payment_module':
                 // members
                 $members = mgm_get_members_with('payment_module', $search_field_value);
                 //super admins check - issue#1219
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 // show nice name
                 $data['search_field_value'] = mgm_get_module($search_field_value)->name;
                 break;
             case 'pack_id':
                 // members
                 $members = mgm_get_members_with('pack_id', $search_field_value);
                 //super admins check
                 $members = array_diff($members, $super_adminids);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND `ID` IN ({$members_in})";
                 break;
         }
     }
     //super admins check - issue#1219
     $super_admin_in = count($super_adminids) == 0 ? 0 : implode(',', $super_adminids);
     // page limit
     $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : 20;
     // page no
     $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
     // limit
     $sql_limit = $pager->get_query_limit($data['page_limit']);
     //init
     $custom_user_list = array();
     $member_custom_sort = false;
     // order
     $sql_order = $data['sort_field'] = $data['sort_type'] = '';
     // sort
     $sort_field_name = mgm_post_var('sort_field_name');
     $sort_type = mgm_post_var('sort_type');
     // check
     if (isset($sort_field_name)) {
         //issue#: 219
         $data['sort_field'] = $sort_field_name;
         $data['sort_type'] = $sort_type;
         // by name
         switch ($sort_field_name) {
             case 'username':
                 $sql_order_by = "user_login";
                 break;
             case 'id':
                 $sql_order_by = "ID";
                 break;
             case 'email':
                 $sql_order_by = "user_email";
                 break;
             case 'membership_type':
                 break;
             case 'reg_date':
                 $sql_order_by = "user_registered";
                 break;
             case 'last_pay_date':
                 $sql_order_by = mgm_member_sort($sort_field_name, $sort_type, $sql_filter, $super_adminids);
                 //mgm_log(mgm_array_dump($sql_order_by,true),__FUNCTION__);
                 // limit
                 $lim = str_replace('LIMIT', '', $sql_limit);
                 $lim = explode(',', $lim);
                 // init
                 $temp_array = array();
                 // loop
                 for ($i = trim($lim[0]); $i < $lim[0] + $lim[1]; $i++) {
                     if (!empty($sql_order_by[$i])) {
                         $temp_array[] = $sql_order_by[$i];
                     }
                 }
                 $in_order = count($temp_array) == 0 ? 0 : implode(',', $temp_array);
                 // order
                 if (!empty($temp_array)) {
                     // set
                     $sql_order = " ORDER BY FIELD( ID, {$in_order} ) ";
                 } else {
                     $sql_order = '';
                 }
                 // sql
                 $sql = "SELECT * FROM `{$wpdb->users}` WHERE ID != 1 AND `ID` IN ({$in_order}) {$sql_order}";
                 //mgm_log($sql,__FUNCTION__);
                 //  list
                 $custom_user_list = $wpdb->get_results($sql);
                 unset($sql_order_by);
                 // flag
                 $member_custom_sort = true;
                 break;
         }
         // set
         if (isset($sql_order_by)) {
             $sql_order = " ORDER BY {$sql_order_by} {$sort_type}";
         }
     }
     // issue #1119
     if (!isset($sql_order_by)) {
         $sql_order = " ORDER BY ID desc";
     }
     // get members
     $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `{$wpdb->users}` WHERE ID NOT IN ({$super_admin_in}) {$sql_filter} {$sql_order} {$sql_limit}";
     //list
     $user_list = $wpdb->get_results($sql);
     // users
     if ($member_custom_sort) {
         // users
         $data['users'] = $custom_user_list;
     } else {
         // users
         $data['users'] = $user_list;
     }
     // page url
     $data['page_url'] = 'admin-ajax.php?action=mgm_admin_ajax_action&page=mgm/admin/members&method=member_list';
     // get page links
     $data['page_links'] = $pager->get_pager_links($data['page_url']);
     // total pages
     $data['page_count'] = $pager->get_page_count();
     // total rows/results
     $data['row_count'] = $pager->get_row_count();
     // search term
     $search_term = '';
     // search provided
     if (!empty($data['search_field_value'])) {
         if (!empty($data['search_field_value_two'])) {
             $search_term = sprintf('where <b>%s</b> between <b>%s</b> and <b>%s</b> dates', isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value'], $data['search_field_value_two']);
         } else {
             $search_term = sprintf('where <b>%s</b> is <b>%s</b>', isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value']);
         }
     }
     // message
     $data['message'] = sprintf(__('%d %s matched %s', 'mgm'), $data['row_count'], $data['row_count'] > 1 ? 'members' : 'member', $search_term);
     // modules
     $data['payment_modules'] = $system_obj->get_active_modules('payment');
     // load template view
     $this->load->template('members/member/lists', array('data' => $data));
 }
 function post_purchase_lists()
 {
     global $wpdb;
     // pager
     $pager = new mgm_pager();
     // data
     $data = array();
     // search fields
     $data['search_fields'] = array('' => __('Select', 'mgm'), 'user_login' => __('Username/Guest', 'mgm'), 'is_gift' => __('Type', 'mgm'), 'purchase_dt' => __('Purchase/Gift Date', 'mgm'), 'post_title' => __('Post', 'mgm'));
     // sort fields
     $data['sort_fields'] = array('purchase_dt' => __('Purchase date', 'mgm'), 'post_title' => __('Post title', 'mgm'), 'user_login' => __('User login', 'mgm'));
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     // post
     $search_field_name = mgm_post_var('search_field_name');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         //searc value
         $search_field_value = esc_sql($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = esc_sql($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'user_login':
                 $sql_filter = " AND (`user_login` LIKE '%{$search_field_value}%' OR `guest_token` LIKE '%{$search_field_value}%')";
                 break;
             case 'post_title':
                 $sql_filter = " AND (`post_title` LIKE '%{$search_field_value}%')";
                 break;
             case 'is_gift':
                 $sql_filter = " AND (`is_gift` = '{$search_field_value}')";
                 break;
             case 'purchase_dt':
                 // date start
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date end
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two);
                 // set
                 $sql_filter = " AND (DATE_FORMAT(`pp`.`purchase_dt`,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}')";
                 break;
         }
     }
     // order
     $sql_order = $data['sort_field'] = $data['sort_type'] = '';
     // sort
     $sort_field_name = mgm_post_var('sort_field_name');
     $sort_type = mgm_post_var('sort_type');
     // check
     if (isset($sort_field_name)) {
         //issue#: 219
         $data['sort_field'] = $sort_field_name;
         $data['sort_type'] = $sort_type;
         // by name
         switch ($sort_field_name) {
             case 'user_login':
                 $sql_order_by = "u.user_login";
                 break;
             case 'post_title':
                 $sql_order_by = "p.post_title";
                 break;
             case 'purchase_dt':
                 $sql_order_by = "pp.purchase_dt";
                 break;
         }
         // set
         if (isset($sql_order_by)) {
             $sql_order = " ORDER BY {$sql_order_by} {$sort_type}";
         }
     }
     // default order
     if (!isset($sql_order_by)) {
         $sql_order = " ORDER BY `pp`.`purchase_dt` DESC";
     }
     // page limit
     $data['page_limit'] = isset($_REQUEST['page_limit']) ? (int) $_REQUEST['page_limit'] : 20;
     // 20
     // page no
     $data['page_no'] = isset($_REQUEST['page_no']) ? (int) $_REQUEST['page_no'] : 1;
     // limit
     $sql_limit = $pager->get_query_limit($data['page_limit']);
     // sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS p.ID AS post_id, p.post_title, pp.purchase_dt, \r\r\n\t\t        IF(user_id IS NULL, CONCAT('guest-', guest_token), u.user_login) AS user_login, \r\r\n\t\t\t\tpp.id, pp.is_gift,pp.is_expire FROM `" . TBL_MGM_POST_PURCHASES . "` pp \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->posts . " p ON (p.id = pp.post_id) \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->users . " u ON (u.ID = pp.user_id) WHERE 1 \r\r\n\t\t\t\t{$sql_filter} {$sql_order} {$sql_limit}";
     // store
     $data['post_purchases'] = $wpdb->get_results($sql);
     // page url
     $data['page_url'] = 'admin-ajax.php?action=mgm_admin_ajax_action&page=mgm/admin/payperpost&method=post_purchase_lists';
     // get page links
     $data['page_links'] = $pager->get_pager_links($data['page_url']);
     // total pages
     $data['page_count'] = $pager->get_page_count();
     // total rows/results
     $data['row_count'] = $pager->get_row_count();
     // search term
     $search_term = '';
     // search provided
     if (!empty($data['search_field_value'])) {
         // date range
         if (!empty($data['search_field_value_two'])) {
             // set
             $search_term = sprintf(__('where <b>%s</b> between <b>%s</b> and <b>%s</b> dates', 'mgm'), isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value'], $data['search_field_value_two']);
         } else {
             $search_term = sprintf(__('where <b>%s</b> is <b>%s</b>', 'mgm'), isset($data['search_fields'][$search_field_name]) ? $data['search_fields'][$search_field_name] : '', $data['search_field_value']);
         }
     }
     // message
     $data['message'] = sprintf(__('%d %s matched %s', 'mgm'), $data['row_count'], $data['row_count'] > 1 ? 'purchases' : 'purchase', $search_term);
     // load template view
     $this->load->template('payperpost/post_purchases/lists', array('data' => $data));
 }