Skip to content

Commit

Permalink
form working
Browse files Browse the repository at this point in the history
  • Loading branch information
Leone25 committed Jul 4, 2024
1 parent 63a8363 commit e66de64
Show file tree
Hide file tree
Showing 10 changed files with 715 additions and 292 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"phpmailer/phpmailer": "^6.1",
"laminas/laminas-diactoros": "^2.2",
"laminas/laminas-httphandlerrunner": "^1.1",
"sabre/vobject": "^4.0"
"sabre/vobject": "^4.0",
"michelf/php-markdown": "^2.0"
},
"suggest": {
"ext-apcu": "Caches LDAP results avoiding continuous lookups"
Expand Down
58 changes: 57 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

324 changes: 275 additions & 49 deletions database/database.sql

Large diffs are not rendered by default.

326 changes: 277 additions & 49 deletions database/update/1.sql

Large diffs are not rendered by default.

95 changes: 59 additions & 36 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,35 +873,66 @@ public function getAllAssignedInterviewsForTable(): array
*
* @return array Array of associative arrays with id, availability, printable name and description
*/
public function getPositions($lang)
public function getPositions($lang = null)
{
if ($lang) {
$stmt = $this->db->prepare('SELECT
p.id AS id,
$stmt = $this->db->prepare("SELECT
p.id,
p.available,
MAX(CASE
WHEN t.id LIKE 'position.' || p.id || '.name' THEN t.value
ELSE NULL
END) AS name,
MAX(CASE
WHEN t.id LIKE 'position.' || p.id || '.description' THEN t.value
ELSE NULL
END) AS description
t_name.value AS name,
t_desc.value AS description
FROM
positions p
LEFT JOIN
translations t
ON
t.id LIKE 'position.' || p.id || '.%'
GROUP BY
p.id, p.available;');
translations t_name ON t_name.id = 'position.' || p.id || '.name' AND t_name.lang = :lang
LEFT JOIN
translations t_desc ON t_desc.id = 'position.' || p.id || '.description' AND t_desc.lang = :lang");
$stmt->bindValue(':lang', $lang, SQLITE3_TEXT);
} else {
$stmt = $this->db->prepare('SELECT id, availability FROM positions');
$stmt = $this->db->prepare('SELECT id, available FROM positions');
}
$result = $stmt->execute();

return $result->fetchArray(SQLITE3_ASSOC);
$positions = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$positions[] = $row;
}
return $positions;
}

/**
* Get available positions
* @param string $lang Language (optional), if not set, won't provide name or description
*
* @return array Array of associative arrays with id, availability, printable name and description
*/
public function getAvailablePositions($lang = null)
{
if ($lang) {
$stmt = $this->db->prepare("SELECT
p.id,
p.available,
t_name.value AS name,
t_desc.value AS description
FROM
positions p
LEFT JOIN
translations t_name ON t_name.id = 'position.' || p.id || '.name' AND t_name.lang = :lang
LEFT JOIN
translations t_desc ON t_desc.id = 'position.' || p.id || '.description' AND t_desc.lang = :lang
WHERE
p.available = 1");
$stmt->bindValue(':lang', $lang, SQLITE3_TEXT);
} else {
$stmt = $this->db->prepare('SELECT id, available FROM positions');
}
$result = $stmt->execute();

$positions = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$positions[] = $row;
}
return $positions;
}

/**
Expand All @@ -912,33 +943,25 @@ public function getPositions($lang)
*
* @return array Associative array with id, availability, printable name and description
*/
public function getPosition($id, $lang)
public function getPosition($id, $lang = null)
{
if ($lang) {
$stmt = $this->db->prepare('SELECT
p.id AS id,
$stmt = $this->db->prepare("SELECT
p.id,
p.available,
MAX(CASE
WHEN t.id LIKE 'position.' || p.id || '.name' THEN t.value
ELSE NULL
END) AS name,
MAX(CASE
WHEN t.id LIKE 'position.' || p.id || '.description' THEN t.value
ELSE NULL
END) AS description
t_name.value AS name,
t_desc.value AS description
FROM
positions p
LEFT JOIN
translations t
ON
t.id LIKE 'position.' || p.id || '.%'
translations t_name ON t_name.id = 'position.' || p.id || '.name' AND t_name.lang = :lang
LEFT JOIN
translations t_desc ON t_desc.id = 'position.' || p.id || '.description' AND t_desc.lang = :lang
WHERE
p.id = :id
GROUP BY
p.id, p.available;');
p.id = :id");
$stmt->bindValue(':lang', $lang, SQLITE3_TEXT);
} else {
$stmt = $this->db->prepare('SELECT id, availability FROM positions WHERE id = :id');
$stmt = $this->db->prepare('SELECT id, available FROM positions WHERE id = :id');
}
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
$result = $stmt->execute();
Expand Down
33 changes: 22 additions & 11 deletions src/PageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$db = new Database();

$expiry = $db->getConfigValue('expiry');
$rolesAvailable = $db->getConfigValue('rolesAvailable');
$rolesAvailableCount = $rolesAvailable ? count(explode('|', $rolesAvailable)) : 0;
$positions = $db->getPositions(Template::getLocale() ?? 'en_US'); // [ ['id' => 1, 'name' => 'name', 'description' => 'description', 'available' => 1], ...]]

if ($rolesAvailableCount === 0) {
$expiry = 1;
if (count($positions) === 0) {
//$expiry = 1;
} else {
// check that there is at least one position available
$isAtLeastOneAvailable = false;
for ($i = 0; $i < count($positions); $i++) {
if ($positions[$i]['available'] == 1) {
$isAtLeastOneAvailable = true;
break;
}
}
if (!$isAtLeastOneAvailable) {
$expiry = 1;
}
}

// Get from DB -> if "unixtime.now >= expiry date" then candidate_close : else show the form
Expand All @@ -41,7 +52,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
];
foreach ($checkboxes as $attr) {
if (!isset($POST[$attr]) || $POST[$attr] !== 'true') {
return new HtmlResponse($template->render('form', ['error' => 'consent', 'rolesAvailable' => $rolesAvailable]), 400);
return new HtmlResponse($template->render('form', ['error' => 'consent', 'positions' => $positions]), 400);
}
}

Expand All @@ -62,23 +73,23 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$user->$attr = trim($user->$attr);
}
} else {
return new HtmlResponse($template->render('form', ['error' => 'form', 'rolesAvailable' => $rolesAvailable]), 400);
return new HtmlResponse($template->render('form', ['error' => 'form', 'positions' => $positions]), 400);
}
}
$user->submitted = time();
$user->matricola = strtolower($user->matricola);
if (preg_match('#^[sd]\d+$#', $user->matricola) !== 1) {
return new HtmlResponse($template->render('form', ['error' => 'form', 'rolesAvailable' => $rolesAvailable]), 400);
return new HtmlResponse($template->render('form', ['error' => 'form', 'positions' => $positions]), 400);
}

try {
list($id, $token) = $db->addUser($user);
} catch (DuplicateUserException $e) {
return new HtmlResponse($template->render('form', ['error' => 'duplicate', 'rolesAvailable' => $rolesAvailable]), 400);
return new HtmlResponse($template->render('form', ['error' => 'duplicate', 'positions' => $positions]), 400);
} catch (DatabaseException $e) {
return new HtmlResponse($template->render('form', ['error' => 'database', 'rolesAvailable' => $rolesAvailable]), 500);
return new HtmlResponse($template->render('form', ['error' => 'database', 'positions' => $positions]), 500);
} catch (Exception $e) {
return new HtmlResponse($template->render('form', ['error' => 'wtf', 'rolesAvailable' => $rolesAvailable]), 500);
return new HtmlResponse($template->render('form', ['error' => 'wtf', 'positions' => $positions]), 500);
}

$query = http_build_query(['id' => $id, 'token' => $token]);
Expand All @@ -105,6 +116,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
return new RedirectResponse("/status.php?$query", 303);
}

return new HtmlResponse($template->render('form', ['rolesAvailable' => $rolesAvailable]));
return new HtmlResponse($template->render('form', ['positions' => $positions]));
}
}
6 changes: 4 additions & 2 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public static function create(UriInterface $uri): Engine
*
* @return string
*/
private static function getLocale(): string
static function getLocale(): string
{
// Must be here, or $_SESSION is not available
session_start();
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['locale'])) {
return $_SESSION['locale'];
}
Expand Down
Loading

0 comments on commit e66de64

Please sign in to comment.