<commit_before><?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/global_config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/database.php';
use Entrenos\Athlete;
use Entrenos\Goal;
use Entrenos\Tag;

session_start();
if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    # Filling data with what it comes from the request
    $form_data = $_REQUEST; 
    // Normal redirection will go to charts.php
    $redirection = "Location: " . $base_url . "/charts.php";
    try {
        switch($form_data['action']) {
            case "personal_data": 
                //Checking if weight decimal separator is comma and changing it if applies
                $fields2check = array('weight','body_fat','body_water','body_muscle');
                foreach ($fields2check as $loop_field) {
                    if (isset($form_data[$loop_field]) and !empty($form_data[$loop_field])) {
                        $form_data[$loop_field] = str_replace(",", ".", $form_data[$loop_field]);
                    } else {
                        $form_data[$loop_field] = 0;
                    }
                }
                $athlete = new Athlete($user_id); 
                $athlete->insert($form_data, $conn);
                $log->info("Updated athlete data for user " . $user_id . ": " . implode("|", $form_data));
                break;
            case "goal":
                if (!isset($form_data['report_enabled']))
                    $form_data['report_enabled'] = 0;
                $goal = new Goal($form_data);
                try {
                    $goal->insert($conn);
                    $log->info("Updated goal data " . $goal->id . " for user " . $user_id . ": " . implode("|", $form_data));
                    //copying report generator file to final destination
                    if ($form_data['report_enabled']) {
                        $source_goal_report_path = $base_path . "/../reports/objetivo.php";
                        $target_goal_report_path = $base_path . "/" . $goal->report_url;
                        if (!copy($source_goal_report_path, $target_goal_report_path)) {
                            throw new Exception("Error when copying goal report: " . json_encode(error_get_last()) . " | Goal: " . json_encode($goal));
                        } else {
                            $log->info("Goal reporting script copied to " . $target_goal_report_path);
                        }
                    } else {
                        $log->info("Reporting not enabled for goal #" . $goal->id);
                    }
                } catch (Exception $e) {
                    $log->error($e->getMessage());
                }
                $redirection = "Location: " . $base_url . "/goals.php";
                break;
            case "tag":
                if (!isset($form_data['report_enabled']))
                    $form_data['report_enabled'] = 0;
                $tag = new Tag($form_data);
                try {
                    $tag->insert($conn);
                    $log->info("Updated tag data " . $tag->id . " for user " . $user_id . ": " . implode("|", $form_data));
                    //ToDo: copy report generator file to final destination<commit_msg>Enable goal report when related input field is checked - issue #10<commit_after><?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/global_config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/database.php';
use Entrenos\Athlete;
use Entrenos\Goal;
use Entrenos\Tag;

session_start();
if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    $log->debug("Request: " . json_encode($_REQUEST));
    # Filling data with what it comes from the request
    $form_data = $_REQUEST; 
    // Normal redirection will go to charts.php
    $redirection = "Location: " . $base_url . "/charts.php";
    try {
        switch($form_data['action']) {
            case "personal_data": 
                //Checking if weight decimal separator is comma and changing it if applies
                $fields2check = array('weight','body_fat','body_water','body_muscle');
                foreach ($fields2check as $loop_field) {
                    if (isset($form_data[$loop_field]) and !empty($form_data[$loop_field])) {
                        $form_data[$loop_field] = str_replace(",", ".", $form_data[$loop_field]);
                    } else {
                        $form_data[$loop_field] = 0;
                    }
                }
                $athlete = new Athlete($user_id); 
                $athlete->insert($form_data, $conn);
                $log->info("Updated athlete data for user " . $user_id . ": " . implode("|", $form_data));
                break;
            case "goal":
                if (!isset($form_data['report_enabled'])) {
                    $form_data['report_enabled'] = 0;
                } else {
                    $form_data['report_enabled'] = 1;
                }
                $goal = new Goal($form_data);
                try {
                    $goal->insert($conn);
                    $log->info("Updated goal data " . $goal->id . " for user " . $user_id . ": " . implode("|", $form_data));
                    //copying report generator file to final destination
                    if ($form_data['report_enabled']) {
                        $source_goal_report_path = $base_path . "/../reports/objetivo.php";
                        $target_goal_report_path = $base_path . "/" . $goal->report_url;
                        if (!copy($source_goal_report_path, $target_goal_report_path)) {
                            throw new Exception("Error when copying goal report: " . json_encode(error_get_last()) . " | Goal: " . json_encode($goal));
                        } else {
                            $log->info("Goal reporting script copied to " . $target_goal_report_path);
                        }
                    } else {
                        $log->info("Reporting not enabled for goal #" . $goal->id);
                    }
                } catch (Exception $e) {
                    $log->error($e->getMessage());
                }
                $redirection = "Location: " . $base_url . "/goals.php";
                break;
            case "tag":
                if (!isset($form_data['report_enabled']))
                    $form_data['report_enabled'] = 0;
                $tag = new Tag($form_data);
                try {
                    $tag->insert($conn);
                    $log->info("Updated tag data " . $tag->id . " for user " . $user_id . ": " . implode("|", $form_data));
                    //ToDo: copy report generator file to final destination
{'fraction_non_alphanumeric': 0.13951408555671485, 'fraction_numerical': 0.0019376956327321509, 'mean_word_length': 1.5857418111753372, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2461644', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1760, 'n_words': 382}