/src/system/application/helpers/tabs_helper.php

https://github.com/rickogden/joind.in · PHP · 337 lines · 212 code · 48 blank · 77 comment · 15 complexity · a97204f16c7779c7651a2c7e58171d11 MD5 · raw file

  1. <?php
  2. /**
  3. * Helpers for creating Accessible Tabs.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Joind.in
  8. * @package Helpers
  9. * @author Kathryn Reeve <kat@BinaryKitten.me.uk>
  10. * @copyright 2009 - 2010 Joind.in
  11. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  12. * @link http://github.com/joindin/joind.in
  13. */
  14. class joindIn_TabContainer implements countable {
  15. /**
  16. * @var array Internal tab data storage
  17. **/
  18. private $_tabs = array();
  19. /**
  20. * @var string base name for this instance of tabs.
  21. **/
  22. private $_containerName = 'view';
  23. /**
  24. * @var string the hash id of the selected tab to display as primary
  25. **/
  26. private $_selectedTab = '';
  27. /**
  28. * @var string The Base URL from which tab links are built
  29. **/
  30. private $_baseUrl = '';
  31. /**
  32. * Set this tab instance's container name. Used as a base name for ids in the html render
  33. *
  34. * @param string $name The New Container name
  35. * @return joindIn_Tabs Itself for a fluid chainable instance
  36. **/
  37. public function setContainerName($name) {
  38. $this->_containerName = $name;
  39. return $this;
  40. }
  41. /**
  42. * Get this tab instance's container name. Used as a base name for ids in the html render
  43. * @return string The Container Name
  44. **/
  45. public function getContainerName() {
  46. return $this->_containerName;
  47. }
  48. /**
  49. * Set this tab instance's Base URL. Used as a base for links in the html render
  50. * @param string $url The New Base URL
  51. * @return joindIn_Tabs Itself for a fluid chainable instance
  52. **/
  53. public function setBaseUrl($url) {
  54. $this->_baseUrl = $url;
  55. return $this;
  56. }
  57. /**
  58. * Get this tab instance's Base URL. Used as a base for links in the html render
  59. * @return string The Base URL
  60. **/
  61. public function getBaseUrl() {
  62. return $this->_baseUrl;
  63. }
  64. /**
  65. * Set this tab instance's Selected Tab.
  66. * @param string $hash the hash of the tab to make selected
  67. * @return joindIn_Tabs Itself for a fluid chainable instance
  68. **/
  69. public function setSelectedTab($id) {
  70. if (isset($this->_tabs[$id])) {
  71. $this->_tabs[$id]->setSelected();
  72. }
  73. $this->_selectedTab = $id;
  74. return $this;
  75. }
  76. /**
  77. * Get this tab instance's selected Tab.
  78. * Please note, this does not get active on client side.
  79. * @return string The Active Tab's Hash
  80. **/
  81. public function getSelectedTab() {
  82. return $this->_tabs[$this->_selectedTab];
  83. }
  84. /**
  85. * Add a Tab
  86. *
  87. * Adds a new tab to the collection
  88. ********************************************
  89. * @return string ID of tab that was just added
  90. **/
  91. public function addTab(joindIn_Tab $tab, $id=null) {
  92. if ($id===null) {
  93. $id = $tab->getId();
  94. }
  95. $this->_tabs[$id] = $tab;
  96. return $id;
  97. }
  98. public function setTabs(array $tabs) {
  99. $this->_tabs = $tabs;
  100. }
  101. public function getTabs() {
  102. return $this->_tabs;
  103. }
  104. public function getTab($id) {
  105. if (isset($this->_tabs[$id])) {
  106. return $this->_tabs[$id];
  107. }
  108. return null;
  109. }
  110. public function addTabs(array $tabs) {
  111. if (count($this->_tabs) > 0) {
  112. $this->_tabs = array_merge($this->_tabs, $tabs);
  113. } else {
  114. $this->_tabs = $tabs;
  115. }
  116. }
  117. public function render() {
  118. if ($this->count() === 0) {
  119. return '';
  120. }
  121. ob_start();
  122. $contentList = array();
  123. reset($this->_tabs);
  124. if (empty($this->_selectedTab) || !isset($this->_tabs[$this->_selectedTab])) {
  125. $tmp_tab = current($this->_tabs);
  126. $this->_selectedTab = $tmp_tab->getId();
  127. reset($this->_tabs);
  128. }
  129. ?>
  130. <div id="<?php echo $this->_containerName; ?>-tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
  131. <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
  132. <?php
  133. foreach($this->_tabs as $tab):
  134. if ($tab->getId() == $this->_selectedTab) {
  135. $tab->setSelected();
  136. }
  137. $tab->setParentId($this->_containerName);
  138. $tab->setBaseUrl($this->_baseUrl);
  139. list($tabTop,$content) = $tab->render();
  140. echo $tabTop;
  141. $contentList[$tab->getId()] = $content;
  142. endforeach;
  143. ?>
  144. </ul>
  145. <?php foreach($contentList as $hash=>$tabContent):
  146. echo $tabContent;
  147. endforeach; ?>
  148. </div>
  149. <?php $content = ob_get_clean();
  150. return $content;
  151. }
  152. public function __toString() {
  153. return $this->render();
  154. }
  155. /**
  156. * Countable Interface
  157. * @return int the amout of tabs that we have
  158. **/
  159. public function count() {
  160. return count($this->_tabs);
  161. }
  162. }
  163. /*
  164. * joindIn_Tab
  165. *
  166. * A Singular Tab to be added to the Tab Container
  167. */
  168. class joindIn_Tab {
  169. private $_id = '';
  170. private $_url = '';
  171. private $_baseURL = '';
  172. private $_selected = false;
  173. private $_caption = '';
  174. private $_content = '';
  175. private $_parentId = '';
  176. private $_headerClassses = array(
  177. 'ui-state-default',
  178. 'ui-corner-top'
  179. );
  180. private $_contentClasses = array(
  181. 'ui-tabs-panel',
  182. 'ui-widget-content',
  183. 'ui-corner-bottom'
  184. );
  185. private $_selectedHeaderClasses = array(
  186. 'ui-tabs-selected',
  187. 'ui-state-active',
  188. 'ui-state-focus'
  189. );
  190. private $_selectedContentClasses = array();
  191. private $_hiddenContentClasses = array('ui-tabs-hide');
  192. private $_headerFormat = '<li class="%1$s"><a href="%2$s#%3$s-tabs" rel="%4$s">%5$s</a></li>';
  193. private $_contentFormat = '<div class="%1$s" id="%2$s"><div class="ui-widget">%3$s</div></div>';
  194. /**
  195. * Constructor
  196. *
  197. * @param string $url The URL of the tab link
  198. * @param string $caption The caption of the tab
  199. * @param string $content the content of the tab
  200. * @param boolean $selected Whether this tab is selected
  201. **/
  202. public function __construct($url='', $caption='', $content='', $selected=false) {
  203. $this->_id = $url;
  204. $this->_url = $url;
  205. $this->_selected = $selected;
  206. $this->_caption = $caption;
  207. $this->_content = $content;
  208. }
  209. public function clearContent() {
  210. $this->content = '';
  211. return $this;
  212. }
  213. public function addContent($content) {
  214. $this->_content .= $content;
  215. return this;
  216. }
  217. public function getContent() {
  218. return $this->_content;
  219. }
  220. public function setContent($content) {
  221. $this->_content = $content;
  222. return $this;
  223. }
  224. public function setId($newId) {
  225. $this->_id = $newId;
  226. return $this;
  227. }
  228. public function getId() {
  229. return $this->_id;
  230. }
  231. public function setCaption($newCaption) {
  232. $this->_caption = $newCaption;
  233. return $this;
  234. }
  235. public function getCaption() {
  236. return $this->_caption;
  237. }
  238. public function setParentId($newParentId) {
  239. $this->_parentId = $newParentId;
  240. return $this;
  241. }
  242. public function getURL() {
  243. return $this->_url;
  244. }
  245. public function setBaseURL($newBaseURL) {
  246. $this->_baseURL = $newBaseURL;
  247. return $this;
  248. }
  249. public function getBaseURL() {
  250. return $this->_baseURL;
  251. }
  252. public function setSelected($selected=true) {
  253. $this->_selected = $selected;
  254. return $this;
  255. }
  256. public function getSelected() {
  257. return $this->_selected;
  258. }
  259. public function render($headerFormat='', $contentFormat='') {
  260. $header = '';
  261. if (empty($headerFormat)) {
  262. $headerFormat = $this->_headerFormat;
  263. }
  264. $content = '';
  265. if (empty($contentFormat)) {
  266. $contentFormat = $this->_contentFormat;
  267. }
  268. if (empty($this->_id)) {
  269. $this->_id = md5($this);
  270. }
  271. $classes = $this->_headerClassses;
  272. if ($this->_selected) {
  273. $classes = array_merge($classes, $this->_selectedHeaderClasses);
  274. }
  275. $classText = implode(' ', $classes);
  276. $header = sprintf($headerFormat, $classText, $this->_baseURL.$this->_url, (!empty($this->_parentId)?$this->_parentId : $this->_id), $this->_id, $this->_caption);
  277. $classes = $this->_contentClasses;
  278. if (!$this->_selected) {
  279. $classes = array_merge($classes, $this->_hiddenContentClasses);
  280. }
  281. $classText = implode(' ', $classes);
  282. $content = sprintf($contentFormat, $classText, $this->_id, $this->_content);
  283. return array($header, $content);
  284. }
  285. public function __toString() {
  286. list($header, $content) = $this->render();
  287. return $header.$content;
  288. }
  289. }