namespace Bitrix\Currency\Integration;
use Bitrix\Main\Localization\Loc;
use Bitrix\Currency\CurrencyTable;
use Bitrix\Currency\Helpers\Editor;
use Bitrix\Main\Type\RandomSequence;
use Bitrix\Currency\CurrencyManager;
use Bitrix\Main\Web\Json;
use Bitrix\Catalog\Component\BaseForm;
Loc::loadMessages(__FILE__);
class IblockMoneyProperty
{
const USER_TYPE = 'Money';
const SEPARATOR = '|';
/**
* Returns property type description.
*
* @return array
*/
public static function getUserTypeDescription()
{
$className = get_called_class();
return array(
'PROPERTY_TYPE' => 'S',
'USER_TYPE' => self::USER_TYPE,
'DESCRIPTION' => Loc::getMessage('CIMP_INPUT_DESCRIPTION'),
'GetPublicEditHTML' => array($className, 'getPublicEditHTML'),
'GetPublicViewHTML' => array($className, 'getPublicViewHTML'),
'GetPropertyFieldHtml' => array($className, 'getPropertyFieldHtml'),
'GetAdminListViewHTML' => array($className, 'getAdminListViewHTML'),
'GetUIEntityEditorProperty' => array($className, 'GetUIEntityEditorProperty'),
'getFormattedValue' => array($className, 'getSeparatedValues'),
'CheckFields' => array($className, 'checkFields'),
'GetLength' => array($className, 'getLength'),
'ConvertToDB' => array($className, 'convertToDB'),
'ConvertFromDB' => array($className, 'convertFromDB'),
'AddFilterFields' => array($className, 'addFilterFields'),
);
}
/**
* Return html for public edit value.
*
* @param array $property Property data.
* @param array $value Current value.
* @param array $controlSettings Form data.
* @return string
*/
public static function getPublicEditHTML($property, $value, $controlSettings)
{
return self::getPropertyFieldHtml($property, $value, $controlSettings);
}
/**
* Return html for public view value.
*
* @param array $property Property data.
* @param array $value Current value.
* @param array $controlSettings Form data.
* @return string
*/
public static function getPublicViewHTML($property, $value, $controlSettings)
{
return self::getAdminListViewHTML($property, $value, $controlSettings);
}
/**
* The method should return the html display for editing property values in the administrative part.
*
* @param array $property Property data.
* @param array $value Current value.
* @param array $controlSettings Form data.
* @return string
*/
public static function getPropertyFieldHtml($property, $value, $controlSettings)
{
$seed = (!empty($controlSettings['VALUE'])) ? $controlSettings['VALUE'] : 'IMPSeed';
$randomGenerator = new RandomSequence($seed);
$randString = mb_strtolower($randomGenerator->randString(6));
$explode = (is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : []);
$currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
$currentCurrency = ($explode[1] ?? '');
$html = '';
$html .= '';
$listCurrency = self::getListCurrency();
if($listCurrency)
{
if($property['MULTIPLE'] == 'Y')
$html .= '';
$html .= '';
$html .= '';
$html .= self::getJsHandlerSelector($randString, $listCurrency);
}
return $html;
}
/**
* The method must return safe HTML display the value of the properties on the list of items the administrative part.
*
* @param array $property Property data.
* @param array $value Current value.
* @param array $controlSettings Form data.
* @return mixed|string
*/
public static function getAdminListViewHTML($property, $value, $controlSettings)
{
$explode = is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : [];
$currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
$currentCurrency = $explode[1] ?? '';
if (!$currentCurrency)
return is_numeric($currentValue) ? $currentValue : '';
if (CurrencyManager::isCurrencyExist($currentCurrency))
{
if(!empty($controlSettings['MODE']))
{
switch($controlSettings['MODE'])
{
case 'CSV_EXPORT':
return $value['VALUE'];
case 'ELEMENT_TEMPLATE':
case 'SIMPLE_TEXT':
return $currentValue;
}
}
list($currentValue, $currentCurrency, $decimalsValue) = array_values(self::getSeparatedValues($value['VALUE']));
$currentValue = $currentValue.'.'.$decimalsValue;
return \CCurrencyLang::CurrencyFormat($currentValue, $currentCurrency, true);
}
return '';
}
/**
* Check fields before inserting into the database.
*
* @param array $property Property data.
* @param array $value Current value.
* @return array An empty array, if no errors.
*/
public static function checkFields($property, $value)
{
$result = [];
if(empty($value['VALUE'])) return $result;
$explode = (is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : []);
$currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
$currentCurrency = ($explode[1] ?? '');
if(!$currentCurrency)
return is_numeric($currentValue) ? $result : array(Loc::getMessage('CIMP_FORMAT_ERROR'));
if(CurrencyManager::isCurrencyExist($currentCurrency))
{
$format = \CCurrencyLang::GetFormatDescription($currentCurrency);
$decPoint = $format['DEC_POINT'];
$thousandsSep = $format['THOUSANDS_SEP'];
$decimals = $format['DECIMALS'];
$regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
if($thousandsSep && $decPoint)
{
if ($decimals > 0)
{
$regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
}
else
{
$regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*$/';
}
}
elseif($thousandsSep && !$decPoint)
{
$regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*$/';
}
elseif(!$thousandsSep && $decPoint)
{
if ($decimals > 0)
{
$regExp = '/^[0-9]*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
}
else
{
$regExp = '/^[0-9]*$/';
}
}
elseif(!$thousandsSep && !$decPoint)
{
$regExp = '/^[0-9]*$/';
}
if(!preg_match($regExp, $currentValue))
{
$result[] = Loc::getMessage('CIMP_FORMAT_ERROR');
}
}
else
{
$result[] = Loc::getMessage('CIMP_FORMAT_ERROR');
}
return $result;
}
/**
* Get the length of the value. Checks completion of mandatory.
*
* @param array $property Property data.
* @param array $value Current value.
* @return int
*/
public static function getLength($property, $value)
{
return mb_strlen(trim($value['VALUE'], "\n\r\t"));
}
/**
* The method is to convert the value of a format suitable for storage in a database.
*
* @param array $property Property data.
* @param array $value Current value.
* @return mixed
*/
public static function convertToDB($property, $value)
{
return $value;
}
/**
* The method is to convert the property value in the processing format.
*
* @param array $property Property data.
* @param array $value Current value.
* @return mixed
*/
public static function convertFromDB($property, $value)
{
return $value;
}
public static function getSeparatedValues($value)
{
$explode = is_string($value) ? explode(self::SEPARATOR, $value) : [];
$currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
$currentCurrency = $explode[1] ?? '';
$format = \CCurrencyLang::GetFormatDescription($currentCurrency);
$explode = explode($format['DEC_POINT'], $currentValue);
$currentValue = ($explode[0] !== '' ? $explode[0] : '');
$decimalsValue = $explode[1] ?? '';
return array(
'AMOUNT' => $currentValue,
'CURRENCY' => $currentCurrency,
'DECIMALS' => $decimalsValue
);
}
public static function getUnitedValue($amount, string $currency): string
{
return
($amount === '')
? ''
: $amount.self::SEPARATOR.$currency
;
}
/**
* Add values in filter.
*
* @param array $property Property data.
* @param array $controlSettings Form data.
* @param array &$filter Filter data.
* @param bool &$filtered Marker filter.
* @return void
*/
public static function addFilterFields($property, $controlSettings, &$filter, &$filtered)
{
$filtered = false;
if(isset($_REQUEST[$controlSettings['VALUE']]))
{
$value = $_REQUEST[$controlSettings['VALUE']];
}
elseif(isset($controlSettings["FILTER_ID"]))
{
$filterOption = new \Bitrix\Main\UI\Filter\Options($controlSettings["FILTER_ID"]);
$filterData = $filterOption->getFilter();
if(!empty($filterData[$controlSettings['VALUE']]))
$value = $filterData[$controlSettings['VALUE']];
}
if(!empty($value))
{
$explode = explode(self::SEPARATOR, $value);
if(empty($explode[1]))
{
$listCurrency = self::getListCurrency();
if($listCurrency)
{
$filter[$controlSettings['VALUE']] = array();
foreach($listCurrency as $currencyType => $currency)
{
$filter[$controlSettings['VALUE']][] = $value.self::SEPARATOR.$currencyType;
}
}
}
$filtered = true;
}
}
protected static function getListCurrency()
{
$result = Editor::getListCurrency();
return (empty($result) ? array() : $result);
}
protected static function getJsHandlerSelector($randString, array $listCurrency)
{
ob_start();
?>