Files
cwsvJudo/homepage/participo/attributer.php
2025-12-07 20:40:33 +01:00

303 lines
9.3 KiB
PHP

<?php
/** @var array $CONFIG basic configurations (defined via bootstraping) */
/** @var array $SECRETS passwords and other stuff worth of protection (defined via bootstraping) */
require_once "bootstrap.php";
// libraries
require_once "participoLib/participo.php";
// init the participo framework
participo::init($CONFIG["cwsvJudo"], $SECRETS["cwsvJudo"]);
// this is an admin function: If user is not an admin redirect back to main
if (!participo::isUserAdmin()) {
header("Location: /participo", true, 301);
}
/// Ein Array als htmlTabelle darstellen
function array2htmlTableString($anArray)
{
$ret = "";
if (!is_array($anArray)) {
return "";
}
$ret .= "<table>";
foreach ($anArray as $row) {
if (!is_array($anArray)) {
continue;
}
$ret .= "<tr>";
foreach ($row as $entry) {
$ret .= "<td>{$entry}</td>";
}
$ret .= "</tr>";
}
$ret .= "</table>";
return $ret;
}
function arrayKeyed2htmlTableString($anArray, $keyList)
{
$ret = "";
if (!is_array($anArray)) {
return "";
}
$ret .= "<table>";
foreach ($anArray as $row) {
if (!is_array($anArray)) {
continue;
}
$ret .= "<tr>";
foreach ($keyList as $key) {
$ret .= "<td>" . $row[$key] . "</td>";
}
$ret .= "</tr>";
}
$ret .= "</table>";
return $ret;
}
/// einem User ein Attribut zuordnen
///
/// @param $aDbConnection PDO-Datenbankverbindung, die benutzt werden soll
/// @param $anUserId ID des Users, der das Attribut erhalten soll
/// @param $anAttributeId ID des zu vergebenden Attributes
///
/// - Es erfolgt keine Kontrolle, ob:
/// - die AttributId überhaupt existiert,
/// - ob die UserId überhaupt existiert,
/// - ob der User das Attribut bereits hat,
function giveUserAnUserAttributeById($anUserId, $anAttributeId)
{
withdrawUsersAttribute($anUserId, $anAttributeId);
try {
$queryString =
"INSERT INTO `wkParticipo_user<=>userAttributes` (userId, attributeId) VALUES (:userId, :attributeId);";
$bindArray = [
":userId" => [
"value" => $anUserId,
"data_type" => PDO::PARAM_INT,
],
":attributeId" => [
"value" => $anAttributeId,
"data_type" => PDO::PARAM_INT,
],
];
dbConnector::query($queryString, $bindArray);
} catch (PDOException $db_error) {
print "Error!: " .
$db_error->getMessage() .
"<br/>queryString: " .
$queryString .
"<br />";
var_dump($bindArray);
}
return;
}
/// einem User ein Attribut entziehen
function withdrawUsersAttribute($anUserId, $anAttributeId, $limit = null)
{
try {
// Variablen für das Binden an die Query vorbereiten
// inklusive Validierung der Werte
$bindArray = [];
$anUserId = filterId($anUserId);
if ($anUserId !== null) {
$bindArray[":userId"] = [
"value" => $anUserId,
"data_type" => PDO::PARAM_INT,
];
} else {
throw new InvalidArgumentException(
"withdrawUsersAttribute: userId must be positive integer!",
);
}
$anAttributeId = filterId($anAttributeId);
if ($anAttributeId !== null) {
$bindArray[":attributeId"] = [
"value" => $anAttributeId,
"data_type" => PDO::PARAM_INT,
];
} else {
throw new InvalidArgumentException(
"withdrawUsersAttribute: attributeid must be positive integer",
);
}
$limit = filterCount($limit);
if ($limit !== null) {
$bindArray[":limit"] = [
"value" => $limit,
"data_type" => PDO::PARAM_INT,
];
}
// Zusammenstellen der Query
$queryString =
"DELETE FROM `wkParticipo_user<=>userAttributes` " .
"WHERE `userId`=:userId " .
"AND `attributeId`=:attributeId" .
($limit !== null ? " LIMIT :limit" : "") .
";";
dbConnector::query($queryString, $bindArray);
} catch (PDOException $db_error) {
print "Error!: " .
$db_error->getMessage() .
"<br/>queryString: " .
($queryString ?? "<invalid>") .
"<br />";
var_dump($bindArray);
}
return;
} // Ende withdrawUsersAttribute
$actions = ["giveUserAnUserAttributeById", "withdrawUsersAttribute"];
if (isset($_GET["action"])) {
switch ($_GET["action"]) {
case "giveUserAnUserAttributeById":
try {
giveUserAnUserAttributeById(
intval($_GET["userId"]),
intval($_GET["attributeId"]),
);
} catch (Exception $e) {
print "UPS: " . $e->getMessage();
}
break;
case "withdrawUsersAttribute":
try {
withdrawUsersAttribute(
intval($_GET["userId"]),
intval($_GET["attributeId"]),
);
} catch (Exception $e) {
print "UPS: " . $e->getMessage();
}
break;
default:
echo "Ungültige Aktion (" . $_GET["action"] . ") erwünscht!";
break;
}
}
$users = dbConnector::query("SELECT * FROM wkParticipo_Users;");
$userAttributes = dbConnector::query(
"SELECT * FROM wkParticipo_userAttributes;",
);
?>
<html>
<head>
</head>
<body>
<form>
<label>Action:
<select name="action">
<option disabled selected value> -- Aktion auswählen -- </option>
<?php foreach ($actions as $action) {
echo "<option>{$action}</option>";
} ?>
</select>
</label>
<label>User:
<select name="userId">
<option disabled selected value> -- User auswählen -- </option>
<?php foreach ($users as $user) {
echo "<option value=\"" .
$user["id"] .
"\">" .
$user["loginName"] .
"</option>";
} ?>
</select>
</label>
<label>Attribut:
<select name="attributeId">
<option disabled selected value> -- Attribut auswählen -- </option>
<?php foreach ($userAttributes as $userAttribute) {
echo "<option value=\"" .
$userAttribute["id"] .
"\">" .
$userAttribute["name"] .
"</option>";
} ?>
</select>
</label>
<button type="submit">Eingaben absenden</button>
</form>
<h1>Attribute</h1>
<?php echo array2htmlTableString($userAttributes); ?>
<?php foreach ($userAttributes as $userAttribute) {
echo "<h2>" . $userAttribute["name"] . "</h2>";
// get the users with the attribute by a cross join
// remark: there are two id-columns, from which one gets lost (the one from the user) in the phpArray.
// so we (have to) use the userId from the attribute
// in short attributed => userId, unattributed => id (is the id of the user)
$attributedUsers = dbConnector::query(
"SELECT * FROM wkParticipo_Users, `wkParticipo_user<=>userAttributes` WHERE wkParticipo_Users.id = `wkParticipo_user<=>userAttributes`.userId AND `wkParticipo_user<=>userAttributes`.attributeId = :attributeId;",
[
":attributeId" => [
"value" => $userAttribute["id"],
"data_type" => PDO::PARAM_INT,
],
],
);
// Add a withthraw link entry
foreach ($attributedUsers as $index => $user) {
$attributedUsers[$index] += [
"withdrawLink" =>
"<a href=\"?action=withdrawUsersAttribute&userId=" .
$user["userId"] .
"&attributeId=" .
$userAttribute["id"] .
"\">withdraw " .
$userAttribute["name"] .
"</a>",
];
}
echo "<h3>have it</h3>" .
arrayKeyed2htmlTableString($attributedUsers, [
"userId",
"name",
"vorname",
"withdrawLink",
]);
$attributedKeyList = [];
foreach ($attributedUsers as $user) {
$attributedKeyList[] = $user["userId"];
}
$unattributedUsers = [];
foreach ($users as $user) {
if (!in_array($user["id"], $attributedKeyList)) {
$unattributedUsers[] = $user;
}
}
// Add a giveAttribute link entry to every user
foreach ($unattributedUsers as $index => $user) {
$unattributedUsers[$index] += [
"giveAttributeLink" =>
"<a href=\"?action=giveUserAnUserAttributeById&userId=" .
$user["id"] .
"&attributeId=" .
$userAttribute["id"] .
"\">give Attribute " .
$userAttribute["name"] .
"</a>",
];
}
echo "<h3>give it</h3>" .
arrayKeyed2htmlTableString($unattributedUsers, [
"id",
"name",
"vorname",
"giveAttributeLink",
]);
} ?>
<h1>User</h1>
<?php echo array2htmlTableString($users); ?>
</body>
</html>