public IEnumerable <Element> GetChildren(EntityToken entityToken, SearchToken searchToken)
        {
            if (entityToken is SharedCodeFolderEntityToken)
            {
                return(GetSharedCodeElements(searchToken));
            }

            if ((entityToken is PageTemplateRootEntityToken) == false)
            {
                return new Element[] { }
            }
            ;

            bool sharedFilesExist = PageTemplateFacade.GetSharedFiles().Any();

            IEnumerable <Element> result = sharedFilesExist
                ? new [] { GetSharedCodeElement() }
                : new Element[0];

            var pageTemplates = PageTemplateFacade.GetPageTemplates();

            if (searchToken.IsValidKeyword())
            {
                string keyword = searchToken.Keyword.ToLowerInvariant();

                pageTemplates = pageTemplates
                                .Where(t => t.Title.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > 0);
            }

            pageTemplates = pageTemplates.OrderBy(template => template.Title).ToList();

            return(result.Concat(GetElements(pageTemplates)));
        }
        private IEnumerable <Element> GetSharedCodeElements(SearchToken searchToken)
        {
            var result = new List <Element>();

            foreach (SharedFile sharedFile in PageTemplateFacade.GetSharedFiles())
            {
                string relativeFilePath = sharedFile.RelativeFilePath;

                string fullPath    = relativeFilePath.StartsWith("~") ? PathUtil.Resolve(relativeFilePath) : relativeFilePath;
                var    websiteFile = new WebsiteFile(fullPath);

                Element element = new Element(_context.CreateElementHandle(new SharedCodeFileEntityToken(relativeFilePath)))
                {
                    VisualData = new ElementVisualizedData()
                    {
                        Label       = websiteFile.FileName,
                        ToolTip     = websiteFile.FileName,
                        HasChildren = false,
                        Icon        = FileElementProvider.WebsiteFileIcon(websiteFile.MimeType),
                        OpenedIcon  = FileElementProvider.WebsiteFileIcon(websiteFile.MimeType)
                    }
                };

                element.PropertyBag.Add("Uri", PathUtil.GetWebsitePath(websiteFile.FullPath));
                element.PropertyBag.Add("ElementType", websiteFile.MimeType);

                // Adding "Edit" action for text-editable files
                if (sharedFile.DefaultEditAction && MimeTypeInfo.IsTextFile(websiteFile.MimeType))
                {
                    element.AddWorkflowAction(
                        "Composite.Plugins.Elements.ElementProviders.PageTemplateElementProvider.EditSharedCodeFileWorkflow",
                        new[] { PermissionType.Edit },
                        new ActionVisualizedData
                    {
                        Label          = GetResourceString("EditSharedCodeFile.Label"),
                        ToolTip        = GetResourceString("EditSharedCodeFile.ToolTip"),
                        Icon           = CommonCommandIcons.Edit,
                        Disabled       = websiteFile.IsReadOnly,
                        ActionLocation = new ActionLocation
                        {
                            ActionType  = ActionType.Edit,
                            IsInFolder  = false,
                            IsInToolbar = true,
                            ActionGroup = EditCodeFileActionGroup
                        }
                    });
                }

                var customActions = sharedFile.GetActions();
                foreach (var action in customActions)
                {
                    element.AddAction(action);
                }

                result.Add(element);
            }

            return(result);
        }
        private string GetFilePath()
        {
            var entityToken = (SharedCodeFileEntityToken)this.EntityToken;

            string relativeFilePath = entityToken.VirtualPath;

            // Security check that validates that the file is a Shared code file
            var sharedFiles = PageTemplateFacade.GetSharedFiles();

            Verify.That(sharedFiles.Any(sf => string.Compare(sf.RelativeFilePath, relativeFilePath, StringComparison.OrdinalIgnoreCase) == 0),
                        "There's no page template provider that would claim ownership over shared code file '{0}'");

            return(PathUtil.Resolve(relativeFilePath));
        }
        // private static readonly string HtmlMimeType = "text/html";

        private string[] GetFiles()
        {
            var entityToken = this.EntityToken;

            if (entityToken is PageTemplateEntityToken)
            {
                var masterTemplate = GetPageTemplate();

                return(masterTemplate.GetFiles());
            }

            if (entityToken is SharedCodeFileEntityToken)
            {
                var sharedFileEntityToken = (SharedCodeFileEntityToken)this.EntityToken;

                string relativeFilePath = sharedFileEntityToken.VirtualPath;

                // Security check that validates that the file is a Shared code file
                var sharedFiles = PageTemplateFacade.GetSharedFiles();

                Verify.That(sharedFiles.Any(sharedFile => string.Compare(sharedFile.RelativeFilePath, relativeFilePath, StringComparison.OrdinalIgnoreCase) == 0),
                            "There's no page template provider that would claim ownership over shared code file '{0}'");

                string fullPath       = PathUtil.Resolve(relativeFilePath);
                string codebehindFile = MasterPagePageTemplateProvider.GetCodebehindFilePath(fullPath);

                var result = new List <string>();
                result.Add(fullPath);

                if (codebehindFile != null)
                {
                    result.Add(codebehindFile);
                }

                return(result.ToArray());
            }

            throw new InvalidOperationException("Invalid entity token type '{0}'".FormatWith(entityToken.GetType().Name));
        }