Tabs.php 3.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\jui;

use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
12 13 14
use yii\helpers\Html;

/**
Alexander Kochetov committed
15
 * Tabs renders a tabs jQuery UI widget.
16 17 18 19 20 21
 *
 * For example:
 *
 * ```php
 * echo Tabs::widget(array(
 *     'items' => array(
Alexander Kochetov committed
22 23
 *         array(
 *             'header' => 'One',
24 25
 *             'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
 *         ),
Alexander Kochetov committed
26 27
 *         array(
 *             'header' => 'Two',
28
 *             'headerOptions' => array(...),
Alexander Kochetov committed
29 30
 *             'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
 *             'options' => array(...),
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *         ),
 *     ),
 * ));
 * ```
 *
 * @see http://api.jqueryui.com/tabs/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
class Tabs extends Widget
{
	/**
	 * @var array list of tabs in the tabs widget. Each array element represents a single
	 * tab with the following structure:
	 *
	 * ```php
Alexander Kochetov committed
47 48 49
	 * array(
	 *     // required, the header (HTML) of the tab
	 *     'header' => 'Tab label',
50 51 52
	 *     // required, the content (HTML) of the tab
	 *     'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
	 *     // optional the HTML attributes of the tab content container
Alexander Kochetov committed
53
	 *     'options'=> array(...),
54 55 56 57 58 59 60 61 62 63 64 65 66 67
	 *     // optional the HTML attributes of the tab header container
	 *     'headerOptions'=> array(...),
	 * )
	 * ```
	 */
	public $items = array();


	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options) . "\n";
Alexander Kochetov committed
68
		echo $this->renderHeaders() . "\n";
Alexander Kochetov committed
69
		echo $this->renderContents() . "\n";
Alexander Kochetov committed
70 71 72 73 74 75 76
		echo Html::endTag('div') . "\n";
		$this->registerWidget('tabs');
	}

	/**
	 * Renders tabs headers as specified on [[items]].
	 * @return string the rendering result.
Alexander Kochetov committed
77
	 * @throws InvalidConfigException.
Alexander Kochetov committed
78 79 80
	 */
	protected function renderHeaders()
	{
81
		$headers = array();
Alexander Kochetov committed
82
		foreach ($this->items as $n => $item) {
Alexander Kochetov committed
83
			if (!isset($item['header'])) {
Alexander Kochetov committed
84 85
				throw new InvalidConfigException("The 'header' option is required.");
			}
Alexander Kochetov committed
86 87 88 89
			$options = ArrayHelper::getValue($item, 'options', array());
			$id = isset($options['id']) ? $options['id'] : $this->options['id'] . '-tab' . $n;
			$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array());
			$headers[] = Html::tag('li', Html::a($item['header'], "#$id"), $headerOptions);
Alexander Kochetov committed
90 91 92 93 94 95
		}

		return Html::tag('ul', implode("\n", $headers));
	}

	/**
Alexander Kochetov committed
96
	 * Renders tabs contents as specified on [[items]].
Alexander Kochetov committed
97 98 99
	 * @return string the rendering result.
	 * @throws InvalidConfigException.
	 */
Alexander Kochetov committed
100
	protected function renderContents()
Alexander Kochetov committed
101
	{
Alexander Kochetov committed
102 103
		$contents = array();
		foreach ($this->items as $n => $item) {
Alexander Kochetov committed
104
			if (!isset($item['content'])) {
105 106
				throw new InvalidConfigException("The 'content' option is required.");
			}
Alexander Kochetov committed
107 108
			$options = ArrayHelper::getValue($item, 'options', array());
			if (!isset($options['id'])) {
Alexander Kochetov committed
109
				$options['id'] = $this->options['id'] . '-tab' . $n;
Alexander Kochetov committed
110 111
			}
			$contents[] = Html::tag('div', $item['content'], $options);
112
		}
Alexander Kochetov committed
113

Alexander Kochetov committed
114
		return implode("\n", $contents);
115 116
	}
}