<?php
chdir('../../');
require_once 'autoload.php';

$programmes = new programmes_controller;

require_once 'simplexlsx/vendor/autoload.php';

use Shuchkin\SimpleXLSX;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['csrf_token']) && isset($_POST['csrf_form_id']) && $waf->csrf_validate($_POST['csrf_form_id'], $_POST['csrf_token'], $utility->action_name())) {

$file = $_FILES['file']['tmp_name'];
$current_record = 0;

if ( $xlsx = SimpleXLSX::parse($file) ) {

foreach( $xlsx->rows() as $row ) {

if ($current_record !== 0) {

$data['name'] = $utility->sanitize(ucwords($row[0]));
$data['status'] = $utility->sanitize($row[1]);

if (empty($programmes->check($data['name']))) {

$programmes->store($data);

}
}
$current_record++;
}

$utility->setMessage("success", 'Data import completed');

header("location:../programmes");

} else {

echo SimpleXLSX::parseError();

}
}else{

header("location:../programmes");

}
?>
