Files
dsp/classes/Aboutus.php
2026-01-29 14:31:48 +07:00

128 lines
5.4 KiB
PHP

<?php
class Aboutus {
private $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
/**
* Adds a new "About Us" entry to the database.
*
* @param string $title_en The English title (e.g., Vision, Mission, Goal).
* @param string $description The detailed description.
* @param int $reg_by The ID of the user who registered this entry (from ist_tbl_users).
* @param int $fkisp_id_of The ID of the person associated with this entry (from ist_tbl_people).
* @return bool True on success.
* @throws Exception If a database error occurs or title already exists.
*/
public function addAboutUs(string $title_en, string $description, int $reg_by, int $fkisp_id_of): bool {
$sql = "INSERT INTO dsps_tbl_dspsabout (dspsabout_title_en, dspsabout_description, dspsabout_reg_by, fkisp_id_of)
VALUES (:title_en, :description, :reg_by, :fkisp_id_of)";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':title_en', $title_en);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':reg_by', $reg_by);
$stmt->bindParam(':fkisp_id_of', $fkisp_id_of);
return $stmt->execute();
} catch (PDOException $e) {
if ($e->getCode() == '23000') { // Integrity constraint violation (e.g., duplicate title if UNIQUE)
throw new Exception("An 'About Us' entry with this title already exists.");
}
error_log("Error adding About Us entry: " . $e->getMessage());
throw new Exception("Could not add About Us entry. Please try again later.");
}
}
/**
* Updates an existing "About Us" entry.
*
* @param int $id The ID of the entry to update.
* @param string $title_en The new English title.
* @param string $description The new description.
* @param int $mod_by The ID of the user who modified this entry.
* @param int $fkisp_id_of The ID of the person associated with this entry.
* @return bool True on success.
* @throws Exception If a database error occurs or title already exists.
*/
public function updateAboutUs(int $id, string $title_en, string $description, int $mod_by, int $fkisp_id_of): bool {
$sql = "UPDATE dsps_tbl_dspsabout
SET dspsabout_title_en = :title_en, dspsabout_description = :description,
dspsabout_mod_datetime = CURRENT_TIMESTAMP, dspsabout_reg_by = :mod_by, fkisp_id_of = :fkisp_id_of
WHERE pkdspsabout_id = :id";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':title_en', $title_en);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':mod_by', $mod_by);
$stmt->bindParam(':fkisp_id_of', $fkisp_id_of);
$stmt->bindParam(':id', $id);
return $stmt->execute();
} catch (PDOException $e) {
if ($e->getCode() == '23000') {
throw new Exception("An 'About Us' entry with this title already exists.");
}
error_log("Error updating About Us entry (ID: $id): " . $e->getMessage());
throw new Exception("Could not update About Us entry. Please try again later.");
}
}
/**
* Deletes an "About Us" entry.
*
* @param int $id The ID of the entry to delete.
* @return bool True on success.
* @throws Exception If a database error occurs.
*/
public function deleteAboutUs(int $id): bool {
$sql = "DELETE FROM dsps_tbl_dspsabout WHERE pkdspsabout_id = :id";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':id', $id);
return $stmt->execute();
} catch (PDOException $e) {
error_log("Error deleting About Us entry (ID: $id): " . $e->getMessage());
throw new Exception("Could not delete About Us entry. Please try again later.");
}
}
/**
* Retrieves a single "About Us" entry by its ID.
*
* @param int $id The ID of the entry.
* @return array|false The entry data as an associative array, or false if not found.
* @throws Exception If a database error occurs.
*/
public function getAboutUsById(int $id) {
$sql = "SELECT * FROM dsps_tbl_dspsabout WHERE pkdspsabout_id = :id";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Error fetching About Us entry by ID ($id): " . $e->getMessage());
throw new Exception("Could not retrieve About Us entry. Please try again later.");
}
}
/**
* Retrieves all "About Us" entries.
*
* @return array An array of "About Us" entry data.
* @throws Exception If a database error occurs.
*/
public function getAllAboutUs(): array {
$sql = "SELECT * FROM dsps_tbl_dspsabout ORDER BY dspsabout_reg_datetime ASC";
try {
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Error fetching all About Us entries: " . $e->getMessage());
throw new Exception("Could not retrieve About Us entries. Please try again later.");
}
}
}