Skip to content

Commit

Permalink
now can CRUD all the things.
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsalisbury committed Dec 4, 2023
1 parent debd412 commit 7a08e7e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 18 deletions.
Binary file modified db.sqlite
Binary file not shown.
37 changes: 37 additions & 0 deletions public/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ function do_layout($file) {
require_once 'layouts/footer.php';
}

function get_all_users() {
global $db;
$results = $db->query('SELECT * FROM users');
$users = [];
while ($row = $results->fetchArray()) {
$users[] = $row;
}
return $users;
}

function get_all_crops() {
global $db;
$results = $db->query('SELECT * FROM crops');
Expand Down Expand Up @@ -127,6 +137,13 @@ function get_crop_name($id) {
return $row['crop'];
}

function get_deadline($id) {
global $db;
$results = $db->query('SELECT * FROM crops_states_deadlines WHERE id = ' . $id);
$row = $results->fetchArray();
return $row;
}

function get_all_deadlines() {
global $db;
$results = $db->query('SELECT * FROM crops_states_deadlines');
Expand All @@ -139,6 +156,26 @@ function get_all_deadlines() {
return $deadlines;
}

function get_all_reminders() {
global $db;
$sql = 'SELECT * FROM deadlines_reminders';
// left join on deadlines table and crop and state so we can have the name of the deadline, crop, and state
$sql = 'SELECT deadlines_reminders.id, deadlines_reminders.deadline_id, deadlines_reminders.reminder_send_time, crops_states_deadlines.deadline_name, crops_states_deadlines.deadline, crops_states_deadlines.state_id, crops_states_deadlines.crop_id, states.state, crops.crop FROM deadlines_reminders LEFT JOIN crops_states_deadlines ON deadlines_reminders.deadline_id = crops_states_deadlines.id LEFT JOIN states ON crops_states_deadlines.state_id = states.id LEFT JOIN crops ON crops_states_deadlines.crop_id = crops.id';
$results = $db->query($sql);
$reminders = [];
while ($row = $results->fetchArray()) {
$row['deadline_id'] = get_deadline($row['deadline_id'])['deadline_name'];
$row['reminder_send_time'] = $row['reminder_send_time'];
$row['deadline_name'] = $row['deadline_name'];
$row['deadline'] = $row['deadline'];
$row['state'] = $row['state'];


$reminders[] = $row;
}
return $reminders;
}

function get_logged_in_user() {
global $db;
$user_id = $_SESSION['user_id'];
Expand Down
7 changes: 6 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
default:
// set http response code to 404
http_response_code(404);
echo '404';

// the route attempted was: $_SERVER['REQUEST_URI'] and it was not found

// show the 404 page
echo "404. " . $_SERVER['REQUEST_URI'] . " not found";

break;
}
52 changes: 39 additions & 13 deletions public/pages/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

<div class="accordion-item">
<h2 class="accordion-header" id="cropsHeading">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#cropsCollapse" aria-expanded="true" aria-controls="cropsCollapse">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#cropsCollapse" aria-expanded="false" aria-controls="cropsCollapse">
Crops
</button>
</h2>
<div id="cropsCollapse" class="accordion-collapse collapse show" aria-labelledby="cropsHeading" data-bs-parent="#accordionSections">
<div id="cropsCollapse" class="accordion-collapse collapse" aria-labelledby="cropsHeading" data-bs-parent="#accordionSections">
<div class="accordion-body">
<?php
$crops = get_all_crops();
Expand Down Expand Up @@ -44,13 +44,6 @@
<?php endforeach; ?>
</tbody>
</table>
<form action="/crops" method="post">
<div class="mb-3">
<label for="crop">Crops</label>
<input type="text" name="crop" id="crop">
<button type="submit">Add Crop</button>
</div>
</form>
</div>
</div>
</div>
Expand Down Expand Up @@ -94,7 +87,9 @@
foreach ($states as $state) {
echo '<tr>';
echo '<td>' . $state['state'] . '</td>';
echo '<td><a href="/states/delete">[ x ]</a></td>';
echo '<td>';
echo '<a href="/states/delete/' . $state['id'] . '" class="btn btn-danger">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
Expand Down Expand Up @@ -171,12 +166,43 @@
}
?>
</table>



</div>
</div>
</div>
<!-- users -->
<div class="accordion-item">
<h2 class="accordion-header" id="usersHeading">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#usersCollapse" aria-expanded="false" aria-controls="usersCollapse">
Users
</button>
</h2>
<div id="usersCollapse" class="accordion-collapse collapse" aria-labelledby="usersHeading" data-bs-parent="#accordionSections">
<div class="accordion-body">
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$users = get_all_users();
foreach ($users as $user) {
echo '<tr>';
echo '<td>' . $user['firstname'] . '</td>';
echo '<td>' . $user['lastname'] . '</td>';
echo '<td>' . $user['email'] . '</td>';
echo '<td><a href="/users/delete/' . $user['id'] . '" class="btn btn-danger">Delete</a></td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
18 changes: 14 additions & 4 deletions seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@
/** create deadlines_reminders table */
$db->exec('CREATE TABLE IF NOT EXISTS deadlines_reminders (
id INTEGER PRIMARY KEY,
crop_id INTEGER NOT NULL,
state_id INTEGER NOT NULL,
deadline_id INTEGER NOT NULL,
reminder_time DATETIME NOT NULL,
reminder_send_time DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)');

Expand Down Expand Up @@ -121,4 +119,16 @@

$db->exec('INSERT INTO crops_states_deadlines (crop_id, state_id, deadline_name, deadline) VALUES (1, 1, "sales_closing", "2023-12-01")');
$db->exec('INSERT INTO crops_states_deadlines (crop_id, state_id, deadline_name, deadline) VALUES (1, 1, "acreage_reporting", "2023-12-01")');
$db->exec('INSERT INTO crops_states_deadlines (crop_id, state_id, deadline_name, deadline) VALUES (1, 1, "production_reporting", "2023-12-01")');
$db->exec('INSERT INTO crops_states_deadlines (crop_id, state_id, deadline_name, deadline) VALUES (1, 1, "production_reporting", "2023-12-01")');

/** create a reminder for deadline #1, scheduled for 1 day earlier than the deadline value, calculated from the deadline value */
/** get deadline #1 */
$results = $db->query('SELECT * FROM crops_states_deadlines WHERE id = 1');
$deadline = $results->fetchArray();

$reminder_send_time = date('Y-m-d H:i:s', strtotime($deadline['deadline'] . ' - 1 day'));

/** insert reminder */
$db->exec('INSERT INTO deadlines_reminders (deadline_id, reminder_send_time) VALUES (1, "' . $reminder_send_time . '")');


0 comments on commit 7a08e7e

Please sign in to comment.