Skip to content

Commit

Permalink
Now IblockAbstractLayer has numeric property ID in phpDocs.
Browse files Browse the repository at this point in the history
introduced a new method in EventHelper.php for setting property values efficiently.

Added `secondsToTimeString` and `timeToIso8601Duration` functions in DateTimeUtils.php for better time conversion (it's need to rewrite, todos)

Add methods to clean unused basket properties on events

Integrated two event handlers in `handlers_add.php` to trigger the cleanup of unused basket properties upon adding an item to the basket and upon saving an order. Also, implemented the `deleteUnUsedBasketProps` method in `Sale.php` to execute the actual deletion in the database.
  • Loading branch information
hipot committed Aug 9, 2024
1 parent a8da786 commit 46d5e87
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 4 deletions.
25 changes: 25 additions & 0 deletions lib/classes/Hipot/BitrixUtils/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,29 @@ public static function orderHasPayedInHistory(int $orderId): bool
}
return false;
}

/**
* Deletes unused basket properties from the database (event handler)
*
* @param bool|int $basketId The ID of the basket to delete unused properties from. Optional, defaults to false.
*
* @return void
*/
public static function deleteUnUsedBasketProps($basketId = false): void
{
global $DB;

$nps = [];
foreach (\App::NEED_BASKET_PROPERTY as $np) {
$nps[] = "\"" . $np . "\"";
}

/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
$sqlDelUnusedProps =
'delete from b_sale_basket_props where ' .
//'delete from b_sale_basket_props WHERE BASKET_ID = ' . (int)$ID . ' AND ' .
'CODE NOT IN ('.implode(', ',$nps).')';
$DB->Query($sqlDelUnusedProps);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class __IblockElementItem_#ABSTRACT_LAYER_SAULT#_#IBLOCK_CODE#_#IBLOCK_ID# exten
*/
private string $oneRowPropertytemplate =
' /**
* #PROPERTY_TITLE#
* #PROPERTY_TITLE# (ID: <b>#PROPERTY_ID#</b>)
* @var #PROPERTY_TYPE#
*/
public $#PROPERTY_CODE#;
Expand All @@ -133,7 +133,7 @@ class __IblockElementItem_#ABSTRACT_LAYER_SAULT#_#IBLOCK_CODE#_#IBLOCK_ID# exten
*/
private string $multipleRowPropertyTemplate =
' /**
* #PROPERTY_TITLE#
* #PROPERTY_TITLE# (ID: <b>#PROPERTY_ID#</b>)
* @var array[#PROPERTY_TYPE#]
* @var #PROPERTY_TYPE#[]
*/
Expand Down Expand Up @@ -557,8 +557,8 @@ public function generate(): bool

$temp = ($prop['MULTIPLE'] != 'Y') ? $this->oneRowPropertytemplate : $this->multipleRowPropertyTemplate;
$outPropsIter .= str_replace(
['#PROPERTY_TITLE#', '#PROPERTY_CODE#', '#PROPERTY_TYPE#'],
[$prop['NAME'], $prop['CODE'], $propType],
['#PROPERTY_TITLE#', '#PROPERTY_CODE#', '#PROPERTY_ID#', '#PROPERTY_TYPE#'],
[$prop['NAME'], $prop['CODE'], $prop['ID'], $propType],
$temp
);
}
Expand Down
30 changes: 30 additions & 0 deletions lib/classes/Hipot/Utils/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,34 @@ public static function getIblockEventPropValue($propIdx)
}
return false;
}

/**
* Удобно использовать в событиях инфоблока для задания значений без сдвига массива
* $arFields['PROPERTY_VALUES'][107][ <b>??? - 1259|0|n0</b> ]['VALUE'] = $newValue;
* <pre>
* setIblockEventPropValue($arFields['PROPERTY_VALUES'][107], 'test')
* </pre>
*
* @param array $propIdx The array index containing the property.
* @param mixed $newValue The new value to set.
*
* @return void
*/
public static function setIblockEventPropValue(&$propIdx, $newValue): void
{
if (is_array($propIdx)) {
$k = array_keys($propIdx);
if (isset($propIdx[ $k[0] ]['VALUE'])) {
$propIdx[ $k[0] ]['VALUE'] = $newValue;
} else {
$propIdx[ $k[0] ] = $newValue;
}
} else {
$propIdx = [
'n0' => [
'VALUE' => $newValue,
]
];
}
}
}
71 changes: 71 additions & 0 deletions lib/classes/Hipot/Utils/Helper/DateTimeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Hipot\Utils\Helper;

use Hipot\Utils\UUtils;

trait DateTimeUtils
{
/**
Expand Down Expand Up @@ -48,4 +50,73 @@ public static function timestampToSeason(\DateTimeInterface $dateTime): string
}
return 'Fall';
}

/**
* Утилитарная функция для красивого вывода периода (в секундах) в дни, часы, минуты и секунды
*
* @param integer $duration Длительность периода в секундах
* @return string
*/
public static function secondsToTimeString($duration): string
{
$timeStrings = array();

$converted = [
'days' => floor($duration / (3600 * 24)),
'hours' => floor($duration / 3600),
'minutes' => floor(($duration / 60) % 60),
'seconds' => ($duration % 60)
];

if ($converted['days'] > 0) {
$timeStrings[] = $converted['days'] . ' ' . UUtils::Suffix($converted['days'], 'день|дня|дней');
}

if ($converted['hours'] > 0) {
$timeStrings[] = $converted['hours'] . ' ' . UUtils::Suffix($converted['hours'], 'час|часа|часов');
}
if ($converted['minutes'] > 0) {
$timeStrings[] = $converted['minutes'] . ' мин.';
}
if ($converted['seconds'] > 0) {
$timeStrings[] = $converted['seconds'] . ' сек.';
}

if(!empty($timeStrings)) {
return implode(' ', $timeStrings);
}

return ' 0 секунд';
}

/**
* @param int $time seconds
* @return string
* @see https://en.wikipedia.org/wiki/ISO_8601#Durations
*/
public static function timeToIso8601Duration(int $time): string
{
$units = [
"Y" => 365*24*3600,
"D" => 24*3600,
"H" => 3600,
"M" => 60,
"S" => 1,
];
$str = "P";
$istime = false;
foreach ($units as $unitName => &$unit) {
$quot = (int)($time / $unit);
$time -= $quot * $unit;
$unit = $quot;
if ($unit > 0) {
if (!$istime && in_array($unitName, ["H", "M", "S"])) { // There may be a better way to do this
$str .= "T";
$istime = true;
}
$str .= $unit . $unitName;
}
}
return $str;
}
}
21 changes: 21 additions & 0 deletions lib/handlers_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Bitrix\Main\Loader;
use Bitrix\Main\EventManager;
use Bitrix\Main\Event;
use Bitrix\Main\Application;
use Bitrix\Main\Web\HttpClient;
use Bitrix\Main\Composite\Page as CompositePage;
Expand Down Expand Up @@ -217,3 +218,23 @@ static function (&$this_al) {
$eventManager->addEventHandler('', 'CustomSettingsOnAfterAdd', [HiBlockApps::class, 'clearCustomSettingsCacheHandler']);
$eventManager->addEventHandler('', 'CustomSettingsOnAfterDelete', [HiBlockApps::class, 'clearCustomSettingsCacheHandler']);

// region очистка из корзины ненужных свойств (при добавлении товара из админки)
$eventManager->addEventHandler("sale", "OnBasketAdd", static function ($ID, $arFields) {
Hipot\BitrixUtils\Sale::deleteUnUsedBasketProps($ID);
});
$eventManager->addEventHandler(
'sale',
'OnSaleOrderSaved',
static function (Event $event) {
/** @var \Bitrix\Sale\Order $order */
$order = $event->getParameter("ENTITY");

if (isset($GLOBALS['deletedOrderUnusedProps_' . $order->getId()])) {
return;
}

Hipot\BitrixUtils\Sale::deleteUnUsedBasketProps();
$GLOBALS['deletedOrderUnusedProps_' . $order->getId()] = true;
}
);
// endregion

0 comments on commit 46d5e87

Please sign in to comment.