Esempio n. 1
0
    private void SetupControl()
    {
        pageTree.LineImagesFolder   = "~/CMSModules/RelationshipsExtended/Controls/RelatedCategories_Files";
        pageTree.NodeStyle.CssClass = "InputNode";
        pageTree.ShowLines          = true;

        // Build a list of the pages
        var docQuery = new DocumentQuery().OrderBy("NodeLevel, NodeOrder");

        foreach (string Path in StartingPathArray)
        {
            docQuery.Path(Path, PathTypeEnum.Section);
        }
        if (!string.IsNullOrWhiteSpace(RelatedNodeSiteName))
        {
            docQuery.OnSite(RelatedNodeSiteName);
        }
        List <CMS.DocumentEngine.TreeNode> Nodes = docQuery.TypedResult.ToList();

        // Get existing selected nodes
        string where = string.Format("NodeClassID in (select ClassID from CMS_Class where ClassName in ('{0}'))",
                                     string.Join("','", AllowedPageTypes.Split(";| ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)));

        // Split off Where condition for if they provide a Where to filter out other items.
        string AdditionalWhere = where;

        // Filter to show items not already selected
        if (ddlCurrentNodeDirection.SelectedValue == "LeftNode")
        {
            where = SqlHelper.AddWhereCondition(where,
                                                string.Format("({0})",
                                                              string.Format("NodeID in (Select RightNodeID from CMS_Relationship where LeftNodeID = {1} and RelationshipNameID in (Select RelationshipNameID from CMS_RelationshipName where RelationshipName = '{0}'))",
                                                                            RelationshipName, CurrentNodeID)));
        }
        else
        {
            where = SqlHelper.AddWhereCondition(where,
                                                string.Format("({0})",
                                                              string.Format("NodeID in (Select LeftNodeID from CMS_Relationship where RightNodeID = {1} and RelationshipNameID in (Select RelationshipNameID from CMS_RelationshipName where RelationshipName = '{0}'))",
                                                                            RelationshipName, CurrentNodeID)));
        }

        where = SqlHelper.AddWhereCondition(where, string.Format("NodeID <> {0}", CurrentNodeID));

        AlreadySelectedNodes = new DocumentQuery().Where(where).Columns("NodeID").Select(x => x.NodeID).ToList();

        // Exclude the current node, can't relate a node to itself.
        AlreadySelectedNodes.Add(CurrentNodeID);

        // If the WhereCondition is set, also add any Nodes that match this to "Already selected" so they can't be selected
        // Filter on the where condition if given
        FilterSelectableNodes = false;
        List <int> VisibleNodes = new List <int>();

        if (!string.IsNullOrWhiteSpace(WhereCondition))
        {
            AdditionalWhere       = SqlHelper.AddWhereCondition(AdditionalWhere, WhereCondition);
            FilterSelectableNodes = true;
            SelectableSelectedNodes.AddRange(new DocumentQuery().Where(AdditionalWhere).Columns("NodeID").Select(x => x.NodeID).ToList());
        }

        pageTree.Nodes.Clear();

        TreeNode RootNode = new TreeNode("[Tree Root]", "0")
        {
            SelectAction = TreeNodeSelectAction.None
        };
        Dictionary <int, TreeNode> NodeIDToTreeNode = new Dictionary <int, TreeNode>();

        NodeIDToTreeNode.Add(0, RootNode);

        // Build the tree
        for (int i = 0; i < Nodes.Count(); i++)
        {
            var Node = Nodes[i];

            // Skip Root node
            if (string.IsNullOrWhiteSpace(Node.NodeName))
            {
                continue;
            }

            TreeNode newNode = CreateTreeNode(Node);
            if (!NodeIDToTreeNode.ContainsKey(Node.NodeID))
            {
                NodeIDToTreeNode.Add(Node.NodeID, newNode);
            }

            // Add to the parent if it exists, if it doesn't then add to root.
            if (NodeIDToTreeNode.ContainsKey(Node.NodeParentID))
            {
                NodeIDToTreeNode[Node.NodeParentID].ChildNodes.Add(newNode);
            }
            else
            {
                NodeIDToTreeNode[0].ChildNodes.Add(newNode);
            }
        }

        if (RemoveUnselectableChildTrees)
        {
            HideUnselectableChildren(RootNode);
        }

        // Add root
        pageTree.Nodes.Add(RootNode);

        if (SelectionMode == "Checkbox")
        {
            btnAdd.Visible = true;
        }
        else
        {
            btnAdd.Visible = false;
            pageTree.SelectedNodeChanged += PageTree_SelectedNodeChanged;
        }
    }