Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented deletion of notices at expiry date #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@
$stmt->execute();
}

public function addNotice($title, $date, $content, $operator)
public function addNotice($title, $date, $content, $expiry, $operator)
{
$stmt = $this->conn->prepare(
"INSERT INTO " . self::TABLE_NOTICES . " (date, title, message) VALUES (:date, :title, :message)"
"INSERT INTO " . self::TABLE_NOTICES . " (date, title, message, expiry) VALUES (:date, :title, :message, :expiry)"

Check warning on line 132 in resources/lib/UnitySQL.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

resources/lib/UnitySQL.php#L132

Line exceeds 120 characters; contains 126 characters (Generic.Files.LineLength.TooLong)
);
$stmt->bindParam(":date", $date);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":message", $content);
$stmt->bindParam(":expiry", $expiry);

$stmt->execute();

Expand All @@ -147,14 +148,15 @@
);
}

public function editNotice($id, $title, $date, $content)
public function editNotice($id, $title, $date, $content, $expiry)
{
$stmt = $this->conn->prepare(
"UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message WHERE id=:id"
"UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message, expiry=:expiry WHERE id=:id"

Check warning on line 154 in resources/lib/UnitySQL.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

resources/lib/UnitySQL.php#L154

Line exceeds 120 characters; contains 124 characters (Generic.Files.LineLength.TooLong)
);
$stmt->bindParam(":date", $date);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":message", $content);
$stmt->bindParam(":expiry", $expiry);
$stmt->bindParam(":id", $id);

$stmt->execute();
Expand Down
9 changes: 5 additions & 4 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,17 @@ CREATE TABLE `notices` (
`id` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT current_timestamp(),
`title` varchar(300) NOT NULL,
`message` longtext NOT NULL
`message` longtext NOT NULL,
`expiry` timestamp NOT NULL DEFAULT DATE_ADD(current_timestamp(), INTERVAL 14 DAY)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `notices`
--

INSERT INTO `notices` (`id`, `date`, `title`, `message`) VALUES
(9, '2022-09-19 15:49:10', 'Example Notice 1', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>'),
(10, '2022-09-14 11:48:39', 'Example Notice 2', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>');
INSERT INTO `notices` (`id`, `date`, `title`, `message`, `expiry`) VALUES
(9, '2022-09-19 15:49:10', 'Example Notice 1', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>', '2025-05-29 01:02:03'),
(10, '2024-03-02 00:00:00', 'Example Notice 2', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>', '2024-03-02 00:00:00');

-- --------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions tools/docker-dev/unity-web-portal
Submodule unity-web-portal added at 7087b7
4 changes: 2 additions & 2 deletions webroot/admin/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
if ($_SERVER["REQUEST_METHOD"] == "POST") {
switch ($_POST["form_type"]) {
case "newNotice":
$SQL->addNotice($_POST["title"], $_POST["date"], $_POST["content"], $USER);
$SQL->addNotice($_POST["title"], $_POST["date"], $_POST["content"], $_POST["expiry"], $USER);

break;
case "editNotice":
$SQL->editNotice($_POST["id"], $_POST["title"], $_POST["date"], $_POST["content"]);
$SQL->editNotice($_POST["id"], $_POST["title"], $_POST["date"], $_POST["content"], $_POST["expiry"]);

break;
case "delNotice":
Expand Down
4 changes: 4 additions & 0 deletions webroot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

$notices = $SQL->getNotices();
foreach ($notices as $notice) {
if ($notice["expiry"] < date('Y-m-d')) {
$SQL->deleteNotice($notice["id"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing the deletion here causes a race condition if two page loads happen at the same time and so try to delete the same items. This will cause it to throw an exception.

continue;
}
echo "<div class='notice'>";
echo "<span class='noticeTitle'>" . $notice["title"] . "</span>";
echo "<span class='noticeDate'>" . date('m-d-Y', strtotime($notice["date"])) . "</span>";
Expand Down
Loading