<?php 
// Include main file
include_once('../libs/main.php');
// Check speciality Login
include_once('checkLogin.php');

// Logical section of page
$specialityObj =  new  speciality();
$adminlogObj = new admin_log();
$domainObj = new domain();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'specialityadd.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['speciality_id'])){
    $specialityDetail = $specialityObj->fromId($_REQUEST['speciality_id']);
}

if(isset($_POST['submit'])){
    	$data=array(
	  'speciality_name'=>trim($_POST['speciality_name']), 
      'speciality_domain_id'=>trim($_POST['speciality_domain_id']), 
          'speciality_slug'=>trim($_POST['speciality_slug']), 
	);
        if(isset($_POST['speciality_status'])){
            $data['speciality_status'] 			= '1';
        }else{
            $data['speciality_status'] 			= '0';
        }

        $specalityDetail = $specialityObj->fromId($_REQUEST['speciality_id']);
        if(!empty($specalityDetail))
        {
            $specalitydata = "<b>Edit Record</b> ";
            if($specalityDetail->speciality_domain_id != trim($_POST['speciality_domain_id']))
            {
                $olddomainDetail = $domainObj->fromId($specalityDetail->speciality_domain_id);
                $newdomainDetail = $domainObj->fromId($_POST['speciality_domain_id']);
                   
                $specalitydata .= " <b>Id</b>: "." ".$olddomainDetail->domain_title." <b>To</b> ".$newdomainDetail->domain_title." and ";
            
                $chages_data = true;
            }

            if($specalityDetail->speciality_name != trim($_POST['speciality_name']))
            {
                $specalitydata .= " <b>Name</b>: "." ".$specalityDetail->speciality_name." <b>To</b> ".$_POST['speciality_name']." and ";
            
                $chages_data = true;
            }

            if($specalityDetail->speciality_slug != trim($_POST['speciality_slug']))
            {
                $specalitydata .= " <b>Slug</b>: "." ".$specalityDetail->speciality_slug." <b>To</b> ".$_POST['speciality_slug']." and ";
            
                $chages_data = true;
            }

            if($specalityDetail->speciality_status != $data['speciality_status'])
            {
                
                if($data['speciality_status'] == '1')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

                if($specalityDetail->speciality_status == '1')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

                $specalitydata .= " <b>Status</b>: "." ".$old_status." To ".$new_status." and ";
            
                $chages_data = true;
            }  
        }
                   
	if (isset($_POST['speciality_id'])){
            $id=$_POST['speciality_id'];
            $rs=$specialityObj->updateRecords($data,$id);
            if ($rs){

                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($specalitydata," and"));
                }
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:specialitylist.php?successMsg='.$successMsg);
                exit;
            }
        }else{
            $specalitydata = "<b>Add Record</b> ";
            $specalitydata.= " <b>Speciality Data</b>: ".json_encode($data);
            $rs = $specialityObj->addRecords($data);
            if($rs){
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$specalitydata);
    		$successMsg = base64_encode(RECORD_ADD);			
    		header('Location:specialitylist.php?successMsg='.$successMsg);
    		exit;
            }
	}
}
$domainObj = new domain();
$allDomain  =$domainObj->getAllRecords(array());
// Include Template 
include_once('../libs/routs.php')

?>