Styles.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @file Styles.inc
  4. * Base class for Styles.
  5. */
  6. class StylesDefault {
  7. // The variables passed from our theme.
  8. public $variables = array();
  9. // The object passed from our theme.
  10. public $object = array();
  11. // Any effects to apply when displaying content matching this style.
  12. public $effects = array();
  13. // The final rendered output for this item.
  14. public $output;
  15. public $prefix;
  16. public $suffix;
  17. public $wrapperType = 'div';
  18. public $classes = array('styles');
  19. public $id;
  20. private $_id = 0;
  21. public function __construct($object = NULL, $effects = NULL, $variables = NULL) {
  22. // @TODO: This is not great IMO, the ->object and ->variables props already have everything
  23. // we shouldn't be duplicating it in different methods / properties.
  24. if (isset($variables)) {
  25. $properties = (array) $variables;
  26. $this->magicSet($properties);
  27. $this->setVariables($variables);
  28. }
  29. // If we are passed an array, then set the object properties from its keys.
  30. if (isset($object)) {
  31. $properties = (array) $object;
  32. $this->magicSet($properties);
  33. $this->setObject($object);
  34. }
  35. if (isset($effects)) {
  36. $this->setEffects($effects);
  37. }
  38. }
  39. /**
  40. * Converts a string in the form with_underscores to withUnderscores
  41. * @param string $str
  42. * @return string
  43. */
  44. protected function _toCamelCase($str) {
  45. $parts = explode("_", $str);
  46. $out = array_shift($parts);
  47. foreach ($parts as $part) {
  48. $out .= ucfirst($part);
  49. }
  50. return $out;
  51. }
  52. /**
  53. * Given an array of k/v pairs calls set$key
  54. * @param array $properties
  55. */
  56. protected function magicSet($properties) {
  57. $methods = get_class_methods($this);
  58. foreach ($properties as $key => $value) {
  59. $propName = "set_{$key}";
  60. $function = $this->_toCamelCase($propName);
  61. if (in_array($function, $methods)) {
  62. $this->$function($value);
  63. }
  64. }
  65. }
  66. /**
  67. * Display the rendered output.
  68. *
  69. * @param boolean $reset
  70. * Optional; if TRUE, the rebuild the output.
  71. * @return
  72. * A fully themed snippet of HTML for output.
  73. */
  74. public function display($reset = FALSE) {
  75. return $this->render($reset);
  76. }
  77. /**
  78. * Build the output for display.
  79. *
  80. * @param boolean $reset
  81. * Optional; if TRUE, the rebuild the output.
  82. * @return
  83. * A fully themed snippet of HTML for output.
  84. */
  85. public function render($reset = FALSE) {
  86. // If we need to reset, then set the output to NULL.
  87. if ($reset) {
  88. $this->setOutput(NULL);
  89. }
  90. // Have we already rendered the output?
  91. $output = $this->getOutput();
  92. // We need to render the output the first time around, or if reset.
  93. if (!isset($output)) {
  94. // First, we get the array of callable class methods for this object.
  95. // These may each be called by effects called for by the style.
  96. // @TODO: Should we have a proper array of allowable effects, rather
  97. // than our current lazy method of allowing all class functions?
  98. $methods = get_class_methods($this);
  99. // Loop through and apply each effect.
  100. foreach ($this->getEffects() as $effect) {
  101. // What is the effect?
  102. $effectName = $effect['name'];
  103. if ($effectName && in_array($effectName, $methods)) {
  104. // Apply the effect with its settings.
  105. $this->$effectName($effect['settings']);
  106. }
  107. else {
  108. // Ouch. We have an invalid effect. Flag for bug reporting.
  109. $variables = $this->getVariables();
  110. $styleName = $variables['style']['name'];
  111. watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $styleName, '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
  112. }
  113. }
  114. // Now the output will have been fully built.
  115. $output = $this->getOutput();
  116. }
  117. return $output;
  118. }
  119. public function getClassName() {
  120. return get_called_class();
  121. }
  122. public function _get($variable) {
  123. if (function_exists('get_' . $variable)) {
  124. return $this->{'get_' . $variable}();
  125. }
  126. else {
  127. return $this->get($variable);
  128. }
  129. }
  130. public function _set($variable, $value) {
  131. if (function_exists('set_' . $variable)) {
  132. return $this->{'set_' . $variable}($value);
  133. }
  134. else {
  135. return $this->set($variable, $value);
  136. }
  137. }
  138. public function arrayPush($variable, $element) {
  139. $array = (array) $this->_get($variable);
  140. array_push($array, $element);
  141. return $this->_set($variable, $array);
  142. }
  143. public function arrayPop($variable) {
  144. $array = (array) $this->_get($variable);
  145. array_pop($array);
  146. return $this->_set($variable, $array);
  147. }
  148. public function arrayUnshift($variable, $element) {
  149. $array = (array) $this->_get($variable);
  150. array_unshift($array, $element);
  151. return $this->_set($variable, $array);
  152. }
  153. public function arrayShift($variable) {
  154. $array = (array) $this->_get($variable);
  155. array_shift($array);
  156. return $this->_set($variable, $array);
  157. }
  158. /**
  159. * Add an effect to the end of the array.
  160. *
  161. * An effect is an array with at least a 'name' key, which corresponds to the
  162. * class function to be called during the rendering process. It will usually
  163. * also contain an array of 'effects' to apply.
  164. */
  165. public function addEffect($effect) {
  166. return $this->pushEffect($effect);
  167. }
  168. public function pushEffect($effect) {
  169. $effectName = $effect['name'];
  170. if (method_exists($this, $effectName)) {
  171. $effects = $this->getEffects();
  172. array_push($effects, $effect);
  173. return $this->setEffects($effects);
  174. }
  175. else {
  176. $variables = $this->getVariables();
  177. $styleName = $variables['style']['label'];
  178. watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $styleName, '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
  179. }
  180. }
  181. public function popEffect() {
  182. $effects = $this->getEffects();
  183. $effect = array_pop($effects);
  184. $this->setEffects($effects);
  185. return $effect;
  186. }
  187. public function unshiftEffect($effect) {
  188. $effectName = $effect['name'];
  189. if (method_exists($this, $effectName)) {
  190. $effects = $this->getEffects();
  191. array_unshift($effects, $effect);
  192. return $this->setEffects($effects);
  193. }
  194. else {
  195. $variables = $this->getVariables();
  196. $styleName = $variables['style']['label'];
  197. watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $this->getName(), '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
  198. }
  199. }
  200. public function shiftEffect() {
  201. $effects = $this->getEffects();
  202. $effect = array_shift($effects);
  203. $this->setEffects($effects);
  204. return $effect;
  205. }
  206. public function getObject() {
  207. return $this->get('object');
  208. }
  209. public function setObject($value) {
  210. return $this->set('object', $value);
  211. }
  212. public function getVariables() {
  213. return $this->get('variables');
  214. }
  215. public function setVariables($value) {
  216. return $this->set('variables', $value);
  217. }
  218. public function getEffects() {
  219. return $this->get('effects');
  220. }
  221. public function setEffects($value) {
  222. return $this->set('effects', $value);
  223. }
  224. public function getWrapperType() {
  225. return $this->get('wrapperType');
  226. }
  227. public function setWrapperType($value) {
  228. return $this->set('wrapperType', $value);
  229. }
  230. public function getClasses() {
  231. $classes = $this->get('classes');
  232. return is_array($classes) ? implode(' ', $classes) : $classes;
  233. }
  234. public function setClasses($value) {
  235. return $this->set('classes', $value);
  236. }
  237. public function getId() {
  238. return $this->get('id');
  239. }
  240. public function setId($value) {
  241. return $this->set('id', $value);
  242. }
  243. public function getOutput() {
  244. return $this->get('output');
  245. }
  246. public function setOutput($value) {
  247. return $this->set('output', $value);
  248. }
  249. public function getPrefix() {
  250. $prefix = $this->get('prefix');
  251. if (!isset($prefix)) {
  252. $wrapperType = $this->getWrapperType();
  253. $id = $this->getId();
  254. $_id = $this->_id++;
  255. $classes = $this->getClasses();
  256. $prefix = "<$wrapperType id=\"styles-$id-$_id\" class=\"$classes\">";
  257. }
  258. return $prefix;
  259. }
  260. public function setPrefix($value) {
  261. return $this->set('prefix', $value);
  262. }
  263. public function getSuffix() {
  264. $suffix = $this->get('suffix');
  265. if (!isset($suffix)) {
  266. $wrapperType = $this->getWrapperType();
  267. $suffix = "</$wrapperType>";
  268. }
  269. return $suffix;
  270. }
  271. public function setSuffix($value) {
  272. return $this->set('suffix', $value);
  273. }
  274. public function get($variable) {
  275. return isset($this->{$variable}) ? $this->{$variable} : NULL;
  276. }
  277. public function set($variable, $value) {
  278. return $this->{$variable} = $value;
  279. }
  280. }