1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11:
12: 13: 14:
15: class TbModal extends CWidget
16: {
17: 18: 19:
20: public $htmlOptions = array();
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: public $buttonOptions = array();
33:
34: 35: 36:
37: public $fade = true;
38:
39: 40: 41:
42: public $keyboard = true;
43:
44: 45: 46:
47: public $show = false;
48:
49: 50: 51: 52:
53: public $backdrop = true;
54:
55: 56: 57: 58:
59: public $remote;
60:
61: 62: 63:
64: public $onShow;
65:
66: 67: 68: 69:
70: public $onShown;
71:
72: 73: 74:
75: public $onHide;
76:
77: 78: 79: 80:
81: public $onHidden;
82:
83: 84: 85:
86: protected $events = array();
87:
88: 89: 90:
91: protected $options = array();
92:
93: 94: 95:
96: public $closeText = TbHtml::CLOSE_TEXT;
97:
98: 99: 100:
101: public ;
102:
103: 104: 105:
106: public $content;
107:
108: 109: 110:
111: public ;
112:
113: 114: 115:
116: public function init()
117: {
118: $this->attachBehavior('TbWidget', new TbWidget);
119:
120: TbArray::defaultValue('id', $this->getId(), $this->htmlOptions);
121: TbArray::defaultValue('role', 'dialog', $this->htmlOptions);
122: TbArray::defaultValue('tabindex', '-1', $this->htmlOptions);
123:
124: TbHtml::addCssClass('modal hide', $this->htmlOptions);
125: if ($this->fade) {
126: TbHtml::addCssClass('fade', $this->htmlOptions);
127: }
128:
129: if (is_array($this->footer)) {
130: $this->footer = implode(' ', $this->footer);
131: }
132:
133: $this->initOptions();
134: $this->initEvents();
135: }
136:
137: 138: 139:
140: public function initEvents()
141: {
142: foreach (array('onShow', 'onShown', 'onHide', 'onHidden') as $event) {
143: if ($this->$event !== null) {
144: $modalEvent = strtolower(substr($event, 2));
145: if ($this->$event instanceof CJavaScriptExpression) {
146: $this->events[$modalEvent] = $this->$event;
147: } else {
148: $this->events[$modalEvent] = new CJavaScriptExpression($this->$event);
149: }
150: }
151: }
152: }
153:
154: 155: 156: 157:
158: public function initOptions()
159: {
160: if ($remote = TbArray::popValue('remote', $this->options)) {
161: $this->options['remote'] = CHtml::normalizeUrl($remote);
162: }
163:
164: TbArray::defaultValue('backdrop', $this->backdrop, $this->options);
165: TbArray::defaultValue('keyboard', $this->keyboard, $this->options);
166: TbArray::defaultValue('show', $this->show, $this->options);
167: }
168:
169: 170: 171:
172: public function run()
173: {
174: $this->renderModal();
175: $this->renderButton();
176: $this->registerClientScript();
177: }
178:
179: 180: 181:
182: public function renderButton()
183: {
184: if (!empty($this->buttonOptions) && is_array($this->buttonOptions)) {
185: TbArray::defaultValue('data-toggle', 'modal', $this->buttonOptions);
186:
187: if ($this->remote !== null) {
188: $this->buttonOptions['data-remote'] = CHtml::normalizeUrl($this->remote);
189: }
190:
191: $selector = '#' . $this->htmlOptions['id'];
192: $label = TbArray::popValue('label', $this->buttonOptions, 'button');
193: $attr = isset($this->buttonOptions['data-remote']) ? 'data-target' : 'href';
194: TbArray::defaultValue($attr, $selector, $this->buttonOptions);
195: echo TbHtml::button($label, $this->buttonOptions);
196: }
197: }
198:
199: 200: 201:
202: public function renderModal()
203: {
204: echo TbHtml::openTag('div', $this->htmlOptions) . PHP_EOL;
205:
206: $this->renderModalHeader();
207: $this->renderModalBody();
208: $this->renderModalFooter();
209:
210: echo '</div>' . PHP_EOL;
211: }
212:
213: 214: 215:
216: public function ()
217: {
218: echo '<div class="modal-header">' . PHP_EOL;
219: if ($this->closeText) {
220: echo TbHtml::closeButton($this->closeText, array('data-dismiss' => 'modal'));
221: }
222: echo TbHtml::tag('h3', array(), $this->header);
223: echo '</div>' . PHP_EOL;
224: }
225:
226: 227: 228:
229: public function renderModalBody()
230: {
231: echo '<div class="modal-body">' . PHP_EOL;
232: echo $this->content;
233: echo '</div>' . PHP_EOL;
234: }
235:
236: 237: 238:
239: public function ()
240: {
241:
242: echo '<div class="modal-footer">' . PHP_EOL;
243: echo $this->footer;
244: echo '</div>' . PHP_EOL;
245: }
246:
247: 248: 249:
250: public function registerClientScript()
251: {
252: $selector = '#' . $this->htmlOptions['id'];
253:
254:
255:
256: if (empty($this->buttonOptions)) {
257: $this->registerPlugin(TbApi::PLUGIN_MODAL, $selector, $this->options);
258: }
259:
260: $this->registerEvents($selector, $this->events);
261: }
262:
263: }