<commit_before><?php
require dirname(__DIR__) . "/connection.php";
require dirname(__DIR__) . "/language.php";
require dirname(__DIR__) . "/utilities.php";
$x = preg_replace("/[^A-Za-z0-9]/", '', $_GET['projectid']);

session_start();
$userID = $_SESSION["userid"] or die("Session died");
$x = preg_replace("/[^A-Za-z0-9]/", '', $_GET['projectid']);
$result = $conn->query("SELECT activity, userID FROM dynamicprojectslogs WHERE projectID = '$x' AND
    ((activity = 'VIEWED' AND userid = $userID) OR ((activity = 'CREATED' OR activity = 'EDITED') AND userID != $userID)) ORDER BY logTime DESC LIMIT 1"); //changes here have to be synced with dynamicProjects.php
if (($row = $result->fetch_assoc()) && $row['activity'] != 'VIEWED') {
    $conn->query("INSERT INTO dynamicprojectslogs (projectid, activity, userID) VALUES ('$x', 'VIEWED', $userID)");
}
$showMissingBookings = true;
$missingBookingsArray = array();

//copied from dynamicProjects (when something changes there, update it here too)
//see open tasks user is part of
$result = $conn->query("SELECT d.projectid, projectname, projectdescription, projectcolor, projectstart, projectend, projectseries, projectstatus, projectpriority, projectowner, projectleader,
    projectpercentage, projecttags, d.companyid, d.clientid, d.clientprojectid, companyData.name AS companyName, clientData.name AS clientName, projectData.name AS projectDataName, needsreview, estimatedHours
    FROM dynamicprojects d LEFT JOIN companyData ON companyData.id = d.companyid LEFT JOIN clientData ON clientData.id = clientid LEFT JOIN projectData ON projectData.id = clientprojectid
    LEFT JOIN dynamicprojectsemployees ON dynamicprojectsemployees.projectid = d.projectid
    LEFT JOIN dynamicprojectsteams ON dynamicprojectsteams.projectid = d.projectid LEFT JOIN teamRelationshipData ON teamRelationshipData.teamID = dynamicprojectsteams.teamid
    WHERE (dynamicprojectsemployees.userid = $userID OR d.projectowner = $userID OR (teamRelationshipData.userID = $userID AND teamRelationshipData.skill >= d.level))
    AND d.projectstart <= UTC_TIMESTAMP and d.projectid = '$x'");
$row = $result->fetch_assoc();
$stmt_booking = $conn->prepare("SELECT userID, p.id FROM projectBookingData p, logs WHERE p.timestampID = logs.indexIM AND `end` = '0000-00-00 00:00:00' AND dynamicID = ?");
$stmt_booking->bind_param('s', $x);
$stmt_booking->execute();
$isInUse = $stmt_booking->get_result(); //max 1 row
$useRow = $isInUse->fetch_assoc();

if($useRow){
    $showMissingBookings = false;
}

if($showMissingBookings){
    echo $conn->error;
    $occupation = array('bookingID' => $useRow['id'], 'dynamicID' => $x, 'companyid' => $row['companyid'], 'clientid' => $row['clientid'], 'projectid' => $row['clientprojectid'], 'percentage' => $row['projectpercentage']);
    // /copied from dynamicProjects

    // from userProjecting:
    $result = mysqli_query($conn, "SELECT * FROM $configTable");
    if ($result && ($row = $result->fetch_assoc())) {
        $cd = $row['cooldownTimer'];
        $bookingTimeBuffer = $row['bookingTimeBuffer'];
    } else {
        $bookingTimeBuffer = 5;
    }

    //first of the day
    $result = mysqli_query($conn, "SELECT * FROM $logTable WHERE userID = $userID AND timeEnd = '0000-00-00 00:00:00'");
    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $start = substr(carryOverAdder_Hours($row['time'], $row['timeToUTC']), 11, 5);
        $end = substr(carryOverAdder_Hours(getCurrentTimestamp(), $row['timeToUTC']), 11, 5);
        $date = substr(carryOverAdder_Hours($row['time'], $row['timeToUTC']), 0, 10);
        $indexIM = $row['indexIM']; //this value must not change
        $timeToUTC = $row['timeToUTC']; //just in case.
        $missingBookingsArray[] = array("start" => $start, "end" => $end, "date" => $date, "indexIM" => $indexIM, "timeToUTC" => $timeToUTC);
    } else {
        echo "no valid timestamp found";
        $showMissingBookings = false;
    }
}

if ($showMissingBookings) {
    //last booking
    $result = $conn->query("SELECT *, $projectTable.name AS projectName, $projectBookingTable.id AS bookingTableID FROM $projectBookingTable
    LEFT JOIN $projectTable ON ($projectBookingTable.projectID = $projectTable.id)
    LEFT JOIN $clientTable ON ($projectTable.clientID = $clientTable.id)
    WHERE $projectBookingTable.timestampID = $indexIM ORDER BY end DESC LIMIT 1;");
    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $date = substr(carryOverAdder_Hours($row['end'], $timeToUTC), 0, 10);
        $start = substr(carryOverAdder_Hours($row['end'], $timeToUTC), 11, 5);
    }

    //addendums
    $request_addendum = false;
    $result = $conn->query("SELECT * FROM logs WHERE userID = $userID AND DATE('" . carryOverAdder_Hours($start, -182) . "') < time"); //192h = 8 days
    // echo "SELECT * FROM logs WHERE userID = $userID AND DATE('".carryOverAdder_Hours($start, -182)."') < time;";
    while ($result && ($row = $result->fetch_assoc())) {
        $has_bookings = false;
        $i = $row['indexIM'];
        $A = $row['time'];
        $res_b = $conn->query("SELECT * FROM projectBookingData WHERE timestampID = $i ORDER BY start ASC");
        while ($row_b = $res_b->fetch_assoc()) { //changes must be carried over to addendum page
            $has_bookings = true;
            $B = $row_b['start'];
            if (timeDiff_Hours($A, $B) > $bookingTimeBuffer / 60) {
                $request_addendum = $i;
                //to process the next ADD<commit_msg>remove debug output<commit_after><?php
require dirname(__DIR__) . "/connection.php";
require dirname(__DIR__) . "/language.php";
require dirname(__DIR__) . "/utilities.php";
$x = preg_replace("/[^A-Za-z0-9]/", '', $_GET['projectid']);

session_start();
$userID = $_SESSION["userid"] or die("Session died");
$x = preg_replace("/[^A-Za-z0-9]/", '', $_GET['projectid']);
$result = $conn->query("SELECT activity, userID FROM dynamicprojectslogs WHERE projectID = '$x' AND
    ((activity = 'VIEWED' AND userid = $userID) OR ((activity = 'CREATED' OR activity = 'EDITED') AND userID != $userID)) ORDER BY logTime DESC LIMIT 1"); //changes here have to be synced with dynamicProjects.php
if (($row = $result->fetch_assoc()) && $row['activity'] != 'VIEWED') {
    $conn->query("INSERT INTO dynamicprojectslogs (projectid, activity, userID) VALUES ('$x', 'VIEWED', $userID)");
}
$showMissingBookings = true;
$missingBookingsArray = array();

//copied from dynamicProjects (when something changes there, update it here too)
//see open tasks user is part of
$result = $conn->query("SELECT d.projectid, projectname, projectdescription, projectcolor, projectstart, projectend, projectseries, projectstatus, projectpriority, projectowner, projectleader,
    projectpercentage, projecttags, d.companyid, d.clientid, d.clientprojectid, companyData.name AS companyName, clientData.name AS clientName, projectData.name AS projectDataName, needsreview, estimatedHours
    FROM dynamicprojects d LEFT JOIN companyData ON companyData.id = d.companyid LEFT JOIN clientData ON clientData.id = clientid LEFT JOIN projectData ON projectData.id = clientprojectid
    LEFT JOIN dynamicprojectsemployees ON dynamicprojectsemployees.projectid = d.projectid
    LEFT JOIN dynamicprojectsteams ON dynamicprojectsteams.projectid = d.projectid LEFT JOIN teamRelationshipData ON teamRelationshipData.teamID = dynamicprojectsteams.teamid
    WHERE (dynamicprojectsemployees.userid = $userID OR d.projectowner = $userID OR (teamRelationshipData.userID = $userID AND teamRelationshipData.skill >= d.level))
    AND d.projectstart <= UTC_TIMESTAMP and d.projectid = '$x'");
$row = $result->fetch_assoc();
$stmt_booking = $conn->prepare("SELECT userID, p.id FROM projectBookingData p, logs WHERE p.timestampID = logs.indexIM AND `end` = '0000-00-00 00:00:00' AND dynamicID = ?");
$stmt_booking->bind_param('s', $x);
$stmt_booking->execute();
$isInUse = $stmt_booking->get_result(); //max 1 row
$useRow = $isInUse->fetch_assoc();

if($useRow){
    $showMissingBookings = false;
}

if($showMissingBookings){
    echo $conn->error;
    $occupation = array('bookingID' => $useRow['id'], 'dynamicID' => $x, 'companyid' => $row['companyid'], 'clientid' => $row['clientid'], 'projectid' => $row['clientprojectid'], 'percentage' => $row['projectpercentage']);
    // /copied from dynamicProjects

    // from userProjecting:
    $result = mysqli_query($conn, "SELECT * FROM $configTable");
    if ($result && ($row = $result->fetch_assoc())) {
        $cd = $row['cooldownTimer'];
        $bookingTimeBuffer = $row['bookingTimeBuffer'];
    } else {
        $bookingTimeBuffer = 5;
    }

    //first of the day
    $result = mysqli_query($conn, "SELECT * FROM $logTable WHERE userID = $userID AND timeEnd = '0000-00-00 00:00:00'");
    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $start = substr(carryOverAdder_Hours($row['time'], $row['timeToUTC']), 11, 5);
        $end = substr(carryOverAdder_Hours(getCurrentTimestamp(), $row['timeToUTC']), 11, 5);
        $date = substr(carryOverAdder_Hours($row['time'], $row['timeToUTC']), 0, 10);
        $indexIM = $row['indexIM']; //this value must not change
        $timeToUTC = $row['timeToUTC']; //just in case.
        $missingBookingsArray[] = array("start" => $start, "end" => $end, "date" => $date, "indexIM" => $indexIM, "timeToUTC" => $timeToUTC);
    } else {
        // echo "no valid timestamp found";
        $showMissingBookings = false;
    }
}

if ($showMissingBookings) {
    //last booking
    $result = $conn->query("SELECT *, $projectTable.name AS projectName, $projectBookingTable.id AS bookingTableID FROM $projectBookingTable
    LEFT JOIN $projectTable ON ($projectBookingTable.projectID = $projectTable.id)
    LEFT JOIN $clientTable ON ($projectTable.clientID = $clientTable.id)
    WHERE $projectBookingTable.timestampID = $indexIM ORDER BY end DESC LIMIT 1;");
    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $date = substr(carryOverAdder_Hours($row['end'], $timeToUTC), 0, 10);
        $start = substr(carryOverAdder_Hours($row['end'], $timeToUTC), 11, 5);
    }

    //addendums
    $request_addendum = false;
    $result = $conn->query("SELECT * FROM logs WHERE userID = $userID AND DATE('" . carryOverAdder_Hours($start, -182) . "') < time"); //192h = 8 days
    // echo "SELECT * FROM logs WHERE userID = $userID AND DATE('".carryOverAdder_Hours($start, -182)."') < time;";
    while ($result && ($row = $result->fetch_assoc())) {
        $has_bookings = false;
        $i = $row['indexIM'];
        $A = $row['time'];
        $res_b = $conn->query("SELECT * FROM projectBookingData WHERE timestampID = $i ORDER BY start ASC");
        while ($row_b = $res_b->fetch_assoc()) { //changes must be carried over to addendum page
            $has_bookings = true;
            $B = $row_b['start'];
            if (timeDiff_Hours($A, $B) > $bookingTimeBuffer / 60) {
                $request_addendum = $i;
                //to process the next ADD
{'fraction_non_alphanumeric': 0.15768157681576817, 'fraction_numerical': 0.011700117001170011, 'mean_word_length': 4.182835820895522, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 75, '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': '2212888', 'n_tokens_mistral': 3646, 'n_tokens_neox': 3487, 'n_words': 897}