خطای افزونهMy commerce
Exceptionsyntax error, unexpected '$pages' (T_VARIABLE) - Line 117
فایل/home/rkcxsznd/modello.ir/content/plugins/mycommerce/class/MCE_Products.php
  1. <?php
    
  2. /**
    
  3.  * @Description : This department advances the products process
    
  4.  * @Author : Salar izadi
    
  5.  */
    
  6. final class MCE_Products {
    
  7.     public string $table           = "mce_products";
    
  8.     public string $table_favorite  = "mce_favorites";
    
  9.     public string $table_colors    = "mce_colors";
    
  10.     public string $table_downloads = "mce_downloads";
    
  11.     public array $types = [
    
  12.         0 => "Physical",
    
  13.         1 => "Downloadable"
    
  14.     ];
    
  15.     /**
    
  16.      * Do not execute this method, this method is automatically executed in the core of MCE
    
  17.      *
    
  18.      * @return void
    
  19.      */
    
  20.     public function init ( ) {
    
  21.         global $ufo;
    
  22.         $ufo->add_work("mce_pages_get_full_url", function ($page) {
    
  23.             global $ufo;
    
  24.             $product = $this->get($page["id"]);
    
  25.             if (!empty($product["extend"]))
    
  26.                 return $ufo->do_work($product["extend"] .  "_pages_get_full_url", $page);
    
  27.             return $product["link"];
    
  28.         });
    
  29.         /** Start UFO_Explorer */
    
  30.         $ufo->exert("ufo-explorer-mce", function ($hunted) {
    
  31.             $product = $this->get($hunted->hunted["id"]);
    
  32.             if (!$product) return false;
    
  33.             return array_merge([
    
  34.                 "prefix" => [
    
  35.                     "slug" => (new MCE_Tables())->meta("slug_product")
    
  36.                 ]
    
  37.             ], $product);
    
  38.         });
    
  39.         $ufo->add_array("ufo-explorer", [
    
  40.             "name"   => "mce-products",
    
  41.             "hunter" => function ($explore) use ($ufo) {
    
  42.                 $explore->query["where"]["extend"] = $explore->query["where"]["extend"] ?? [
    
  43.                     "", "="
    
  44.                 ];
    
  45.                 if (!isset($explore->query["filters"]["category"])) {
    
  46.                     $explore->query["filters"]["category"] = !empty(
    
  47.                         $explore->query["category"]
    
  48.                     ) ? $explore->query["category"] : [];
    
  49.                 }
    
  50.                 return (new MCE_Products())->all($explore->query);
    
  51.             }
    
  52.         ]);
    
  53.         $ufo->add_array("ufo-explorer", [
    
  54.             "name"   => "single-mce",
    
  55.             "hunter" => function ($explorer) {
    
  56.                 return $this->all([
    
  57.                     "where" => [
    
  58.                         "product" => $explorer->query["id"]
    
  59.                     ]
    
  60.                 ]);
    
  61.             },
    
  62.             "reset"  => false
    
  63.         ]);
    
  64.         /** End UFO_Explorer */
    
  65.     }
    
  66.     /**
    
  67.      * @param int $type
    
  68.      * @return false|string
    
  69.      */
    
  70.     public function readable_type (int $type) {
    
  71.         return mce_lng($this->types[$type]) ?? false;
    
  72.     }
    
  73.     /**
    
  74.      * @param array|int $page
    
  75.      * @param $limit
    
  76.      * @param int $type
    
  77.      * @param $search
    
  78.      * @param $paging_action
    
  79.      * @param array $where
    
  80.      * @param string $sort
    
  81.      * @param array $filters
    
  82.      * @return array
    
  83.      * @throws Exception
    
  84.      */
    
  85.     public function all (
    
  86.         $page = 1,
    
  87.         $limit = null,
    
  88.         int $type = -1,
    
  89.         $search = null,
    
  90.         $paging_action = null,
    
  91.         array $where = [],
    
  92.         string $sort = "",
    
  93.         array $filters = []
    
  94.     ): array {
    
  95.         global $ufo, $db, $MCE_PRELOAD;
    
  96.         if (is_array($page))
    
  97.             extract($page);
    
  98.         file_put_contents('products.json', json_encode($page, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE))
    
  99.         $pages     = new UFO_Pages();
    
  100.         $newList   = [];
    
  101.         $tableRows = empty($limit) ? $db->table_rows : $limit;
    
  102.         $db->helper->join("pages p", "p.id=%prefix%" . $this->table . ".product", "LEFT");
    
  103.         if (isset($status))
    
  104.             $db->where("p.status", $status);
    
  105.         if (!empty($search)) $db->helper->where(
    
  106.             "p." . ($search["prop"] ?? "title"),
    
  107.             "%" . ($search["value"] ?? $search) . "%",
    
  108.             "LIKE"
    
  109.         );
    
  110.         if (isset($this->types[$type]))
    
  111.             $where["type"] = $type;
    
  112.         $this->__filters($filters, $search);
    
  113.         $fields = $this->__sort($sort, $search);
    
  114.         foreach ($where as $kw => $vw) {
    
  115.             $where[$db->prefix . $this->table . "." . $kw] = $vw;
    
  116.             unset($where[$kw]);
    
  117.         }
    
  118.         print_r($sort);
    
  119.         $products = $db->pagination($this->table, [
    
  120.             "page"  => $page,
    
  121.             "limit" => (int) ($limit ?? $tableRows),
    
  122.             "paging_action" => $paging_action ?? "mce-products-paging"
    
  123.         ], $where, array_merge(
    
  124.             /** Limit table columns page */
    
  125.             explode(", ", ltrim(join(
    
  126.                 ", p.", array_merge(
    
  127.                     [""], $MCE_PRELOAD["table_columns_pages"])
    
  128.             ), ", ")), ["%prefix%" . $this->table . ".*"],
    
  129.             is_array($fields) ? $fields : []
    
  130.         ));
    
  131. //        print_r($products);
    
  132.         foreach ($products["rows"] ?? $products as $item) {
    
  133.             $pageProduct = $pages->get($item["product"]);
    
  134.             if (is_array($pageProduct)) {
    
  135.                 unset($pageProduct["id"]);
    
  136.                 unset($pageProduct["type"]);
    
  137.                 unset($item["id"]);
    
  138.                 if (!empty($item["extend"]))
    
  139.                     $item[$item["extend"]] = $ufo->do_work("mce-get-product-extend-" . $item["extend"], $item["product"]);
    
  140.                 $newList[] = $this->productEditValues($pageProduct + $item);
    
  141.             }
    
  142.         }
    
  143.         $products["rows"] = $newList;
    
  144.         /** Colors */
    
  145.         foreach ($products["rows"] ?? $products as $key => $item) {
    
  146.             if (is_array($item["colors"])) {
    
  147.                 foreach ($item["colors"] as $k => $v) {
    
  148.                     if ($v["inv"] <= 0)
    
  149.                         unset($products["rows"][$key]["colors"][$k]);
    
  150.                 }
    
  151.             }
    
  152.         }
    
  153.         return $products;
    
  154.     }
    
  155.     /**
    
  156.      * @param $product
    
  157.      * @return array|false
    
  158.      * @throws Exception
    
  159.      */
    
  160.     public function get ($product) {
    
  161.         global $ufo, $db;
    
  162.         $page = (new UFO_Pages())->get($product);
    
  163.         if ($page) {
    
  164.             $row = $db->get($this->table, "product", $page["id"]);
    
  165.             if (isset($row[0])) {
    
  166.                 $row = $row[0];
    
  167.                 unset($row["id"]);
    
  168.                 unset($page["id"]);
    
  169.                 unset($page["type"]);
    
  170.                 $row = $this->productEditValues(array_merge($row, $page));
    
  171.                 $row["old_link"] = $page["link"];
    
  172.                 $colors = [];
    
  173.                 foreach (is_array($row["colors"]) ? $row["colors"] : [] as $color) {
    
  174.                     $get = $this->get_color($color["id"]);
    
  175.                     if ($get) {
    
  176.                         unset($color["id"]);
    
  177.                         $colors[] = array_merge($get, $color);
    
  178.                     }
    
  179.                 }
    
  180.                 $row["colors"] = $colors;
    
  181.                 if ($ufo->isset_key($row["more"], "transport")) {
    
  182.                     if ($ufo->isset_key($row["more"]["transport"], "class"))
    
  183.                         $row["more"]["transport"]["class"] = (new MCE_Tables())->get_transport_class($row["more"]["transport"]["class"])[0] ?? 0;
    
  184.                 }
    
  185.                 if (!empty($row["extend"]))
    
  186.                     $row[$row["extend"]] = $ufo->do_work("mce-get-product-extend-" . $row["extend"], $row["product"]);
    
  187.                 if ($ufo->isset_key($row["more"], "similar-products")) {
    
  188.                     $sps = [];
    
  189.                     foreach ($row["more"]["similar-products"] as $item) {
    
  190.                         $p = (new UFO_Pages())->get((int) $item);
    
  191.                         if ($p) {
    
  192.                             $sps[] = [
    
  193.                                 "id"    => (int) $item,
    
  194.                                 "title" => $p["title"]
    
  195.                             ];
    
  196.                         }
    
  197.                     }
    
  198.                     $row["more"]["similar-products"] = $sps;
    
  199.                 }
    
  200.                 return $row;
    
  201.             } else {
    
  202.                 return false;
    
  203.             }
    
  204.         } else {
    
  205.             return false;
    
  206.         }
    
  207.     }
    
  208.     /**
    
  209.      * @param $array
    
  210.      * @return mixed
    
  211.      * @throws Exception
    
  212.      */
    
  213.     protected function productEditValues ($array) {
    
  214.         global $ufo;
    
  215.         $array["link"] = $ufo->web_link() . $ufo->sanitize_link(
    
  216.             (string) (new MCE_Tables())->meta("slug_product")
    
  217.         ) . "/" . urlencode($array["link"]);
    
  218.         $ufo->is_json($array["property"], $array["property"]);
    
  219.         $ufo->is_array($array["colors"], $array["colors"]);
    
  220.         $ufo->is_array($array["attributes"], $array["attributes"]);
    
  221.         $ufo->is_json($array["files"], $array["files"]);
    
  222.         $ufo->is_json($array["more"], $array["more"]);
    
  223.         return $array;
    
  224.     }
    
  225.     /**
    
  226.      * @param int $product
    
  227.      * @return bool
    
  228.      * @throws Exception
    
  229.      */
    
  230.     public function remove (int $product): bool {
    
  231.         global $db;
    
  232.         if ((new UFO_Pages())->delete($product)) {
    
  233.             try {
    
  234.                 /** Remove from products */
    
  235.                 $db->remove($this->table, "product", $product);
    
  236.                 /** Remove from cart */
    
  237.                 (new MCE_Cart())->remove_product($product);
    
  238.                 /** Remove from favorites */
    
  239.                 $this->remove_favorite($product);
    
  240.                 return true;
    
  241.             } catch (Exception $e) {
    
  242.                 return false;
    
  243.             }
    
  244.         }
    
  245.         return false;
    
  246.     }
    
  247.     /**
    
  248.      * @param int $product
    
  249.      * @param $mid
    
  250.      * @return bool
    
  251.      * @throws Exception
    
  252.      */
    
  253.     public function remove_favorite (int $product, $mid = null): bool {
    
  254.         global $db;
    
  255.         if (!empty($mid)) {
    
  256.             return $db->remove($this->table_favorite, [
    
  257.                 "mid"     => $mid,
    
  258.                 "product" => $product
    
  259.             ]);
    
  260.         } else {
    
  261.             return $db->remove($this->table_favorite, "product", $product);
    
  262.         }
    
  263.     }
    
  264.     /**
    
  265.      * @param bool $paging
    
  266.      * @param int $pp
    
  267.      * @param $limit
    
  268.      * @param $search
    
  269.      * @return array
    
  270.      * @throws Exception
    
  271.      */
    
  272.     public function get_colors (bool $paging = true, int $pp = 1, $limit = null, $search = null): array {
    
  273.         global $db;
    
  274.         if (empty($search)) {
    
  275.             if ($paging) {
    
  276.                 $Colors = $db->pagination($this->table_colors, [
    
  277.                     "page"  => $pp,
    
  278.                     "limit" => $limit ?? $db->meta("table_rows"),
    
  279.                     "paging_action" => "mce-colors-paging"
    
  280.                 ]);
    
  281.                 $Colors["rows"] = array_reverse($Colors["rows"]);
    
  282.             } else {
    
  283.                 $Colors = array_reverse($db->get($this->table_colors));
    
  284.             }
    
  285.         } else {
    
  286.             $Colors = array_reverse($db->query("SELECT * FROM `%prefix%mce_colors` WHERE `name` LIKE '%".$search."%'"));
    
  287.         }
    
  288.         return $Colors;
    
  289.     }
    
  290.     /**
    
  291.      * @param int $color
    
  292.      * @return false|mixed
    
  293.      * @throws ReflectionException
    
  294.      */
    
  295.     public function get_color (int $color) {
    
  296.         global $db;
    
  297.         return $db->get($this->table_colors, "id", $color)[0] ?? false;
    
  298.     }
    
  299.     /**
    
  300.      * @param string $name
    
  301.      * @param string $hex
    
  302.      * @return false|int|void
    
  303.      * @throws Exception
    
  304.      */
    
  305.     public function add_color (string $name, string $hex) {
    
  306.         global $db;
    
  307.         $has = $db->get($this->table_colors, [
    
  308.             "name" => $name,
    
  309.             "hex"  => $hex
    
  310.         ], null, "OR");
    
  311.         if (empty($has)) {
    
  312.             return $db->insert($this->table_colors, [
    
  313.                 "name" => $name,
    
  314.                 "hex"  => $hex
    
  315.             ]);
    
  316.         }
    
  317.         return 0;
    
  318.     }
    
  319.     /**
    
  320.      * @param int $color
    
  321.      * @return bool
    
  322.      * @throws Exception
    
  323.      */
    
  324.     public function remove_color (int $color): bool {
    
  325.         global $db;
    
  326.         return $db->remove($this->table_colors, "id", $color);
    
  327.     }
    
  328.     /**
    
  329.      * @return int|string
    
  330.      * @throws Exception
    
  331.      */
    
  332.     public function add_category (array $data) {
    
  333.         return (new UFO_Pages())->create_category($data + [
    
  334.             "from" => "mce"
    
  335.         ]);
    
  336.     }
    
  337.     /**
    
  338.      * @param array $data
    
  339.      * @return false|int|string
    
  340.      * @throws Exception
    
  341.      */
    
  342.     public function edit_category (array $data) {
    
  343.         if (isset($data["category"]))
    
  344.             return (new UFO_Pages())->update_category($data["category"], $data);
    
  345.         return false;
    
  346.     }
    
  347.     /**
    
  348.      * @param int $id
    
  349.      * @return bool
    
  350.      * @throws Exception
    
  351.      */
    
  352.     public function delete_category (int $id): bool {
    
  353.         return (new UFO_Pages())->delete_category($id);
    
  354.     }
    
  355.     /**
    
  356.      * @param array $info
    
  357.      * @return array
    
  358.      * @throws Exception
    
  359.      */
    
  360.     public function change_product_info (array $info): array {
    
  361.         global $ufo, $db;
    
  362.         /**
    
  363.          * Status
    
  364.          */
    
  365.         $status = [
    
  366.             "status"  => 403,
    
  367.             "message" => $ufo->lng("Access denied")
    
  368.         ];
    
  369.         /**
    
  370.          * Check parameters
    
  371.          */
    
  372.         $required_params = ["inputs", "type"];
    
  373.         if ($info["type"] == 0)
    
  374.             $required_inputs = ["name"];
    
  375.         else if ($info["type"] == 1)
    
  376.             $required_inputs = ["name"];
    
  377.         else return $status;
    
  378.         /**
    
  379.          * Status
    
  380.          */
    
  381.         $status = [
    
  382.             "status"  => 404,
    
  383.             "message" => $ufo->lng("Parameters not found")
    
  384.         ];
    
  385.         if (
    
  386.             !$ufo->has_in_array($required_params, $info) ||
    
  387.             !$ufo->has_in_array($required_inputs, $info["inputs"])
    
  388.         ) return $status;
    
  389.         if (isset($info["props"]["%NULL%"]))
    
  390.             unset($info["props"]["%NULL%"]);
    
  391.         /** END **/
    
  392.         /** Add product */
    
  393.         $update     = isset($info["update"]) && $info["update"];
    
  394.         $inputs     = $info["inputs"];
    
  395.         $properties = $info["props"] ?? [];
    
  396.         $transport  = $info["transport"] ?? [];
    
  397.         $colors     = $info["colors"] ?? [];
    
  398.         $attributes = $info["attributes"] ?? [];
    
  399.         $oldInfo    = $update ? $this->get((int) $info["update"]) : [];
    
  400.         /**
    
  401.          * Page actions
    
  402.          */
    
  403.         if ($update) {
    
  404.             $oldPageData = (new UFO_Pages())->get((int) $info["update"]);
    
  405.             /**
    
  406.              * Check for exists
    
  407.              */
    
  408.             if (!isset($db->get($this->table, "product", (int) $info["update"])[0]) || !$oldPageData) {
    
  409.                 $status["status"]  = 404;
    
  410.                 $status["message"] = mce_lng("There is no product with this ID");
    
  411.                 return $status;
    
  412.             }
    
  413.             $pageEditor = $ufo->do_work("ufo_page_editor_update", [
    
  414.                 "title" => $inputs["name"],
    
  415.                 "page"  => (int) $info["update"],
    
  416.                 "from"  => $info["from"] ?? "admin"
    
  417.             ]);
    
  418.         } else {
    
  419.             $pageEditor = $ufo->do_work("ufo_page_editor_save", [
    
  420.                 "title"      => $inputs["name"],
    
  421.                 "content"    => "",
    
  422.                 "short_desc" => "",
    
  423.                 "photo"      => $inputs["photo"] ?? "[]",
    
  424.                 "link"       => $inputs["link"] ?? $inputs["name"],
    
  425.                 "category"   => "[]",
    
  426.                 "tags"       => "",
    
  427.                 "status"     => 1,
    
  428.                 "type"       => "mce",
    
  429.                 "script"     => [],
    
  430.                 "setting"    => [],
    
  431.                 "from"       => $info["from"] ?? "admin"
    
  432.             ]);
    
  433.         }
    
  434.         /**
    
  435.          * Product actions
    
  436.          */
    
  437.         if ($ufo->isset_key($pageEditor, "id") && $pageEditor["status"] == 200) {
    
  438.             if ($ufo->is_bas64($pageEditor["id"] ?? ""))
    
  439.                 $pageEditor["id"] = (int) base64_decode($pageEditor["id"]);
    
  440.             if ($info["type"] == 0) {
    
  441.                 /** Physical product */
    
  442.                 $Data = [
    
  443.                     "code"      => ($inputs["code"] ?? ($oldInfo["code"] ?? rand(0, 9999) . rand(0, 9999))),
    
  444.                     "price"     => (int) ($inputs["price"] ?? 0),
    
  445.                     "discount"  => (int) ($inputs["discount"] ?? 0),
    
  446.                     "discount_expire" => (int) ($inputs["discount_expire"] ?? 0),
    
  447.                     "tax"       => (int) ($inputs["tax"] ?? 0),
    
  448.                     "inventory" => (int) ($inputs["inventory"] ?? 0),
    
  449.                     "note"      => $inputs["note"] ?? "",
    
  450.                     "more"      => json_encode([
    
  451.                         "inventory-alert"  => (int) ($inputs["inventory-alert"] ?? 0),
    
  452.                         "similar-products" => $info["similarProducts"] ?? [],
    
  453.                         "transport" => [
    
  454.                             "weight"  => (int) ($transport["weight"] ?? 0),
    
  455.                             "length"  => (int) ($transport["length"] ?? 0),
    
  456.                             "width"   => (int) ($transport["width"] ?? 0),
    
  457.                             "height"  => (int) ($transport["height"] ?? 0),
    
  458.                             "class"   => (int) ($transport["transportClass"] ?? 0)
    
  459.                         ]
    
  460.                     ]),
    
  461.                     "property"   => json_encode($properties, JSON_UNESCAPED_UNICODE),
    
  462.                     "colors"     => json_encode($colors),
    
  463.                     "attributes" => json_encode($attributes, JSON_UNESCAPED_SLASHES),
    
  464.                     "bought"   => 0,
    
  465.                     "type"     => $info["type"],
    
  466.                     "extend"   => $info["extend"] ?? ""
    
  467.                 ];
    
  468.             } elseif ($info["type"] == 1) {
    
  469.                 /** Downloadable product */
    
  470.                 if ($ufo->isset_key($inputs, "files")) {
    
  471.                     if (!$ufo->is_array($inputs["files"])) {
    
  472.                         $FILES = [];
    
  473.                         foreach (explode(",", $inputs["files"]) as $item)
    
  474.                             $FILES[] = $item;
    
  475.                         $inputs["files"] = $FILES;
    
  476.                     }
    
  477.                 }
    
  478.                 $Data = [
    
  479.                     "code"      => $inputs["code"] ?? ($oldInfo["code"] ?? rand(0, 9999) . rand(0, 9999)),
    
  480.                     "price"     => (int) ($inputs["price"] ?? 0),
    
  481.                     "discount"  => (int) ($inputs["discount"] ?? 0),
    
  482.                     "discount_expire" => (int) ($inputs["discount_expire"] ?? 0),
    
  483.                     "tax"       => (int) ($inputs["tax"] ?? 0),
    
  484.                     "inventory" => (int) (empty($inputs["inventory"]) ? -1 : $inputs["inventory"]),
    
  485.                     "note"      => $inputs["note"] ?? "",
    
  486.                     "property"   => json_encode($properties, JSON_UNESCAPED_UNICODE),
    
  487.                     "attributes" => json_encode($attributes, JSON_UNESCAPED_SLASHES),
    
  488.                     "files" => json_encode($info["files"] ?? [], JSON_UNESCAPED_UNICODE),
    
  489.                     "more"  => json_encode([
    
  490.                         "similar-products" => $info["similarProducts"] ?? [],
    
  491.                         "expire_link" => $inputs["expire-dl"] ?? 12
    
  492.                     ]),
    
  493.                     "bought" => 0,
    
  494.                     "type"   => $info["type"],
    
  495.                     "extend" => $info["extend"] ?? ""
    
  496.                 ];
    
  497.             } else return $status;
    
  498.             if (!$update) {
    
  499.                 $Data += [
    
  500.                     "product" => $pageEditor["id"]
    
  501.                 ];
    
  502.                 $insert = $db->insert($this->table, $Data);
    
  503.             } else
    
  504.                 $insert = $db->update($this->table, $Data, "product", $pageEditor["id"]);
    
  505.             if ($insert) {
    
  506.                 if (isset($info["extend"]))
    
  507.                     $ufo->do_work("mce-save-product-extend-" . $info["extend"], array_merge($_POST, $info, [
    
  508.                         "mce_product_id" => $pageEditor["id"]
    
  509.                     ]));
    
  510.                 $status = [
    
  511.                     "status"  => 200,
    
  512.                     "message" => $ufo->lng("Done successfully"),
    
  513.                     "id" => $pageEditor["id"]
    
  514.                 ];
    
  515.             } else $status = [
    
  516.                 "status"  => 503,
    
  517.                 "message" => $ufo->lng("System error")
    
  518.             ];
    
  519.             return $status;
    
  520.         } else return [
    
  521.             "status"  => $pageEditor["status"],
    
  522.             "message" => str_replace($ufo->lng("A page with this name has already been created"), mce_lng("A product with this name has already been created"), $pageEditor["message"])
    
  523.         ];
    
  524.     }
    
  525.     /**
    
  526.      * @return int|mixed
    
  527.      * @throws Exception
    
  528.      */
    
  529.     public function getMaxPrice () {
    
  530.         global $db;
    
  531.         return $db->query("SELECT MAX(`price`) as price FROM `%prefix%".$this->table."`")[0]["price"] ?? 0;
    
  532.     }
    
  533.     /**
    
  534.      * @return int|mixed
    
  535.      * @throws Exception
    
  536.      */
    
  537.     public function getMinPrice () {
    
  538.         global $db;
    
  539.         return $db->query("SELECT MIN(`price`) as price FROM `%prefix%".$this->table."`")[0]["price"] ?? 0;
    
  540.     }
    
  541.     /**
    
  542.      * @param array $product
    
  543.      * @param $link
    
  544.      * @return false|string
    
  545.      * @throws Exception
    
  546.      */
    
  547.     public function checkLinkDLAdded (array $product, $link) {
    
  548.         global $ufo, $db;
    
  549.         $member = $ufo->get_member()["uid"] ?? 0;
    
  550.         $row = $db->get($this->table_downloads, [
    
  551.             "pid"  => $product["product"],
    
  552.             "link" => $link,
    
  553.             ($member == 0 ? "ip" : "mid") => $member == 0 ? $ufo->viewer_ip() : $member,
    
  554.         ]); $row = $row[0] ?? false;
    
  555.         if ($row) {
    
  556.             if ($row["expire"] >= $ufo->dateTime())
    
  557.                 return $ufo->web_link() . (new MCE_Tables())->meta("slug_dl") . "/" . $row["hash"];
    
  558.             else
    
  559.                 $db->remove($this->table_downloads, "id", $row["id"]);
    
  560.         } return false;
    
  561.     }
    
  562.     /**
    
  563.      * @param int $pid
    
  564.      * @param $file
    
  565.      * @return false|string
    
  566.      * @throws Exception
    
  567.      */
    
  568.     public function addLinkDownload (int $pid, $file) {
    
  569.         global $ufo, $db;
    
  570.         if ($product = $this->get($pid)) {
    
  571.             if (isset($product["files"][$file]["link"])) {
    
  572.                 $oldLink = $this->checkLinkDLAdded($product, $product["files"][$file]["link"]);
    
  573.                 if (!$oldLink) {
    
  574.                     if ($product["files"][$file]) {
    
  575.                         $hash = $ufo->hash_generator("sha1");
    
  576.                         $insert = $db->insert($this->table_downloads, [
    
  577.                             "pid"    => $product["product"],
    
  578.                             "ip"     => $ufo->viewer_ip(),
    
  579.                             "mid"    => $ufo->get_member()["uid"] ?? 0,
    
  580.                             "hash"   => $hash,
    
  581.                             "name"   => $file,
    
  582.                             "link"   => $product["files"][$file]["link"],
    
  583.                             "expire" => !empty($product["more"]["expire_link"]) || $product["more"]["expire_link"] != 0 ? $ufo->addTime(
    
  584.                                 $product["more"]["expire_link"], "h"
    
  585.                             ) : 0
    
  586.                         ]);
    
  587.                         return $insert ? $ufo->web_link() . (new MCE_Tables())->meta("slug_dl") . "/" . $hash : false;
    
  588.                     }
    
  589.                 }
    
  590.                 return $oldLink;
    
  591.             }
    
  592.         }
    
  593.         return false;
    
  594.     }
    
  595.     /**
    
  596.      * @return mixed
    
  597.      * @throws Exception
    
  598.      */
    
  599.     public function count () {
    
  600.         global $db;
    
  601.         return $db->query("SELECT COUNT(`id`) as c FROM `%prefix%".$this->table."`")[0]["c"];
    
  602.     }
    
  603.     /**
    
  604.      * @return bool
    
  605.      * @throws Exception
    
  606.      */
    
  607.     public function task ( ): bool {
    
  608.         global $ufo, $db;
    
  609.         if (defined("UFO_TASK")) {
    
  610.             $UFO_PAGES = new UFO_Pages();
    
  611.             /** Discounted products expire */
    
  612.             $db->where("discount", 0, ">");
    
  613.             $db->where("discount_expire", 0, ">");
    
  614.             $discounted_products = $db->get($this->table);
    
  615.             foreach ($discounted_products as $product) {
    
  616.                 $page = $UFO_PAGES->get($product["product"]);
    
  617.                 $days = (new DateTime($page["dateTime"]))->diff(
    
  618.                     new DateTime($ufo->dateTime())
    
  619.                 )->days;
    
  620.                 if ($days > (int) $product["discount_expire"]) {
    
  621.                     $db->update($this->table, [
    
  622.                         "discount" => 0,
    
  623.                         "discount_expire" => 0
    
  624.                     ], "id", $product["id"]);
    
  625.                 }
    
  626.             }
    
  627.             /** END */
    
  628.             return true;
    
  629.         } return false;
    
  630.     }
    
  631.     /**
    
  632.      * @param $sort
    
  633.      * @param $search
    
  634.      * @return string[]|null
    
  635.      * @throws Exception
    
  636.      */
    
  637.     protected function __sort ($sort, $search = ""): ?array {
    
  638.         global $db;
    
  639.         $fields = null;
    
  640.         $sort   = explode("*", $sort);
    
  641.         switch ($sort[0]) {
    
  642.             case "discount":
    
  643.                 $db->where("discount", 0, "!=");
    
  644.                 break;
    
  645.             case "best-selling":
    
  646.                 $db->helper
    
  647.                     ->where("bought", 0, ">")
    
  648.                     ->orderBy("SUM(`bought`)");
    
  649.                 break;
    
  650.             case "most-popular":
    
  651.                 $db->helper->join("comments", "%prefix%" . $this->table . ".product=" . "%prefix%comments.pid AND " . "%prefix%comments.accept=1", "LEFT OUTER");
    
  652.                 $db->helper->orderBy("rate");
    
  653.                 foreach ($sort as $s) {
    
  654.                     if ($s == "limit-null-rate") {
    
  655.                         $db->helper->where("rate", "null", "!=");
    
  656.                         break;
    
  657.                     }
    
  658.                 }
    
  659.                 $fields = ["AVG(rate) as rate", "%prefix%" . $this->table . ".*"];
    
  660.                 break;
    
  661.             case "newest":
    
  662.                 $db->helper->orderBy("%prefix%" . $this->table . ".id");
    
  663.                 break;
    
  664.             case "cheapest":
    
  665.                 $db->helper->orderBy("price", "ASC");
    
  666.                 break;
    
  667.             case "most-expensive":
    
  668.                 $db->helper->orderBy("price");
    
  669.                 break;
    
  670.         }
    
  671.         return $fields;
    
  672.     }
    
  673.     /**
    
  674.      * @param array $filters
    
  675.      * @param $search
    
  676.      * @return void
    
  677.      * @throws Exception
    
  678.      */
    
  679.     protected function __filters (array $filters, $search = "") {
    
  680.         global $db;
    
  681.         if (isset($filters["price"])) {
    
  682.             if (isset($filters["price"][0]) && isset($filters["price"][1])) {
    
  683.                 $db->where("price", (int) $filters["price"][0], ">="); // Min
    
  684.                 $db->where("price", (int) $filters["price"][1], "<="); // Max
    
  685.             }
    
  686.         }
    
  687.         if (isset($filters["category"])) {
    
  688.             if (!is_array($filters["category"]))
    
  689.                 $filters["category"] = [$filters["category"]];
    
  690.             foreach ($filters["category"] as $category)
    
  691.                 $db->where("p.category", "%$category%", "LIKE");
    
  692.         }
    
  693.         if (isset($filters["hasSelling"]))
    
  694.             $db->where("inventory", 0, "!=");
    
  695.     }
    
  696. }