<?php 

// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$userObj =  new  user();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'useradd.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['user_id'])){
    $userDetail = $userObj->fromId($_REQUEST['user_id']);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    if($_REQUEST['email']==''){
        echo ENTER_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['email'])){

        $select=array('user_email_address','user_id');
        $where =array('user_email_address='=>$_REQUEST['email']);

        if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!=""){
            $where['user_id!='] =$_REQUEST['user_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        

        if(isset($_REQUEST['user_id']) && $userObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }else if(!isset($_REQUEST['user_id']) && $userObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }
    }else{
        echo VALID_EMAIL;
        exit;
    }
    return true;
}

if(isset($_POST['submit'])){
    $data=array(
        'user_fname'=>trim($_POST['user_fname']),
        'user_lname'=>trim($_POST['user_lname']),
        'user_email_address'=>trim($_POST['user_email_address']),
        'user_faxEmail'=>trim($_POST['user_faxEmail']),
        'user_faxPassword'=>trim($_POST['user_faxPassword']),
        'user_emailHost'=>trim($_POST['user_emailHost']),
        'user_mangoCallUser'=>trim($_POST['user_mangoCallUser']),
    );
    if(isset($_POST['user_status'])){
        $data['user_status'] 			= '1';
    }else{
        $data['user_status'] 			= '0';
    } 
    if(isset($_POST['user_owner'])){
        $data['user_owner'] 			= '1';
    }else{
        $data['user_owner'] 			= '0';
    }
    if(isset($_POST['user_verified'])){
        $data['user_email_verified'] = date('Y-m-d H:i:s');
    }else{
        $data['user_email_verified']             = null;
    } 
    if (isset($_POST['user_id'])){
        $id=$_POST['user_id'];

        $user_detail = '';
        $user_detail = 'Edit User Old';
        $userDetailLog = $userObj->fromId($_POST['user_id']);
        if($userDetailLog->user_fname != $_POST['user_fname'])
        {
            $user_detail .= " <b>First name</b>: "." ".$userDetailLog->user_fname." <b>To</b> ".$_POST['user_fname']." and ";
            
            $chages_data = true;
        }
        if($userDetailLog->user_lname != $_POST['user_lname'])
        {
            $user_detail .= " <b>Last name</b>: "." ".$userDetailLog->user_lname." <b>To</b> ".$_POST['user_lname']." and ";
            
            $chages_data = true;
        }
        if($userDetailLog->user_email_address != $_POST['user_email_address'])
        {
            $user_detail .= " <b>Email</b>: "." ".$userDetailLog->user_email_address." <b>To</b> ".$_POST['user_email_address']." and ";
            
            $chages_data = true;
        }
        if($userDetailLog->user_owner != $data['user_owner'])
        {
            
            if($userDetailLog->user_owner == '1')
            {
                $old_user_owner = 'Yes';
            }else{
                $old_user_owner = 'No';
            }

            if($data['user_owner'] == '1')
            {
                $user_owner = 'Yes';
            }else{
                $user_owner = 'No';
            }

            $user_detail .= " <b>Owner</b>: "." ".$old_user_owner." <b>To</b> ".$user_owner." and ";
            
            $chages_data = true;
        }
        if($userDetailLog->user_status != $data['user_status'])
        {
            
            if($userDetailLog->user_status == '1')
            {
                $old_user_status = 'Active';
            }else{
                $old_user_status = 'Inactive';
            }

            if($data['user_status'] == '1')
            {
                $user_status = 'Active';
            }else{
                $user_status = 'Inactive';
            }

            $user_detail .= " <b>Status</b>: "." ".$old_user_status." <b>To</b> ".$user_status." and ";
            
            $chages_data = true;
        }


        $data['user_updated_at'] = date('Y-m-d H:i:s');
        $rs=$userObj->updateRecords($data,$id);
        if ($rs)
        {

            if($chages_data == true)
            {
                $ruser_detail = substr($user_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$ruser_detail);
            }
            $successMsg = base64_encode(RECORD_UPDATE);			
            //?successMsg='.$successMsg
            header('Location:userlist.php');
            exit;
        }
    }else{
        $data['user_unique_id'] = $userObj->getNextUniqueId('user_unique_id','user');
        $data['user_password'] = w::password($_POST['user_password']);
        $data['user_created_at'] = date('Y-m-d H:i:s');
        
        $rs = $userObj->addRecords($data);
        if($rs){
            
            /* Start reset password link sent to email */
            $urldata = array(
                "id"=>$rs,
                "uniqueid"=>$data['user_unique_id'],
                "email"=>$data['user_email_address'],
                "modelType" =>"user",
                "time" =>time()+emailTemplates::ACCESS_TIME,
            );
            $mailArray =array(
                "new"=>array("email"=>$data['user_email_address'],'password'=>$_POST['user_password']),
                "urldata" => $urldata,
            );
            $subject = emailTemplates::subject('verify');
            $mailText = emailTemplates::verifyOrSignup($mailArray);
            //sendMail(emailTemplates::ADMIN_FROM_EMAIL,emailTemplates::ADMIN_FROM_NAME,$data['user_email_address'],"",$subject,$mailText);
            /* End reset password link sent to email */

            if($data['user_status'] == '1')
            {
                $user_status = 'Active';
            }else{
                $user_status = 'Inactive';
            }

            if($data['user_owner'] == '1')
            {
                $user_owner = 'Yes';
            }else{
                $user_owner = 'No';
            }

            $user_detail = "Add User data "."<b>First name</b>: ".trim($_POST['user_fname'])." <b>and Last Name</b>: ".trim($_POST['user_lname'])." <b>and Email address</b>: ".trim($_POST['user_email_address'])." <b>and Password</b>: ".trim($_POST['user_password'])." <b>and Owner</b>: ".$user_owner." <b>and Status</b>: ".$user_status;

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$user_detail);

            $successMsg = base64_encode(RECORD_ADD);			
            //?successMsg='.$successMsg
            header('Location:userlist.php');
            exit;
        }
    }
}

// Include Template 
include_once('../libs/routs.php')

?>