public void SaveCopiedContent (int contentId, int categoryId, int languageId)
    {
        Category category = Category.Find(categoryId);
        CheckPermissions(category, Permission.Modify);
        if (category == null)
        {
            Flash["error"] = "Categoria padre inexistente";
            RedirectToAction(Constants.CATEGORIES);
            return;
        }
        Content originalContent = Content.Find(contentId);
        Content content = new Content(category);
        content.Lang = Language.Find(languageId);
        if (content.Lang == null)
        {
            logger.Error("language is null!!!: " + languageId);
            return;
        }
        content.Save();  // Get an Id
        if (originalContent != null)
        {
            foreach(string key in originalContent.DataHash.Keys) // duplicate content
            {
                DataModel originalDataModel = (DataModel)originalContent.DataHash[key];
                DataModel copyDataModel = new DataModel(content, originalDataModel.Field, 
                                                        originalDataModel.Value);
                content.DataHash[key] = copyDataModel;
                copyDataModel.Save();
            }
            content.Save();
        }
        //contentId = content.Id;
        // Read form inputs and attached files
        bool emptyForm  = true;
        if (Request.Form.Count > 0 || Request.Files.Count > 0)
        {
            foreach (string input in Request.Form)
            {
                DataModel data;
                if (content.DataHash != null && content.DataHash.Contains(input))
                {
                    data = (DataModel) content.DataHash[input];
                    data.Value = Request.Form[input];
                }
                else
                {
                    Field field = Field.GetByName(input);
                    if (field == null)
                        continue; 			//  invalid field name
                    data = new DataModel(content, field, Request.Form[input]);
                }
                data.Save();
                emptyForm = false;
            }
            foreach (string input in Request.Files.Keys)
            {
                DataModel data;
                System.Web.HttpPostedFile postedFile = Request.Files[input] as System.Web.HttpPostedFile;
                if ((postedFile == null) || (postedFile.FileName.Length == 0) ||
                        (postedFile.ContentLength == 0))
                {
                    logger.Debug("File not uploaded");
                    continue;
                }
                else if (content.DataHash != null && content.DataHash.Contains(input))
                {
                    data = (DataModel) content.DataHash[input];
                    File oldfile = (File) data.GetObjectFromValue();
                    File file = new File( config.GetValue(Constants.MEDIA_FOLDER));
                    file.SaveAttach(postedFile);
                    data.Value = file.Id.ToString();
                    oldfile.RemoveAttach();
                }
                else
                {
                    Field field = Field.GetByName(input);
                    if (field == null)
                        continue;
                    File file = new File(config.GetValue(Constants.MEDIA_FOLDER));
                    file.SaveAttach(postedFile);
                    data = new DataModel(content, field, file.Id.ToString());
#if CACHE
                    content.DataHash[input] = data; // force update cache
#endif

                }
                data.Save();
                emptyForm = false;
            }
            if (emptyForm)
            {
                content.Delete();
            }
            else
            {
                bool isNew = true;
                foreach (Content c in category.ContentList)
                if (c.Id == content.Id)
                    isNew = false;
                if (isNew)
                {
                    category.ContentList.Add(content);
                    category.ContentListSortedByReverseDate.Add(content);
                    category.Save();
                    logger.Debug("save NEW content/category:" + content.Id +","+ category.Id);
                }
                else
                {
                    content.Save();
                    category.Save();
                    logger.Debug("save NOT NEW content/category:" + content.Id +","+ category.Id);
                }
            }
            Hashtable parameters = new Hashtable();
            parameters["Id"] = category.Id;
            RedirectToAction("viewcategory", parameters);
        }
    }
    private void SavePortalContent (int contentId, int categoryId, string language)
    {
        Category category = Category.Find(categoryId);
        CheckPermissions(category, Permission.Modify);
        if (category == null)
        {
            RedirectToAction(Constants.CATEGORIES);
            return;
        }
        Content content;
        if (contentId != 0)
        {
            content = Content.Find (contentId);
        }
        else
        {
            content = new Content(category);
            Language lang = Language.FindByName(language);
            if (lang != null)
                content.Lang = lang;
            content.Save(); //				r.Save();
        }
        contentId = content.Id;
        // Read form inputs and attached files
        bool emptyForm  = true;
        if (Request.Form.Count > 0 || Request.Files.Count > 0)
        {
            foreach (string input in Request.Form)
            {
                DataModel data;
                if (content.DataHash != null && content.DataHash.Contains(input))
                {
                    data = (DataModel) content.DataHash[input];
                    data.Value = Request.Form[input];
                }
                else
                {
                    Field field = Field.GetByName(input);
                    if (field == null)
                        continue; 			//  invalid field name
                    data = new DataModel(content, field, Request.Form[input]);
                }
                data.Save();
                emptyForm = false;
            }
            foreach (string input in Request.Files.Keys)
            {
                DataModel data;
                System.Web.HttpPostedFile postedFile = Request.Files[input] as System.Web.HttpPostedFile;
                if ((postedFile == null) || (postedFile.FileName.Length == 0) ||
                        (postedFile.ContentLength == 0))
                {
                    logger.Debug("File not uploaded");
                    continue;
                }
                else if (content.DataHash != null && content.DataHash.Contains(input))
                {
                    data = (DataModel) content.DataHash[input];
                    File oldfile = (File) data.GetObjectFromValue();
                    File file = new File( config.GetValue(Constants.MEDIA_FOLDER));
                    file.SaveAttach(postedFile);
                    data.Value = file.Id.ToString();
                    oldfile.RemoveAttach();
                }
                else
                {
                    Field field = Field.GetByName(input);
                    if (field == null)
                        continue;
                    File file = new File(config.GetValue(Constants.MEDIA_FOLDER));
                    file.SaveAttach(postedFile);
                    data = new DataModel(content, field, file.Id.ToString());
#if CACHE
                    content.DataHash[input] = data; // force update cache
#endif

                }
                data.Save();
                emptyForm = false;
            }
            if (emptyForm)
            {
                content.Delete();
            }
            else
            {
                bool isNew = true;
                foreach (Content c in category.ContentList)
                if (c.Id == content.Id)
                    isNew = false;
                if (isNew)
                {
                    category.ContentList.Add(content);
                    category.ContentListSortedByReverseDate.Add(content);
                    category.Save();
                    logger.Debug("save NEW content/category:" + content.Id +","+ category.Id);
                }
                else
                {
                    content.Save();
                    category.Save();
                    logger.Debug("save NOT NEW content/category:" + content.Id +","+ category.Id);
                }
            }
        }
    }
Esempio n. 3
0
		public void PopulateAllTemplates()
		{
		    Console.WriteLine("*** PopulateAllTemplates");
		    CastlePortal.Template[] templates = CastlePortal.Template.FindAll();
		    foreach (CastlePortal.Template template in templates)
		    {
		        Category category = new Category();
		        category.Name = "just testing...";
		        category.Template = template;
		        //category.Save();
		        Content content = new Content();
                content.Category = category;
                //content.Save();
		        category.ContentList = new System.Collections.ArrayList();
		        DataModel data = null;
		        if (template.Fields.Count > 0)
		        {
		            //Console.WriteLine("total fields= :" + template.Fields.Count);
		            foreach(CastlePortal.FieldTemplate fieldTemplate in template.Fields)
		            {
		                //Console.WriteLine("FIELD =" + fieldTemplate.Field.Name);
		                string name = fieldTemplate.Field.Name;
		                if ((name != "file") && (name != "image"))
		                {
		                    data = new DataModel(content, fieldTemplate.Field, "just a test field");
		                    content.DataHash[fieldTemplate.Field.Name] = data;
		                }
		                //data.Save();
		            }
		        }
		        category.ContentList.Add(content);
		        Console.WriteLine("Template:" + template.Id +","+ template.Name);
		        string path = Path.Combine(TestsCommons.TEMPLATESDIR, TestsCommons.GENERALTEMPLATES);
		        path = Path.Combine(path, template.Name);
		        path += TestsCommons.EXTENSION;
		        NVelocity.Template nvtemplate = velocity.GetTemplate(path);
		        VelocityContext context = new VelocityContext();
		        context.Put(TestsCommons.TEMPLATESDIRVAR, TestsCommons.TEMPLATESDIR);
                context.Put(TestsCommons.CATEGORYVAR, category);
                StringWriter writer = new StringWriter();
                nvtemplate.Merge(context, writer);
                if (data != null)
                {
                    //data.Delete();
                }
                //content.Delete();
                //category.Delete();
            }
		}
    private static string ReadDataModel(XmlNode node, Content content)
    {
        try
        {
            if (node.Name == "DataModel")
            {
                string val = NodeGetString(node, Constants.GENERATOR_VALUE);

                Field field = (Field)NodeGetObject(node, Constants.GENERATOR_FIELDCODE, fields);
                DataModel dm = new DataModel(content, field, val);
                dm.Save();
                System.Console.WriteLine ("DataModel: " + content.Category.Code);

                foreach (XmlNode n in node.ChildNodes)
                {
                    if (n.Name == "Value")
                    {
                        dm.Value = n.InnerText;
                        dm.Save();
                    }
                }

                foreach (XmlNode n in node.ChildNodes)
                {
                    if (n.Name == "File")
                    {
                        dm.Value = ReadFile(n).ToString();
                        dm.Save();
                    }
                }
            }

            return null;
        }
        catch(System.FormatException)
        {
            return "No se pudo leeer columna ordering";
        }
    }
    private static string ReadContent(XmlNode node)
    {
        try
        {
            if (node.Name == "Content")
            {
                Category category = (Category) NodeGetObject(node, Constants.GENERATOR_CATEGORYCODE, categories);
                if (category != null)
                {
//                string categorycode = NodeGetString(node, Constants.GENERATOR_CATEGORYCODE);
                    string lang = NodeGetString(node, Constants.GENERATOR_LANG);
                    DateTime creationdate = NodeGetDateTime(node, Constants.GENERATOR_CREATIONDATE);
                    bool published = NodeGetBool(node, Constants.GENERATOR_PUBLISHED);
                    bool frontpage = NodeGetBool(node, Constants.GENERATOR_FRONTPAGE);
                    bool sectionfrontpage = NodeGetBool(node, Constants.GENERATOR_SECTIONFRONTPAGE);

//                Category category = Category.FindByCode(categorycode);
                    Language l = Language.FindByName(lang);
                    Content c = new Content(category, l, published, frontpage, sectionfrontpage, creationdate);
                    c.Save();
                    contents[category.Code] = c;
                    System.Console.WriteLine ("Content: "+ creationdate.ToString() + " " + category.Code);

                    foreach (XmlNode n in node.ChildNodes)
                    {
                        if (n.Name == "DataModel")
                            ReadDataModel(n, c);
                    }
                }
                else
                {
                    string categorycode = NodeGetString(node, Constants.GENERATOR_CATEGORYCODE);
                    System.Console.WriteLine("No existe la categoria {0}", categorycode);
                }
            }

            return null;
        }
        catch(System.FormatException)
        {
            return "No se pudo leeer columna ordering";
        }
    }
    private static string ReadContent(XmlNode node, Category c)
    {
        string  keys = NodeGetString(node, "keys");
        string  vals = NodeGetString(node, "vals");

        string[] colname = keys.Split(',');
        string[] namedescr = vals.Split(',');

        int l = colname.Length;

        Field [] f = new Field[l];
        for (int i=0; i<l; i++)
            f[i] = Field.GetByName(colname[i]);

        Content r = new Content(c);
        r.Save();

        for (int i=0; i<l; i++)
        {
            DataModel d = new DataModel(r, f[i], namedescr[i]);
            d.Save();
        }

        return null;

    }