PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/OntoWiki/Toolbar.php

https://code.google.com/p/ontowiki/
PHP | 286 lines | 138 code | 33 blank | 115 comment | 31 complexity | 0904a5ec603d2c50813389cab883a815 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  4. *
  5. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  6. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  7. */
  8. /**
  9. * OntoWiki Toolbar class.
  10. *
  11. * Facilitates the programmatical construction of toolbars.
  12. *
  13. * @category OntoWiki
  14. * @package Toolbar
  15. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  16. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  17. * @author Norman Heino <norman.heino@gmail.com>
  18. */
  19. class OntoWiki_Toolbar
  20. {
  21. /**
  22. * Constants for default buttons
  23. */
  24. const CANCEL = 0;
  25. const SAVE = 1;
  26. const EDIT = 2;
  27. const ADD = 3;
  28. const EDITADD = 4;
  29. const DELETE = 5;
  30. const EXPORT = 6;
  31. const SUBMIT = 10;
  32. const RESET = 11;
  33. const SEPARATOR = 100;
  34. /**
  35. * Default button configurations
  36. * @var array
  37. */
  38. protected $_defaultButtons = array(
  39. self::CANCEL => array('name' => 'Cancel', 'image' => 'cancel', 'class' => 'edit cancel'),
  40. self::SAVE => array('name' => 'Save Changes', 'image' => 'save2', 'class' => 'edit save'),
  41. self::EDIT => array('name' => 'Edit', 'image' => 'edit', 'class' => 'edit-enable'),
  42. self::ADD => array('name' => 'Add', 'image' => 'add'),
  43. self::EDITADD => array('name' => 'Add', 'image' => 'editadd'),
  44. self::DELETE => array('name' => 'Delete', 'image' => 'delete'),
  45. self::SUBMIT => array('name' => 'Submit', 'class' => 'submit', 'image' => 'go2'),
  46. self::RESET => array('name' => 'Reset', 'class' => 'reset', 'image' => 'reset'),
  47. self::EXPORT => array('name' => 'Export', 'class' => 'export', 'image' => 'save')
  48. );
  49. /**
  50. * Array of toolbar buttons
  51. * @var array
  52. */
  53. protected $_buttons = array();
  54. /**
  55. * Singleton instance
  56. * @var OntoWiki_Toolbar
  57. */
  58. private static $_instance = null;
  59. /**
  60. * Translation object
  61. * @var Zend_Translate
  62. */
  63. protected $_translate = null;
  64. /**
  65. * Constructor
  66. */
  67. private function __construct()
  68. {
  69. }
  70. /**
  71. * Disallow cloning
  72. */
  73. private function __clone() {}
  74. /**
  75. * Adds a button to the global toolbar.
  76. *
  77. * @param mixed $type either a button constant defined by OntoWiki_Toolbar or
  78. * a name string that identifies a custom button.
  79. * @param array $options If $type is a custom type, providing $options is mandatory.
  80. * For default buttons $options is optional but you can overwrite the behaviour
  81. * of default buttons by providing $options. The following keys are regognized:
  82. * - name: the button's name
  83. * - class: the button's css class(es)
  84. * - id: the button's css id
  85. * - url: the URL to be fetched when the button has been clicked.
  86. * - title: value for the HTML title attribute (displayed as a tooltip in most browsers).
  87. * - image: the button's theme image name (w/o icon- and .png, eg. 'edit' for 'icon-edit.png')
  88. * - image_url: the complete URL of the button's image. Use this to define custom images that are
  89. * stored anywhere on the web.
  90. * @return OntoWiki_Toolbar
  91. */
  92. public function appendButton($type, array $options = array())
  93. {
  94. if ($button = $this->_getButton($type, $options)) {
  95. array_push($this->_buttons, $button);
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Returns an instance of OntoWiki_Toolbar
  101. *
  102. * @return OntoWiki_Toolbar
  103. */
  104. public static function getInstance()
  105. {
  106. if (null === self::$_instance) {
  107. self::$_instance = new self();
  108. }
  109. return self::$_instance;
  110. }
  111. /**
  112. * Adds a button to the front of the global toolbar.
  113. *
  114. * @param mixed $type either a button constant defined by OntoWiki_Toolbar or
  115. * a name string that identifies a custom button.
  116. * @param array $options If $type is a custom type, providing $options is mandatory.
  117. * For default buttons $options is optional but you can overwrite the behaviour
  118. * of default buttons by providing $options. The following keys are regognized:
  119. * - name: the button's name
  120. * - class: the button's css class(es)
  121. * - id: the button's css id
  122. * - url: the URL to be fetched when the button has been clicked.
  123. * - title: value for the HTML title attribute (displayed as a tooltip in most browsers).
  124. * - image: the button's theme image name (w/o icon- and .png, eg. 'edit' for 'icon-edit.png')
  125. * - image_url: the complete URL of the button's image. Use this to define custom images that are
  126. * stored anywhere on the web.
  127. * @return OntoWiki_Toolbar
  128. */
  129. public function prependButton($type, array $options = array())
  130. {
  131. if ($button = $this->_getButton($type, $options)) {
  132. array_unshift($this->_buttons, $button);
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Sets the URL base for the current theme
  138. *
  139. * @since 0.9.5
  140. * @param string $themeUrlBase The URL base into the theme dir
  141. * @return OntoWiki_Toolbar
  142. */
  143. public function setThemeUrlBase($themeUrlBase)
  144. {
  145. $this->_themeUrlBase = (string)$themeUrlBase;
  146. return $this;
  147. }
  148. /**
  149. * Sets the translation object for the current UI language
  150. *
  151. * @since 0.9.5
  152. * @param Zend_Translate $translate The translation object
  153. * @return OntoWiki_Toolbar
  154. */
  155. public function setTranslate(Zend_Translate $translate)
  156. {
  157. $this->_translate = $translate;
  158. return $this;
  159. }
  160. /**
  161. * Renders the toolbar as an HTML string.
  162. *
  163. * @return string
  164. */
  165. public function __toString()
  166. {
  167. return '<div class="toolbar">' . implode('', $this->_buttons) . '</div>';
  168. }
  169. /**
  170. * Returns HTML for the specified button type.
  171. *
  172. * @param int $type the button type
  173. * @param array $options button options
  174. * @return string
  175. */
  176. private function _getButton($type, $options = array())
  177. {
  178. if ($type == self::SEPARATOR) {
  179. return '<a class="button separator"></a>';
  180. } else if (array_key_exists($type, $this->_defaultButtons)) {
  181. $options = array_merge($this->_defaultButtons[$type], $options);
  182. } else {
  183. if (empty($options)) {
  184. throw new OntoWiki_Exception("Missing options for button '$type'.");
  185. }
  186. if (!array_key_exists('name', $options)) {
  187. $options['name'] = $type;
  188. }
  189. }
  190. // translate name
  191. if (array_key_exists('name', $options)) {
  192. if ($this->_translate instanceof Zend_Translate) {
  193. $label = $this->_translate->translate($options['name']);
  194. } else {
  195. $label = $options['name'];
  196. }
  197. } else {
  198. $label = null;
  199. }
  200. // set class
  201. if (array_key_exists('+class', $options)) {
  202. $addedClasses = $options['+class'];
  203. }
  204. // set class
  205. if (array_key_exists('class', $options)) {
  206. $class = $options['class'];
  207. if (isset($addedClasses)) {
  208. $class = $class
  209. . ' '
  210. . $addedClasses;
  211. }
  212. } else {
  213. if (isset($addedClasses)) {
  214. $class = $addedClasses;
  215. } else {
  216. $class = null;
  217. }
  218. }
  219. // set id
  220. if (array_key_exists('id', $options)) {
  221. $id = 'id="' . $options['id'] . '"';
  222. } else {
  223. $id = null;
  224. }
  225. if (array_key_exists('url', $options)) {
  226. $href = 'href="' . $options['url'] . '"';
  227. } else {
  228. $href = null;
  229. }
  230. if (array_key_exists('title', $options)) {
  231. $title = 'title="' . $options['title'] . '"';
  232. } else {
  233. $title = null;
  234. }
  235. // set image
  236. if (array_key_exists('image_url', $options)) {
  237. $image = $options['image_url'];
  238. } else if (array_key_exists('image', $options)) {
  239. $image = $this->_themeUrlBase . 'images/icon-' . $options['image'] . '.png';
  240. } else {
  241. $image = null;
  242. }
  243. // construct button link
  244. $button = sprintf('<a class="button %s" %s %s %s><img src="%s"/><span>&nbsp;%s</span></a>',
  245. $class,
  246. $id,
  247. $href,
  248. $title,
  249. $image,
  250. $label);
  251. return $button;
  252. }
  253. }