Priority File Manager

๐Ÿ“ public_html
Base Directory:
/home/ecedu/public_html/new/Models
NameTypeSizeActions
๐Ÿ“ .. Folder -
๐Ÿ“„ BasicManager.php File 2087
Edit Download
๐Ÿ“„ DB.php File 935
Edit Download
๐Ÿ“„ DetailsManager.php File 4312
Edit Download
๐Ÿ“„ DeviceAuth.php File 698
Edit Download
๐Ÿ“„ Functions.php File 6774
Edit Download
๐Ÿ“„ ImageManager.php File 2862
Edit Download
๐Ÿ“„ News.php File 13517
Edit Download
๐Ÿ“„ NewsModel.php File 9523
Edit Download

View File: DetailsManager.php

<?php
declare(strict_types=1);

require_once __DIR__ . '/DB.php'; // ูƒู„ุงุณ Singleton ูŠูˆูุฑ ุงุชุตุงู„ PDO ู…ุซู„ุงู‹

final class DetailsManager
{
    private PDO $db;
    private const IMG_ALLOWED = ['jpg','jpeg','png','gif','bmp'];
    protected $menuData = [];

    public function __construct()
    {
        $this->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();
}

}