2
0

TabContainer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see ZendX_JQuery_View_Helper_UiWidget
  24. */
  25. require_once "ZendX/JQuery/View/Helper/UiWidget.php";
  26. /**
  27. * jQuery Tabs Container View Helper
  28. *
  29. * @uses Zend_Json
  30. * @package ZendX_JQuery
  31. * @subpackage View
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class ZendX_JQuery_View_Helper_TabContainer extends ZendX_JQuery_View_Helper_UiWidget
  36. {
  37. /**
  38. * Save all the pre-rendered tab panes to each tab container
  39. *
  40. * @var array
  41. */
  42. protected $_tabs = array();
  43. /**
  44. * Add Tab to TabsContainer
  45. *
  46. * @param string $id
  47. * @param string $name
  48. * @param string $content
  49. * @param array $options
  50. * @return ZendX_JQuery_View_Helper_TabsContainer
  51. */
  52. public function addPane($id, $name, $content, array $options=array())
  53. {
  54. if(!isset($this->_tabs[$id])) {
  55. $this->_tabs[$id] = array();
  56. }
  57. if(strlen($name) == 0 && isset($options['title'])) {
  58. $name = $options['title'];
  59. }
  60. $this->_tabs[$id][] = array('name' => $name, 'content' => $content, 'options' => $options);
  61. return $this;
  62. }
  63. /**
  64. * Render TabsContainer with all the currently registered tabs.
  65. *
  66. * Render all tabs to the given $id. If no arguments are given the
  67. * tabsContainer view helper object is returned and can be used
  68. * for chaining {@link addPane()} for tab pane adding.
  69. *
  70. * @link http://docs.jquery.com/UI/Tabs
  71. * @param string $id
  72. * @param array $params
  73. * @param array $attribs
  74. * @return string|ZendX_JQuery_View_Helper_TabsContainer
  75. */
  76. public function tabContainer($id=null, $params=array(), $attribs=array())
  77. {
  78. if(func_num_args() === 0) {
  79. return $this;
  80. }
  81. if(!isset($attribs['id'])) {
  82. $attribs['id'] = $id;
  83. }
  84. $content = "";
  85. if(isset($this->_tabs[$id])) {
  86. $list = '<ul class="ui-tabs-nav">'.PHP_EOL;
  87. $html = '';
  88. $fragment_counter = 1;
  89. foreach($this->_tabs[$id] AS $k => $v) {
  90. $frag_name = sprintf('%s-frag-%d', $attribs['id'], $fragment_counter++);
  91. $opts = $v['options'];
  92. if(isset($opts['contentUrl'])) {
  93. $list .= '<li class="ui-tabs-nav-item"><a href="'.$opts['contentUrl'].'"><span>'.$v['name'].'</span></a></li>'.PHP_EOL;
  94. } else {
  95. $list .= '<li class="ui-tabs-nav-item"><a href="#'.$frag_name.'"><span>'.$v['name'].'</span></a></li>'.PHP_EOL;
  96. $html .= '<div id="'.$frag_name.'" class="ui-tabs-panel">'.$v['content'].'</div>'.PHP_EOL;
  97. }
  98. }
  99. $list .= '</ul>'.PHP_EOL;
  100. $content = $list.$html;
  101. unset($this->_tabs[$id]);
  102. }
  103. if(count($params)) {
  104. $params = ZendX_JQuery::encodeJson($params);
  105. } else {
  106. $params = '{}';
  107. }
  108. $js = sprintf('%s("#%s").tabs(%s);',
  109. ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
  110. $attribs['id'],
  111. $params
  112. );
  113. $this->jquery->addOnLoad($js);
  114. $html = '<div'
  115. . $this->_htmlAttribs($attribs)
  116. . '>'.PHP_EOL
  117. . $content
  118. . '</div>'.PHP_EOL;
  119. return $html;
  120. }
  121. }