["ID"], "filter" => ["=IBLOCK_TYPE_ID" => self::IBLOCK_TYPE, "=CODE" => $code], ]); self::$iblockIds[$code] = (int)($row["ID"] ?? 0); } return self::$iblockIds[$code]; } public static function onElementChange(array &$fields): void { $id = (int)($fields["ID"] ?? 0); if ($id <= 0 || (int)($fields["IBLOCK_ID"] ?? 0) !== self::iblockId(self::REVIEWS_CODE)) { return; } self::recalc(self::productOf($id)); } public static function onBeforeDelete(int $id): void { $row = ElementTable::getRow(["select" => ["IBLOCK_ID"], "filter" => ["=ID" => $id]]); if ((int)($row["IBLOCK_ID"] ?? 0) === self::iblockId(self::REVIEWS_CODE)) { self::$deleting[$id] = self::productOf($id); } } public static function onAfterDelete(array $fields): void { $id = (int)($fields["ID"] ?? 0); if (isset(self::$deleting[$id])) { self::recalc(self::$deleting[$id]); unset(self::$deleting[$id]); } } public static function summary(int $productId): ?array { $catalogId = self::iblockId(self::CATALOG_CODE); if ($productId <= 0 || $catalogId <= 0) { return null; } $values = []; foreach (["REVIEW_COUNT", "REVIEW_RATING"] as $code) { $property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => $code]]); $row = $property === null ? null : ElementPropertyTable::getRow([ "select" => ["VALUE"], "filter" => ["=IBLOCK_ELEMENT_ID" => $productId, "=IBLOCK_PROPERTY_ID" => (int)$property["ID"]], ]); $values[$code] = $row === null ? 0.0 : (float)$row["VALUE"]; } return ["count" => (int)$values["REVIEW_COUNT"], "avg" => round($values["REVIEW_RATING"], 1)]; } public static function items(int $productId, int $limit = 50): array { $reviewsId = self::iblockId(self::REVIEWS_CODE); if ($productId <= 0 || $reviewsId <= 0) { return []; } $items = []; $rows = \CIBlockElement::GetList( ["ACTIVE_FROM" => "DESC", "ID" => "DESC"], ["IBLOCK_ID" => $reviewsId, "ACTIVE" => "Y", "PROPERTY_PRODUCT" => $productId], false, ["nTopCount" => $limit], ["ID", "PREVIEW_TEXT", "ACTIVE_FROM", "DATE_CREATE", "PROPERTY_AUTHOR", "PROPERTY_RATING"] ); while ($row = $rows->Fetch()) { $rating = (int)$row["PROPERTY_RATING_VALUE"]; $author = trim((string)$row["PROPERTY_AUTHOR_VALUE"]); $text = trim((string)preg_replace('/\s+/u', " ", strip_tags((string)$row["PREVIEW_TEXT"]))); if ($rating < 1 || $rating > 5 || $author === "" || $text === "") { continue; } $items[] = [ "author" => $author, "rating" => $rating, "text" => $text, "date" => date("Y-m-d", MakeTimeStamp((string)($row["ACTIVE_FROM"] ?: $row["DATE_CREATE"]))), ]; } return $items; } public static function recalc(int $productId): void { $catalogId = self::iblockId(self::CATALOG_CODE); if ($productId <= 0 || $catalogId <= 0) { return; } $sum = 0; $count = 0; $rows = \CIBlockElement::GetList( [], ["IBLOCK_ID" => self::iblockId(self::REVIEWS_CODE), "ACTIVE" => "Y", "PROPERTY_PRODUCT" => $productId], false, false, ["ID", "PROPERTY_RATING"] ); while ($row = $rows->Fetch()) { $rating = (int)$row["PROPERTY_RATING_VALUE"]; if ($rating >= 1 && $rating <= 5) { $sum += $rating; $count++; } } \CIBlockElement::SetPropertyValuesEx($productId, $catalogId, [ "REVIEW_COUNT" => $count, "REVIEW_RATING" => $count > 0 ? round($sum / $count, 1) : false, ]); \CIBlock::clearIblockTagCache($catalogId); } private static function productOf(int $reviewId): int { $value = \CIBlockElement::GetProperty(self::iblockId(self::REVIEWS_CODE), $reviewId, [], ["CODE" => "PRODUCT"])->Fetch(); return (int)($value["VALUE"] ?? 0); } }