CBPDocumentService::getJSFunctionsForFields

  1. Bitrix24 API (v. 23.675.0)
  2. bizproc
  3. CBPDocumentService
  4. getJSFunctionsForFields
  • Модуль: bizproc
  • Путь к файлу: ~/bitrix/modules/bizproc/classes/general/documentservice.php
  • Класс: CBPDocumentService
  • Вызов: CBPDocumentService::getJSFunctionsForFields
public function getJSFunctionsForFields($parameterDocumentType, $objectName, $arDocumentFields = array(), $arDocumentFieldTypes = array())
{
	if (!is_array($arDocumentFields) || count($arDocumentFields) <= 0)
		$arDocumentFields = self::GetDocumentFields($parameterDocumentType);
	if (!is_array($arDocumentFieldTypes) || count($arDocumentFieldTypes) <= 0)
		$arDocumentFieldTypes = self::GetDocumentFieldTypes($parameterDocumentType);

	[$moduleId, $entity, $documentType] = CBPHelper::ParseDocumentId($parameterDocumentType);

	if ($moduleId)
	{
		CModule::IncludeModule($moduleId);
	}

	$documentFieldsString = "";
	foreach ($arDocumentFields as $fieldKey => $arFieldValue)
	{
		if ($documentFieldsString <> '')
			$documentFieldsString .= ",";

		$documentFieldsString .= "'".Cutil::JSEscape($fieldKey)."':{";

		$documentFieldsString .= "'Name':'".CUtil::JSEscape($arFieldValue["Name"])."',";
		$documentFieldsString .= "'Type':'".CUtil::JSEscape($arFieldValue["Type"])."',";
		$documentFieldsString .= "'Multiple':'".CUtil::JSEscape(!empty($arFieldValue["Multiple"]) ? "Y" : "N")."',";
		$documentFieldsString .= "'Complex':'".CUtil::JSEscape(!empty($arFieldValue["Complex"]) ? "Y" : "N")."',";

		$documentFieldsString .= "'Options':";
		if (array_key_exists("Options", $arFieldValue))
		{
			if (is_array($arFieldValue["Options"]))
			{
				$documentFieldsString .= "{";
				$flTmp = false;
				foreach ($arFieldValue["Options"] as $k => $v)
				{
					if (!is_scalar($v))
					{
						continue;
					}

					if ($flTmp)
						$documentFieldsString .= ",";
					$documentFieldsString .= "'".CUtil::JSEscape($k)."':'".CUtil::JSEscape($v)."'";
					$flTmp = true;
				}
				$documentFieldsString .= "}";
			}
			else
			{
				$documentFieldsString .= "'".CUtil::JSEscape($arFieldValue["Options"])."'";
			}
		}
		else
		{
			$documentFieldsString .= "''";
		}

		if (isset($arFieldValue["Options"]) && CBPHelper::IsAssociativeArray($arFieldValue["Options"]))
		{
			$documentFieldsString .= ", 'OptionsSort':";
			$documentFieldsString .= CUtil::PhpToJSObject(array_keys($arFieldValue["Options"]));
		}

		if(isset($arFieldValue["Settings"]) && is_array($arFieldValue["Settings"]))
		{
			$documentFieldsString .= ", 'Settings':";
			$documentFieldsString .= CUtil::PhpToJSObject($arFieldValue["Settings"]);
		}

		$documentFieldsString .= "}";
	}

	$fieldTypesString = "";
	$ind = -1;
	foreach ($arDocumentFieldTypes as $typeKey => $arTypeValue)
	{
		$ind++;
		if ($fieldTypesString <> '')
			$fieldTypesString .= ",";

		$fieldTypesString .= "'".CUtil::JSEscape($typeKey)."':{";

		$fieldTypesString .= "'Name':'".CUtil::JSEscape($arTypeValue["Name"])."',";
		$fieldTypesString .= "'BaseType':'".CUtil::JSEscape($arTypeValue["BaseType"])."',";
		$fieldTypesString .= "'Complex':'".CUtil::JSEscape(!empty($arTypeValue["Complex"]) ? "Y" : "N")."',";
		$fieldTypesString .= "'Index':".$ind."";

		$fieldTypesString .= "}";
	}

	$documentTypeString = CUtil::PhpToJSObject($parameterDocumentType);
	$bitrixSessId = bitrix_sessid();

$result = <<
var $objectName = {};

$objectName.arDocumentFields = { $documentFieldsString };
$objectName.arFieldTypes = { $fieldTypesString };

$objectName.AddField = function(fldCode, fldName, fldType, fldMultiple, fldOptions)
{
this.arDocumentFields[fldCode] = {};
this.arDocumentFields[fldCode]["Name"] = fldName;
this.arDocumentFields[fldCode]["Type"] = fldType;
this.arDocumentFields[fldCode]["Multiple"] = fldMultiple;
this.arDocumentFields[fldCode]["Options"] = fldOptions;
}

$objectName._PrepareResponse = function(v)
{
v = v.replace(/^s+|s+$/g, '');
while (v.length > 0 && v.charCodeAt(0) == 65279)
	v = v.substring(1);

if (v.length <= 0)
	return undefined;

eval("v = " + v);

return v;
}

$objectName.GetFieldInputControl4Type = function(type, value, name, subtypeFunctionName, func)
{
this.GetFieldInputControlInternal(
	type,
	value,
	name,
	function(v)
	{
		var p = v.indexOf('', p);
			type['Options'] = v.substring(p, p1);
		}

		var newPromt = "";

		p = v.indexOf('', p);
			newPromt = v.substring(p, p1);
		}

		func(v, newPromt);
	},
	false,
	subtypeFunctionName,
	'Type'
);
}

$objectName.GetFieldInputControl4Subtype = function(type, value, name, func)
{
$objectName.GetFieldInputControlInternal(type, value, name, func, false, '', '');
}

$objectName.GetFieldInputControl = function(type, value, name, func, als)
{
$objectName.GetFieldInputControlInternal(type, value, name, func, als, '', '');
}

$objectName.GetFieldInputControlInternal = function(type, value, name, func, als, subtypeFunctionName, mode)
{
if (typeof name == "undefined" || name.length <= 0)
	name = "BPVDDefaultValue";

if (typeof type != "object")
	type = {'Type' : type, 'Multiple' : 0, 'Required' : 0, 'Options' : null};

if (typeof name != "object")
	name = {'Field' : name, 'Form' : null};

BX.ajax.post(
	'/bitrix/tools/bizproc_get_field.php',
	{
		'DocumentType' : $documentTypeString,
		'Field' : name,
		'Value' : value,
		'Type' : type,
		'Als' : als ? 1 : 0,
		'rnd' : Math.random(),
		'Mode' : mode,
		'Func' : subtypeFunctionName,
		'sessid' : '$bitrixSessId'
	},
	func
);
}

$objectName.GetFieldValueByTagName = function(tag, name, form)
{
var fieldValues = {};

var ar;
if (form && (form.length > 0))
{
	var obj = document.getElementById(form);
	if (!obj)
	{
		for (var i in document.forms)
		{
			if (document.forms[i].name == form)
			{
				obj = document.forms[i];
				break;
			}
		}
	}

	if (!obj)
		return;

	ar = obj.getElementsByTagName(tag);
}
else
{
	ar = document.getElementsByTagName(tag);
}

for (var i in ar)
{
	if (ar[i] && ar[i].name && (ar[i].name.length >= name.length) && (ar[i].name.substr(0, name.length) == name))
	{
		if (ar[i].type.substr(0, "select".length) == "select")
		{
			if (ar[i].multiple)
			{
				var newName = ar[i].name.replace(/[]/g, "");
				for (var j = 0; j < ar[i].options.length; j++)
				{
					if (ar[i].options[j].selected)
					{
						if ((typeof(fieldValues[newName]) != 'object') || !(fieldValues[newName] instanceof Array))
						{
							if (fieldValues[newName])
								fieldValues[newName] = [fieldValues[newName]];
							else
								fieldValues[newName] = [];
						}
						fieldValues[newName][fieldValues[newName].length] = ar[i].options[j].value;
					}
				}
			}
			else
			{
				if (ar[i].selectedIndex >= 0)
				{
					const name = ar[i].name;
					const value = ar[i].options[ar[i].selectedIndex].value;

					if (name.indexOf("[]", 0) >= 0)
					{
						const newName = name.replace(/[]/g, "");
						if ((typeof(fieldValues[newName]) !== 'object') || !(fieldValues[newName] instanceof Array))
						{
							if (fieldValues[newName])
							{
								fieldValues[newName] = [fieldValues[newName]];
							}
							else
							{
								fieldValues[newName] = [];
							}
						}

						fieldValues[newName][fieldValues[newName].length] = value;
					}
					else
					{
						fieldValues[name] = value;
					}
				}
			}
		}
		else
		{
			if (ar[i].name.indexOf("[]", 0) >= 0)
			{
				var newName = ar[i].name.replace(/[]/g, "");

				if ((typeof(fieldValues[newName]) != 'object') || !(fieldValues[newName] instanceof Array))
				{
					if (fieldValues[newName])
						fieldValues[newName] = [fieldValues[newName]];
					else
						fieldValues[newName] = [];
				}

				fieldValues[newName][fieldValues[newName].length] = ar[i].value;
			}
			else
			{
				fieldValues[ar[i].name] = ar[i].value;
			}
		}
	}
}

return fieldValues;
}

$objectName.GetFieldInputValue = function(type, name, func)
{
if (typeof name == "undefined" || name.length <= 0)
	name = "BPVDDefaultValue";

if (typeof type != "object")
	type = {'Type' : type, 'Multiple' : 0, 'Required' : 0, 'Options' : null};

if (typeof name != "object")
	name = {'Field' : name, 'Form' : null};

var s = {
	'DocumentType' : $documentTypeString,
	'Field' : name,
	'Type' : type,
	'rnd' : Math.random(),
	'sessid' : '$bitrixSessId'
};

if (name['Form'])
{
	var objForm = document.getElementById(name['Form']);
	if (!objForm)
	{
		for (var i in document.forms)
		{
			if (document.forms[i].name == name['Form'])
			{
				objForm = document.forms[i];
				break;
			}
		}
	}

	if (objForm)
	{
		BX.ajax.prepareForm(objForm, s);
	}
}

if (type != null && type['Type'] != "F")
{
	var ar = this.GetFieldValueByTagName('input', name['Field'], name['Form']);
	for (var v in ar)
		s[v] = ar[v];
	ar = this.GetFieldValueByTagName('select', name['Field'], name['Form']);
	for (var v in ar)
		s[v] = ar[v];
	ar = this.GetFieldValueByTagName('textarea', name['Field'], name['Form']);
	for (var v in ar)
		s[v] = ar[v];
	ar = this.GetFieldValueByTagName('hidden', name['Field'], name['Form']);
	for (var v in ar)
		s[v] = ar[v];
}

BX.ajax.post('/bitrix/tools/bizproc_set_field.php', s, function(v){v = $objectName._PrepareResponse(v); func(v);});
}

$objectName.HtmlSpecialChars = function(string, quote)
{
string = string.toString();
string = string.replace(/&/g, '&');
string = string.replace(//g, '>');
string = string.replace(/"/g, '"');

if (quote)
	string = string.replace(/'/g, ''');

return string;
}

$objectName.GetGUITypeEdit = function(type)
{
return "";
}

$objectName.SetGUITypeEdit = function(type)
{
return "";
}

function __dump_bx(arr, limitLevel, txt)
{
if (limitLevel == undefined)
	limitLevel = 3;
if (txt == undefined)
	txt = "";
else
	txt += ":\n";
alert(txt+__dumpInternal_bx(arr, 0, limitLevel));
}
function __dumpInternal_bx(arr, level, limitLevel) {
var dumped_text = "";
if(!level) level = 0;
if (level > limitLevel)
	return "";
var level_padding = "";
for(var j=0;j '" + value + "'\n";
		}
	}
} else {
	dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}

return dumped_text;
}


EOS;

	if (class_exists($entity) && method_exists($entity, "GetJSFunctionsForFields"))
	{
		$result .= call_user_func_array(array($entity, "GetJSFunctionsForFields"), array($documentType, $objectName, $arDocumentFields, $arDocumentFieldTypes));
	}
	else
	{
		if (!is_array($arDocumentFields) || count($arDocumentFields) <= 0)
			$arDocumentFields = $this->GetDocumentFields($parameterDocumentType);
		if (!is_array($arDocumentFieldTypes) || count($arDocumentFieldTypes) <= 0)
			$arDocumentFieldTypes = $this->GetDocumentFieldTypes($parameterDocumentType);

		$result .= CBPHelper::GetJSFunctionsForFields($objectName, $arDocumentFields, $arDocumentFieldTypes);
	}

	return $result;
}

Добавить комментарий