styles.variables.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file styles.variables.inc
  4. * Variable defaults for Styles.
  5. */
  6. /**
  7. * Define our constants.
  8. */
  9. /**
  10. * This is the variable namespace, automatically prepended to module variables.
  11. */
  12. define('STYLES_NAMESPACE', 'styles__');
  13. /**
  14. * Styles constant for user styles in the database.
  15. */
  16. define('STYLES_STORAGE_NORMAL', 1);
  17. /**
  18. * Styles constant for user styles that override module-defined styles.
  19. */
  20. define('STYLES_STORAGE_OVERRIDE', 2);
  21. /**
  22. * Styles constant for module-defined styles in code.
  23. */
  24. define('STYLES_STORAGE_DEFAULT', 4);
  25. /**
  26. * Styles constant to represent an editable preset.
  27. */
  28. define('STYLES_STORAGE_EDITABLE', STYLES_STORAGE_NORMAL | STYLES_STORAGE_OVERRIDE);
  29. /**
  30. * Styles constant to represent any module-based preset.
  31. */
  32. define('STYLES_STORAGE_MODULE', STYLES_STORAGE_OVERRIDE | STYLES_STORAGE_DEFAULT);
  33. /**
  34. * Wrapper for variable_get() using the Styles variable registry.
  35. *
  36. * @param string $name
  37. * The variable name to retrieve. Note that it will be namespaced by
  38. * pre-pending STYLES_NAMESPACE, as to avoid variable collisions
  39. * with other modules.
  40. * @param unknown $default
  41. * An optional default variable to return if the variable hasn't been set
  42. * yet. Note that within this module, all variables should already be set
  43. * in the styles_variable_default() function.
  44. * @return unknown
  45. * Returns the stored variable or its default.
  46. *
  47. * @see styles_variable_set()
  48. * @see styles_variable_del()
  49. * @see styles_variable_default()
  50. */
  51. function styles_variable_get($name, $default = NULL) {
  52. // Allow for an override of the default.
  53. // Useful when a variable is required (like $path), but namespacing is still
  54. // desired.
  55. if (!isset($default)) {
  56. $default = styles_variable_default($name);
  57. }
  58. // Namespace all variables.
  59. $variable_name = STYLES_NAMESPACE . $name;
  60. return variable_get($variable_name, $default);
  61. }
  62. /**
  63. * Wrapper for variable_set() using the Styles variable registry.
  64. *
  65. * @param string $name
  66. * The variable name to set. Note that it will be namespaced by
  67. * pre-pending STYLES_NAMESPACE, as to avoid variable collisions with
  68. * other modules.
  69. * @param unknown $value
  70. * The value for which to set the variable.
  71. * @return unknown
  72. * Returns the stored variable after setting.
  73. *
  74. * @see styles_variable_get()
  75. * @see styles_variable_del()
  76. * @see styles_variable_default()
  77. */
  78. function styles_variable_set($name, $value) {
  79. $variable_name = STYLES_NAMESPACE . $name;
  80. return variable_set($variable_name, $value);
  81. }
  82. /**
  83. * Wrapper for variable_del() using the Styles variable registry.
  84. *
  85. * @param string $name
  86. * The variable name to delete. Note that it will be namespaced by
  87. * pre-pending STYLES_NAMESPACE, as to avoid variable collisions with
  88. * other modules.
  89. *
  90. * @see styles_variable_get()
  91. * @see styles_variable_set()
  92. * @see styles_variable_default()
  93. */
  94. function styles_variable_del($name) {
  95. $variable_name = STYLES_NAMESPACE . $name;
  96. variable_del($variable_name);
  97. }
  98. /**
  99. * The default variables within the Styles namespace.
  100. *
  101. * @param string $name
  102. * Optional variable name to retrieve the default. Note that it has not yet
  103. * been pre-pended with the STYLES_NAMESPACE namespace at this time.
  104. * @return unknown
  105. * The default value of this variable, if it's been set, or NULL, unless
  106. * $name is NULL, in which case we return an array of all default values.
  107. *
  108. * @see styles_variable_get()
  109. * @see styles_variable_set()
  110. * @see styles_variable_del()
  111. */
  112. function styles_variable_default($name = NULL) {
  113. static $defaults;
  114. if (!isset($defaults)) {
  115. $defaults = array(
  116. );
  117. }
  118. if (!isset($name)) {
  119. return $defaults;
  120. }
  121. if (isset($defaults[$name])) {
  122. return $defaults[$name];
  123. }
  124. }
  125. /**
  126. * Return the fully namespace variable name.
  127. *
  128. * @param string $name
  129. * The variable name to retrieve the namespaced name.
  130. * @return string
  131. * The fully namespace variable name, prepended with
  132. * STYLES_NAMESPACE.
  133. */
  134. function styles_variable_name($name) {
  135. return STYLES_NAMESPACE . $name;
  136. }