<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.members.php
//+ Members admin helper
//+-----------------------------------------------------+//

defined('BASEPATH') or die('You can not directly access this file');//check its within this framework

//____DEFAULT TEMPLATE_____________________________
$inner_template = ADMIN_TEMPLATES.'tpl_members.htm';


//__________Check for actions_______________________
$action = Sanatize(($_GET['action'] != '') ? $_GET['action'] : $_POST['action']); //check if its post or get 

if($action != ''){

//run through
switch($action){

case 1: //delete
foreach($_POST['list'] as $key => $value){
$value = Sanatize(@mysql_real_escape_string($value));
(DeleteMember($value)) ? $notification = $lang['request_completed'] : $notification = $lang['generic_error'];
}
break;

case 2: //suspend
foreach($_POST['list'] as $key => $value){
$value = Sanatize(@mysql_real_escape_string($value));
(MemberStatus($value, 'suspended')) ? $notification = $lang['request_completed'] : $notification = $lang['generic_error'];
}
break;

case 3: //activate
foreach($_POST['list'] as $key => $value){
$value = Sanatize(@mysql_real_escape_string($value));
(MemberStatus($value, 'active')) ? $notification = $lang['request_completed'] : $notification = $lang['generic_error'];
}
break;

}
}

//_____Get any sorting_________________________________
$order_by =Sanatize((isset($_GET['sort']))?$_GET['sort']:$_POST['sort']);

$order_by_members = 1;
$order_by_messages = 3;
$order_by_images = 5;
$order_by_followers = 7;
$order_by_following = 9;
$order_by_reg = 11;
$order_by_status = 13;

switch ($order_by) {
    case 1:
        $sort = 'ORDER BY user_name DESC';
        $order_by_members = 2;
        break;
    case 2:
        $sort = 'ORDER BY user_name ASC';
        $order_by_members = 1;
        break;
    case 3:
        $sort = 'ORDER BY messages DESC';
        $order_by_messages = 4;
        break;
    case 4:
        $sort = 'ORDER BY messages ASC';
        $order_by_messages = 3;
        break;
    case 5:
        $sort = 'ORDER BY images DESC';
        $order_by_images = 6;
        break;
    case 6:
        $sort = 'ORDER BY images ASC';
        $order_by_images = 5;
        break;
    case 7:
        $sort = 'ORDER BY followers DESC';
        $order_by_followers = 8;
        break;
    case 8:
        $sort = 'ORDER BY followers ASC';
        $order_by_followers = 7;
        break;
    case 9:
        $sort = 'ORDER BY following DESC';
        $order_by_following = 10;
        break;
    case 10:
        $sort = 'ORDER BY following ASC';
        $order_by_following = 9;
        break;

    case 11:
        $sort = 'ORDER BY user_id DESC';
        $order_by_reg = 12;
        break;
    case 12:
        $sort = 'ORDER BY user_id ASC';
        $order_by_reg = 11;
        break;

    case 13:
        $sort = 'ORDER BY account_status DESC';
        $order_by_status = 14;
        break;
    case 14:
        $sort = 'ORDER BY account_status ASC';
        $order_by_status = 13;
        break;
}


//_________List Mambers_____________________________
$paginate = array(20, Sanatize($_GET['page']), 'Next >>', '<< Previous'); //(limit, current page, next, previous)
$pagi_addurl_pre = $env_base_url."/index.php?option=com_admin&type=members&sort=$order_by&"; //Optional - added in paginated list e.g. 'http://xyz.com?option=asa&'
$results = $dbase->PaginatedRecordLoop("SELECT m.user_name, m.email_address, m.date_created, m.account_status, m.avatar, m.user_id, (
										SELECT COUNT( c.user_name ) AS my_messages
										FROM content_text c
										WHERE user_name = m.user_name
										GROUP BY c.user_name
										) AS messages, (

										SELECT COUNT( f.user_name ) AS my_followers
										FROM following f
										WHERE followers_user_name = m.user_name
										GROUP BY f.followers_user_name
										) AS following, (

										SELECT COUNT( f.user_name ) AS my_following
										FROM following f
										WHERE user_name = m.user_name
										GROUP BY f.user_name
										) AS followers, (

										SELECT COUNT( i.user_name ) AS my_images
										FROM content_images i
										WHERE user_name = m.user_name
										GROUP BY i.user_name
										) AS images
										
										FROM member_profile m $sort");

//get pagination html code
$pagination = $dbase->pagination_simple_html;

if(!empty($results)){
$show_more = 1;
}

//____________Set page (used only in html)_____________________________
$page =Sanatize(($_GET['page'] != '') ? $_GET['page']:$_POST['page']);
$page = (($page == "")? 1:$page);

?>