<?php

wp_enqueue_script('orbis-autocomplete');
wp_enqueue_style('select2');
// Errors
global $orbis_errors;
// Inputs
$entry = orbis_timesheets_get_entry_from_input();
if (filter_has_var(INPUT_GET, 'date')) {
    $date_string = filter_input(INPUT_GET, 'date', FILTER_SANITIZE_STRING);
    $date = new DateTime($date_string);
    $entry->set_date($date);
}
?>
<div class="panel">
	<div class="content">
		<form action="" method="post">
			<?php 
wp_nonce_field('orbis_timesheets_add_new_registration', 'orbis_timesheets_new_registration_nonce');
?>

			<legend><?php 
_e('Add registration', 'orbis_timesheets');
?>
</legend>

			<?php 
include 'work_registration_fields.php';
?>

			<div class="form-actions">
function orbis_timesheets_maybe_add_entry()
{
    global $orbis_errors;
    // Add
    if (filter_has_var(INPUT_POST, 'orbis_timesheets_add_registration')) {
        $entry = orbis_timesheets_get_entry_from_input();
        // Verify nonce
        $nonce = filter_input(INPUT_POST, 'orbis_timesheets_new_registration_nonce', FILTER_SANITIZE_STRING);
        if (wp_verify_nonce($nonce, 'orbis_timesheets_add_new_registration')) {
            if (empty($entry->company_id) && empty($entry->project_id) && empty($entry->subscription_id)) {
                orbis_timesheets_register_error('orbis_registration_company_id', '');
                // __( 'You have to specify an company.', 'orbis_timesheets' ) );
                orbis_timesheets_register_error('orbis_registration_project_id', '');
                // __( 'You have to specify an project.', 'orbis_timesheets' ) );
                orbis_timesheets_register_error('orbis_registration_subscription_id', '');
                // __( 'You have to specify an subscription.', 'orbis_timesheets' ) );
                orbis_timesheets_register_error('orbis_registration_on', __('You have to specify an company or project.', 'orbis_timesheets'));
            }
            if (empty($entry->project_id)) {
                orbis_timesheets_register_error('orbis_registration_project_id', '');
                // __( 'You have to specify an project.', 'orbis_timesheets' ) );
                orbis_timesheets_register_error('orbis_registration_on', __('You have to specify an project.', 'orbis_timesheets'));
            }
            $required_word_count = 2;
            if (str_word_count($entry->description) < $required_word_count) {
                orbis_timesheets_register_error('orbis_registration_description', sprintf(__('You have to specify an description (%d words).', 'orbis_timesheets'), $required_word_count));
            }
            if (empty($entry->time)) {
                // $orbis_errors['orbis_registration_time'] = __( 'You have to specify an time.', 'orbis_timesheets' );
            }
            if (empty($entry->activity_id)) {
                orbis_timesheets_register_error('orbis_registration_activity_id', __('You have to specify an activity.', 'orbis_timesheets'));
            }
            if (!orbis_timesheets_can_register($entry->get_date()->format('U'))) {
                orbis_timesheets_register_error('orbis_registration_date', __('You can not register on this date.', 'orbis_timesheets'));
            }
            $message = empty($entry->id) ? 'added' : 'updated';
            if (empty($orbis_errors)) {
                $result = orbis_insert_timesheet_entry($entry);
                if ($result) {
                    $url = add_query_arg(array('entry_id' => false, 'action' => false, 'message' => $message, 'date' => $entry->get_date()->format('Y-m-d')));
                    wp_redirect($url);
                    exit;
                } else {
                    orbis_timesheets_register_error('orbis_registration_error', __('Could not add timesheet entry.', 'orbis_timesheets'));
                }
            }
        }
    }
}