Esempio n. 1
0
 public function _sync()
 {
     // check
     if ($class_vars = get_object_vars($this)) {
         // has options
         if (is_array($this->options)) {
             // loop
             foreach ($this->options as $option_name => $option_value) {
                 // check
                 if (in_array($option_name, $class_vars)) {
                     // array, needs merge
                     if (is_array($this->{$option_name})) {
                         // callback, to extend merging scenario for some optionswhich needs to overwrite default option vars
                         if (method_exists($this, '_option_merge_callback')) {
                             // invoke, name, current value, new value
                             $this->_option_merge_callback($option_name, $this->{$option_name}, $option_value);
                         } else {
                             // default, merge current value and new value
                             $this->{$option_name} = mgm_array_merge_recursive_unique($this->{$option_name}, $option_value);
                         }
                     } elseif (is_object($this->{$option_name})) {
                         // object, convert to array and merge. @TODO, needs test
                         $this->{$option_name} = (object) array_merge((array) mgm_object2array($this->{$option_name}), (array) $option_value);
                     } else {
                         // string, current value reset with new value
                         // set
                         $this->{$option_name} = $option_value;
                     }
                 }
             }
             // return
             return $this->options;
         }
     }
     // return
     return false;
 }
/**
 * create purchase another button
 *
 * @param array userdata
 * @retun string html output
 */
function mgm_get_purchase_another_subscription_button($args = array())
{
    global $wpdb;
    //ceck settings
    $settings = mgm_get_class('system')->get_setting();
    // check
    if (!isset($settings['enable_multiple_level_purchase']) || isset($settings['enable_multiple_level_purchase']) && !bool_from_yn($settings['enable_multiple_level_purchase'])) {
        return;
    }
    // current user
    $user = wp_get_current_user();
    // validate
    if (!$user->ID) {
        // query string
        $user = mgm_get_user_from_querystring();
    }
    // validate
    if (!$user->ID) {
        return __('No such user', 'mgm');
    }
    // userdata
    $username = $user->user_login;
    $mgm_home = get_option('siteurl');
    $member = mgm_get_member($user->ID);
    $system_obj = mgm_get_class('system');
    $membership_type = mgm_get_user_membership_type($user->ID, 'code');
    $packs_obj = mgm_get_class('subscription_packs');
    $packs = $packs_obj->get_packs('upgrade');
    $duration_str = $packs_obj->duration_str;
    $trial_taken = $member->trial_taken;
    // pack_ids
    $pack_ids = mgm_get_members_packids($member);
    $pack_membership_types = mgm_get_subscribed_membershiptypes($user->ID, $member);
    // query_arg
    $form_action = mgm_get_custom_url('transactions', false, array('action' => 'purchase_another', 'username' => $username));
    //issue 1009
    if (isset($settings['membership_details_url'])) {
        $membership_details_url = $settings['membership_details_url'];
    } else {
        $membership_details_url = get_admin_url() . 'profile.php?page=mgm/profile';
    }
    // cancel
    $cancel_url = $membership_details_url;
    // $cancel_url = mgm_get_custom_url('membership_details');
    // active modules
    $a_payment_modules = $system_obj->get_active_modules('payment');
    // 	selected_subscription
    $selected_subs = mgm_get_selected_subscription($args);
    // second step, after post
    if (isset($_POST['submit'])) {
        // verify selected
        if (!isset($_POST['subs_opt'])) {
            // die
            return sprintf(__('Package not selected, <a href="%s">go back</a>.', 'mgm'), $_POST['form_action']);
            exit;
        }
        // get subs data
        $subs_opt_pack = mgm_decode_package($_POST['subs_opt']);
        extract($subs_opt_pack);
        // check
        $valid = false;
        // loop packs
        foreach ($packs as $pack) {
            // check
            //check pack id as well: issue#: 580
            if ($pack['cost'] == $cost && $pack['duration'] == $duration && $pack['duration_type'] == $duration_type && $membership_type == $pack['membership_type'] && $pack_id == $pack['id']) {
                $valid = true;
                break;
            }
        }
        // error
        if (!$valid) {
            return __('Invalid data passed', 'mgm');
            exit;
        }
        // get object
        $member = new mgm_member($user->ID);
        $temp_membership = $member->_default_fields();
        $temp_membership['membership_type'] = $membership_type;
        $temp_membership['pack_id'] = $pack_id;
        //issue #860
        //if (isset($_POST['mgm_upgrade_field']['autoresponder']) && ($_POST['mgm_upgrade_field']['autoresponder'])=='Y') {
        if (isset($_POST['mgm_upgrade_field']['autoresponder']) && substr($_POST['mgm_upgrade_field']['autoresponder'], 0, 1) == 'Y') {
            $temp_membership['subscribed'] = 'Y';
            $temp_membership['autoresponder'] = $system_obj->active_modules['autoresponder'];
        }
        //issue #1236
        if (isset($_POST['mgm_upgrade_field']['coupon']) && !empty($_POST['mgm_upgrade_field']['coupon'])) {
            //issue #1250 - Coupon validation
            if (!empty($_POST['form_action'])) {
                // check if its a valid coupon
                if (!($coupon = mgm_get_coupon_data($_POST['mgm_upgrade_field']['coupon']))) {
                    //redirect back to the form
                    $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_upgrade_field']['coupon']);
                    $redirect = add_query_arg($q_arg, $_POST['form_action']);
                    // redirect
                    mgm_redirect($redirect);
                    exit;
                } else {
                    // membership type check
                    // get subs
                    if ($subs_pack = mgm_decode_package(mgm_post_var('subs_opt'))) {
                        // values
                        $coupon_values = mgm_get_coupon_values(NULL, $coupon['value'], true);
                        // check
                        if (isset($coupon_values['new_membership_type']) && $coupon_values['new_membership_type'] != $subs_pack['membership_type']) {
                            $new_membership_type = mgm_get_membership_type_name($coupon_values['new_membership_type']);
                            $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'membership_type' => $coupon_values['new_membership_type'], 'error_field_value' => $_POST['mgm_upgrade_field']['coupon']);
                            $redirect = add_query_arg($q_arg, $_POST['form_action']);
                            // redirect
                            mgm_redirect($redirect);
                            exit;
                        }
                    }
                }
            }
        }
        //inserted an incomplete entry for the selected subscription type
        mgm_save_another_membership_fields($temp_membership, $user->ID);
        // save coupon fields and update member object
        $member = mgm_save_partial_fields_purchase_more($user->ID, $membership_type, $cost);
        // coupon
        $purchase_another_coupon = false;
        // array
        if (isset($member->upgrade)) {
            if (is_array($member->upgrade) && isset($member->upgrade['coupon']['id'])) {
                $purchase_another_coupon = $member->upgrade['coupon'];
            } elseif (is_object($member->upgrade) && isset($member->upgrade->coupon->id)) {
                $purchase_another_coupon = mgm_object2array($member->upgrade->coupon);
            }
            // coupon
            mgm_get_purchase_another_coupon_pack($purchase_another_coupon, $pack);
        }
        //save custom fields -issue #1285
        if (isset($_POST['mgm_upgrade_field']) && !empty($_POST['mgm_upgrade_field'])) {
            //member
            $cf_member = mgm_get_member($user->ID);
            //upgrade custom fileds
            $cfu_fileds = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_multiple_membership_level_purchase' => true)));
            //loop fields
            foreach ($cfu_fileds as $cf_filed) {
                //skip coupon and autoresponder
                if (in_array($cf_filed['name'], array('coupon', 'autoresponder'))) {
                    continue;
                }
                //check
                if (isset($_POST['mgm_upgrade_field'][$cf_filed['name']])) {
                    // check upgrae and required
                    if ((bool) $cf_filed['attributes']['required'] === true) {
                        if (empty($_POST['mgm_upgrade_field'][$cf_filed['name']])) {
                            //redirect back to the form
                            $q_arg = array('error_field' => $cf_filed['label'], 'error_type' => 'empty', 'error_field_value' => $_POST['mgm_upgrade_field'][$cf_filed['name']]);
                            $redirect = add_query_arg($q_arg, $_POST['form_action']);
                            mgm_redirect($redirect);
                            exit;
                        }
                    }
                    //appending custom fields
                    if (isset($cf_member->custom_fields->{$cf_filed}['name'])) {
                        $cf_member->custom_fields->{$cf_filed}['name'] = $_POST['mgm_upgrade_field'][$cf_filed['name']];
                    } else {
                        $cf_member->custom_fields->{$cf_filed}['name'] = $_POST['mgm_upgrade_field'][$cf_filed['name']];
                    }
                }
            }
            $cf_member->save();
        }
        // start html
        $html = '<div>';
        // free
        if (($pack['cost'] == 0 || $membership_type == 'free') && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->is_enabled()) {
            $html .= sprintf('<div>%s - %s</div>', __('Create a free account ', 'mgm'), ucwords($membership_type));
            $module = 'mgm_free';
            // payments url
            $payments_url = mgm_get_custom_url('transactions');
            // if tril module selected and cost is 0 and free moduleis not active
            if ($membership_type == 'trial') {
                if (in_array('mgm_trial', $a_payment_modules)) {
                    $module = 'mgm_trial';
                }
            }
            //Purchase Another Membership Level problem : issue #: 752
            $redirect = add_query_arg(array('method' => 'payment_return', 'module' => $module, 'custom' => $user->ID . '_' . $duration . '_' . $duration_type . '_' . $pack_id . '_Y', 'redirector' => $redirector), $payments_url);
            // redirect
            if (!headers_sent()) {
                @header('location: ' . $redirect);
                exit;
            }
            // js redirect
            $html .= sprintf('<script type="text/javascript">window.location = "%s";</script><div>%s</div>', $redirect, $packs_obj->get_pack_desc($pack));
        } else {
            $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', $packs_obj->get_pack_desc($pack));
            // coupon
            if (isset($purchase_another_coupon['id'])) {
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', sprintf(__('Using Coupon "%s" - %s', 'mgm'), $purchase_another_coupon['name'], $purchase_another_coupon['description']));
            }
            $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', __('Please Select from Available Payment Gateways', 'mgm'));
        }
        //bypass if payment gateway field is selected -issue #1764
        if ((double) $pack['cost'] > 0 && isset($_POST['mgm_payment_gateways']) && !empty($_POST['mgm_payment_gateways'])) {
            //init
            $tran_id = 0;
            if (!$tran_id) {
                $tran_id = mgm_add_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
            }
            // module
            $mod_obj = mgm_get_module($_POST['mgm_payment_gateways'], 'payment');
            // module end point
            $redirect = $mod_obj->_get_endpoint('html_redirect', false);
            // encode id:
            //encode transaction id
            $encode_tran_id = mgm_encode_id($tran_id);
            //args
            $redirect = add_query_arg(array('tran_id' => $encode_tran_id), $redirect);
            // do the redirect to payment
            mgm_redirect($redirect);
        }
        // init
        $payment_modules = array();
        // when active
        if ($a_payment_modules) {
            // loop
            foreach ($a_payment_modules as $payment_module) {
                // not trial
                if (in_array($payment_module, array('mgm_free', 'mgm_trial'))) {
                    continue;
                }
                //consider only the modules assigned to pack
                if (isset($pack['modules']) && !in_array($payment_module, (array) $pack['modules'])) {
                    continue;
                }
                // store
                $payment_modules[] = $payment_module;
            }
        }
        // loop payment mods if not free
        if (count($payment_modules) && $cost) {
            // transaction
            $tran_id = 0;
            // loop
            foreach ($payment_modules as $module) {
                // module
                $mod_obj = mgm_get_module($module, 'payment');
                // create transaction
                // if(!$tran_id) $tran_id = $mod_obj->_create_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
                if (!$tran_id) {
                    $tran_id = mgm_add_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
                }
                // button
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', $mod_obj->get_button_subscribe(array('pack' => $pack, 'tran_id' => $tran_id)));
            }
        } else {
            if ($cost) {
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', __('There are no payment gateways available at this time.', 'mgm'));
            }
        }
        // html
        $html .= '</div>';
    } else {
        // first step show upgrade options
        // html
        $html = sprintf('<p class="message register">%s</p>', __('Please Select from Available Membership Packages', 'mgm'));
        // upgrade_packages
        $upgrade_packages = '';
        // pack to modules
        $pack_modules = array();
        // loop
        foreach ($packs as $pack) {
            // default
            $checked = '';
            // skip already purchased packs
            if (in_array($pack['id'], $pack_ids)) {
                continue;
            }
            //skip same membership level subscriptions
            if (in_array($pack['membership_type'], $pack_membership_types)) {
                continue;
            }
            // do not show trial or free as upgradre
            if ($pack['membership_type'] == 'trial' || $pack['membership_type'] == 'free') {
                continue;
            }
            // reset
            $checked = mgm_select_subscription($pack, $selected_subs);
            // skip other when a package sent as selected
            if ($selected_subs !== false) {
                if (empty($checked)) {
                    continue;
                }
            }
            // checked
            if (!$checked) {
                $checked = (int) $pack['default'] == 1 ? ' checked="checked"' : '';
            }
            // duration
            if ($pack['duration'] == 1) {
                $dur_str = rtrim($duration_str[$pack['duration_type']], 's');
            } else {
                $dur_str = $duration_str[$pack['duration_type']];
            }
            $css_group = mgm_get_css_group();
            // encode pack
            $subs_opt_enc = mgm_encode_package($pack);
            // set
            $pack_modules[$subs_opt_enc] = $pack['modules'];
            //issue #867
            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.form.fields.css';
                $upgrade_packages .= sprintf($css_link_format, $css_file);
            }
            // free
            if (($pack['cost'] == 0 || strtolower($pack['membership_type']) == 'free') && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->enabled == 'Y') {
                // input
                $input = sprintf('<input type="radio" %s class="checkbox" name="subs_opt" value="%s" rel="mgm_subscription_options"/>', $checked, $subs_opt_enc);
                // html
                $upgrade_packages .= '  
							<div class="mgm_subs_wrapper ' . $pack['membership_type'] . '">
								<div class="mgm_subs_option ' . $pack['membership_type'] . '">
									' . $input . '
								</div>
								<div class="mgm_subs_pack_desc ' . $pack['membership_type'] . '">
									' . $packs_obj->get_pack_desc($pack) . '
								</div>
								 <div class="clearfix"></div>
								 <div class="mgm_subs_desc ' . $pack['membership_type'] . '">
									' . mgm_stripslashes_deep($pack['description']) . '
								 </div>
							</div>';
            } else {
                // input
                $input = sprintf('<input type="radio" %s class="checkbox" name="subs_opt" value="%s" rel="mgm_subscription_options"/>', $checked, $subs_opt_enc);
                // html
                $upgrade_packages .= '  
							<div class="mgm_subs_wrapper ' . $pack['membership_type'] . '">
								<div class="mgm_subs_option ' . $pack['membership_type'] . '">
									' . $input . '
								</div>
								<div class="mgm_subs_pack_desc ' . $pack['membership_type'] . '">
									' . $packs_obj->get_pack_desc($pack) . '
								</div>
								 <div class="clearfix"></div>
								 <div class="mgm_subs_desc ' . $pack['membership_type'] . '">
									' . mgm_stripslashes_deep($pack['description']) . '
								 </div>
							</div>';
            }
        }
        // add pack_modules as json data, may consider jquery data later
        if (!empty($pack_modules)) {
            $html .= sprintf('<script language="javascript">var mgm_pack_modules = %s</script>', json_encode($pack_modules));
        }
        // show error
        if (!$upgrade_packages) {
            // html
            $html .= '<div class="mgm_subs_wrapper">
						<div class="mgm_subs_pack_desc">
							' . __('Sorry, no packages available.', 'mgm') . '
						</div>
					 </div>
					 <p>						
						<input type="button" name="cancel" onclick="window.location=\'' . $cancel_url . '\'" value="' . __('Cancel', 'mgm') . '" class="button-primary" />&nbsp;					
					 </p>';
        } else {
            /*			$error_field = mgm_request_var('error_field'); 
            			if(!empty($error_field)) {
            				$errors = new WP_Error();
            				switch (mgm_request_var('error_type')) {
            					case 'empty':
            						$error_string = 'You must provide a ';
            						break;
            					case 'invalid':
            						$error_string = 'Invalid ';
            						break;	
            				}				
            				//issue #703
            				$errors->add( $error_field, __( '<strong>ERROR</strong>: '.$error_string, 'mgm' ).$error_field );
            				$html .= mgm_set_errors($errors, true);					
            			}*/
            // check errors if any:
            $html .= mgm_subscription_purchase_errors();
            // form
            $html .= '<form action="' . $form_action . '" method="post" class="mgm_form"><div class="mgm_get_pack_form_container">';
            $html .= '<input type="hidden" name="form_action" value="' . $form_action . '" />';
            $html .= $upgrade_packages;
            //issue #1285
            $html .= mgm_get_custom_fields($user->ID, array('on_multiple_membership_level_purchase' => true), 'mgm_upgrade_field');
            // html
            $html .= '<input type="hidden" name="ref" value="' . md5($member->amount . '_' . $member->duration . '_' . $member->duration_type . '_' . $member->membership_type) . '" />';
            // set
            $html .= '<p>						
						<input class="button" type="submit" name="submit" value="' . __('Next', 'mgm') . '" />&nbsp;&nbsp;
						<input class="button" type="button" name="cancel" onclick="window.location=\'' . $cancel_url . '\'" value="' . __('Cancel', 'mgm') . '"/>&nbsp;					
					  </p>';
            // html
            $html .= '</div></form>';
        }
    }
    // return
    return $html;
}
/**
 * create csv file from given data
 */
function mgm_create_csv_file($rowset, $name = 'export')
{
    // file name
    $filename = sprintf('%s_%s.csv', $name, date('mdYHis'));
    // create
    if ($fp = fopen(MGM_FILES_EXPORT_DIR . $filename, 'w')) {
        // columns
        $columns = array();
        // loop data
        foreach ($rowset as $row) {
            // values
            $values = array();
            // create arry
            $data = mgm_object2array($row);
            // header
            if (empty($columns)) {
                // get colums
                $labels = array_keys($data);
                // loop
                foreach ($labels as $label) {
                    // trim
                    $label = trim($label);
                    // store
                    $columns[] = $label;
                }
                // put
                fputcsv($fp, $columns, ",");
            }
            // reset col
            $col = 0;
            // loop cols
            foreach ($columns as $column) {
                // value
                $value = isset($data[$column]) && !empty($data[$column]) ? $data[$column] : 'n/a';
                // array #783
                if (is_array($value)) {
                    $value = implode(' ', $value);
                }
                // limit string length: issue#: 492
                if (strlen($value) > 1024) {
                    $value = substr($value, 0, 1020) . '...';
                }
                // trim special char
                $value = str_replace(array(",", "\r\n", "\n", "\r", "\t"), ' ', $value);
                // values
                $values[] = $value;
            }
            // put
            fputcsv($fp, $values, ",");
        }
        // close
        fclose($fp);
        // mod
        @chmod(MGM_FILES_EXPORT_DIR . $filename, 0777);
        // return
        return $filename;
    }
    // return
    return false;
}
    function member_export()
    {
        global $wpdb;
        // error -- use WP_DEBUG with WP_DEBUG_LOG
        // if(!WP_DEBUG) error_reporting(0);
        // extract
        extract($_POST);
        // log
        // mgm_log($_POST, __FUNCTION__);
        // get format
        $sformat = mgm_get_date_format('date_format_short');
        // process
        if (isset($export_member_info)) {
            // init
            $success = 0;
            // type
            $membership_type = isset($bk_membership_type) ? $bk_membership_type : 'all';
            // status
            $membership_status = isset($bk_membership_status) ? $bk_membership_status : 'all';
            // date
            $date_start = isset($bk_date_start) ? $bk_date_start : '';
            $date_end = isset($bk_date_end) ? $bk_date_end : '';
            // query inut
            $query = '';
            // selected only
            if (isset($bk_only_selected)) {
                // check
                if (isset($bk_selected_members) && is_array($bk_selected_members)) {
                    $query = " AND `id` IN(" . mgm_map_for_in($bk_selected_members) . ")";
                }
            }
            // start date
            if ($date_start) {
                // Issue #700
                // convert to mysql date
                $date_start = strtotime(mgm_format_inputdate_to_mysql($date_start, $sformat));
                // end date
                if ($date_end) {
                    // Issue #700
                    // convert to mysql date
                    $date_end = mgm_format_inputdate_to_mysql($date_end, $sformat);
                    $date_end = strtotime($date_end);
                    // issue#" 492
                    $query .= " AND UNIX_TIMESTAMP(user_registered) >= '{$date_start}' \r\r\n\t\t\t\t\t            AND UNIX_TIMESTAMP(DATE_FORMAT(user_registered, '%Y-%m-%d')) <= '{$date_end}'";
                } else {
                    $query .= " AND UNIX_TIMESTAMP(user_registered) >= '{$date_start}'";
                }
            } else {
                if ($date_end) {
                    // Issue #700
                    // convert to mysql date
                    $date_end = strtotime(mgm_format_inputdate_to_mysql($date_end, $sformat));
                    // query
                    $query .= " AND UNIX_TIMESTAMP(DATE_FORMAT(user_registered, '%Y-%m-%d')) <= '{$date_end}' ";
                }
            }
            // all users
            $sql = 'SELECT ID, user_login, user_email, user_registered, display_name FROM `' . $wpdb->users . '` 
			        WHERE ID <> 1 ' . $query . ' ORDER BY `user_registered` ASC';
            // users
            $users = $wpdb->get_results($sql);
            // filter
            $export_users = array();
            // date
            $current_date = time();
            //issue #844
            $skip_fileds = array('subscription_introduction', 'coupon', 'privacy_policy', 'payment_gateways', 'terms_conditions', 'subscription_options', 'autoresponder', 'captcha');
            // check - issue #1382
            if (isset($bk_users_to_import)) {
                $custom_fields = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true, 'on_profile' => true)));
                $import_user_fileds = array('user_login', 'user_email', 'pack_id', 'membership_type');
                foreach ($custom_fields as $field) {
                    if (!in_array($field['name'], $skip_fileds)) {
                        $import_user_fileds[] = $field['name'];
                    }
                }
            }
            // Custom fields
            $cf_profile_pg = mgm_get_class('member_custom_fields');
            $to_unserialize = array();
            foreach (array_unique($cf_profile_pg->sort_orders) as $id) {
                foreach ($cf_profile_pg->custom_fields as $field) {
                    // issue #954: show the field only if it is enabled for profile page
                    if ($field['id'] == $id && $field['type'] == 'checkbox') {
                        $to_unserialize[] = $field['name'];
                    }
                }
            }
            // loop
            foreach ($users as $user) {
                // user cloned
                $user_obj = clone $user;
                // member
                $member = mgm_get_member($user->ID);
                // check
                if (!isset($bk_inactive)) {
                    $bk_inactive = false;
                }
                // check search parameters:
                if ($this->_get_membership_details($member, $bk_msexp_dur_unit, $bk_msexp_dur, $membership_type, $current_date, $bk_inactive, $membership_status)) {
                    // merge
                    if (method_exists($member, 'merge_fields')) {
                        $user = $member->merge_fields($user);
                    }
                    // log
                    // mgm_log($user, __FUNCTION__);
                    // issue #844
                    foreach ($skip_fileds as $skip_filed) {
                        unset($user->{$skip_filed});
                    }
                    // format dates
                    $user->user_registered = date($sformat, strtotime($user->user_registered));
                    $user->last_pay_date = (int) $user->last_pay_date > 0 ? date($sformat, strtotime($user->last_pay_date)) : 'N/A';
                    $user->expire_date = !empty($user->expire_date) ? date($sformat, strtotime($user->expire_date)) : 'N/A';
                    $user->join_date = (int) $user->join_date > 0 ? date($sformat, $user->join_date) : 'N/A';
                    // issue#: 672
                    // DO not show actual password: #1002
                    // $user->user_password = mgm_decrypt_password($member->user_password, $user->ID);
                    $user->rss_token = $member->rss_token;
                    // unset password
                    unset($user->password, $user->password_conf);
                    // unserialize checkbox values
                    if (count($to_unserialize)) {
                        foreach ($to_unserialize as $chkname) {
                            if (isset($user->{$chkname}) && !empty($user->{$chkname})) {
                                $chk_val = @unserialize($user->{$chkname});
                                if (is_array($chk_val)) {
                                    $user->{$chkname} = implode("|", $chk_val);
                                }
                            }
                        }
                    }
                    // check - issue #1382
                    if (isset($bk_users_to_import)) {
                        $importuser = new stdClass();
                        foreach ($import_user_fileds as $import_user_filed) {
                            if (isset($user->{$import_user_filed})) {
                                $importuser->{$import_user_filed} = $user->{$import_user_filed};
                            }
                            if ($import_user_filed == 'pack_id') {
                                $importuser->{$import_user_filed} = $member->pack_id;
                            }
                        }
                        $export_users[] = $importuser;
                        unset($importuser);
                    } else {
                        $export_users[] = $user;
                    }
                }
                // consider multiple memberships as well:
                if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                    // loop
                    foreach ($member->other_membership_types as $key => $memtypes) {
                        // types
                        if (is_array($memtypes)) {
                            $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                        }
                        // check search parameters:
                        if ($this->_get_membership_details($memtypes, $bk_msexp_dur_unit, $bk_msexp_dur, $membership_type, $current_date, $bk_inactive, $membership_status)) {
                            // copy
                            $user_mem = clone $user_obj;
                            // add custom fields as well:
                            if (!empty($member->custom_fields)) {
                                // loop
                                foreach ($member->custom_fields as $index => $val) {
                                    // custom field
                                    if ($index == 'birthdate' && !empty($val)) {
                                        // convert saved date to input field format
                                        $val = mgm_get_datepicker_format('date', $val);
                                    }
                                    // set
                                    $user_mem->{$index} = $val;
                                }
                            }
                            // check types
                            if (is_object($memtypes) && method_exists($memtypes, 'merge_fields')) {
                                // merge
                                $user_mem = $memtypes->merge_fields($user_mem);
                            } else {
                                // convert to array
                                $data = mgm_object2array($memtypes);
                                // check payment
                                if (isset($memtypes->payment_info) && count($memtypes->payment_info) > 0) {
                                    // loop payments
                                    foreach ($memtypes->payment_info as $index => $val) {
                                        // set
                                        $data['payment_info_' . $index] = str_replace('mgm_', '', $val);
                                    }
                                }
                                // loop data
                                foreach ($data as $index => $val) {
                                    $user_mem->{$index} = $val;
                                }
                            }
                            //issue #844
                            foreach ($skip_fileds as $skip_filed) {
                                unset($user->{$skip_filed});
                            }
                            // format dates
                            $user_mem->user_registered = date($sformat, strtotime($user_mem->user_registered));
                            $user_mem->last_pay_date = (int) $memtypes->last_pay_date > 0 ? date($sformat, strtotime($memtypes->last_pay_date)) : 'N/A';
                            $user_mem->expire_date = !empty($memtypes->expire_date) ? date($sformat, strtotime($memtypes->expire_date)) : 'N/A';
                            $user_mem->join_date = (int) $memtypes->join_date > 0 ? date($sformat, $memtypes->join_date) : 'N/A';
                            // check - issue #1382
                            if (isset($bk_users_to_import)) {
                                $importuser = new stdClass();
                                foreach ($import_user_fileds as $import_user_filed) {
                                    if ($user_mem->{$import_user_filed}) {
                                        $importuser->{$import_user_filed} = $user_mem->{$import_user_filed};
                                    }
                                    if ($import_user_filed == 'pack_id') {
                                        $importuser->{$import_user_filed} = $memtypes->pack_id;
                                    }
                                }
                                $export_users[] = $importuser;
                                unset($importuser);
                            } else {
                                $export_users[] = $user_mem;
                            }
                            // unset
                            unset($user_mem);
                        }
                    }
                }
            }
            // end for
            mgm_log('export_users : ' . mgm_array_dump($export_users, true));
            // default response
            $response = array('status' => 'error', 'message' => __('Error while exporting members. Could not find any member with requested search parameters.', 'mgm'));
            // check
            if (($expcount = count($export_users)) > 0) {
                // Issue #1559: standardization of Membership type
                for ($k = 0; $k < $expcount; $k++) {
                    if (isset($export_users[$k]->membership_type)) {
                        $export_users[$k]->membership_type = strtolower($export_users[$k]->membership_type);
                    }
                }
                // success
                $success = count($export_users);
                // create
                if ($bk_export_format == 'csv') {
                    $filename = mgm_create_csv_file($export_users, 'export_users');
                } else {
                    $filename = mgm_create_xls_file($export_users, 'export_users');
                }
                // src
                $file_src = MGM_FILES_EXPORT_URL . $filename;
                // message
                $response['message'] = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'users' : 'user');
                $response['status'] = 'success';
                $response['src'] = $file_src;
                // for download iframe
            }
            // return response
            echo json_encode($response);
            exit;
        }
        // data
        $data = array();
        // load template view
        $this->load->template('members/member/export', array('data' => $data));
    }