PageRenderTime 24ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/joindin/joind.in
PHP | 559 lines | 271 code | 48 blank | 240 comment | 15 complexity | 5913784b9cba720ec7981fa48cb08141 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Helpers for creating Accessible Tabs.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Joind.in
  8. * @package Helpers
  9. * @copyright 2009 - 2010 Joind.in
  10. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  11. * @link http://github.com/joindin/joind.in
  12. */
  13. /**
  14. * Helpers for creating Accessible Tabs.
  15. *
  16. * PHP version 5
  17. *
  18. * @category Joind.in
  19. * @package Helpers
  20. * @copyright 2009 - 2010 Joind.in
  21. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  22. * @link http://github.com/joindin/joind.in
  23. */
  24. class joindIn_TabContainer implements countable
  25. {
  26. /**
  27. * @var array Internal tab data storage
  28. */
  29. private $_tabs = array();
  30. /**
  31. * @var string base name for this instance of tabs.
  32. */
  33. private $_containerName = 'view';
  34. /**
  35. * @var string the hash id of the selected tab to display as primary
  36. */
  37. private $_selectedTab = '';
  38. /**
  39. * @var string The Base URL from which tab links are built
  40. */
  41. private $_baseUrl = '';
  42. /**
  43. * Set this tab instance's container name. Used as a base name for ids
  44. * in the html render
  45. *
  46. * @param string $name The New Container name
  47. *
  48. * @return joindIn_Tabs Itself for a fluid chainable instance
  49. */
  50. public function setContainerName($name)
  51. {
  52. $this->_containerName = $name;
  53. return $this;
  54. }
  55. /**
  56. * Get this tab instance's container name. Used as a base name for ids
  57. * in the html render
  58. *
  59. * @return string The Container Name
  60. */
  61. public function getContainerName()
  62. {
  63. return $this->_containerName;
  64. }
  65. /**
  66. * Set this tab instance's Base URL. Used as a base for links in the
  67. * html render
  68. *
  69. * @param string $url The New Base URL
  70. *
  71. * @return joindIn_Tabs Itself for a fluid chainable instance
  72. */
  73. public function setBaseUrl($url)
  74. {
  75. $this->_baseUrl = $url;
  76. return $this;
  77. }
  78. /**
  79. * Get this tab instance's Base URL. Used as a base for links in the
  80. * html render
  81. *
  82. * @return string The Base URL
  83. */
  84. public function getBaseUrl()
  85. {
  86. return $this->_baseUrl;
  87. }
  88. /**
  89. * Set this tab instance's Selected Tab.
  90. *
  91. * @param string $id Id of the tab to make selected
  92. *
  93. * @return joindIn_Tabs Itself for a fluid chainable instance
  94. */
  95. public function setSelectedTab($id)
  96. {
  97. if (isset($this->_tabs[$id])) {
  98. $this->_tabs[$id]->setSelected();
  99. }
  100. $this->_selectedTab = $id;
  101. return $this;
  102. }
  103. /**
  104. * Get this tab instance's selected Tab.
  105. * Please note, this does not get active on client side.
  106. *
  107. * @return string The Active Tab's Hash
  108. */
  109. public function getSelectedTab()
  110. {
  111. return $this->_tabs[$this->_selectedTab];
  112. }
  113. /**
  114. * Adds a new tab to the collection. If you specify an id it will
  115. * use that in the collection, otherwise it will use the tab's own
  116. * id
  117. *
  118. * @param joindIn_Tab $tab Tab to add
  119. * @param string $id Optional id.
  120. *
  121. * @return string ID of tab that was just added
  122. */
  123. public function addTab(joindIn_Tab $tab, $id=null)
  124. {
  125. if ($id===null) {
  126. $id = $tab->getId();
  127. }
  128. $this->_tabs[$id] = $tab;
  129. return $id;
  130. }
  131. /**
  132. * Sets the tabs in the collection
  133. *
  134. * @param array $tabs Tabs to set
  135. *
  136. * @return null
  137. */
  138. public function setTabs(array $tabs)
  139. {
  140. $this->_tabs = $tabs;
  141. }
  142. /**
  143. * Retrieves the tabs in the collection
  144. *
  145. * @return array
  146. */
  147. public function getTabs()
  148. {
  149. return $this->_tabs;
  150. }
  151. /**
  152. * Retrieves a tab by its id
  153. *
  154. * @param string $id Tab id to fetch
  155. *
  156. * @return joindIn_Tab|null
  157. */
  158. public function getTab($id)
  159. {
  160. if (isset($this->_tabs[$id])) {
  161. return $this->_tabs[$id];
  162. }
  163. return null;
  164. }
  165. /**
  166. * Adds tabs to the collection
  167. *
  168. * @param array $tabs Tabs to add to collection
  169. *
  170. * @return null
  171. */
  172. public function addTabs(array $tabs)
  173. {
  174. if (count($this->_tabs) > 0) {
  175. $this->_tabs = array_merge($this->_tabs, $tabs);
  176. } else {
  177. $this->_tabs = $tabs;
  178. }
  179. }
  180. /**
  181. * Renders a tab container
  182. *
  183. * @return string
  184. */
  185. public function render()
  186. {
  187. if ($this->count() === 0) {
  188. return '';
  189. }
  190. ob_start();
  191. $contentList = array();
  192. reset($this->_tabs);
  193. if (empty($this->_selectedTab)
  194. || !isset($this->_tabs[$this->_selectedTab])
  195. ) {
  196. $tmp_tab = current($this->_tabs);
  197. $this->_selectedTab = $tmp_tab->getId();
  198. reset($this->_tabs);
  199. }
  200. ?>
  201. <div id="<?php echo $this->_containerName; ?>-tabs" class="ui-tabs
  202. ui-widget ui-widget-content ui-corner-all">
  203. <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix
  204. ui-widget-header ui-corner-all">
  205. <?php
  206. foreach ($this->_tabs as $tab) {
  207. if ($tab->getId() == $this->_selectedTab) {
  208. $tab->setSelected();
  209. }
  210. $tab->setParentId($this->_containerName);
  211. $tab->setBaseUrl($this->_baseUrl);
  212. list($tabTop, $content) = $tab->render();
  213. echo $tabTop;
  214. $contentList[$tab->getId()] = $content;
  215. }
  216. ?>
  217. </ul>
  218. <?php
  219. foreach ($contentList as $hash=>$tabContent) {
  220. echo $tabContent;
  221. }
  222. ?>
  223. </div>
  224. <?php $content = ob_get_clean();
  225. return $content;
  226. }
  227. /**
  228. * Returns a string representation of a tab container
  229. *
  230. * @return string
  231. */
  232. public function __toString()
  233. {
  234. return $this->render();
  235. }
  236. /**
  237. * Countable Interface
  238. *
  239. * @return int the amount of tabs that we have
  240. */
  241. public function count()
  242. {
  243. return count($this->_tabs);
  244. }
  245. }
  246. /**
  247. * joindIn_Tab
  248. *
  249. * A Singular Tab to be added to the Tab Container
  250. *
  251. * @category Tab
  252. * @package Helper
  253. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  254. */
  255. class joindIn_Tab
  256. {
  257. private $_id = '';
  258. private $_url = '';
  259. private $_baseURL = '';
  260. private $_selected = false;
  261. private $_caption = '';
  262. private $_content = '';
  263. private $_parentId = '';
  264. private $_headerClassses = array(
  265. 'ui-state-default',
  266. 'ui-corner-top'
  267. );
  268. private $_contentClasses = array(
  269. 'ui-tabs-panel',
  270. 'ui-widget-content',
  271. 'ui-corner-bottom'
  272. );
  273. private $_selectedHeaderClasses = array(
  274. 'ui-tabs-selected',
  275. 'ui-state-active',
  276. 'ui-state-focus'
  277. );
  278. private $_selectedContentClasses = array();
  279. private $_hiddenContentClasses = array('ui-tabs-hide');
  280. private $_headerFormat;
  281. private $_contentFormat;
  282. /**
  283. * Constructor
  284. *
  285. * @param string $url The URL of the tab link
  286. * @param string $caption The caption of the tab
  287. * @param string $content the content of the tab
  288. * @param boolean $selected Whether this tab is selected
  289. */
  290. public function __construct(
  291. $url = '',
  292. $caption = '',
  293. $content = '',
  294. $selected = false
  295. ) {
  296. $this->_id = $url;
  297. $this->_url = $url;
  298. $this->_selected = $selected;
  299. $this->_caption = $caption;
  300. $this->_content = $content;
  301. $this->_headerFormat = '<li class="%1$s">'.
  302. '<a href="%2$s#%3$s-tabs" rel="%4$s">%5$s</a></li>';
  303. $this->_contentFormat = '<div class="%1$s" id="%2$s">'.
  304. '<div class="ui-widget">%3$s</div></div>';
  305. }
  306. /**
  307. * Clears the content of this tab. This method is chainable.
  308. *
  309. * @return joindIn_TabContainer
  310. */
  311. public function clearContent()
  312. {
  313. $this->content = '';
  314. return $this;
  315. }
  316. /**
  317. * Appends content to the tab. This method is chainable.
  318. *
  319. * @param string $content Content to add to tab
  320. *
  321. * @return joindIn_TabContainer
  322. */
  323. public function addContent($content)
  324. {
  325. $this->_content .= $content;
  326. return $this;
  327. }
  328. /**
  329. * Returns the contents for this tab.
  330. *
  331. * @return string
  332. */
  333. public function getContent()
  334. {
  335. return $this->_content;
  336. }
  337. /**
  338. * Sets the content for this tab. This method is chainable.
  339. *
  340. * @param string $content Content to set for tab
  341. *
  342. * @return joindIn_TabContainer
  343. */
  344. public function setContent($content)
  345. {
  346. $this->_content = $content;
  347. return $this;
  348. }
  349. /**
  350. * Sets the id of this tab. This method is chainable.
  351. *
  352. * @param string $newId New Id for this tab
  353. *
  354. * @return joindIn_TabContainer
  355. */
  356. public function setId($newId)
  357. {
  358. $this->_id = $newId;
  359. return $this;
  360. }
  361. /**
  362. * Retrieves the id for the tab
  363. *
  364. * @return int
  365. */
  366. public function getId()
  367. {
  368. return $this->_id;
  369. }
  370. /**
  371. * Sets the caption for the tab. This method is chainable.
  372. *
  373. * @param string $newCaption New caption for the tab
  374. *
  375. * @return joindIn_TabContainer
  376. */
  377. public function setCaption($newCaption)
  378. {
  379. $this->_caption = $newCaption;
  380. return $this;
  381. }
  382. /**
  383. * Retrieves the caption for the tab.
  384. *
  385. * @return string
  386. */
  387. public function getCaption()
  388. {
  389. return $this->_caption;
  390. }
  391. /**
  392. * Sets the parent id of the tab. This method is chainable.
  393. *
  394. * @param int $newParentId New Parent ID to set
  395. *
  396. * @return joindIn_TabContainer
  397. */
  398. public function setParentId($newParentId)
  399. {
  400. $this->_parentId = $newParentId;
  401. return $this;
  402. }
  403. /**
  404. * Retrieves the url for the tab
  405. *
  406. * @return string
  407. */
  408. public function getURL()
  409. {
  410. return $this->_url;
  411. }
  412. /**
  413. * Sets the base url for the tab. This method is chainable.
  414. *
  415. * @param string $newBaseURL New URL to set as base
  416. *
  417. * @return joindIn_TabContainer
  418. */
  419. public function setBaseURL($newBaseURL)
  420. {
  421. $this->_baseURL = $newBaseURL;
  422. return $this;
  423. }
  424. /**
  425. * Retrieves the base url for the tab
  426. *
  427. * @return string
  428. */
  429. public function getBaseURL()
  430. {
  431. return $this->_baseURL;
  432. }
  433. /**
  434. * Sets the tab as selected. This method is chainable.
  435. *
  436. * @param boolean $selected Whether the tab is selected
  437. *
  438. * @return joindIn_TabContainer
  439. */
  440. public function setSelected($selected=true)
  441. {
  442. $this->_selected = $selected;
  443. return $this;
  444. }
  445. /**
  446. * Returns the selected tab
  447. *
  448. * @return boolean
  449. */
  450. public function getSelected()
  451. {
  452. return $this->_selected;
  453. }
  454. /**
  455. * Renders some tabs
  456. *
  457. * @param string $headerFormat Format string for header
  458. * @param string $contentFormat Format string for content
  459. *
  460. * @return array
  461. */
  462. public function render($headerFormat = '', $contentFormat = '')
  463. {
  464. $header = '';
  465. if (empty($headerFormat)) {
  466. $headerFormat = $this->_headerFormat;
  467. }
  468. $content = '';
  469. if (empty($contentFormat)) {
  470. $contentFormat = $this->_contentFormat;
  471. }
  472. if (empty($this->_id)) {
  473. $this->_id = md5($this);
  474. }
  475. $classes = $this->_headerClassses;
  476. if ($this->_selected) {
  477. $classes = array_merge($classes, $this->_selectedHeaderClasses);
  478. }
  479. $classText = implode(' ', $classes);
  480. $header = sprintf(
  481. $headerFormat,
  482. $classText,
  483. $this->_baseURL.$this->_url,
  484. (!empty($this->_parentId) ? $this->_parentId : $this->_id),
  485. $this->_id,
  486. $this->_caption
  487. );
  488. $classes = $this->_contentClasses;
  489. if (!$this->_selected) {
  490. $classes = array_merge($classes, $this->_hiddenContentClasses);
  491. }
  492. $classText = implode(' ', $classes);
  493. $content = sprintf(
  494. $contentFormat,
  495. $classText,
  496. $this->_id,
  497. $this->_content
  498. );
  499. return array($header, $content);
  500. }
  501. /**
  502. * Returns a string representation of tabs
  503. *
  504. * @return string
  505. */
  506. public function __toString()
  507. {
  508. list($header, $content) = $this->render();
  509. return $header.$content;
  510. }
  511. }