diff --git a/src/NodaNameGetter.php b/src/NodaNameGetter.php new file mode 100644 index 0000000..b283bc8 --- /dev/null +++ b/src/NodaNameGetter.php @@ -0,0 +1,198 @@ + + */ +declare(strict_types = 1); + +/** + * Class for the batch getting of noda name. + */ +final class NodaNameGetter { + /** + * Static function for getting tag names in bulk. + * + * @param MDMysqli $mysqli_noda DB connection. + * @param string $lang Language. + * @param array $tag_ids Tag IDs. + * + * @return array + */ + public static function getBatchTagNames(MDMysqli $mysqli_noda, string $lang, array $tag_ids):array { + + $output = []; + + // Get translations + $result = $mysqli_noda->do_read_query("SELECT `tag_id`, `trans_name` + FROM `tag_translation` + WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "' + AND `tag_id` IN (" . implode(', ', $tag_ids) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['tag_id']] = (string)$cur['trans_name']; + } + $result->close(); + $result = null; + + if (!empty($tag_ids_left = array_diff($tag_ids, array_keys($output)))) { + + $result = $mysqli_noda->do_read_query("SELECT `tag_id`, `tag_name` + FROM `tag` + WHERE `tag_id` IN (" . implode(', ', $tag_ids_left) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['tag_id']] = (string)$cur['tag_name']; + } + $result->close(); + $result = null; + + } + + if (count($output) > 1) { + asort($output); + } + + return $output; + + } + + /** + * Static function for getting place names in bulk. + * + * @param MDMysqli $mysqli_noda DB connection. + * @param string $lang Language. + * @param array $place_ids Place IDs. + * + * @return array + */ + public static function getBatchPlaceNames(MDMysqli $mysqli_noda, string $lang, array $place_ids):array { + + $output = []; + + // Get translations + $result = $mysqli_noda->do_read_query("SELECT `ort_id`, `trans_name` + FROM `ort_translation` + WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "' + AND `ort_id` IN (" . implode(', ', $place_ids) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['ort_id']] = (string)$cur['trans_name']; + } + $result->close(); + $result = null; + + if (!empty($place_ids_left = array_diff($place_ids, array_keys($output)))) { + + $result = $mysqli_noda->do_read_query("SELECT `ort_id`, `ort_name` + FROM `orte` + WHERE `ort_id` IN (" . implode(', ', $place_ids_left) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['ort_id']] = (string)$cur['ort_name']; + } + $result->close(); + $result = null; + + } + + if (count($output) > 1) { + asort($output); + } + + return $output; + + } + + /** + * Static function for getting persinst names in bulk. + * + * @param MDMysqli $mysqli_noda DB connection. + * @param string $lang Language. + * @param array $persinst_ids Persinst IDs. + * + * @return array + */ + public static function getBatchPersinstNames(MDMysqli $mysqli_noda, string $lang, array $persinst_ids):array { + + $output = []; + + // Get translations + $result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `trans_name` + FROM `persinst_translation` + WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "' + AND `persinst_id` IN (" . implode(', ', $persinst_ids) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['persinst_id']] = (string)$cur['trans_name']; + } + $result->close(); + $result = null; + + if (!empty($persinst_ids_left = array_diff($persinst_ids, array_keys($output)))) { + $result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `persinst_name_en` + FROM `persinst` + WHERE `persinst_id` IN (" . implode(', ', $persinst_ids_left) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['persinst_id']] = (string)$cur['persinst_name_en']; + } + $result->close(); + $result = null; + } + + if (count($output) > 1) { + asort($output); + } + + return $output; + + } + + /** + * Static function for getting time names in bulk. + * + * @param MDMysqli $mysqli_noda DB connection. + * @param string $lang Language. + * @param array $time_ids Time IDs. + * + * @return array + */ + public static function getBatchTimeNames(MDMysqli $mysqli_noda, string $lang, array $time_ids):array { + + $output = []; + + // Get translations + $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `trans_name` + FROM `zeit_translation` + WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "' + AND `zeit_id` IN (" . implode(', ', $time_ids) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['zeit_id']] = (string)$cur['trans_name']; + } + $result->close(); + $result = null; + + if (!empty($time_ids_left = array_diff($time_ids, array_keys($output)))) { + + $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `zeit_name` + FROM `zeiten` + WHERE `zeit_id` IN (" . implode(', ', $time_ids_left) . ")"); + + while ($cur = $result->fetch_assoc()) { + $output[(int)$cur['zeit_id']] = (string)$cur['zeit_name']; + } + $result->close(); + $result = null; + + } + + if (count($output) > 1) { + asort($output); + } + + return $output; + + } +}