PageRenderTime 36ms CodeModel.GetById 17ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/CMSAPIExamples/Code/Settings/Default.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 428 lines | 218 code | 98 blank | 112 comment | 18 complexity | d513a2ad5a55a4e08507ce20671049db MD5 | raw file
  1using System;
  2using System.Data;
  3
  4using CMS.GlobalHelper;
  5using CMS.UIControls;
  6using CMS.CMSHelper;
  7using CMS.SiteProvider;
  8using CMS.SettingsProvider;
  9
 10[Title(Text = "Settings", ImageUrl = "Objects/CMS_SettingsKey/object.png")]
 11public partial class CMSAPIExamples_Code_Settings_Default : CMSAPIExamplePage
 12{
 13    #region "Initialization"
 14
 15    protected void Page_Load(object sender, EventArgs e)
 16    {
 17        // Settings category
 18        this.apiCreateSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsCategory);
 19        this.apiGetAndUpdateSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsCategory);
 20        this.apiGetAndBulkUpdateSettingsCategories.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsCategories);
 21        this.apiDeleteSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsCategory);
 22
 23        // Settings group
 24        this.apiCreateSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsGroup);
 25        this.apiGetAndUpdateSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsGroup);
 26        this.apiGetAndBulkUpdateSettingsGroups.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsGroups);
 27        this.apiDeleteSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsGroup);
 28
 29        // Settings key
 30        this.apiCreateSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsKey);
 31        this.apiGetAndUpdateSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsKey);
 32        this.apiGetAndBulkUpdateSettingsKeys.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsKeys);
 33        this.apiDeleteSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsKey);
 34
 35        // Web.config setting
 36        this.apiGetWebConfigSetting.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetWebConfigSetting);
 37    }
 38
 39    #endregion
 40
 41
 42    #region "Mass actions"
 43
 44    /// <summary>
 45    /// Runs all creating and managing examples.
 46    /// </summary>
 47    public override void RunAll()
 48    {
 49        base.RunAll();
 50
 51        // Settings category
 52        this.apiCreateSettingsCategory.Run();
 53        this.apiGetAndUpdateSettingsCategory.Run();
 54        this.apiGetAndBulkUpdateSettingsCategories.Run();
 55
 56        // Settings group
 57        this.apiCreateSettingsGroup.Run();
 58        this.apiGetAndUpdateSettingsGroup.Run();
 59        this.apiGetAndBulkUpdateSettingsGroups.Run();
 60
 61        // Settings key
 62        this.apiCreateSettingsKey.Run();
 63        this.apiGetAndUpdateSettingsKey.Run();
 64        this.apiGetAndBulkUpdateSettingsKeys.Run();
 65
 66        // Web.config setting
 67        this.apiGetWebConfigSetting.Run();
 68    }
 69
 70
 71    /// <summary>
 72    /// Runs all cleanup examples.
 73    /// </summary>
 74    public override void CleanUpAll()
 75    {
 76        base.CleanUpAll();
 77
 78        // Settings key
 79        this.apiDeleteSettingsKey.Run();
 80
 81        // Settings group
 82        this.apiDeleteSettingsGroup.Run();
 83
 84        // Settings category
 85        this.apiDeleteSettingsCategory.Run();
 86    }
 87
 88    #endregion
 89
 90
 91    #region "API examples - Settings category"
 92
 93    /// <summary>
 94    /// Creates settings category. Called when the "Create category" button is pressed.
 95    /// </summary>
 96    private bool CreateSettingsCategory()
 97    {
 98        // Get parent category ID
 99        SettingsCategoryInfo parentCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("CMS.CustomSettings");
100        if (parentCategory != null)
101        {
102            // Create new settings category object
103            SettingsCategoryInfo newCategory = new SettingsCategoryInfo();
104
105            // Set the properties
106            newCategory.CategoryDisplayName = "My New Settings Category";
107            newCategory.CategoryName = "MyNewSettingsCategory";
108            newCategory.CategoryOrder = 0;
109            newCategory.CategoryParentID = parentCategory.CategoryID;
110            newCategory.CategoryIsGroup = false;
111            newCategory.CategoryIsCustom = true;
112
113            // Create settings category
114            SettingsCategoryInfoProvider.SetSettingsCategoryInfo(newCategory);
115
116            return true;
117        }
118
119        return false;
120    }
121
122
123    /// <summary>
124    /// Gets and updates settings category. Called when the "Get and update category" button is pressed.
125    /// Expects the CreateSettingsCategory method to be run first.
126    /// </summary>
127    private bool GetAndUpdateSettingsCategory()
128    {
129        // Get the settings category
130        SettingsCategoryInfo updateCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
131        if (updateCategory != null)
132        {
133            // Update the property
134            updateCategory.CategoryDisplayName = updateCategory.CategoryDisplayName.ToLower();
135
136            // Update settings category
137            SettingsCategoryInfoProvider.SetSettingsCategoryInfo(updateCategory);
138
139            return true;
140        }
141
142        return false;
143    }
144
145
146    /// <summary>
147    /// Gets and bulk updates settings categories. Called when the "Get and bulk update categories" button is pressed.
148    /// Expects the CreateSettingsCategory method to be run first.
149    /// </summary>
150    private bool GetAndBulkUpdateSettingsCategories()
151    {
152        // Prepare the parameters
153        string where = "CategoryName LIKE 'MyNew%' AND CategoryIsGroup = 0";
154
155        // Get the settings categories
156        DataSet categories = SettingsCategoryInfoProvider.GetSettingsCategories(where, null);
157        if (!DataHelper.DataSourceIsEmpty(categories))
158        {
159            // Loop through the individual items
160            foreach (DataRow categoryDr in categories.Tables[0].Rows)
161            {
162                // Create object from DataRow
163                SettingsCategoryInfo modifyCategory = new SettingsCategoryInfo(categoryDr);
164
165                // Update the property
166                modifyCategory.CategoryDisplayName = modifyCategory.CategoryDisplayName.ToUpper();
167
168                // Update the settings category
169                SettingsCategoryInfoProvider.SetSettingsCategoryInfo(modifyCategory);
170            }
171
172            return true;
173        }
174
175        return false;
176    }
177
178
179    /// <summary>
180    /// Deletes settings category. Called when the "Delete category" button is pressed.
181    /// Expects the CreateSettingsCategory method to be run first.
182    /// </summary>
183    private bool DeleteSettingsCategory()
184    {
185        // Get the settings category
186        SettingsCategoryInfo deleteCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
187
188        // Delete the settings category
189        SettingsCategoryInfoProvider.DeleteSettingsCategoryInfo(deleteCategory);
190
191        return (deleteCategory != null);
192    }
193
194    #endregion
195
196
197    #region "API examples - Settings group"
198
199    /// <summary>
200    /// Creates settings group. Called when the "Create group" button is pressed.
201    /// Expects the CreateSettingsCategory method to be run first.
202    /// </summary>
203    private bool CreateSettingsGroup()
204    {
205        // Get the settings category
206        SettingsCategoryInfo settingsCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
207        if (settingsCategory != null)
208        {
209            // Create new settings group object
210            SettingsCategoryInfo newGroup = new SettingsCategoryInfo();
211
212            // Set the properties
213            newGroup.CategoryDisplayName = "My New Settings Group";
214            newGroup.CategoryName = "MyNewSettingsGroup";
215            newGroup.CategoryOrder = 0;
216            newGroup.CategoryParentID = settingsCategory.CategoryID;
217            newGroup.CategoryIsGroup = true;
218            newGroup.CategoryIsCustom = true;
219
220            // Create the settings group
221            SettingsCategoryInfoProvider.SetSettingsCategoryInfo(newGroup);
222
223            return true;
224        }
225
226        return false;
227    }
228
229
230    /// <summary>
231    /// Gets and updates settings group. Called when the "Get and update group" button is pressed.
232    /// Expects the CreateSettingsGroup method to be run first.
233    /// </summary>
234    private bool GetAndUpdateSettingsGroup()
235    {
236        // Get the settings group
237        SettingsCategoryInfo updateGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
238        if (updateGroup != null)
239        {
240            // Update the property
241            updateGroup.CategoryDisplayName = updateGroup.CategoryDisplayName.ToLower();
242
243            // Update the settings group
244            SettingsCategoryInfoProvider.SetSettingsCategoryInfo(updateGroup);
245
246            return true;
247        }
248
249        return false;
250    }
251
252
253    /// <summary>
254    /// Gets and bulk updates settings groups. Called when the "Get and bulk update groups" button is pressed.
255    /// Expects the CreateSettingsGroup method to be run first.
256    /// </summary>
257    private bool GetAndBulkUpdateSettingsGroups()
258    {
259        // Prepare the parameters
260        string where = "CategoryName LIKE 'MyNew%' AND CategoryIsGroup = 1";
261        string orderBy = "CategoryName";
262
263        // Get the data
264        DataSet groups = SettingsCategoryInfoProvider.GetSettingsCategories(where, orderBy);
265        if (!DataHelper.DataSourceIsEmpty(groups))
266        {
267            // Loop through the individual items
268            foreach (DataRow groupDr in groups.Tables[0].Rows)
269            {
270                // Create object from DataRow
271                SettingsCategoryInfo modifyGroup = new SettingsCategoryInfo(groupDr);
272
273                // Update the property
274                modifyGroup.CategoryDisplayName = modifyGroup.CategoryDisplayName.ToUpper();
275
276                // Update settings group
277                SettingsCategoryInfoProvider.SetSettingsCategoryInfo(modifyGroup);
278            }
279
280            return true;
281        }
282
283        return false;
284    }
285
286
287    /// <summary>
288    /// Deletes settings group. Called when the "Delete group" button is pressed.
289    /// Expects the CreateSettingsGroup method to be run first.
290    /// </summary>
291    private bool DeleteSettingsGroup()
292    {
293        // Get the settings group
294        SettingsCategoryInfo deleteGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
295
296        // Delete the settings group
297        SettingsCategoryInfoProvider.DeleteSettingsCategoryInfo(deleteGroup);
298
299        return (deleteGroup != null);
300    }
301
302    #endregion
303
304
305    #region "API examples - Settings key"
306
307    /// <summary>
308    /// Creates settings key. Called when the "Create key" button is pressed.
309    /// </summary>
310    private bool CreateSettingsKey()
311    {
312        // Get the settings group
313        SettingsCategoryInfo settingsGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
314        if (settingsGroup != null)
315        {
316            // Create new settings key object
317            SettingsKeyInfo newKey = new SettingsKeyInfo();
318
319            // Set the properties
320            newKey.KeyDisplayName = "My new key";
321            newKey.KeyName = "MyNewKey";
322            newKey.KeyDescription = "My new key description";
323            newKey.KeyType = "string";
324            newKey.KeyValue = "My new value";
325            newKey.KeyCategoryID = settingsGroup.CategoryID;
326            newKey.KeyDefaultValue = null;
327
328            // Set Site ID for site specific settings key (for global settings key is default value 0).
329            newKey.SiteID = CMSContext.CurrentSiteID;
330
331            // Create the settings key
332            SettingsKeyProvider.SetValue(newKey);
333
334            return true;
335        }
336
337        return false;
338    }
339
340
341    /// <summary>
342    /// Gets and updates settings key. Called when the "Get and update key" button is pressed.
343    /// Expects the CreateSettingsKey method to be run first.
344    /// </summary>
345    private bool GetAndUpdateSettingsKey()
346    {
347        // Get the settings key
348        SettingsKeyInfo updateKey = SettingsKeyProvider.GetSettingsKeyInfo("MyNewKey", CMSContext.CurrentSiteID);
349        if (updateKey != null)
350        {
351            // Update the property
352            updateKey.KeyDisplayName = updateKey.KeyDisplayName.ToLower();
353
354            // Update the settings key
355            SettingsKeyProvider.SetValue(updateKey);
356
357            return true;
358        }
359
360        return false;
361    }
362
363
364    /// <summary>
365    /// Gets and bulk updates settings keys. Called when the "Get and bulk update keys" button is pressed.
366    /// Expects the CreateSettingsKey method to be run first.
367    /// </summary>
368    private bool GetAndBulkUpdateSettingsKeys()
369    {
370        // Prepare the parameters
371        string where = "KeyName LIKE N'MyNew%'";
372
373        // Get the data
374        DataSet keys = SettingsKeyProvider.GetSettingsKeys(where, null, 0, null);
375        if (!DataHelper.DataSourceIsEmpty(keys))
376        {
377            // Loop through the individual items
378            foreach (DataRow keyDr in keys.Tables[0].Rows)
379            {
380                // Create object from DataRow
381                SettingsKeyInfo modifyKey = new SettingsKeyInfo(keyDr);
382
383                // Update the property
384                modifyKey.KeyDisplayName = modifyKey.KeyDisplayName.ToUpper();
385
386                // Update the settings key
387                SettingsKeyProvider.SetValue(modifyKey);
388            }
389
390            return true;
391        }
392
393        return false;
394    }
395
396
397    /// <summary>
398    /// Deletes settings key. Called when the "Delete key" button is pressed.
399    /// Expects the CreateSettingsKey method to be run first.
400    /// </summary>
401    private bool DeleteSettingsKey()
402    {
403        // Get the settings key
404        SettingsKeyInfo deleteKey = SettingsKeyProvider.GetSettingsKeyInfo("MyNewKey", CMSContext.CurrentSiteID);
405
406        // Delete the settings key
407        SettingsKeyProvider.DeleteKey(deleteKey);
408
409        return (deleteKey != null);
410    }
411
412    #endregion
413
414
415    #region "API examples - Web.config setting"
416
417    /// <summary>
418    /// Gets web.config setting. Called when the button "Get web.config setting" is pressed.
419    /// </summary>
420    private bool GetWebConfigSetting()
421    {
422        string webConfigSetting = ValidationHelper.GetString(SettingsHelper.AppSettings["WS.webservice"], "");
423
424        return (!String.IsNullOrEmpty(webConfigSetting));
425    }
426
427    #endregion
428}