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);
                }
            }
        }
    }