Base Directory:
/home/ecedu/public_html/new/Models
maxTables; $i++) {
$this->tables[$i] = 'dbs_news' . $i;
}
$this->db = DB::instance()->getConnection();
}
public function getDb(): PDO
{
return $this->db;
}
/*-------------------------------------------------
| أدوات مساعدة داخلية |
*------------------------------------------------*/
/** يقسّم عبارة البحث إلى كلمات مفاتيح بعد تنظيفها. */
private function explodeKeywords(string $text): array
{
$text = trim(preg_replace('/[^\p{L}\p{N}\s]/u', '', $text)); // إزالة علامات الترقيم
return array_values(
array_filter(
preg_split('/\s+/u', $text),
fn($w) => $w !== ''
)
);
}
/**
* يبني شرط LIKE ومعاملات الربط المناسب للغة معيّنة.
* يرجع:
* [
* 'sql' => '(news_title LIKE :kw0 OR ...)',
* 'params' => [':kw0' => '%foo%', ...]
* ]
*/
private function buildKeywordClause(array $keywords, string $lang): array
{
// اختيار عمود العنوان
$titleCol = 'news_title';
switch ($lang) {
case 'ku':
$titleCol = 'news_title1';
break;
case 'en':
$titleCol = 'news_title2';
break;
default: // 'ar' أو أي قيمة أخرى
$titleCol = 'news_title';
}
// 2) ابنِ شرط AND بدل OR
$parts = [];
$params = [];
foreach ($keywords as $i => $word) {
$ph = ":kw{$i}";
$parts[] = "{$titleCol} LIKE {$ph}";
$params[$ph] = "%{$word}%";
}
return [
// ملاحظة: AND يضمن وجود **جميع** الكلمات
'sql' => '(' . implode(' AND ', $parts) . ')',
'params' => $params,
];
}
/*-------------------------------------------------
| واجهة البحث – عدّ النتائج |
*------------------------------------------------*/
public function getCountByText(string $text, string $lang): int
{
$keywords = $this->explodeKeywords($text);
if (empty($keywords)) {
return 0;
}
$clause = $this->buildKeywordClause($keywords, $lang);
$tables = $this->getAllTableNames();
$unions = [];
foreach ($tables as $tableId => $tableName) {
$unions[] = "SELECT COUNT(*) AS cnt FROM $tableName WHERE {$clause['sql']}";
}
$sql = implode(' UNION ALL ', $unions);
$stmt = $this->db->prepare("SELECT SUM(cnt) FROM ($sql) AS combined");
foreach ($clause['params'] as $ph => $val) {
$stmt->bindValue($ph, $val, \PDO::PARAM_STR);
}
$stmt->execute();
return (int) $stmt->fetchColumn();
}
/*-------------------------------------------------
| واجهة البحث – جلب قائمة الأخبار |
*------------------------------------------------*/
public function getListNewsByText(string $text, string $lang, int $limit, int $offset): array
{
$keywords = $this->explodeKeywords($text);
if (empty($keywords)) {
return [];
}
$clause = $this->buildKeywordClause($keywords, $lang);
$tables = $this->getAllTableNames();
$unions = [];
foreach ($tables as $tableId => $tableName) {
$unions[] = "SELECT *, {$tableId} AS table_id, '{$tableName}' AS table_name
FROM {$tableName}
WHERE {$clause['sql']}";
}
$sql = implode(' UNION ALL ', $unions) . " ORDER BY news_date DESC LIMIT :limit OFFSET :offset";
try {
$stmt = $this->db->prepare($sql);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
foreach ($clause['params'] as $ph => $val) {
$stmt->bindValue($ph, $val, PDO::PARAM_STR);
}
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
error_log('News::getListNewsByText Error: ' . $e->getMessage());
return [];
}
}
/*-------------------------------------------------
| دوال أخرى (اختياري) – حسب حاجتك |
*------------------------------------------------*/
public function getCountByType(int $typeId): int
{
try {
$table = $this->getTableName($typeId);
$stmt = $this->db->prepare("SELECT COUNT(*) FROM $table");
$stmt->execute();
return (int) $stmt->fetchColumn();
} catch (\PDOException $e) {
error_log('News::getCountByType Error: ' . $e->getMessage());
return 0;
}
}
public function getListNewsByType(
int $typeId,
int $limit,
int $offset
): array {
$table = $this->getTableName($typeId); // ← استخدم this->
$stmt = $this->db->prepare(
"SELECT * FROM $table
ORDER BY news_date DESC
LIMIT :limit OFFSET :offset"
);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function getNewsById(int $type,int $id): ?array
{
try {
$table = $this->getTableName($type); // ← استخدم this->
$sql = "SELECT * FROM $table WHERE news_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::getSingleNewsById Error: ' . $e->getMessage());
return null;
}
}
public function increaseViewCount(int $table_id,int $news_id)
{
$sql = "INSERT INTO dbs_view (news_id,table_id, num_view)
VALUES (:news_id,:table_id, 1)
ON DUPLICATE KEY UPDATE num_view = num_view + 1";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(':news_id', $news_id, PDO::PARAM_INT);
$stmt->bindValue(':table_id', $table_id, PDO::PARAM_INT);
$stmt->execute();
}
public function getNewsByType(int $tableId, int $limit = 10)
{
$table = $this->getTableName($tableId); // ← استخدم this->
$sql = "SELECT * FROM $table
ORDER BY news_date DESC
LIMIT :limit";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function getBasic($table)
{
try {
$sql = "SELECT * FROM $table LIMIT 1";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
return $row ?: null;
} catch (\PDOException $e) {
error_log('News::getBasic Error: ' . $e->getMessage());
return null;
}
}
public function listSocials()
{
$sql = "SELECT * FROM dbs_social_media ";
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
public function listImages()
{
$sql = "SELECT * FROM dbs_image ";
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
public function listAds()
{
$sql = "SELECT * FROM dbs_ads ";
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
public function getViewCount(int $limit = 5): array
{
$sql ="
SELECT news_id, table_id, SUM(num_view) AS total_views
FROM dbs_view
WHERE table_id != 11 AND table_id != 17
GROUP BY news_id, table_id
ORDER BY total_views DESC
LIMIT :limit;" ;
try {
$stmt = $this->db->prepare($sql);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
error_log('News::getViewCount Error: ' . $e->getMessage());
return [];
}
}
public function getAllTableNames(): array
{
for ($i = 1; $i <= $this->maxTables; $i++) {
$this->tables[$i] = 'dbs_news' . $i;
}
return $this->tables;
}
public function getMaxTables(): int {
return $this->maxTables;
}
public function getTableName(int $tableId): string
{
$tables = $this->getAllTableNames();
if (!isset($tables[$tableId])) {
throw new Exception("معرف الجدول غير صالح: $tableId");
}
return $tables[$tableId];
}
}