Пример #1
0
        public async Task <IActionResult> TemplateBuilder()
        {
            string type = string.Empty;
            string id   = string.Empty;

            if (QueryParameters != null && QueryParameters.Length > 0)
            {
                type = QueryParameters[0].ToString();
                id   = QueryParameters[1].ToString();
            }
            int templateid = 0;

            int.TryParse(id, out templateid);
            Post post = null;

            if (type != string.Empty && templateid > 0)
            {
                TemplatePost templatePost             = new TemplatePost();
                DynamicComponentController controller = new DynamicComponentController();
                bool isDevelopmentMode = CurrentHostEnvironment.IsDevelopment;
                templatePost.PostTypeFields  = "\"[]\"";
                templatePost.PostTemplateObj = "{}";
                templatePost.TemplateType    = type;
                Template postTemplate;
                templatePost.IsEdit = 0;
                //string componentJsPath = _hostingEnvironment.WebRootPath + "/DynamicPost/PostTemplate/Assets/js/component/exec_component.js";
                //if (System.IO.File.Exists(componentJsPath))
                //{
                //    System.IO.File.Delete(componentJsPath);
                //}
                //string componentJsPathLive = _hostingEnvironment.WebRootPath + "/DynamicPost/PostTemplate/Assets/js/component/exec_component.live.js";
                //if (!isDevelopmentMode && System.IO.File.Exists(componentJsPathLive))
                //{
                //    System.IO.File.Copy(componentJsPathLive, componentJsPath);
                //}

                if (type == "edit")
                {
                    postTemplate = await controller.GetTemplateById(templateid, GetSiteID);

                    if (postTemplate == null || postTemplate.TemplateId == 0)
                    {
                        throw new Exception("Template not found.");
                    }
                    post = await GetPostById(postTemplate.PostId);

                    templatePost.Post            = post;
                    templatePost.PostTypeFields  = post.Form;
                    templatePost.Template        = postTemplate;
                    templatePost.PostTemplateObj = Newtonsoft.Json.JsonConvert.SerializeObject(postTemplate);
                    templatePost.IsEdit          = 1;
                }
                else if (type == "list" || type == "detail")
                {
                    post = await GetPostById(templateid);

                    templatePost.Post                = post;
                    templatePost.PostTypeFields      = post.Form;
                    templatePost.Template            = new Template();
                    templatePost.Template.TemplateId = templateid;
                    if (type == "detail" && post.HasDetail == false)
                    {
                        throw new Exception(post.Name + " does not have detail.");
                    }
                }
                else
                {
                    throw new Exception("Invalid Post Template");
                }
                AddJS(new AssetLocation()
                {
                    FileName = "webbuilder_overrides", FilePath = "/DynamicPost/PostTemplate/Assets/js/webbuilder_overrides.js", Position = AssetPosition.Header
                });
                AddJS("handlebars", "/DynamicPost/Assets/handlebars/handlebars.min.js");
                AddJS("templates", "/DynamicPost/PostTemplate/Assets/js/handlebars/templates.js");
                //AddJS("webbuilder_overrides", "/DynamicPost/PostTemplate/Assets/js/webbuilder_overrides.js");
                AddJS("template_builder", "/DynamicPost/PostTemplate/Assets/js/template_builder.js");
                AddJS("TemplateBuilderPlugins", "/DynamicPost/PostTemplate/Assets/js/TemplateBuilderPlugins.js");
                AddJS("SageMediaManagement", "/cbuilderassets/js/SageMediaManagement.js");
                AddJS("html2canvas", "/DynamicPost/PostTemplate/Assets/js/html2canvas.min.js");
                if (isDevelopmentMode)
                {
                    AddJS("component_template", "/DynamicPost/PostTemplate/Assets/js/component_template.js");
                }
                else
                {
                    AddJS("component_template", "/DynamicPost/PostTemplate/Assets/js/component_template.live.js");
                }
                return(View(templatePost));
            }
            else
            {
                ActionMessage("No post found.", MessageType.Error);
                string redirectURI = nameof(PostTemplate);
                return(RedirectToAction(redirectURI));
            }
        }