Пример #1
0
        public static bool set_element_value(string value, ref FormElement element)
        {
            if (string.IsNullOrEmpty(value))
            {
                value = string.Empty;
            }
            FormElementTypes type = element.Type.HasValue ? element.Type.Value : FormElementTypes.Text;

            switch (type)
            {
            case FormElementTypes.Text:
            case FormElementTypes.Select:
            case FormElementTypes.Checkbox:
                element.TextValue = value;
                return(!string.IsNullOrEmpty(element.TextValue));

            case FormElementTypes.Binary:
                element.BitValue = value.ToLower() == "true";
                return(true);

            case FormElementTypes.Date:
                if (value.ToLower() == "now")
                {
                    element.DateValue = DateTime.Now;
                }
                else
                {
                    DateTime dt = new DateTime();
                    if (DateTime.TryParse(value, out dt))
                    {
                        element.DateValue = dt;
                    }
                }
                return(element.DateValue.HasValue);

            case FormElementTypes.Node:
            case FormElementTypes.User:
                SelectedGuidItemType tp = type == FormElementTypes.Node ? SelectedGuidItemType.Node : SelectedGuidItemType.User;
                Guid id = Guid.Empty;
                if (Guid.TryParse(value, out id) && id != Guid.Empty)
                {
                    element.GuidItems.Add(new SelectedGuidItem(id, string.Empty, string.Empty, tp));
                }
                return(id != Guid.Empty);

            case FormElementTypes.Numeric:
                float flt = 0;
                if (float.TryParse(value, out flt))
                {
                    element.FloatValue = flt;
                }
                return(element.FloatValue.HasValue);
            }

            return(false);
        }
Пример #2
0
        public static List <FormElement> get_form_elements(string strElements, bool formDesignMode = false)
        {
            try
            {
                List <FormElement> retList = new List <FormElement>();

                if (string.IsNullOrEmpty(strElements))
                {
                    return(retList);
                }

                Dictionary <string, object> dic = PublicMethods.fromJSON(Base64.decode(strElements));

                if (!dic.ContainsKey("Elements") || dic["Elements"].GetType() != typeof(ArrayList))
                {
                    return(retList);
                }

                foreach (Dictionary <string, object> elem in (ArrayList)dic["Elements"])
                {
                    FormElement newElement = new FormElement()
                    {
                        ElementID      = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "ElementID")),
                        RefElementID   = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "RefElementID")),
                        FormInstanceID = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "InstanceID")),
                        Title          = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Title"), defaultValue: null),
                        Name           = formDesignMode ? Base64.decode(PublicMethods.get_dic_value(elem, "Name", defaultValue: null)) :
                                         PublicMethods.get_dic_value(elem, "Name", defaultValue: null),
                        Filled         = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "Filled"), defaultValue: false),
                        SequenceNumber = PublicMethods.parse_int(PublicMethods.get_dic_value(elem, "SequenceNumber"), defaultValue: -1),
                        Necessary      = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "Necessary")),
                        UniqueValue    = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "UniqueValue")),
                        Help           = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Help"), defaultValue: null),
                        Info           = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Info"), defaultValue: null),
                        Weight         = PublicMethods.parse_double(PublicMethods.get_dic_value(elem, "Weight")),
                        TextValue      = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "TextValue"), defaultValue: null),
                        FloatValue     = PublicMethods.parse_double(PublicMethods.get_dic_value(elem, "FloatValue")),
                        BitValue       = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "BitValue")),
                        DateValue      = PublicMethods.parse_date(PublicMethods.get_dic_value(elem, "DateValue")),
                        AttachedFiles  = DocumentUtilities.get_files_info(PublicMethods.get_dic_value(elem, "Files"))
                    };

                    if (newElement.Type == FormElementTypes.Separator)
                    {
                        newElement.Necessary = null;
                    }
                    if (newElement.Type != FormElementTypes.Text && newElement.Type != FormElementTypes.Numeric)
                    {
                        newElement.UniqueValue = null;
                    }

                    FormElementTypes fet = FormElementTypes.Text;
                    if (Enum.TryParse <FormElementTypes>(PublicMethods.get_dic_value(elem, "Type"), out fet))
                    {
                        newElement.Type = fet;
                    }

                    if (elem.ContainsKey("GuidItems") && elem["GuidItems"] != null && elem["GuidItems"].GetType() == typeof(ArrayList))
                    {
                        SelectedGuidItemType tp = SelectedGuidItemType.None;
                        if (newElement.Type == FormElementTypes.Node)
                        {
                            tp = SelectedGuidItemType.Node;
                        }
                        else if (newElement.Type == FormElementTypes.User)
                        {
                            tp = SelectedGuidItemType.User;
                        }

                        foreach (Dictionary <string, object> d in (ArrayList)elem["GuidItems"])
                        {
                            newElement.GuidItems.Add(new SelectedGuidItem(Guid.Parse((string)d["ID"]), code: string.Empty,
                                                                          name: Base64.decode((string)d["Name"]), type: tp));
                        }
                    }

                    if (newElement.ElementID.HasValue && !formDesignMode && (!newElement.Filled.HasValue || !newElement.Filled.Value))
                    {
                        newElement.RefElementID = newElement.ElementID;
                        newElement.ElementID    = Guid.NewGuid();
                    }

                    if (newElement.AttachedFiles != null)
                    {
                        for (var i = 0; i < newElement.AttachedFiles.Count; ++i)
                        {
                            newElement.AttachedFiles[i].OwnerID   = newElement.ElementID;
                            newElement.AttachedFiles[i].OwnerType = FileOwnerTypes.FormElement;
                        }
                    }

                    retList.Add(newElement);
                }

                return(retList);
            }
            catch { return(new List <FormElement>()); }
        }
Пример #3
0
        public static List <FormFilter> get_filters_from_json(Dictionary <string, object> dic,
                                                              Guid?ownerId = null, Guid?formId = null, Guid?applicationId = null)
        {
            List <FormFilter> retFilters = new List <FormFilter>();

            Dictionary <string, Guid> namesDic = new Dictionary <string, Guid>();

            if (formId.HasValue && applicationId.HasValue)
            {
                Guid          tempId     = Guid.Empty;
                List <String> fieldNames = dic.Keys.Where(i => !Guid.TryParse(i, out tempId)).ToList();
                namesDic = FGController.get_form_element_ids(applicationId.Value, formId.Value, fieldNames);
            }

            foreach (string id in dic.Keys)
            {
                try
                {
                    Guid elementId = namesDic.ContainsKey(id.ToLower()) ? namesDic[id.ToLower()] : Guid.Empty;

                    if ((elementId == Guid.Empty && !Guid.TryParse(id, out elementId)) ||
                        (dic[id] != null && dic[id].GetType() != typeof(Dictionary <string, object>)))
                    {
                        continue;
                    }

                    Dictionary <string, object> elemDic = (Dictionary <string, object>)dic[id];

                    FormElementTypes type = FormElementTypes.Text;
                    //if (!Enum.TryParse<FormElementTypes>(elemDic.ContainsKey("Type") ?
                    //    elemDic["Type"].ToString() : string.Empty, out type)) continue;
                    if (!Enum.TryParse <FormElementTypes>(PublicMethods.get_dic_value(elemDic, "Type", string.Empty), out type))
                    {
                        type = FormElementTypes.Text;
                    }

                    if (type == FormElementTypes.Form)
                    {
                        List <FormFilter> subFilters = get_filters_from_json(elemDic, elementId);

                        if (subFilters != null && subFilters.Count > 0)
                        {
                            retFilters.Add(new FormFilter()
                            {
                                ElementID = elementId, OwnerID = ownerId
                            });                                                                            //just to say that this field has filters
                            retFilters.AddRange(subFilters);
                        }

                        continue;
                    }

                    FormFilter filter = new FormFilter()
                    {
                        ElementID = elementId, OwnerID = ownerId
                    };

                    filter.Exact      = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Exact"));
                    filter.Or         = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Or"));
                    filter.Bit        = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Bit"));
                    filter.FloatFrom  = PublicMethods.parse_double(PublicMethods.get_dic_value(elemDic, "FloatFrom"));
                    filter.FloatTo    = PublicMethods.parse_double(PublicMethods.get_dic_value(elemDic, "FloatTo"));
                    filter.DateFrom   = PublicMethods.parse_date(PublicMethods.get_dic_value(elemDic, "DateFrom"));
                    filter.DateTo     = PublicMethods.parse_date(PublicMethods.get_dic_value(elemDic, "DateTo"), 1);
                    filter.Compulsory = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Compulsory"));

                    if (elemDic.ContainsKey("TextItems") && elemDic["TextItems"] != null &&
                        elemDic["TextItems"].GetType() == typeof(ArrayList))
                    {
                        ArrayList lst = (ArrayList)elemDic["TextItems"];
                        foreach (object o in lst)
                        {
                            if (o != null)
                            {
                                filter.TextItems.Add(Base64.decode(o.ToString()));
                            }
                        }
                    }

                    if (elemDic.ContainsKey("GuidItems") && elemDic["GuidItems"] != null &&
                        elemDic["GuidItems"].GetType() == typeof(ArrayList))
                    {
                        ArrayList lst = (ArrayList)elemDic["GuidItems"];
                        foreach (object o in lst)
                        {
                            Guid val = Guid.Empty;
                            if (Guid.TryParse(o.ToString(), out val))
                            {
                                filter.GuidItems.Add(val);
                            }
                        }
                    }

                    retFilters.Add(filter);
                }
                catch { }
            }

            return(retFilters);
        }
Пример #4
0
        private FormType activate_form(Guid applicationId, Guid currentUserId, string id)
        {
            TemplateForm tempForm = !Forms.ContainsKey(id) ? null : Forms[id];

            if (tempForm == null)
            {
                return(null);
            }

            FormType form = new FormType()
            {
                FormID         = IDs.new_id(id),
                TemplateFormID = IDs.get_id_from_template(id),
                Title          = Base64.decode(tempForm.Title) + " " + PublicMethods.get_random_number().ToString(),
                Creator        = new User()
                {
                    UserID = currentUserId
                }
            };

            if (!FGController.create_form(applicationId, form))
            {
                return(null);
            }

            List <FormElement> elements = tempForm.Elements == null ? new List <FormElement>() : tempForm.Elements.Select(e =>
            {
                FormElement newElem = new FormElement()
                {
                    ElementID         = IDs.new_id(e.ID),
                    TemplateElementID = IDs.get_id_from_template(e.ID),
                    Name   = Base64.decode(e.Code),
                    Title  = Base64.decode(e.Title),
                    Weight = e.Weight,
                    Help   = e.Help
                };

                e.Info = PublicMethods.fromJSON(PublicMethods.toJSON_typed(e.Info));

                FormElementTypes tp = FormElementTypes.Text;
                if (!Enum.TryParse <FormElementTypes>(e.Type, out tp))
                {
                    return(null);
                }
                else
                {
                    newElem.Type = tp;
                }

                if (tp == FormElementTypes.MultiLevel && e.Info.ContainsKey("NodeType"))
                {
                    Dictionary <string, object> infoNt = PublicMethods.get_dic_value <Dictionary <string, object> >(e.Info, "NodeType");

                    string _id = infoNt == null || !infoNt.ContainsKey("ID") ? null : infoNt["ID"].ToString();

                    NodeType nodeType = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                            CNController.get_node_type(applicationId, IDs.get_existing_id(_id).Value) :
                                                                            activate_node_type(applicationId, currentUserId, _id));

                    if (nodeType == null)
                    {
                        return(null);
                    }

                    ((Dictionary <string, object>)e.Info["NodeType"])["ID"]   = nodeType.NodeTypeID.Value.ToString();
                    ((Dictionary <string, object>)e.Info["NodeType"])["Name"] = Base64.encode(nodeType.Name);
                }
                else if (tp == FormElementTypes.Form && e.Info.ContainsKey("FormID"))
                {
                    string _id = e.Info["FormID"].ToString();

                    FormType elmForm = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                           FGController.get_form(applicationId, IDs.get_existing_id(_id).Value) :
                                                                           activate_form(applicationId, currentUserId, _id));

                    if (elmForm == null)
                    {
                        return(null);
                    }

                    e.Info["FormID"]   = elmForm.FormID.Value.ToString();
                    e.Info["FormName"] = Base64.encode(elmForm.Title);
                }
                else if (tp == FormElementTypes.Node && e.Info.ContainsKey("NodeTypes"))
                {
                    ArrayList infoNts = PublicMethods.get_dic_value <ArrayList>(e.Info, "NodeTypes");

                    List <Dictionary <string, object> > newNts = (infoNts == null ? new ArrayList() : infoNts).ToArray().ToList()
                                                                 .Where(itm => itm.GetType() == typeof(Dictionary <string, object>))
                                                                 .Select(itm => (Dictionary <string, object>)itm)
                                                                 .Where(itm => itm.ContainsKey("NodeTypeID")).ToList()
                                                                 .Select(itm =>
                    {
                        string _id = itm["NodeTypeID"].ToString();

                        NodeType nodeType = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                                CNController.get_node_type(applicationId, IDs.get_existing_id(_id).Value) :
                                                                                activate_node_type(applicationId, currentUserId, _id));

                        if (nodeType == null)
                        {
                            return(null);
                        }

                        itm["NodeTypeID"] = nodeType.NodeTypeID.Value;
                        itm["NodeType"]   = Base64.encode(nodeType.Name);

                        return(itm);
                    }).Where(itm => itm != null).ToList();

                    e.Info["NodeTypes"] = newNts;
                }

                newElem.Info = PublicMethods.toJSON(e.Info);

                return(newElem);
            }).Where(e => e != null).ToList();

            FGController.save_form_elements(applicationId, form.FormID.Value, null, null, null, elements, currentUserId);

            return(form);
        }