Accordion.php 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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;
use yii\helpers\base\ArrayHelper;
use yii\helpers\Html;

/**
 * Accordion renders an accordion jQuery UI widget.
 *
 * For example:
 *
 * ```php
 * echo Accordion::widget(array(
 *     'items' => array(
22 23
 *         array(
 *             'header' => 'Section 1',
24 25
 *             'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
 *         ),
26 27
 *         array(
 *             'header' => 'Section 2',
28
 *             'headerOptions' => array(...),
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
 *         ),
 *     ),
 * ));
 * ```
 *
 * @see http://api.jqueryui.com/accordion/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
class Accordion extends Widget
{
	/**
Alexander Kochetov committed
43 44
	 * @var array list of sections in the accordion widget. Each array element represents a single
	 * section with the following structure:
45 46
	 *
	 * ```php
47 48 49
	 * array(
	 *     // required, the header (HTML) of the section
	 *     'header' => 'Section label',
Alexander Kochetov committed
50
	 *     // required, the content (HTML) of the section
51
	 *     'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
Alexander Kochetov committed
52
	 *     // optional the HTML attributes of the section content container
53
	 *     'options'=> array(...),
Alexander Kochetov committed
54
	 *     // optional the HTML attributes of the section header container
55 56 57 58 59 60 61 62 63 64 65 66 67
	 *     'headerOptions'=> array(...),
	 * )
	 * ```
	 */
	public $items = array();


	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options) . "\n";
68
		echo $this->renderSections() . "\n";
69 70 71 72 73
		echo Html::endTag('div') . "\n";
		$this->registerWidget('accordion');
	}

	/**
74
	 * Renders collapsible sections as specified on [[items]].
75
	 * @return string the rendering result.
76
	 * @throws InvalidConfigException.
77
	 */
78
	protected function renderSections()
79
	{
80 81 82 83 84 85 86 87
		$sections = array();
		foreach ($this->items as $item) {
			if (!isset($item['header'])) {
				throw new InvalidConfigException("The 'header' option is required.");
			}
			if (!isset($item['content'])) {
				throw new InvalidConfigException("The 'content' option is required.");
			}
Alexander Kochetov committed
88 89
			$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array());
			$sections[] = Html::tag('h3', $item['header'], $headerOptions);
90 91 92
			$options = ArrayHelper::getValue($item, 'options', array());
			$sections[] = Html::tag('div', $item['content'], $options);;
		}
93

94
		return implode("\n", $sections);
95 96
	}
}