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); } list($moduleId, $entity, $documentType) = CBPHelper::ParseDocumentId($parameterDocumentType); if (strlen($moduleId) > 0) { CModule::IncludeModule($moduleId); } $documentFieldsString = ""; foreach ($arDocumentFields as $fieldKey => $arFieldValue) { if (strlen($documentFieldsString) > 0) { $documentFieldsString .= ","; } $documentFieldsString .= "'" . Cutil::JSEscape($fieldKey) . "':{"; $documentFieldsString .= "'Name':'" . CUtil::JSEscape($arFieldValue["Name"]) . "',"; $documentFieldsString .= "'Type':'" . CUtil::JSEscape($arFieldValue["Type"]) . "',"; $documentFieldsString .= "'Multiple':'" . CUtil::JSEscape($arFieldValue["Multiple"] ? "Y" : "N") . "',"; $documentFieldsString .= "'Complex':'" . CUtil::JSEscape($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 ($flTmp) { $documentFieldsString .= ","; } $documentFieldsString .= "'" . CUtil::JSEscape($k) . "':'" . CUtil::JSEscape($v) . "'"; $flTmp = true; } $documentFieldsString .= "}"; } else { $documentFieldsString .= "'" . CUtil::JSEscape($arFieldValue["Options"]) . "'"; } } else { $documentFieldsString .= "''"; } $documentFieldsString .= "}"; } $fieldTypesString = ""; $ind = -1; foreach ($arDocumentFieldTypes as $typeKey => $arTypeValue) { $ind++; if (strlen($fieldTypesString) > 0) { $fieldTypesString .= ","; } $fieldTypesString .= "'" . CUtil::JSEscape($typeKey) . "':{"; $fieldTypesString .= "'Name':'" . CUtil::JSEscape($arTypeValue["Name"]) . "',"; $fieldTypesString .= "'BaseType':'" . CUtil::JSEscape($arTypeValue["BaseType"]) . "',"; $fieldTypesString .= "'Complex':'" . CUtil::JSEscape($arTypeValue["Complex"] ? "Y" : "N") . "',"; $fieldTypesString .= "'Index':" . $ind . ""; $fieldTypesString .= "}"; } $documentTypeString = CUtil::PhpToJSObject($parameterDocumentType); $bitrixSessId = bitrix_sessid(); $result = <<<EOS <script type="text/javascript"> var {$objectName} = {}; {$objectName}.arDocumentFields = { {$documentFieldsString} }; {$objectName}.arFieldTypes = { {$fieldTypesString} }; {$objectName}.AddField = function(fldCode, fldName, fldType, fldMultiple, fldOptions) { \tthis.arDocumentFields[fldCode] = {}; \tthis.arDocumentFields[fldCode]["Name"] = fldName; \tthis.arDocumentFields[fldCode]["Type"] = fldType; \tthis.arDocumentFields[fldCode]["Multiple"] = fldMultiple; \tthis.arDocumentFields[fldCode]["Options"] = fldOptions; } {$objectName}._PrepareResponse = function(v) { \tv = v.replace(/^\\s+|\\s+\$/g, ''); \twhile (v.length > 0 && v.charCodeAt(0) == 65279) \t\tv = v.substring(1); \tif (v.length <= 0) \t\treturn undefined; \teval("v = " + v); \treturn v; } {$objectName}.GetFieldInputControl4Type = function(type, value, name, subtypeFunctionName, func) { \tthis.GetFieldInputControlInternal( \t\ttype, \t\tvalue, \t\tname, \t\tfunction(v) \t\t{ \t\t\tvar p = v.indexOf('<!--__defaultOptionsValue:'); \t\t\tif (p >= 0) \t\t\t{ \t\t\t\tp = p + '<!--__defaultOptionsValue:'.length; \t\t\t\tvar p1 = v.indexOf('-->', p); \t\t\t\ttype['Options'] = v.substring(p, p1); \t\t\t} \t\t\tvar newPromt = ""; \t\t\tp = v.indexOf('<!--__modifyOptionsPromt:'); \t\t\tif (p >= 0) \t\t\t{ \t\t\t\tp = p + '<!--__modifyOptionsPromt:'.length; \t\t\t\tp1 = v.indexOf('-->', p); \t\t\t\tnewPromt = v.substring(p, p1); \t\t\t} \t\t\tfunc(v, newPromt); \t\t}, \t\tfalse, \t\tsubtypeFunctionName, \t\t'Type' \t); } {$objectName}.GetFieldInputControl4Subtype = function(type, value, name, func) { \t{$objectName}.GetFieldInputControlInternal(type, value, name, func, false, '', ''); } {$objectName}.GetFieldInputControl = function(type, value, name, func, als) { \t{$objectName}.GetFieldInputControlInternal(type, value, name, func, als, '', ''); } {$objectName}.GetFieldInputControlInternal = function(type, value, name, func, als, subtypeFunctionName, mode) { \tif (typeof name == "undefined" || name.length <= 0) \t\tname = "BPVDDefaultValue"; \tif (typeof type != "object") \t\ttype = {'Type' : type, 'Multiple' : 0, 'Required' : 0, 'Options' : null}; \tif (typeof name != "object") \t\tname = {'Field' : name, 'Form' : null}; \tBX.ajax.post( \t\t'/bitrix/tools/bizproc_get_field.php', \t\t{ \t\t\t'DocumentType' : {$documentTypeString}, \t\t\t'Field' : name, \t\t\t'Value' : value, \t\t\t'Type' : type, \t\t\t'Als' : als ? 1 : 0, \t\t\t'rnd' : Math.random(), \t\t\t'Mode' : mode, \t\t\t'Func' : subtypeFunctionName, \t\t\t'sessid' : '{$bitrixSessId}' \t\t}, \t\tfunc \t); } {$objectName}.GetFieldValueByTagName = function(tag, name, form) { \tvar fieldValues = {}; \tvar ar; \tif (form && (form.length > 0)) \t{ \t\tvar obj = document.getElementById(form); \t\tif (!obj) \t\t{ \t\t\tfor (var i in document.forms) \t\t\t{ \t\t\t\tif (document.forms[i].name == form) \t\t\t\t{ \t\t\t\t\tobj = document.forms[i]; \t\t\t\t\tbreak; \t\t\t\t} \t\t\t} \t\t} \t\tif (!obj) \t\t\treturn; \t\tar = obj.getElementsByTagName(tag); \t} \telse \t{ \t\tar = document.getElementsByTagName(tag); \t} \tfor (var i in ar) \t{ \t\tif (ar[i] && ar[i].name && (ar[i].name.length >= name.length) && (ar[i].name.substr(0, name.length) == name)) \t\t{ \t\t\tif (ar[i].type.substr(0, "select".length) == "select") \t\t\t{ \t\t\t\tif (ar[i].multiple) \t\t\t\t{ \t\t\t\t\tvar newName = ar[i].name.replace(/\\[\\]/g, ""); \t\t\t\t\tfor (var j = 0; j < ar[i].options.length; j++) \t\t\t\t\t{ \t\t\t\t\t\tif (ar[i].options[j].selected) \t\t\t\t\t\t{ \t\t\t\t\t\t\tif ((typeof(fieldValues[newName]) != 'object') || !(fieldValues[newName] instanceof Array)) \t\t\t\t\t\t\t{ \t\t\t\t\t\t\t\tif (fieldValues[newName]) \t\t\t\t\t\t\t\t\tfieldValues[newName] = [fieldValues[newName]]; \t\t\t\t\t\t\t\telse \t\t\t\t\t\t\t\t\tfieldValues[newName] = []; \t\t\t\t\t\t\t} \t\t\t\t\t\t\tfieldValues[newName][fieldValues[newName].length] = ar[i].options[j].value; \t\t\t\t\t\t} \t\t\t\t\t} \t\t\t\t} \t\t\t\telse \t\t\t\t{ \t\t\t\t\tif (ar[i].selectedIndex >= 0) \t\t\t\t\t\tfieldValues[ar[i].name] = ar[i].options[ar[i].selectedIndex].value; \t\t\t\t} \t\t\t} \t\t\telse \t\t\t{ \t\t\t\tif (ar[i].name.indexOf("[]", 0) >= 0) \t\t\t\t{ \t\t\t\t\tvar newName = ar[i].name.replace(/\\[\\]/g, ""); \t\t\t\t\tif ((typeof(fieldValues[newName]) != 'object') || !(fieldValues[newName] instanceof Array)) \t\t\t\t\t{ \t\t\t\t\t\tif (fieldValues[newName]) \t\t\t\t\t\t\tfieldValues[newName] = [fieldValues[newName]]; \t\t\t\t\t\telse \t\t\t\t\t\t\tfieldValues[newName] = []; \t\t\t\t\t} \t\t\t\t\tfieldValues[newName][fieldValues[newName].length] = ar[i].value; \t\t\t\t} \t\t\t\telse \t\t\t\t{ \t\t\t\t\tfieldValues[ar[i].name] = ar[i].value; \t\t\t\t} \t\t\t} \t\t} \t} \treturn fieldValues; } {$objectName}.GetFieldInputValue = function(type, name, func) { \tif (typeof name == "undefined" || name.length <= 0) \t\tname = "BPVDDefaultValue"; \tif (typeof type != "object") \t\ttype = {'Type' : type, 'Multiple' : 0, 'Required' : 0, 'Options' : null}; \tif (typeof name != "object") \t\tname = {'Field' : name, 'Form' : null}; \tvar s = { \t\t'DocumentType' : {$documentTypeString}, \t\t'Field' : name, \t\t'Type' : type, \t\t'rnd' : Math.random(), \t\t'sessid' : '{$bitrixSessId}' \t}; \tif (type != null && type['Type'] != "F") \t{ \t\tvar ar = this.GetFieldValueByTagName('input', name['Field'], name['Form']); \t\tfor (var v in ar) \t\t\ts[v] = ar[v]; \t\tar = this.GetFieldValueByTagName('select', name['Field'], name['Form']); \t\tfor (var v in ar) \t\t\ts[v] = ar[v]; \t\tar = this.GetFieldValueByTagName('textarea', name['Field'], name['Form']); \t\tfor (var v in ar) \t\t\ts[v] = ar[v]; \t\tar = this.GetFieldValueByTagName('hidden', name['Field'], name['Form']); \t\tfor (var v in ar) \t\t\ts[v] = ar[v]; \t} \tBX.ajax.post('/bitrix/tools/bizproc_set_field.php', s, function(v){v = {$objectName}._PrepareResponse(v); func(v);}); } {$objectName}.HtmlSpecialChars = function(string, quote) { \tstring = string.toString(); \tstring = string.replace(/&/g, '&'); \tstring = string.replace(/</g, '<'); \tstring = string.replace(/>/g, '>'); \tstring = string.replace(/"/g, '"'); \tif (quote) \t\tstring = string.replace(/'/g, '''); \treturn string; } {$objectName}.GetGUITypeEdit = function(type) { \treturn ""; } {$objectName}.SetGUITypeEdit = function(type) { \treturn ""; } function __dump_bx(arr, limitLevel, txt) { \tif (limitLevel == undefined) \t\tlimitLevel = 3; \tif (txt == undefined) \t\ttxt = ""; \telse \t\ttxt += ":\\n"; \talert(txt+__dumpInternal_bx(arr, 0, limitLevel)); } function __dumpInternal_bx(arr, level, limitLevel) { \tvar dumped_text = ""; \tif(!level) level = 0; \tif (level > limitLevel) \t\treturn ""; \tvar level_padding = ""; \tfor(var j=0;j<level+1;j++) level_padding += " "; \tif(typeof(arr) == 'object') { \t\tfor(var item in arr) { \t\t\tvar value = arr[item]; \t\t\tif(typeof(value) == 'object') { \t\t\t\tdumped_text += level_padding + "'" + item + "' ...\\n"; \t\t\t\tdumped_text += __dumpInternal_bx(value, level+1, limitLevel); \t\t\t} else { \t\t\t\tdumped_text += level_padding + "'" + item + "' => '" + value + "'\\n"; \t\t\t} \t\t} \t} else { \t\tdumped_text = "===>"+arr+"<===("+typeof(arr)+")"; \t} \treturn dumped_text; } </script> 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; }