<?php 
require_once 'dao/Profile.php';
require_once 'dao/Setup.php';
$success = "";
$err = "";
if (isset($_POST['email'])) {
	$email = $_POST['email'];
	$data = array("email"=>$email);
	$success = $profile->update($data,$_SESSION['staff_id']);
}
if (isset($_POST['first_name'])) {
	$first_name = $_POST['first_name'];
	$data = array("first_name"=>$first_name);
	$success = $profile->update($data,$_SESSION['staff_id']);
}

if (isset($_POST['sex'])) {
	$sex = $_POST['sex'];
	if ($sex==1) {
		$sex="Male";
	}else if ($sex==2) {
		$sex="Female";
	}
	$data = array("sex"=>$sex);
	$success = $profile->update($data,$_SESSION['staff_id']);
}

if (isset($_POST['first_name'])) {
	$first_name = $_POST['first_name'];
	$data = array("first_name"=>$first_name);
	$success = $profile->update($data,$_SESSION['staff_id']);
}
if (isset($_GET['dob'])) {
	$dob = $_GET['dob'];
	$data = array("dob"=>$dob);
	$success = $profile->update($data,$_SESSION['staff_id']);
	echo $dob;
}
if (isset($_POST['phone'])) {
	$phone = $_POST['phone'];
	for ($i=0; $i < sizeof($phone) ; $i++) { 
		if ($i==sizeof($phone)-1) {
			$single2 = $phone['phone3'];
		}else{
			$single = $phone['phone1'].','.$phone['phone2'].',';
		}
		
	}
	$data = array("phone"=>$single.$single2);
	$success = $profile->update($data,$_SESSION['staff_id']);
	
}
if (isset($_POST['last_name'])) {
	$last_name = $_POST['last_name'];
	$data = array("last_name"=>$last_name);
	$success = $profile->update($data,$_SESSION['staff_id']);
}

//if (isset($_GET['pic'])) {
	if (isset($_FILES['pic'])) {
		$generated_name_1 = '';
		  $ext_1 = explode('.', basename($_FILES["pic"]["name"])); //explode file name from dot(.) 
                $file_extension_1 = end($ext_1); //store extensions in the variable
                $generated_name_1 .= md5(uniqid()) . "." . $ext_1[count($ext_1) - 1];
                move_uploaded_file($_FILES["pic"]["tmp_name"], "../userImage/".$generated_name_1);
                $data = array("picture"=>$generated_name_1);
                $profile->update($data,$_SESSION['staff_id']);
	}
//}

$get_staff_detail = "";
$get_staff_detail = $profile->get_staff_detail($_SESSION['staff_id']);
$get_all_staff = "";
$get_all_staff = $profile->get_all_staff();
$get_all_parents = $profile->get_all_parents();

//Class array manipulation
$classArray = "";
$countClass = 0;
$classArray18 = "";
$classRow = $profile->get_classes_old($_SESSION['staff_id']);if($classRow  != "" )  {
  $classArray = explode(",",$classRow[0]['class']);
  $countClass = count($classArray);
  $classArray18 = array_slice($classArray, 0,18);
  }

  if (isset($_POST['assined_classes'])) {
  	$staff_id = $_POST['staff_id'];
  $assined_classes = $_POST['assined_classes'];
  
  $check_assignment = $profile->check_assignment($staff_id);
   		$classArray = "";
$countClass = 0;
$classArray1 = "";
if($assined_classes  != "" )  {
  $classArray = explode(",",$assined_classes);
  $countClass = count($classArray);
  $classArray1 = array_slice($classArray, 0,18);
  }
  if ($check_assignment) {
  	$newArray = array();
  	foreach ($check_assignment as $key) {
  		array_push($newArray, $key['class_id']);
  	}
  	for ($j=0; $j <sizeof($classArray1) ; $j++) { 
  			
  			if (in_array($classArray1[$j], $newArray)) {
  			$err = 9;
  		}else{
  			$err = "";
  			$data = array("class_id"=>$classArray1[$j],
			    "time"=>'',
			    "staff_id"=>$staff_id);
  			$success = $profile->assign_class($data);
  				  if ($success) {
  	$err = 0;
  }else{
  	$err = 1;
  }
  		}
  		
  		}
  		
  echo $err;	

  }else{
 		$classArray = "";
$countClass = 0;
$classArray1 = "";
if($assined_classes  != "" )  {
  $classArray = explode(",",$assined_classes);
  $countClass = count($classArray);
  $classArray1 = array_slice($classArray, 0,18);
  }
  foreach($classArray1 as $key=>$value){
  		$data = array("class_id"=>$value,
			    "time"=>'',
			    "staff_id"=>$staff_id);
  	$success = $profile->assign_class($data);
  }
   if ($success) {
  	echo 0;
  }else{
  	echo 1;
  }		
  }
  
  }

  if (isset($_POST['form_class'])) {
  	$staff_id = $_POST['staff_id'];
  $form_class = $_POST['form_class'];
  $data = array("is_form_master"=>$form_class);
  $success = $profile->update($data,$staff_id);
  if ($success) {
  	echo 0;
  }else{
  	echo 1;
  }
  }


   if (isset($_POST['assign_house_master'])) {
    $staff_id = $_POST['staff_id'];
  $house_id = $_POST['house_id'];
  $data = array("house_id"=>$house_id,
                "staff_id"=>$staff_id);
  $success = $profile->assign_house_master($data);
  if ($success) {
    echo 0;
  }else{
    echo 1;
  }
  }

  if (isset($_POST['assign_subject'])) {
  	$class_id = $_POST['class_id'];
  	$subject_id = $_POST['subject'];
  	$staff_id = $_POST['staff_id'];
  	$data = array("staff_id"=>$staff_id);
  	$success = $profile->assign_subject($data,$subject_id,$class_id);
  	if ($success) {
  		echo 0;
  	}else{
  		echo 1;
  	}
    $class_list = $profile->get_class_list();
$houses = $setup->get_houses();
$all_staff = $profile->get_all_staff();
$subject_list = $profile->get_subject_list();
$load_classes = $profile->get_subjects($_SESSION['staff_id']);
$load_classes_admin = $profile->get_subjects_admin();
$staff_form_class = $profile->get_form_class($_SESSION['staff_id']);
$roles = $profile->get_all_roles();

//refined
$all_classes = $profile->get_all_classes();
  }

  $edit_staff = "";
   $staff_id = "";
  if (isset($_GET['sf::'])) {
  $staff_id = $_GET['sf::'];
  $edit_staff = $profile->get_staff($staff_id);

}

if (isset($_GET['sf%%'])) {
  $staff_id = $_GET['sf%%'];
  $success = $profile->remove_staff($staff_id);
  if ($success) {
    echo 0;
  }else{
    echo 1;
  }
}

if (isset($_POST['update_staff'])) {
  $last_name = $_POST['new_last_name'];
  $first_name = $_POST['new_first_name'];
  $email = $_POST['new_email'];
  $role = $_POST['new_role'];
  $is_form_master = $_POST['new_is_form_master'];
  $phone = $_POST['new_phone'];
    $phoneArray = "";
$countPhones = 0;
$phoneArray1 = "";
$new_phone = "";
if($phone  != "" )  {
  $phoneArray = explode(",",$phone);
  $countPhones = count($phoneArray);
  $phoneArray1 = array_slice($phoneArray, 0,18);
  }
  if (is_array($phoneArray1)) {
    foreach ($phoneArray1 as $key => $value) {
  $new_phone = $value;
}
  }

  $dob = $_POST['new_dob'];
  $generated_name_1 = '';
  if ($_FILES['new_pic']&&$_FILES["new_pic"]["name"]!='') {
    
      $ext_1 = explode('.', basename($_FILES["new_pic"]["name"])); //explode file name from dot(.) 
                $file_extension_1 = end($ext_1); //store extensions in the variable
                $generated_name_1 .= md5(uniqid()) . "." . $ext_1[count($ext_1) - 1];
                move_uploaded_file($_FILES["new_pic"]["tmp_name"], "../userImage/".$generated_name_1);
                $data = array("picture"=>$generated_name_1);
                $profile->update($data,$_SESSION['staff_id']);
  }else{
    $generated_name_1 = $_POST['old_pic'];
  }
$staff_id = $_POST['staff_id'];
  $data = array("last_name"=>$last_name,"first_name"=>$first_name,
                "email"=>$email,"role"=>$role,
                "is_form_master"=>$is_form_master,
                "phone"=>$new_phone,"dob"=>$dob,
                "picture"=>$generated_name_1);
  $success = $profile->update($data,$staff_id);
  if ($success) {
    $class_list = $profile->get_class_list();
$houses = $setup->get_houses();
$all_staff = $profile->get_all_staff();
$subject_list = $profile->get_subject_list();
$load_classes = $profile->get_subjects($_SESSION['staff_id']);
$staff_form_class = $profile->get_form_class($_SESSION['staff_id']);
$roles = $profile->get_all_roles();

//refined
$all_classes = $profile->get_all_classes();
    echo 2;
  }else{
    echo 3;
  }

}


if (isset($_POST['add_staff'])) {
  $last_name = $_POST['new_last_name'];
  $first_name = $_POST['new_first_name'];
  $email = $_POST['new_email'];
  $role = $_POST['new_role'];
  $is_form_master = $_POST['new_is_form_master'];
  $sex = $_POST['new_sex'];
  $phone = $_POST['new_phone'];
  $phoneArray = "";
  $countPhones = 0;
  $phoneArray1 = "";
  $new_phone = "";
if($phone  != "" )  {
  $phoneArray = explode(",",$phone);
  $countPhones = count($phoneArray);
  $phoneArray1 = array_slice($phoneArray, 0,18);
  }
foreach ($phoneArray1 as $key => $value) {
  $new_phone = $value;
}
  $dob = $_POST['new_dob'];
  $id = implode('',str_split(rand(10000,99999),3));
  $generated_name_1 = '';
  if ($_FILES['new_pic']) {
    
      $ext_1 = explode('.', basename($_FILES["new_pic"]["name"])); //explode file name from dot(.) 
                $file_extension_1 = end($ext_1); //store extensions in the variable
                $generated_name_1 .= md5(uniqid()) . "." . $ext_1[count($ext_1) - 1];
                move_uploaded_file($_FILES["new_pic"]["tmp_name"], "../userImage/".$generated_name_1);
                // $data = array("picture"=>$generated_name_1);
                // $profile->update($data,$_SESSION['staff_id']);
  }

  $data = array("last_name"=>$last_name,"first_name"=>$first_name,
                "email"=>$email,"role"=>$role,
                "is_form_master"=>$is_form_master,"sex"=>$sex,
                "phone"=>$new_phone,"dob"=>$dob,
                "picture"=>$generated_name_1,
                "staff_id"=>$id
              );
  $role_data = $profile->get_role($role);
  $role_name = $role_data[0]['role_name'];

  $login_details = array(
    "staff_id"=>$id,
    "role"=>$role_name,
    "password"=>md5($id)
  );
  $profile->create_login($login_details);
  $success = $profile->insert($data);
  if ($success) {
    echo 0;
  }else{
    echo 1;
  }

}

if (isset($_POST['assign_sub_class'])) {
  $subjects = $_POST['subjects'];
  $class_id = $_POST['class_id'];
  $profile->clear_assigned_classes($class_id);
  foreach ($subjects as $key => $value) {
    $data = array("class_id"=>$class_id,
                  "subject_id"=>$value);
    $success = $profile->assign_class($data);
  }
  if ($success) {
   echo 0;
  }else{
    echo 1;
  }
}

if (isset($_POST['update_form'])) {
  $form_id = $_POST['form_id'];
  $staff_id = $_POST['staff_id'];
  $data = array('is_form_master'=>$form_id);
  $success = $profile->isFormMasterFor($data,$staff_id);
  if ($success) {
    echo 0;
  }else{
    echo 1;
  }
}


$class_list = $profile->get_class_list();
$houses = $profile->get_houses();
$houses = $setup->get_houses();
$all_staff = $profile->get_all_staff();
$subject_list = $profile->get_subject_list();
$load_classes = $profile->get_subjects($_SESSION['staff_id']);
$load_classes_admin = $profile->get_subjects_admin();
$staff_form_class = $profile->get_form_class($_SESSION['staff_id']);
$roles = $profile->get_all_roles();
$forms = $profile->get_forms();

if (isset($_GET['hid'])) {
  $house_id = $_GET['hid'];
  $student_houses = $profile->get_student_houses($house_id);
  
}

//refined
$all_classes = $profile->get_all_classes();
 ?>