<?php 
// Include main file
include_once('../libs/main.php');

// Check module Login
include_once('checkLogin.php');

// Logical section of page
$modulesObj =  new  modules();
$modulepagesObj =  new  module_pages();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'moduleadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['id'])){
	//$moduleDetail = $modulesObj->fromId($_REQUEST['id']);
    $where = "id=".$_REQUEST['id'];
    $data   = array('where' => $where);
    $moduleDetails = $modulesObj->fromFields($data);

    $wherepage = "module_id=".$_REQUEST['id'];
    $datapage   = array('where' => $wherepage);
    $modulepageDetails = $modulepagesObj->getAllRecords($datapage);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_modulename' && isset($_REQUEST['module_name']) && $_REQUEST['module_name'] != "")
{
    
    if(isset($_REQUEST['module_id']) && $_REQUEST['module_id'] != '' && $_REQUEST['module_name'] != "")
    {

        $fields = array('id','name');
        $where = "name='".$_REQUEST['module_name']."'";
        $data =array('fields'=>$fields,'where'=>$where);        
        $moduleDetails = $modulesObj->fromFields($data);
        //print_r($moduleDetails);exit;
        if($moduleDetails)
        {
            if($_REQUEST['module_id'] != $moduleDetails->id)
            {
                echo "Module name already exists";
                exit;    
            }else{
                echo "success";
                exit;
            }
        }else
        {
            echo "success";
            exit;
        }
    }else
    {
        $fields = array('id','name');
        $where = "name='".$_REQUEST['module_name']."'";
        $data =array('fields'=>$fields,'where'=>$where);        
        $moduleDetails = $modulesObj->fromFields($data);
        //print_r($moduleDetails);exit;
        if($moduleDetails)
        {
            echo "Module name already exists";
            exit;
        }else
        {
            echo "success";
            exit;
        }    
    }    
    
    return true;

}


//if(isset($_POST['submit'])){
    if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['module_name']) && $_REQUEST['module_name'] != "")
    {
	
    	$data=array(
		  'name'=>trim($_POST['module_name']),
	    );
        
        //echo $_POST['module_id']; exit;
	if (isset($_POST['module_id'])){
        $module_id =  $_POST['module_id'];
		$mod_id=$module_id;

        $where = "id=".$module_id;
        $data_mod   = array('where' => $where);
        $modulegetDetails = $modulesObj->fromFields($data_mod);
        if(!empty($modulegetDetails))
        {
            $modulelogdetail = "<b>Edit Record</b>";
            if($modulegetDetails->name != trim($_POST['module_name']))
            {
                $modulelogdetail .= " <b>Module Name</b>: "." ".$modulegetDetails->name." <b>To</b> ".$_POST['module_name']." and ";
            
                $chages_data = true;
            }    
        }
        

		$rs=$modulesObj->updateRecords($data,$mod_id);
		if ($rs)
        {

            $module_page = $_POST['module_page'];
            $modulepageid = $_POST['module_pageid'];
            //print_r($modulepageid);exit;
            
            $fields = array('id','page');
            $where = "module_id='".$mod_id."'";
            $modpagedata   = array('fields'=>$fields,'where' => $where);
            $modulepageidDetails = $modulepagesObj->getAllRecords($modpagedata);
            $mpageidvalue = array();
            $mpagenamevalue = array();
            foreach ($modulepageidDetails as $key => $mpdvalue) 
            {
                array_push($mpageidvalue, $mpdvalue->id);
                array_push($mpagenamevalue, $mpdvalue->page);
            } 
            //print_r($mpagenamevalue);
            if(isset($mpageidvalue) && count($mpageidvalue) > 0 && isset($modulepageid) && count($modulepageid) > 0)
            {
                $mpageiddiff = array_diff($mpageidvalue,$modulepageid);
                if(isset($mpageidvalue) && count($mpageidvalue) > 0)
                {   
                    foreach ($mpageiddiff as $key => $mpagdelvalue) 
                    {
                       //$modulepagedel = $modulepagesObj->deleteFromId($mpagdelvalue);
                    }
                }
            }else{
                if(isset($mpageidvalue) && count($mpageidvalue) > 0)
                {
                    foreach ($mpageidvalue as $key => $mpagdelvalue) 
                    {
                       //$modulepagedel = $modulepagesObj->deleteFromId($mpagdelvalue);
                    }
                }
                
            }
            if(!empty($mpagenamevalue) && !empty($module_page))
            {
                //print_r($mpagenamevalue);
                //print_r($module_page);
                
                $mpagelogdiff = array_diff($module_page,$mpagenamevalue);
                //print_r($mpagelogdiff);exit;
                if(!empty($mpagelogdiff))
                {
                    $new_all_pages = implode(",",$module_page);
                    $old_all_pages = implode(",",$mpagenamevalue);
                    
                    $modulelogdetail .= " <b>old data page</b>: ".$old_all_pages." <b>To page</b>: ".$new_all_pages." and ";
                    $chages_data = true;  
                }else{
                    
                    if((count($module_page) > count($mpagenamevalue)) || (count($mpagenamevalue) > count($module_page)))
                    {
                        $new_all_pages = implode(",",$module_page);
                        $old_all_pages = implode(",",$mpagenamevalue);

                        $modulelogdetail .= " <b>old data page</b>: ".$old_all_pages." <b>To page</b>: ".$new_all_pages." and ";
                        $chages_data = true;
                    }
                }
            }
            
            if(isset($module_page) && count($module_page) > 0)
            {
                $pageId_diff = array_diff($mpageidvalue,$modulepageid);
                
                if(!empty($pageId_diff))
                {
                    foreach ($pageId_diff as $pgkey => $pagedelvalue) 
                    { 
                        
                       $modulepagedel = $modulepagesObj->deleteFromId($pagedelvalue);
                      
                    }
                }

                foreach ($module_page as $skey => $mpagename) 
                {       
                    if(isset($mpagename) && $mpagename != '')
                    {
                        $mpageid = $modulepageid[$skey];
                        if(isset($mpageid) && $mpageid != '')
                        {   
                             
                            $moduledata=array(
                            'module_id'=>$mod_id,
                            'page'=>trim($mpagename),
                            );
                            $moduleupd = $modulepagesObj->updateRecords($moduledata,$mpageid);
                        }else{
                            
                            $moduledata=array(
                                'module_id'=>$mod_id,
                                'page'=>trim($mpagename)
                            );

                            $moduleins = $modulepagesObj->addRecords($moduledata);
                        }
                        
                    }
                }
            }
            
            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($modulelogdetail," and"));
            }

            header('Location:modulelist.php');
			exit;
		}

	}else{
        $rs = $modulesObj->addRecords($data);
        $moduledatas = "<b>Add Record</b> ";
        $moduledatas.= " <b>Module Data</b>: ".json_encode($data);
        if($rs){

            $module_page = $_POST['module_page'];
            if(isset($module_page) && count($module_page) > 0)
            {
                
                foreach ($module_page as $skey => $mpagename) 
                {        
                    $moduledata=array(
                        'module_id'=>$rs,
                        'page'=>trim($mpagename)
                    );

                    $moduleins = $modulepagesObj->addRecords($moduledata);

                    $modulepagedata = "<b>Add Record Page Data</b>: ".json_encode($moduledata);
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$modulepagedata);
                }
            }
            
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$moduledatas);
    		header('Location:modulelist.php');
    		exit;
        }
	}
}

// Include Template 
include_once('../libs/routs.php');
?>