CBPVirtualDocument::GetFieldInputControl: Bitrix method

      
<?php 
//  CBPVirtualDocument :: GetFieldInputControl()
//  /dist/bitrix/modules/bizproc/classes/general/virtualdocument.php:158

    
function GetFieldInputControl($documentType$arFieldType$arFieldName$fieldValue$bAllowSelection false$publicMode false)
    {
        
$v substr($documentTypestrlen("type_"));
        if (
intval($v)."!" != $v."!")
            return 
"";
        
$iblockId intval($v);

        static 
$arDocumentFieldTypes = array();
        if (!
array_key_exists($documentType$arDocumentFieldTypes))
            
$arDocumentFieldTypes[$documentType] = self::GetDocumentFieldTypes($documentType);

        
$arFieldType["BaseType"] = "string";
        
$arFieldType["Complex"] = false;
        if (
array_key_exists($arFieldType["Type"], $arDocumentFieldTypes[$documentType]))
        {
            
$arFieldType["BaseType"] = $arDocumentFieldTypes[$documentType][$arFieldType["Type"]]["BaseType"];
            
$arFieldType["Complex"] = $arDocumentFieldTypes[$documentType][$arFieldType["Type"]]["Complex"];
        }

        if (!
is_array($fieldValue) || is_array($fieldValue) && CBPHelper::IsAssociativeArray($fieldValue))
            
$fieldValue = array($fieldValue);

        
$customMethodName "";
        
$customMethodNameMulty "";
        if (
strpos($arFieldType["Type"], ":") !== false)
        {
            
$ar CIBlockProperty::GetUserType(substr($arFieldType["Type"], 2));
            if (
array_key_exists("GetPublicEditHTML"$ar))
                
$customMethodName $ar["GetPublicEditHTML"];
            if (
array_key_exists("GetPublicEditHTMLMulty"$ar))
                
$customMethodNameMulty $ar["GetPublicEditHTMLMulty"];
        }

        
ob_start();

        if (
$arFieldType["Type"] == "L")
        {
            
$fieldValueTmp $fieldValue;
            
?>
            <select id="id_<?= htmlspecialcharsbx($arFieldName["Field"]) ?>" name="<?= htmlspecialcharsbx($arFieldName["Field"]).($arFieldType["Multiple"] ? "[]" ""?>"<?= ($arFieldType["Multiple"] ? ' size="5" multiple' ''?>>
                <?
                
if (!$arFieldType["Required"])
                    echo 
'<option value="">['.GetMessage("BPVDX_NOT_SET").']</option>';
                foreach (
$arFieldType["Options"] as $k => $v)
                {
                    if (
is_array($v) && count($v) == 2)
                    {
                        
$v1 array_values($v);
                        
$k $v1[0];
                        
$v $v1[1];
                    }

                    
$ind array_search($k$fieldValueTmp);
                    echo 
'<option value="'.htmlspecialcharsbx($k).'"'.($ind !== false ' selected' '').'>'.htmlspecialcharsbx($v).'</option>';
                    if (
$ind !== false)
                        unset(
$fieldValueTmp[$ind]);
                }
                
?>
            </select>
            <?
            
if ($bAllowSelection)
            {
                
?>
                <br /><input type="text" id="id_<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" name="<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" value="<?
                
if (count($fieldValueTmp) > 0)
                {
                    
$a array_values($fieldValueTmp);
                    echo 
htmlspecialcharsbx($a[0]);
                }
                
?>"><?
                
echo CBPHelper::renderControlSelectorButton('id_'.$arFieldName["Field"].'_text''select');
            }
        }
        elseif (
$arFieldType["Type"] == "S:UserID")
        {
            
$fieldValue CBPHelper::UsersArrayToString($fieldValuenull, array("bizproc""CBPVirtualDocument"$documentType));
            
?><input type="text" size="40" style="max-width: 85%" id="id_<?= $arFieldName["Field"?>" name="<?= $arFieldName["Field"?>" value="<?= htmlspecialcharsbx($fieldValue?>">
            <? echo CBPHelper::renderControlSelectorButton('id_'.$arFieldName["Field"], 'user');
        }
        elseif ((
strpos($arFieldType["Type"], ":") !== false)
            && 
$arFieldType["Multiple"]
            && (
                
is_array($customMethodNameMulty) && count($customMethodNameMulty) > 0
                
|| !is_array($customMethodNameMulty) && strlen($customMethodNameMulty) > 0
            
)
        )
        {
            if (!
is_array($fieldValue))
                
$fieldValue = array();

            if (
$bAllowSelection)
            {
                
$fieldValueTmp1 = array();
                
$fieldValueTmp2 = array();
                foreach (
$fieldValue as $v)
                {
                    
$vTrim trim($v);
                    if (
CBPActivity::isExpression($vTrim))
                        
$fieldValueTmp1[] = $vTrim;
                    else
                        
$fieldValueTmp2[] = $v;
                }
            }
            else
            {
                
$fieldValueTmp1 = array();
                
$fieldValueTmp2 $fieldValue;
            }

            if ((
$arFieldType["Type"] == "S:employee") && COption::GetOptionString("bizproc""employee_compatible_mode""N") != "Y")
                
$fieldValueTmp2 CBPHelper::StripUserPrefix($fieldValueTmp2);
            if (
$arFieldType["Type"] == "E:EList")
            {
                static 
$fl true;
                if (
$fl)
                {
                    
$GLOBALS["APPLICATION"]->AddHeadScript('/bitrix/js/iblock/iblock_edit.js');
                }

                
$fl false;
            }

            
$fieldValueTmp21 = array();
            foreach (
$fieldValueTmp2 as $k => $fld)
            {
                if (
$fld === null || $fld === "")
                    continue;
                if (
is_array($fld) && isset($fld["VALUE"]))
                    
$fieldValueTmp21[$k] = $fld;
                else
                    
$fieldValueTmp21[$k] = array("VALUE" => $fld);
            }
            
$fieldValueTmp2 $fieldValueTmp21;

            echo 
call_user_func_array(
                
$customMethodNameMulty,
                array(
                    array(
"LINK_IBLOCK_ID" => $arFieldType["Options"]),
                    
$fieldValueTmp2,
                    array(
                        
"FORM_NAME" => $arFieldName["Form"],
                        
"VALUE" => htmlspecialcharsbx($arFieldName["Field"])
                    ),
                    
true
                
)
            );

            if (
$bAllowSelection)
            {
                
?>
                <br /><input type="text" id="id_<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" name="<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" value="<?
                
if (count($fieldValueTmp1) > 0)
                {
                    
$a array_values($fieldValueTmp1);
                    echo 
htmlspecialcharsbx($a[0]);
                }
                
?>"><?
                
echo CBPHelper::renderControlSelectorButton(
                    
'id_'.$arFieldName["Field"].'_text',
                    
$arFieldType["BaseType"],
                    array(
'mode' => $arFieldType["Type"] == 'S:employee''employee' '')
                );
            }
        }
        else
        {
            if (!
array_key_exists("CBPVirtualDocumentCloneRowPrinted_".$documentType$GLOBALS) && $arFieldType["Multiple"])
            {
                
$GLOBALS["CBPVirtualDocumentCloneRowPrinted_".$documentType] = 1;
                
?>
                <script language="JavaScript">
                <!--
                function CBPVirtualDocumentCloneRow(tableID)
                {
                    var tbl = document.getElementById(tableID);
                    var cnt = tbl.rows.length;
                    var oRow = tbl.insertRow(cnt);
                    var oCell = oRow.insertCell(0);
                    var sHTML = tbl.rows[cnt - 1].cells[0].innerHTML;
                    var p = 0;
                    while (true)
                    {
                        var s = sHTML.indexOf('[n', p);
                        if (s < 0)
                            break;
                        var e = sHTML.indexOf(']', s);
                        if (e < 0)
                            break;
                        var n = parseInt(sHTML.substr(s + 2, e - s));
                        sHTML = sHTML.substr(0, s) + '[n' + (++n) + ']' + sHTML.substr(e + 1);
                        p = s + 1;
                    }
                    var p = 0;
                    while (true)
                    {
                        var s = sHTML.indexOf('__n', p);
                        if (s < 0)
                            break;
                        var e = sHTML.indexOf('_', s + 2);
                        if (e < 0)
                            break;
                        var n = parseInt(sHTML.substr(s + 3, e - s));
                        sHTML = sHTML.substr(0, s) + '__n' + (++n) + '_' + sHTML.substr(e + 1);
                        p = e + 1;
                    }
                    oCell.innerHTML = sHTML;
                    var patt = new RegExp('<' + 'script' + '>[^\000]*?<' + '\/' + 'script' + '>', 'ig');
                    var code = sHTML.match(patt);
                    if (code)
                    {
                        for (var i = 0; i < code.length; i++)
                        {
                            if (code[i] != '')
                            {
                                var s = code[i].substring(8, code[i].length - 9);
                                jsUtils.EvalGlobal(s);
                            }
                        }
                    }
                }
                //-->
                </script>
                <?
            
}

            if (
$arFieldType["Multiple"])
                echo 
'<table width="100%" border="0" cellpadding="2" cellspacing="2" id="CBPVirtualDocument_'.$arFieldName["Field"].'_Table">';

            
$fieldValueTmp $fieldValue;

            if (
sizeof($fieldValue) == 0)
                
$fieldValue[] = null;

            
$ind = -1;
            foreach (
$fieldValue as $key => $value)
            {
                
$ind++;
                
$fieldNameId 'id_'.htmlspecialcharsbx($arFieldName["Field"]).'__n'.$ind.'_';
                
$fieldNameName htmlspecialcharsbx($arFieldName["Field"]).($arFieldType["Multiple"] ? "[n".$ind."]" "");

                if (
$arFieldType["Multiple"])
                    echo 
'<tr><td>';

                if (
is_array($customMethodName) && count($customMethodName) > || !is_array($customMethodName) && strlen($customMethodName) > 0)
                {
                    
$value1 $value;
                    if (
$bAllowSelection && CBPActivity::isExpression($value1))
                        
$value1 null;
                    else
                        unset(
$fieldValueTmp[$key]);

                    if ((
$arFieldType["Type"] == "S:employee") && COption::GetOptionString("bizproc""employee_compatible_mode""N") != "Y")
                        
$value1 CBPHelper::StripUserPrefix($value1);

                    echo 
call_user_func_array(
                        
$customMethodName,
                        array(
                            array(
"LINK_IBLOCK_ID" => $arFieldType["Options"]),
                            array(
"VALUE" => $value1),
                            array(
                                
"FORM_NAME" => $arFieldName["Form"],
                                
"VALUE" => $fieldNameName
                            
),
                            
true
                        
)
                    );
                }
                else
                {
                    switch (
$arFieldType["Type"])
                    {
                        case 
"N":
                            unset(
$fieldValueTmp[$key]);
                            
?><input type="text" size="10" id="<?= $fieldNameId ?>" name="<?= $fieldNameName ?>" value="<?= htmlspecialcharsbx($value?>"><?
                            
break;
                        case 
"F":
                            if (
$publicMode)
                            {
                                
//unset($fieldValueTmp[$key]);
                                
?><input type="file" id="<?= $fieldNameId ?>" name="<?= $fieldNameName ?>"><?
                            
}
                            break;
                        case 
"B":
                            if (
in_array($value, array("Y""N")))
                                unset(
$fieldValueTmp[$key]);
                            
?>
                            <select id="<?= $fieldNameId ?>" name="<?= $fieldNameName ?>">
                                <?
                                
if (!$arFieldType["Required"])
                                    echo 
'<option value="">['.GetMessage("BPVDX_NOT_SET").']</option>';
                                
?>
                                <option value="Y"<?= (in_array("Y"$fieldValue) ? ' selected' ''?>><?= GetMessage("BPVDX_YES"?></option>
                                <option value="N"<?= (in_array("N"$fieldValue) ? ' selected' ''?>><?= GetMessage("BPVDX_NO"?></option>
                            </select>
                            <?
                            
break;
                        case 
"T":
                            unset(
$fieldValueTmp[$key]);
                            
?><textarea rows="5" cols="40" id="<?= $fieldNameId ?>" name="<?= $fieldNameName ?>"><?= htmlspecialcharsbx($value?></textarea><?
                            
break;
                        default:
                            unset(
$fieldValueTmp[$key]);
                            
?><input type="text" size="40" id="<?= $fieldNameId ?>" name="<?= $fieldNameName ?>" value="<?= htmlspecialcharsbx($value?>"><?
                    
}
                }

                if (
$bAllowSelection)
                {
                    if (!
in_array($arFieldType["Type"], array("F""B")) && (is_array($customMethodName) && count($customMethodName) <= || !is_array($customMethodName) && strlen($customMethodName) <= 0))
                    {
                        echo 
CBPHelper::renderControlSelectorButton($fieldNameId$arFieldType["BaseType"]);
                    }
                }

                if (
$arFieldType["Multiple"])
                    echo 
'</td></tr>';
            }

            if (
$arFieldType["Multiple"])
                echo 
"</table>";

            if (
$arFieldType["Multiple"] && (($arFieldType["Type"] != "F") || $publicMode))
                echo 
'<input type="button" value="'.GetMessage("BPVDX_ADD").'" onclick="CBPVirtualDocumentCloneRow(\'CBPVirtualDocument_'.htmlspecialcharsbx($arFieldName["Field"]).'_Table\')"/><br />';

            if (
$bAllowSelection)
            {
                if (
in_array($arFieldType["Type"], array("F""B")) || (is_array($customMethodName) && count($customMethodName) > || !is_array($customMethodName) && strlen($customMethodName) > 0))
                {
                    
?>
                    <input type="text" id="id_<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" name="<?= htmlspecialcharsbx($arFieldName["Field"]) ?>_text" value="<?
                    
if (count($fieldValueTmp) > 0)
                    {
                        
$a array_values($fieldValueTmp);
                        echo 
htmlspecialcharsbx($a[0]);
                    }
                    
?>"><?
                    
echo CBPHelper::renderControlSelectorButton(
                        
'id_'.$arFieldName["Field"].'_text',
                        
$arFieldType["BaseType"],
                        array(
'mode' => $arFieldType["Type"] == 'S:employee''employee' '')
                    );
                }
            }
        }

        
$s ob_get_contents();
        
ob_end_clean();

        return 
$s;
    }