Пример #1
0
        public string SaveTemplate(string uid, string name, string url, string path, string master, bool active)
        {
            //make sure the user has access
            RatnaUser user = base.ValidatedUser();
            if (!(IsAccessAllowed(user)))
            {
                return SendAccessDenied();
            }

            ServiceOutput output = new ServiceOutput();
            output.Success = false;

            Guid guid = Guid.Empty;
            if (Guid.TryParse(uid, out guid))
            {

                if (!string.IsNullOrEmpty(name) &&
                    !string.IsNullOrEmpty(url) &&
                    !string.IsNullOrEmpty(path) &&
                    !string.IsNullOrEmpty(master))
                {

                    try
                    {
                        // save the template
                        Template template = new Template()
                        {
                            Name = name,
                            TemplatePath = path,
                            MasterFileName = master,
                            UrlPath = url,
                            UId = guid
                        };

                        // add the template
                        TemplatePlugin.Instance.Add(template);

                        if (active)
                        {
                            TemplatePlugin.Instance.Activate(template);
                        }
                        else
                        {
                            TemplatePlugin.Instance.Deactivate(template);
                        }

                        output.Success = true;
                    }
                    catch (MessageException me)
                    {
                        output.AddOutput(Constants.Json.Error, me.Message);
                    }
                }
            }

            return output.GetJson();
        }
Пример #2
0
        public void Add(Template template)
        {
            #region argument checking

            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            if (!template.IsValid())
            {
                throw new InvalidOperationException("template is invalid");
            }

            #endregion

            try
            {
                // add the template
                PluginStore.Instance.Save(this, template);

                Invalidate();
            }
            catch (MessageException me)
            {
                if (me.ErrorNumber == PluginErrorCodes.IdAlreadyInUse)
                {
                    // throw with a new message
                    throw new MessageException(
                            TemplateErrorCodes.PathAlreadyInUse,
                            ResourceManager.GetLiteral("Admin.Templates.Upload.PathAlreadyInUse")
                        );

                }

                throw;
            }
        }
Пример #3
0
        public void Deactivate(Template template)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            if (template.IsActivated)
            {
                template.IsActivated = false;
                Save(template);

                Invalidate();
            }
        }
Пример #4
0
        public override void Saved(HttpRequest request, string actualPath, string vpath)
        {
            #region arguments

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            #endregion

            // once the template has been uploaded.
            // unzip the files and delete the uploaded file
            string templateName = request["uploadtemplatename"];
            string templateUrlPath = request["uploadtemplateurlPath"];
            if (string.IsNullOrEmpty(templateName))
            {
                templateName = Constants.DefaultTemplateName;
            }

            if (string.IsNullOrEmpty(templateUrlPath))
            {
                templateUrlPath = Constants.RootUrl;
            }

            //get the virtual path of the template extraction
            string templateVirtualPath = TemplatePlugin.GetTemplateVirtualPath(templateName);

            //convert virtual path to real path
            string templateExtractPath = request.MapPath(templateVirtualPath);
            logger.Log(LogLevel.Debug, "Template file uploaded at : {0}.", actualPath);

            try
            {

                string master = null;

                using (ZipFile zip = ZipFile.Read(actualPath))
                {
                    master = GetMasterFileName(zip);

                    if (Directory.Exists(templateExtractPath))
                    {
                        logger.Log(LogLevel.Info, "Cleaning folder {0}.", templateExtractPath);
                        Directory.Delete(templateExtractPath, true);
                    }

                    // create or empty the existing template data
                    if (!Directory.Exists(templateExtractPath))
                    {
                        logger.Log(LogLevel.Info, "Creating folder {0}.", templateExtractPath);
                        Directory.CreateDirectory(templateExtractPath);
                    }

                    logger.Log(LogLevel.Info, "Extracting template at folder {0}.", templateExtractPath);

                    foreach (ZipEntry entry in zip)
                    {
                        entry.Extract(templateExtractPath, ExtractExistingFileAction.OverwriteSilently);
                    }
                }

                // save the template.
                Template template = TemplatePlugin.Instance.GetTemplateUrlPath(templateUrlPath);
                Template newTemplate = new Template()
                {
                    Name = templateName,
                    TemplatePath = templateName,
                    MasterFileName = master,
                    UrlPath = templateUrlPath
                };

                if (template != null)
                {
                    //delete the existing template.
                    TemplatePlugin.Instance.Delete(template.UId);
                }

                TemplatePlugin.Instance.Add(newTemplate);
                Dictionary<string, object> properties = new Dictionary<string, object>(1);
                properties["Id"] = newTemplate.Id;
                this.Properties = properties;
                this.Success = true;
            }
            finally
            {
                //delete the uploaded zip file
                this.DeleteUploaded = true;
            }
        }