<?php

require_once 'config.php';
include_once "includes/noentry.php";
include_once $CFG->dirroot . "/includes/func_generallist.inc.php";
global $CFG;
$ModuleName = $_REQUEST['file'];
$RelatedArr = getExtraArray_List($ModuleName);
$FieldArr = getFieldArray_List($ModuleName);
$table_array = getTableNameandPrimaryId_List($RelatedArr);
if ($_POST) {
    if ($_POST['mode'] != "Search") {
        $var_msg1 = ActiveInactiveRecord_List($_POST, $ModuleName, $table_array);
        echo $var_msg1;
        exit;
    }
}
#print_r($_REQUEST);
$ListFile = 'index.php?file=user_generallist&Module=' . $ModuleName . $RelatedArr['Extraquerystr'];
if (trim($RelatedArr['ListFile']) != "") {
    $ListFile = $RelatedArr['ListFile'] . "&Module=" . $ModuleName;
}
$SQL_Query_TableName = getTableNameForSQLQuery_List($RelatedArr);
#echo $extrasql;
$SQL_Query = getSearchSQL_List($RelatedArr, $extrasql);
$sort_group_array = getSortandGroupVaribale_List($FieldArr, $RelatedArr);
$GroupBy = $sort_group_array['GroupBy'];
$sort = $sort_group_array['sort'];
$moduelsql = "";
//echo "<br>==>".$SQL_Query."<br>";exit;
//--------------
function ActiveInactiveRecord_List($post_array, $ModuleName = '', $table_array = array())
{
    #print_r($post_array); exit;
    global $CFG, $DEFINE_ARRAY, $obj;
    //echo $CFG->dirroot."/lib/classes/";exit;
    $msg = "";
    if (isset($post_array) && ($post_array['Module'] != "" || $post_array['file'] != "")) {
        if (trim($ModuleName) == '') {
            $ModuleName = $post_array['Module'];
        }
        if (trim($ModuleName) == '') {
            $ModuleName = $_REQUEST['file'];
        }
        #echo "<br> in function >> $ModuleName >> ".$post_array[mode];
        if (count($table_array) == 0 || !is_array($table_array)) {
            $RelatedArr = getExtraArray_List($ModuleName);
            $table_array = getTableNameandPrimaryId_List($RelatedArr);
        }
        $mode = $post_array[mode];
        $array = array();
        for ($i = 0; $i < $post_array['no']; $i++) {
            $iId = $post_array["ch" . $i];
            if ($iId != "") {
                $array[] = $iId;
            }
        }
        $count = count($array);
        $array_val = implode(",", $array);
        if (count($array) > 0) {
            $change_id_array = implode(",", $array);
            //	print_r($change_id_array);exit;
            switch ($mode) {
                case "Delete":
                    if ($count > 0) {
                        if ($ModuleName == 'Company') {
                            //$delete_sql = "DELETE FROM SubGroup WHERE iCompanyId IN(" . $change_id_array . ")";
                            // $obj->sql_query($delete_sql);
                            //$delete_sql = "DELETE FROM admin WHERE iCompanyId IN(" . $change_id_array . ")";
                            // $obj->sql_query($delete_sql);
                        } else {
                            if ($ModuleName == 'Courses') {
                                $delete_sql = "DELETE FROM CourseClasses WHERE iCourseId IN(" . $change_id_array . ")";
                                $obj->sql_query($delete_sql);
                            } else {
                                if ($ModuleName == 'Library') {
                                    $delete_sql = "DELETE FROM Document WHERE iLibCategoryId IN(" . $change_id_array . ")";
                                    $obj->sql_query($delete_sql);
                                } else {
                                    if ($ModuleName == 'VideoCategory') {
                                        $delete_sql = "DELETE FROM Video WHERE iVideoCategoryId IN(" . $change_id_array . ")";
                                        $obj->sql_query($delete_sql);
                                    } else {
                                        if ($ModuleName == 'AudioCategory') {
                                            $delete_sql = "DELETE FROM Audio WHERE iAudioCategoryId IN(" . $change_id_array . ")";
                                            $obj->sql_query($delete_sql);
                                        } else {
                                            if ($ModuleName == 'SubGroup') {
                                                //  $delete_sql = "DELETE FROM admin WHERE iSGroupId IN(" . $change_id_array . ")";
                                                //   $obj->sql_query($delete_sql);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        $db_sql = $obj->DB_query_change_status($table_array['TableName'], $change_id_array, $table_array['Primary_Field'], $table_array['Status_Field'], "Delete");
                    }
                    $msg = 'MSG_' . strtoupper($mode);
                    $msg = $DEFINE_ARRAY[$msg];
                    $msg = rawurlencode($count . " Record(s) " . $msg);
                    $msg .= $delete_msg;
                    break;
                case "Active":
                    if ($ModuleName == 'Company') {
                        $update_sql = "UPDATE SubGroup sg SET sg.eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE sg.iCompanyId IN(" . $change_id_array . ")";
                        #echo $update_sql;exit;
                        $obj->sql_query($update_sql);
                        //$update_sql = "UPDATE admin SET eStatus = 'Active'
                        //									WHERE iCompanyId IN(" . $change_id_array . ")";
                        #echo $update_sql;exit;
                        // $obj->sql_query($update_sql);
                    } else {
                        if ($ModuleName == 'Courses') {
                            $update_sql = "UPDATE CourseClasses SET eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE iCourseId IN(" . $change_id_array . ")";
                            #echo $update_sql;exit;
                            $obj->sql_query($update_sql);
                        } else {
                            if ($ModuleName == 'Library') {
                                $update_sql = "UPDATE Document SET eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE iLibCategoryId IN(" . $change_id_array . ")";
                                #echo $update_sql;exit;
                                $obj->sql_query($update_sql);
                            } else {
                                if ($ModuleName == 'VideoCategory') {
                                    $update_sql = "UPDATE Video SET eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE \tiVideoCategoryId IN(" . $change_id_array . ")";
                                    #echo $update_sql;exit;
                                    $obj->sql_query($update_sql);
                                } else {
                                    if ($ModuleName == 'AudioCategory') {
                                        $update_sql = "UPDATE Audio SET eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE iAudioCategoryId IN(" . $change_id_array . ")";
                                        #echo $update_sql;exit;
                                        $obj->sql_query($update_sql);
                                    } else {
                                        if ($ModuleName == 'SubGroup') {
                                            $update_sql = "UPDATE admin SET eStatus = 'Active'\r\n\t\t\t\t\t\t\t\tWHERE iSGroupId IN(" . $change_id_array . ")";
                                            #echo $update_sql;exit;
                                            $obj->sql_query($update_sql);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($count > 0) {
                        $mode1 = $mode;
                        $db_sql = $obj->DB_query_change_status($table_array['TableName'], $change_id_array, $table_array['Primary_Field'], $table_array['Status_Field'], $mode1);
                    }
                    $msg = 'MSG_' . strtoupper($mode);
                    $msg = $DEFINE_ARRAY[$msg];
                    $msg = rawurlencode($count . " Record(s) " . $msg);
                    $msg .= $delete_msg;
                    break;
                case "Inactive":
                    if ($ModuleName == 'Company') {
                        $update_sql = "UPDATE SubGroup sg SET sg.eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE sg.iCompanyId IN(" . $change_id_array . ")";
                        #echo $update_sql;exit;
                        $obj->sql_query($update_sql);
                        //   $update_sql = "UPDATE admin SET eStatus = 'Inactive'
                        //									WHERE iCompanyId IN(" . $change_id_array . ")";
                        #echo $update_sql;exit;
                        $obj->sql_query($update_sql);
                    } else {
                        if ($ModuleName == 'Courses') {
                            $update_sql = "UPDATE CourseClasses SET eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE iCourseId IN(" . $change_id_array . ")";
                            #echo $update_sql;exit;
                            $obj->sql_query($update_sql);
                        } else {
                            if ($ModuleName == 'Library') {
                                $update_sql = "UPDATE Document SET eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE iLibCategoryId IN(" . $change_id_array . ")";
                                #echo $update_sql;exit;
                                $obj->sql_query($update_sql);
                            } else {
                                if ($ModuleName == 'VideoCategory') {
                                    $update_sql = "UPDATE Video SET eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE iVideoCategoryId IN(" . $change_id_array . ")";
                                    #echo $update_sql;exit;
                                    $obj->sql_query($update_sql);
                                } else {
                                    if ($ModuleName == 'AudioCategory') {
                                        $update_sql = "UPDATE Audio SET eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE iAudioCategoryId IN(" . $change_id_array . ")";
                                        #echo $update_sql;exit;
                                        $obj->sql_query($update_sql);
                                    } else {
                                        if ($ModuleName == 'SubGroup') {
                                            $update_sql = "UPDATE admin SET eStatus = 'Inactive'\r\n\t\t\t\t\t\t\t\tWHERE iSGroupId IN(" . $change_id_array . ")";
                                            #echo $update_sql;exit;
                                            $obj->sql_query($update_sql);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($count > 0) {
                        $mode1 = $mode;
                        $db_sql = $obj->DB_query_change_status($table_array['TableName'], $change_id_array, $table_array['Primary_Field'], $table_array['Status_Field'], $mode1);
                    }
                    $msg = 'MSG_' . strtoupper($mode);
                    $msg = $DEFINE_ARRAY[$msg];
                    $msg = rawurlencode($count . " Record(s) " . $msg);
                    $msg .= $delete_msg;
                    break;
                case "Assign":
                    switch ($ModuleName) {
                        case 'GroupsForUsers':
                            if ($post_array['iUserId'] != '') {
                                $sql = "insert ignore into  SubGroupUserAssign(iUserId,iSGroupId) select " . $post_array['iUserId'] . ",SubGroup.iSGroupId from SubGroup where SubGroup.iSGroupId in (" . $change_id_array . ")";
                                $result = $obj->insert($sql);
                                $msg = " Record(s) " . ' are added.';
                            } else {
                                $msg = "Cannot assign groups to unknown user!";
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                case "UnAssign":
                    switch ($ModuleName) {
                        case 'UsersGroups':
                            if ($post_array['iUserId'] != '') {
                                $sql = 'select a.iSGroupId from SubGroupUserAssign a join SubGroup sg on sg.iSGroupId=a.iSGroupId where sg.iIsDefault=0 and a.iUserId=' . $post_array['iUserId'] . ' and sg.iSGroupId not in (' . $change_id_array . ")";
                                $result = $obj->sql_query($sql);
                                if ($result && count($result) > 0) {
                                    $sql = 'delete from SubGroupUserAssign where iUserId=' . $post_array['iUserId'] . ' and  iSGroupId in (' . $change_id_array . ")";
                                    $result = $obj->sql_query($sql);
                                    $msg = " Record(s) " . ' are deleted.';
                                } else {
                                    $msg = "Can not perfom this operation as user will not have any group assigned.";
                                }
                            } else {
                                $msg = "Cannot unassign groups from unknown user!";
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                case "Approved":
                case "Declined":
                case "InProcess":
                case "Pending":
                case "Expired":
                case "Cancelled":
                    if ($count > 0) {
                        $mode1 = $mode;
                        if ($ModuleName == "CountryTable") {
                            $change_id_array = implode(',', $array);
                            $db_sql = $obj->DB_query_change_status($table_array['TableName'], $change_id_array, $table_array['Primary_Field'], $table_array['Status_Field'], $mode1);
                        } else {
                            $db_sql = $obj->DB_query_change_status($table_array['TableName'], $change_id_array, $table_array['Primary_Field'], $table_array['Status_Field'], $mode1);
                        }
                    }
                    $msg = 'MSG_' . strtoupper($mode);
                    $msg = $DEFINE_ARRAY[$msg];
                    $msg = rawurlencode($count . " Record(s) " . $msg);
                    $msg .= $delete_msg;
                    break;
            }
        } else {
            $msg = rawurlencode($count . " Record(s) " . MSG_ . strtoupper($mode) . _ERROR);
        }
    }
    return rawurldecode($msg);
}