Extend NodaGroup to allow adding new ones and linking vocabulary entries

to one
This commit is contained in:
Joshua Ramon Enslin 2023-12-12 04:29:23 +01:00
parent f3831965a3
commit 8a8f55b38c
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -44,6 +44,190 @@ final class NodaGroup {
} }
/**
* Adds a group.
*
* @param string $name Name of the group.
* @param string $comment Comment / note on the group.
*
* @return integer
*/
public function insert(string $name, string $comment = ''):int {
$insertStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `group`
(`group_name`, `comment`)
VALUES
(?, ?)");
$insertStmt->bind_param("ss", $name, $comment);
$insertStmt->execute();
$output = $insertStmt->get_insert_id();
$insertStmt->close();
return $output;
}
/**
* Adds actors to a nodac group.
*
* @param integer $group_id Group ID.
* @param array<integer> $input_ids List of entries to link to the group.
*
* @return void
*/
public function linkActors(int $group_id, array $input_ids):void {
if (empty($input_ids = MD_STD_IN::sanitize_id_array($input_ids))) return;
// Check which entries actually exist
$idsToLink = [];
$result = $this->_mysqli_noda->do_read_query("SELECT `persinst_id`
FROM `persinst`
WHERE `persinst_id` IN (" . $this->_mysqli_noda->escape_in($input_ids) . ")");
while ($cur = $result->fetch_row()) {
$idsToLink[] = (int)$cur[0];
}
$result->close();
$this->_mysqli_noda->autocommit(false);
$linkStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `v_group_persinst`
(`group_id`, `persinst_id`)
VALUES (?, ?)");
foreach ($idsToLink as $id) {
$linkStmt->bind_param("ii", $group_id, $id);
$linkStmt->execute();
}
$linkStmt->close();
$this->_mysqli_noda->commit();
$this->_mysqli_noda->autocommit(true);
}
/**
* Adds places to a nodac group.
*
* @param integer $group_id Group ID.
* @param array<integer> $input_ids List of entries to link to the group.
*
* @return void
*/
public function linkPlaces(int $group_id, array $input_ids):void {
if (empty($input_ids = MD_STD_IN::sanitize_id_array($input_ids))) return;
// Check which entries actually exist
$idsToLink = [];
$result = $this->_mysqli_noda->do_read_query("SELECT `ort_id`
FROM `orte`
WHERE `ort_id` IN (" . $this->_mysqli_noda->escape_in($input_ids) . ")");
while ($cur = $result->fetch_row()) {
$idsToLink[] = (int)$cur[0];
}
$result->close();
$this->_mysqli_noda->autocommit(false);
$linkStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `v_group_orte`
(`group_id`, `ort_id`)
VALUES (?, ?)");
foreach ($idsToLink as $id) {
$linkStmt->bind_param("ii", $group_id, $id);
$linkStmt->execute();
}
$linkStmt->close();
$this->_mysqli_noda->commit();
$this->_mysqli_noda->autocommit(true);
}
/**
* Adds times to a nodac group.
*
* @param integer $group_id Group ID.
* @param array<integer> $input_ids List of entries to link to the group.
*
* @return void
*/
public function linkTimes(int $group_id, array $input_ids):void {
if (empty($input_ids = MD_STD_IN::sanitize_id_array($input_ids))) return;
// Check which entries actually exist
$idsToLink = [];
$result = $this->_mysqli_noda->do_read_query("SELECT `zeit_id`
FROM `zeiten`
WHERE `zeit_id` IN (" . $this->_mysqli_noda->escape_in($input_ids) . ")");
while ($cur = $result->fetch_row()) {
$idsToLink[] = (int)$cur[0];
}
$result->close();
$this->_mysqli_noda->autocommit(false);
$linkStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `v_group_zeiten`
(`group_id`, `zeit_id`)
VALUES (?, ?)");
foreach ($idsToLink as $id) {
$linkStmt->bind_param("ii", $group_id, $id);
$linkStmt->execute();
}
$linkStmt->close();
$this->_mysqli_noda->commit();
$this->_mysqli_noda->autocommit(true);
}
/**
* Adds tags to a nodac group.
*
* @param integer $group_id Group ID.
* @param array<integer> $input_ids List of entries to link to the group.
*
* @return void
*/
public function linkTags(int $group_id, array $input_ids):void {
if (empty($input_ids = MD_STD_IN::sanitize_id_array($input_ids))) return;
// Check which entries actually exist
$idsToLink = [];
$result = $this->_mysqli_noda->do_read_query("SELECT `tag_id`
FROM `tag`
WHERE `tag_id` IN (" . $this->_mysqli_noda->escape_in($input_ids) . ")");
while ($cur = $result->fetch_row()) {
$idsToLink[] = (int)$cur[0];
}
$result->close();
$this->_mysqli_noda->autocommit(false);
$linkStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `v_group_tag`
(`group_id`, `tag_id`)
VALUES (?, ?)");
foreach ($idsToLink as $id) {
$linkStmt->bind_param("ii", $group_id, $id);
$linkStmt->execute();
}
$linkStmt->close();
$this->_mysqli_noda->commit();
$this->_mysqli_noda->autocommit(true);
}
/** /**
* Constructor. * Constructor.
* *