Base Directory:
/home/ecedu/public_html/cce/Models
db = DB::instance()->getConnection();
}
/** ุงุชุตุงู PDO ุฅู ูุฒู
*/
public function getDb(): PDO
{
return $this->db;
}
public function getMenu(): array
{
$file = __DIR__.'/../Languages/menu.json';
if (!file_exists($file)) {
return []; // โ ุตููู ูุงุฑุบ ูุง ูุณุจูุจ ุฃุฎุทุงุก ูุงุญููุง
}
$json = file_get_contents($file);
return json_decode($json, true) ?: [];
}
public function safeLabel(?array $item): array
{
return $item['title']
?? ['ar'=>'', 'ku'=>'', 'en'=>'']; // ูู ููููููุฐ ุฅูุง ุฅุฐุง $item ููุณุช null
}
public function findById(array $items, int $id): ?array
{
foreach ($items as $item) {
if (($item['id'] ?? null) === $id) {
return $item; // ููุฌูุฏ ูู ูุฐุง ุงูู
ุณุชูู
}
if (!empty($item['children'])) {
$found = $this->findById($item['children'], $id); // โฌ
๏ธ ููุง ุงูุชุบููุฑ
if ($found !== null) {
return $found; // ููุฌูุฏ ูู ู
ุณุชูู ุฃุนู
ู
}
}
}
return null; // ูู
ููุนุซุฑ ุนููู
}
public function getDetailsById(int $id): ?array
{
try {
$sql = 'SELECT * FROM dbs_details WHERE item_id = :id LIMIT 1';
$stmt = $this->db->prepare($sql);
$stmt->bindValue(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
return $row ?: null;
} catch (\PDOException $e) {
error_log('News::getDetailsById Error: ' . $e->getMessage());
return null;
}
}
public function upsertDetails(
int $itemId,
int $typeId, // ุฃุจููููุงู ููุชูุณุฌููู โููุนโ ุงูููุฑุฉ
int $userId,
array $titles, // ['ar'=>..,'ku'=>..,'en'=>..]
array $texts, // ['ar'=>..,'ku'=>..,'en'=>..]
?string $imgExt = null
): bool {
if ($imgExt !== null && !in_array(strtolower($imgExt), self::IMG_ALLOWED, true)) {
return false; // ุงู
ุชุฏุงุฏ ุบูุฑ ู
ุณู
ูุญ
}
$sql = "
INSERT INTO dbs_details
(item_id, type_id, user_id, img_ext,
details_title, details1_title, details2_title,
details_description, details1_description, details2_description)
VALUES
(:item, :type, :user, :ext,
:t_ar, :t_ku, :t_en,
:x_ar, :x_ku, :x_en)
ON DUPLICATE KEY UPDATE
type_id = VALUES(type_id), -- ูู
ูู ุชุบููุฑู
img_ext = VALUES(img_ext),
details_title = VALUES(details_title),
details1_title = VALUES(details1_title),
details2_title = VALUES(details2_title),
details_description = VALUES(details_description),
details1_description = VALUES(details1_description),
details2_description = VALUES(details2_description),
user_id = VALUES(user_id)
";
$st = $this->db->prepare($sql);
$st->bindValue(':item', $itemId, PDO::PARAM_INT);
$st->bindValue(':type', $typeId, PDO::PARAM_INT);
$st->bindValue(':user', $userId, PDO::PARAM_INT);
$st->bindValue(':ext', $imgExt, PDO::PARAM_STR);
$st->bindValue(':t_ar', $titles['ar'] ?? '' , PDO::PARAM_STR);
$st->bindValue(':t_ku', $titles['ku'] ?? '' , PDO::PARAM_STR);
$st->bindValue(':t_en', $titles['en'] ?? '' , PDO::PARAM_STR);
$st->bindValue(':x_ar', $texts ['ar'] ?? '' , PDO::PARAM_STR);
$st->bindValue(':x_ku', $texts ['ku'] ?? '' , PDO::PARAM_STR);
$st->bindValue(':x_en', $texts ['en'] ?? '' , PDO::PARAM_STR);
return $st->execute();
}
}