/// <summary> /// Update item /// </summary> /// <param name="item">Custom field</param> /// <returns>True on success</returns> public bool Update(CustomField item) { if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages)) throw new System.UnauthorizedAccessException(); try { BlogEngine.Core.Providers.BlogService.SaveCustomField(item); CustomFieldsParser.ClearCache(); return true; } catch (Exception ex) { Utils.Log("Error updaging custom field", ex); return false; } }
/// <summary> /// Update item /// </summary> /// <param name="item">Custom field</param> /// <returns>True on success</returns> public bool Update(CustomField item) { if (!Security.IsAuthorizedTo(Rights.AccessAdminPages)) throw new UnauthorizedAccessException(); try { item.BlogId = Blog.CurrentInstance.Id; Providers.BlogService.SaveCustomField(item); CustomFieldsParser.ClearCache(); return true; } catch (Exception ex) { Utils.Log("Error updaging custom field", ex); return false; } }
/// <summary> /// Add new item /// </summary> /// <param name="item">Custom field</param> /// <returns>Added field</returns> public CustomField Add(CustomField item) { if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages)) throw new System.UnauthorizedAccessException(); try { if (AlreadyExists(item)) throw new ApplicationException("Custom field already exists"); BlogEngine.Core.Providers.BlogService.SaveCustomField(item); CustomFieldsParser.ClearCache(); return item; } catch (Exception ex) { Utils.Log("Error adding custom field", ex); throw; } }
static CustomField FindInCollection(List<CustomField> items, CustomField item) { foreach (var i in items) { if (i.CustomType == item.CustomType && i.ObjectId == item.ObjectId && i.Key == item.Key) return i; } return null; }
/// <summary> /// Deletes custom field /// </summary> /// <param name="field">Object field</param> public override void DeleteCustomField(CustomField field) { using (var conn = this.CreateConnection()) { if (conn.HasConnection) { var sqlQuery = string.Format("delete from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key", this.tablePrefix, this.parmPrefix); using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); cmd.ExecuteNonQuery(); } } } }
/// <summary> /// Fills list of custom fields for a blog /// </summary> /// <returns>List of custom fields</returns> public override List<CustomField> FillCustomFields() { var items = new List<CustomField>(); using (var conn = this.CreateConnection()) { if (conn.HasConnection) { using (var cmd = conn.CreateTextCommand(string.Format("SELECT CustomType, BlogId, ObjectId, [Key], [Value], [Attribute] FROM {0}CustomFields where BlogId = '{1}'", tablePrefix, Blog.CurrentInstance.Id.ToString()))) { using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var n = new CustomField() { CustomType = rdr.GetString(0), BlogId = rdr.GetGuid(1), ObjectId = rdr.GetString(2), Key = rdr.GetString(3), Value = rdr.GetString(4), Attribute = rdr.GetString(5) }; items.Add(n); } } } } } return items; }
/// <summary> /// Saves custom field /// </summary> /// <param name="field">Object custom field</param> public override void SaveCustomField(CustomField field) { using (var conn = this.CreateConnection()) { if (conn.HasConnection) { var sqlQuery = string.Format("select count(*) from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key", this.tablePrefix, this.parmPrefix); object cnt; using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), field.BlogId)); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); cnt = cmd.ExecuteScalar(); } if (int.Parse(cnt.ToString()) > 0) sqlQuery = string.Format("update {0}CustomFields set Value = {1}value, Attribute = {1}attribute where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key", this.tablePrefix, this.parmPrefix); else sqlQuery = string.Format("insert into {0}CustomFields (CustomType, BlogId, ObjectId, [Key], [Value], [Attribute]) values ({1}customtype, {1}blogid, {1}objectid, {1}key, {1}value, {1}attribute)", this.tablePrefix, this.parmPrefix); using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); p.Add(conn.CreateParameter(FormatParamName("value"), field.Value)); p.Add(conn.CreateParameter(FormatParamName("attribute"), field.Attribute)); cmd.ExecuteNonQuery(); } } } }
/// <summary> /// Deletes custom field /// </summary> /// <param name="field">Object field</param> public override void DeleteCustomField(CustomField field) { using (var conn = this.CreateConnection()) { if (conn.HasConnection) { string conPrv = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ProviderName; var sqlQuery = "delete from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key"; if (conPrv == "MySql.Data.MySqlClient") { sqlQuery = "delete from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and `Key` = {1}key"; } using (var cmd = conn.CreateTextCommand(string.Format(sqlQuery, this.tablePrefix, this.parmPrefix))) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); cmd.ExecuteNonQuery(); } } } }
/// <summary> /// Fills list of custom fields for a blog /// </summary> /// <returns>List of custom fields</returns> public override List<CustomField> FillCustomFields() { var items = new List<CustomField>(); string conPrv = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ProviderName; var q = "SELECT CustomType, BlogId, ObjectId, [Key], [Value], [Attribute] FROM {0}CustomFields where BlogId = '{1}'"; if (conPrv == "MySql.Data.MySqlClient") { q = "SELECT CustomType, BlogId, ObjectId, `Key`, `Value`, `Attribute` FROM {0}CustomFields where BlogId = '{1}'"; } using (var conn = this.CreateConnection()) { if (conn.HasConnection) { using (var cmd = conn.CreateTextCommand(string.Format(q, tablePrefix, Blog.CurrentInstance.Id.ToString()))) { using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var n = new CustomField() { CustomType = rdr.GetString(0), BlogId = rdr.GetGuid(1), ObjectId = rdr.GetString(2), Key = rdr.GetString(3), Value = rdr.GetString(4), Attribute = rdr.GetString(5) }; items.Add(n); } } } } } return items; }
/// <summary> /// Saves custom field /// </summary> /// <param name="field">Object custom field</param> public override void SaveCustomField(CustomField field) { using (var conn = this.CreateConnection()) { if (conn.HasConnection) { string conPrv = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ProviderName; var q1 = "select count(*) from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key"; var q2 = "insert into {0}CustomFields (CustomType, BlogId, ObjectId, [Key], [Value], [Attribute]) values ({1}customtype, {1}blogid, {1}objectid, {1}key, {1}value, {1}attribute)"; var q3 = "update {0}CustomFields set Value = {1}value, Attribute = {1}attribute where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and [Key] = {1}key"; if (conPrv == "MySql.Data.MySqlClient") { q1 = "select count(*) from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and `Key` = {1}key"; q2 = "insert into {0}CustomFields (CustomType, BlogId, ObjectId, `Key`, `Value`, `Attribute`) values ({1}customtype, {1}blogid, {1}objectid, {1}key, {1}value, {1}attribute)"; q3 = "update {0}CustomFields set Value = {1}value, Attribute = {1}attribute where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid and `Key` = {1}key"; } var sqlQuery = string.Format(q1, this.tablePrefix, this.parmPrefix); object cnt; using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), field.BlogId)); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); cnt = cmd.ExecuteScalar(); } if (int.Parse(cnt.ToString()) > 0) sqlQuery = string.Format(q3, this.tablePrefix, this.parmPrefix); else sqlQuery = string.Format(q2, this.tablePrefix, this.parmPrefix); using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("customtype"), field.CustomType)); p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("objectid"), field.ObjectId)); p.Add(conn.CreateParameter(FormatParamName("key"), field.Key)); p.Add(conn.CreateParameter(FormatParamName("value"), field.Value)); p.Add(conn.CreateParameter(FormatParamName("attribute"), (field.Attribute != null ? field.Attribute : string.Empty))); cmd.ExecuteNonQuery(); } } } }
/// <summary> /// Delete item /// </summary> /// <param name="type">Type (theme, post etc)</param> /// <param name="id">Id, for example "standard" for a theme</param> /// <param name="key">Key in the key/value for a field</param> /// <returns>True on success</returns> public bool Remove(string type, string id, string key) { if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages)) throw new System.UnauthorizedAccessException(); try { var item = new CustomField { CustomType = type, BlogId = Blog.CurrentInstance.BlogId, ObjectId = id, Key = key }; BlogEngine.Core.Providers.BlogService.DeleteCustomField(item); CustomFieldsParser.ClearCache(); return true; } catch (Exception ex) { Utils.Log("Error updaging custom field", ex); return false; } }
bool AlreadyExists(CustomField item) { var field = CustomFieldsParser.CachedFields.Where(f => f.BlogId == item.BlogId && f.CustomType == item.CustomType && f.ObjectId == item.ObjectId && f.Key == item.Key).FirstOrDefault(); return field != null; }
public bool Update(CustomField item) { return true; }
public CustomField Add(CustomField item) { return new CustomField { CustomType = "test", BlogId = new Guid() }; }