Friday, June 14, 2019

How to enable a few fields on a form data source

/// <summary>
/// Helper for form data source functionality
/// </summary>
class FormDataSourceHelper
{
    /// <summary>
    /// Allow edit for given fields only; the rest is non-editable
    /// </summary>
    /// <param name = "_fds">Caller form data source</param>
    /// <param name = "_fields">Container with fields numbers for allowing</param>
    public static client void allowEditFields(FormDataSource _fds, container _fields)
    {
        DictTable                           dictTable;
        int                                 fieldCnt, fieldNumber;
        Set                                 fieldsSet;
        
        if(!_fds)
        {
            throw Error(Error::wrongUseOfFunction(funcName()));
        }

        dictTable = new DictTable(_fds.table());
        
        if(!dictTable)
        {
            throw Error(Error::wrongUseOfFunction(funcName()));
        }
        // everything is fine if we are here already

        // first convert a given container of fields numbers to a set of unique values
        fieldsSet = new Set(Types::Integer);
        for(fieldCnt = 1 ; fieldCnt <= conLen(_fields); fieldCnt++)
        {
            fieldsSet.add(conPeek(_fields, fieldCnt));
        }
        // disbale a field if it is not included in the set
        for(fieldCnt = 1 ; fieldCnt <= dictTable.fieldCnt(); fieldCnt++)
        {
            fieldNumber = dictTable.fieldCnt2Id(fieldCnt);
            if(_fds.object(fieldNumber) && !fieldsSet.in(fieldNumber))
            {
                _fds.object(fieldNumber).allowEdit(false);
            }
        }
    }

}