qa-theme-base.php 64.4 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Default theme class, broken into lots of little functions for easy overriding


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

22 23 24 25
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
Scott committed
26 27 28 29 30 31 32 33 34 35 36 37 38


/*
	How do I make a theme which goes beyond CSS to actually modify the HTML output?

	Create a file named qa-theme.php in your new theme directory which defines a class qa_html_theme
	that extends this base class qa_html_theme_base. You can then override any of the methods below,
	referring back to the default method using double colon (qa_html_theme_base::) notation.

	Plugins can also do something similar by using a layer. For more information and to see some example
	code, please consult the online Q2A documentation.
*/

39 40
class qa_html_theme_base
{
41 42 43 44 45 46
	public $template;
	public $content;
	public $rooturl;
	public $request;
	public $isRTL; // (boolean) whether text direction is Right-To-Left

47
	protected $minifyHtml; // (boolean) whether to indent the HTML
48 49 50
	protected $indent = 0;
	protected $lines = 0;
	protected $context = array();
Scott committed
51

52 53
	// whether to use new block layout in rankings (true) or fall back to tables (false)
	protected $ranking_block_layout = false;
54 55
	// theme 'slug' to use as CSS class
	protected $theme;
Scott committed
56 57


Scott committed
58 59
	/**
	 * Initialize the object and assign local variables.
Scott committed
60 61 62 63
	 * @param $template
	 * @param $content
	 * @param $rooturl
	 * @param $request
Scott committed
64
	 */
65 66 67 68 69 70 71
	public function __construct($template, $content, $rooturl, $request)
	{
		$this->template = $template;
		$this->content = $content;
		$this->rooturl = $rooturl;
		$this->request = $request;
		$this->isRTL = isset($content['direction']) && $content['direction'] === 'rtl';
72
		$this->minifyHtml = !empty($content['options']['minify_html']);
73
	}
74

75 76 77
	/**
	 * @deprecated PHP4-style constructor deprecated from 1.7; please use proper `__construct`
	 * function instead.
Scott committed
78 79 80 81
	 * @param $template
	 * @param $content
	 * @param $rooturl
	 * @param $request
82 83 84 85 86
	 */
	public function qa_html_theme_base($template, $content, $rooturl, $request)
	{
		self::__construct($template, $content, $rooturl, $request);
	}
Scott committed
87

88

Scott committed
89 90 91 92
	/**
	 * Output each element in $elements on a separate line, with automatic HTML indenting.
	 * This should be passed markup which uses the <tag/> form for unpaired tags, to help keep
	 * track of indenting, although its actual output converts these to <tag> for W3C validation.
Scott committed
93
	 * @param $elements
Scott committed
94
	 */
95 96 97
	public function output_array($elements)
	{
		foreach ($elements as $element) {
98
			$line = str_replace('/>', '>', $element);
99

100 101
			if ($this->minifyHtml) {
				if (strlen($line))
Scott committed
102 103 104
					echo $line . "\n";
			} else {
				$delta = substr_count($element, '<') - substr_count($element, '<!') - 2 * substr_count($element, '</') - substr_count($element, '/>');
105

106 107 108
				if ($delta < 0) {
					$this->indent += $delta;
				}
109

Scott committed
110
				echo str_repeat("\t", max(0, $this->indent)) . $line . "\n";
111 112 113 114

				if ($delta > 0) {
					$this->indent += $delta;
				}
Scott committed
115
			}
116 117

			$this->lines++;
Scott committed
118
		}
119
	}
Scott committed
120 121


Scott committed
122 123 124
	/**
	 * Output each passed parameter on a separate line - see output_array() comments.
	 */
125 126 127 128 129
	public function output() // other parameters picked up via func_get_args()
	{
		$args = func_get_args();
		$this->output_array($args);
	}
Scott committed
130 131


Scott committed
132 133 134
	/**
	 * Output $html at the current indent level, but don't change indent level based on the markup within.
	 * Useful for user-entered HTML which is unlikely to follow the rules we need to track indenting.
Scott committed
135
	 * @param $html
Scott committed
136
	 */
137 138 139
	public function output_raw($html)
	{
		if (strlen($html))
Scott committed
140
			echo str_repeat("\t", max(0, $this->indent)) . $html . "\n";
141
	}
Scott committed
142 143


Scott committed
144 145 146
	/**
	 * Output the three elements ['prefix'], ['data'] and ['suffix'] of $parts (if they're defined),
	 * with appropriate CSS classes based on $class, using $outertag and $innertag in the markup.
Scott committed
147 148 149 150 151
	 * @param $parts
	 * @param $class
	 * @param string $outertag
	 * @param string $innertag
	 * @param string $extraclass
Scott committed
152
	 */
Scott committed
153
	public function output_split($parts, $class, $outertag = 'span', $innertag = 'span', $extraclass = null)
154 155 156 157 158
	{
		if (empty($parts) && strtolower($outertag) != 'td')
			return;

		$this->output(
Scott committed
159 160 161 162 163
			'<' . $outertag . ' class="' . $class . (isset($extraclass) ? (' ' . $extraclass) : '') . '">',
			(strlen(@$parts['prefix']) ? ('<' . $innertag . ' class="' . $class . '-pad">' . $parts['prefix'] . '</' . $innertag . '>') : '') .
			(strlen(@$parts['data']) ? ('<' . $innertag . ' class="' . $class . '-data">' . $parts['data'] . '</' . $innertag . '>') : '') .
			(strlen(@$parts['suffix']) ? ('<' . $innertag . ' class="' . $class . '-pad">' . $parts['suffix'] . '</' . $innertag . '>') : ''),
			'</' . $outertag . '>'
164 165
		);
	}
Scott committed
166 167


Scott committed
168 169
	/**
	 * Set some context, which be accessed via $this->context for a function to know where it's being used on the page.
Scott committed
170 171
	 * @param $key
	 * @param $value
Scott committed
172
	 */
173 174 175 176
	public function set_context($key, $value)
	{
		$this->context[$key] = $value;
	}
Scott committed
177 178


Scott committed
179 180
	/**
	 * Clear some context (used at the end of the appropriate loop).
Scott committed
181
	 * @param $key
Scott committed
182
	 */
183 184 185 186
	public function clear_context($key)
	{
		unset($this->context[$key]);
	}
Scott committed
187 188


Scott committed
189 190 191
	/**
	 * Reorder the parts of the page according to the $parts array which contains part keys in their new order. Call this
	 * before main_parts(). See the docs for qa_array_reorder() in util/sort.php for the other parameters.
Scott committed
192 193 194
	 * @param $parts
	 * @param string $beforekey
	 * @param bool $reorderrelative
Scott committed
195
	 */
Scott committed
196
	public function reorder_parts($parts, $beforekey = null, $reorderrelative = true)
197
	{
Scott committed
198
		require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
199

200 201
		qa_array_reorder($this->content, $parts, $beforekey, $reorderrelative);
	}
Scott committed
202 203


Scott committed
204 205
	/**
	 * Output the widgets (as provided in $this->content['widgets']) for $region and $place.
Scott committed
206 207
	 * @param $region
	 * @param $place
Scott committed
208
	 */
209 210
	public function widgets($region, $place)
	{
Scott committed
211 212
		$widgetsHere = isset($this->content['widgets'][$region][$place]) ? $this->content['widgets'][$region][$place] : array();
		if (is_array($widgetsHere) && count($widgetsHere) > 0) {
Scott committed
213
			$this->output('<div class="qa-widgets-' . $region . ' qa-widgets-' . $region . '-' . $place . '">');
214

Scott committed
215
			foreach ($widgetsHere as $module) {
Scott committed
216
				$this->output('<div class="qa-widget-' . $region . ' qa-widget-' . $region . '-' . $place . '">');
217 218 219 220 221
				$module->output_widget($region, $place, $this, $this->template, $this->request, $this->content);
				$this->output('</div>');
			}

			$this->output('</div>', '');
Scott committed
222
		}
223
	}
Scott committed
224

225 226
	/**
	 * Pre-output initialization. Immediately called after loading of the module. Content and template variables are
Scott committed
227
	 * already setup at this point. Useful to perform layer initialization in the earliest and safest stage possible.
228
	 */
Scott committed
229 230 231 232
	public function initialize()
	{
		// abstract method
	}
Scott committed
233

Scott committed
234 235 236
	/**
	 * Post-output cleanup. For now, check that the indenting ended right, and if not, output a warning in an HTML comment.
	 */
237 238
	public function finish()
	{
239
		if ($this->indent !== 0 && !$this->minifyHtml) {
Scott committed
240 241 242 243
			echo "<!--\nIt's no big deal, but your HTML could not be indented properly. To fix, please:\n" .
				"1. Use this->output() to output all HTML.\n" .
				"2. Balance all paired tags like <td>...</td> or <div>...</div>.\n" .
				"3. Use a slash at the end of unpaired tags like <img/> or <input/>.\n" .
244
				"Thanks!\n-->\n";
Scott committed
245
		}
246
	}
Scott committed
247 248


Scott committed
249
	// From here on, we have a large number of class methods which output particular pieces of HTML markup
250
	// The calling chain is initiated from qa-page.php, or ajax/*.php for refreshing parts of a page,
Scott committed
251 252
	// For most HTML elements, the name of the function is similar to the element's CSS class, for example:
	// search() outputs <div class="qa-search">, q_list() outputs <div class="qa-q-list">, etc...
Scott committed
253

254 255 256 257
	public function doctype()
	{
		$this->output('<!DOCTYPE html>');
	}
Scott committed
258

259 260 261
	public function html()
	{
		$attribution = '<!-- Powered by Question2Answer - http://www.question2answer.org/ -->';
Scott committed
262
		$extratags = isset($this->content['html_tags']) ? $this->content['html_tags'] : '';
263

264
		$this->output(
Scott committed
265
			'<html ' . $extratags . '>',
266 267 268 269 270 271 272 273 274 275 276
			$attribution
		);

		$this->head();
		$this->body();

		$this->output(
			$attribution,
			'</html>'
		);
	}
Scott committed
277

278 279 280 281
	public function head()
	{
		$this->output(
			'<head>',
Scott committed
282
			'<meta charset="' . $this->content['charset'] . '"/>'
283 284 285 286 287 288 289 290 291 292 293 294
		);

		$this->head_title();
		$this->head_metas();
		$this->head_css();
		$this->head_links();
		$this->head_lines();
		$this->head_script();
		$this->head_custom();

		$this->output('</head>');
	}
Scott committed
295

296 297 298
	public function head_title()
	{
		$pagetitle = strlen($this->request) ? strip_tags(@$this->content['title']) : '';
Scott committed
299
		$headtitle = (strlen($pagetitle) ? "$pagetitle - " : '') . $this->content['site_title'];
Scott committed
300

Scott committed
301
		$this->output('<title>' . $headtitle . '</title>');
302
	}
Scott committed
303

304 305
	public function head_metas()
	{
Scott committed
306 307 308
		if (strlen(@$this->content['description'])) {
			$this->output('<meta name="description" content="' . $this->content['description'] . '"/>');
		}
Scott committed
309

Scott committed
310 311 312 313
		if (strlen(@$this->content['keywords'])) {
			// as far as I know, meta keywords have zero effect on search rankings or listings
			$this->output('<meta name="keywords" content="' . $this->content['keywords'] . '"/>');
		}
314 315 316 317
	}

	public function head_links()
	{
Scott committed
318 319 320
		if (isset($this->content['canonical'])) {
			$this->output('<link rel="canonical" href="' . $this->content['canonical'] . '"/>');
		}
321

Scott committed
322
		if (isset($this->content['feed']['url'])) {
Scott committed
323
			$this->output('<link rel="alternate" type="application/rss+xml" href="' . $this->content['feed']['url'] . '" title="' . @$this->content['feed']['label'] . '"/>');
Scott committed
324
		}
325 326 327 328 329 330

		// convert page links to rel=prev and rel=next tags
		if (isset($this->content['page_links']['items'])) {
			foreach ($this->content['page_links']['items'] as $page_link) {
				if (in_array($page_link['type'], array('prev', 'next')))
					$this->output('<link rel="' . $page_link['type'] . '" href="' . $page_link['url'] . '" />');
Scott committed
331 332
			}
		}
333
	}
Scott committed
334

335 336 337
	public function head_script()
	{
		if (isset($this->content['script'])) {
Scott committed
338
			foreach ($this->content['script'] as $scriptline) {
339
				$this->output_raw($scriptline);
Scott committed
340
			}
341 342
		}
	}
Scott committed
343

344 345
	public function head_css()
	{
Scott committed
346
		$this->output('<link rel="stylesheet" href="' . $this->rooturl . $this->css_name() . '"/>');
Scott committed
347

348
		if (isset($this->content['css_src'])) {
Scott committed
349
			foreach ($this->content['css_src'] as $css_src) {
Scott committed
350
				$this->output('<link rel="stylesheet" href="' . $css_src . '"/>');
Scott committed
351
			}
Scott committed
352 353
		}

354
		if (!empty($this->content['notices'])) {
Scott committed
355
			$this->output(
356 357 358
				'<style>',
				'.qa-body-js-on .qa-notice {display:none;}',
				'</style>'
Scott committed
359
			);
360 361
		}
	}
Scott committed
362

363 364
	public function css_name()
	{
Scott committed
365
		return 'qa-styles.css?' . QA_VERSION;
366
	}
Scott committed
367

368 369 370
	public function head_lines()
	{
		if (isset($this->content['head_lines'])) {
Scott committed
371
			foreach ($this->content['head_lines'] as $line) {
372
				$this->output_raw($line);
Scott committed
373
			}
Scott committed
374
		}
375
	}
Scott committed
376

377 378 379 380
	public function head_custom()
	{
		// abstract method
	}
Scott committed
381

382 383 384 385 386
	public function body()
	{
		$this->output('<body');
		$this->body_tags();
		$this->output('>');
Scott committed
387

388 389 390 391 392
		$this->body_script();
		$this->body_header();
		$this->body_content();
		$this->body_footer();
		$this->body_hidden();
Scott committed
393

394 395
		$this->output('</body>');
	}
Scott committed
396

397 398
	public function body_hidden()
	{
399
		$this->output('<div style="position:absolute;overflow:hidden;clip:rect(0 0 0 0);height:0;width:0;margin:0;padding:0;border:0;">');
400 401 402
		$this->waiting_template();
		$this->output('</div>');
	}
Scott committed
403

404 405 406 407
	public function waiting_template()
	{
		$this->output('<span id="qa-waiting-template" class="qa-waiting">...</span>');
	}
Scott committed
408

409 410 411 412
	public function body_script()
	{
		$this->output(
			'<script>',
Scott committed
413 414
			"var b = document.getElementsByTagName('body')[0];",
			"b.className = b.className.replace('qa-body-js-off', 'qa-body-js-on');",
415 416 417
			'</script>'
		);
	}
Scott committed
418

419 420
	public function body_header()
	{
Scott committed
421
		if (isset($this->content['body_header'])) {
422
			$this->output_raw($this->content['body_header']);
Scott committed
423
		}
424
	}
Scott committed
425

426 427
	public function body_footer()
	{
Scott committed
428
		if (isset($this->content['body_footer'])) {
429
			$this->output_raw($this->content['body_footer']);
Scott committed
430
		}
431
	}
Scott committed
432

433 434 435 436
	public function body_content()
	{
		$this->body_prefix();
		$this->notices();
Scott committed
437

438
		$this->output('<div class="qa-body-wrapper">', '');
Scott committed
439

440 441 442 443 444 445 446 447
		$this->widgets('full', 'top');
		$this->header();
		$this->widgets('full', 'high');
		$this->sidepanel();
		$this->main();
		$this->widgets('full', 'low');
		$this->footer();
		$this->widgets('full', 'bottom');
Scott committed
448

449
		$this->output('</div> <!-- END body-wrapper -->');
Scott committed
450

451 452
		$this->body_suffix();
	}
Scott committed
453

454 455
	public function body_tags()
	{
Scott committed
456
		$class = 'qa-template-' . qa_html($this->template);
457
		$class .= empty($this->theme) ? '' : ' qa-theme-' . qa_html($this->theme);
Scott committed
458

459
		if (isset($this->content['categoryids'])) {
Scott committed
460 461 462
			foreach ($this->content['categoryids'] as $categoryid) {
				$class .= ' qa-category-' . qa_html($categoryid);
			}
Scott committed
463 464
		}

Scott committed
465
		$this->output('class="' . $class . ' qa-body-js-off"');
466
	}
Scott committed
467

468 469 470 471
	public function body_prefix()
	{
		// abstract method
	}
Scott committed
472

473 474 475 476
	public function body_suffix()
	{
		// abstract method
	}
Scott committed
477

478 479 480
	public function notices()
	{
		if (!empty($this->content['notices'])) {
Scott committed
481
			foreach ($this->content['notices'] as $notice) {
482
				$this->notice($notice);
Scott committed
483
			}
Scott committed
484
		}
485
	}
Scott committed
486

487 488
	public function notice($notice)
	{
Scott committed
489
		$this->output('<div class="qa-notice" id="' . $notice['id'] . '">');
Scott committed
490

491
		if (isset($notice['form_tags']))
Scott committed
492
			$this->output('<form ' . $notice['form_tags'] . '>');
Scott committed
493

494
		$this->output_raw($notice['content']);
Scott committed
495

Scott committed
496
		$this->output('<input ' . $notice['close_tags'] . ' type="submit" value="X" class="qa-notice-close-button"/> ');
Scott committed
497

498 499 500
		if (isset($notice['form_tags'])) {
			$this->form_hidden_elements(@$notice['form_hidden']);
			$this->output('</form>');
Scott committed
501 502
		}

503 504
		$this->output('</div>');
	}
Scott committed
505

506 507 508
	public function header()
	{
		$this->output('<div class="qa-header">');
Scott committed
509

510 511 512 513
		$this->logo();
		$this->nav_user_search();
		$this->nav_main_sub();
		$this->header_clear();
Scott committed
514

515 516
		$this->output('</div> <!-- END qa-header -->', '');
	}
Scott committed
517

518 519 520 521 522
	public function nav_user_search()
	{
		$this->nav('user');
		$this->search();
	}
Scott committed
523

524 525 526 527 528
	public function nav_main_sub()
	{
		$this->nav('main');
		$this->nav('sub');
	}
Scott committed
529

530 531 532 533 534 535 536 537
	public function logo()
	{
		$this->output(
			'<div class="qa-logo">',
			$this->content['logo'],
			'</div>'
		);
	}
Scott committed
538

539 540 541
	public function search()
	{
		$search = $this->content['search'];
Scott committed
542

543 544
		$this->output(
			'<div class="qa-search">',
Scott committed
545
			'<form ' . $search['form_tags'] . '>',
546 547
			@$search['form_extra']
		);
Scott committed
548

549 550
		$this->search_field($search);
		$this->search_button($search);
Scott committed
551

552 553 554 555 556
		$this->output(
			'</form>',
			'</div>'
		);
	}
Scott committed
557

558 559
	public function search_field($search)
	{
Scott committed
560
		$this->output('<input type="text" ' . $search['field_tags'] . ' value="' . @$search['value'] . '" class="qa-search-field"/>');
561
	}
Scott committed
562

563 564
	public function search_button($search)
	{
Scott committed
565
		$this->output('<input type="submit" value="' . $search['button_label'] . '" class="qa-search-button"/>');
566
	}
Scott committed
567

Scott committed
568
	public function nav($navtype, $level = null)
569 570
	{
		$navigation = @$this->content['navigation'][$navtype];
Scott committed
571

572
		if ($navtype == 'user' || isset($navigation)) {
Scott committed
573
			$this->output('<div class="qa-nav-' . $navtype . '">');
Scott committed
574

575 576 577 578 579 580 581 582 583
			if ($navtype == 'user')
				$this->logged_in();

			// reverse order of 'opposite' items since they float right
			foreach (array_reverse($navigation, true) as $key => $navlink) {
				if (@$navlink['opposite']) {
					unset($navigation[$key]);
					$navigation[$key] = $navlink;
				}
Scott committed
584 585
			}

586
			$this->set_context('nav_type', $navtype);
Scott committed
587
			$this->nav_list($navigation, 'nav-' . $navtype, $level);
588 589 590
			$this->nav_clear($navtype);
			$this->clear_context('nav_type');

Scott committed
591 592
			$this->output('</div>');
		}
593
	}
Scott committed
594

Scott committed
595
	public function nav_list($navigation, $class, $level = null)
596
	{
Scott committed
597
		$this->output('<ul class="qa-' . $class . '-list' . (isset($level) ? (' qa-' . $class . '-list-' . $level) : '') . '">');
Scott committed
598

599
		$index = 0;
Scott committed
600

601 602 603 604
		foreach ($navigation as $key => $navlink) {
			$this->set_context('nav_key', $key);
			$this->set_context('nav_index', $index++);
			$this->nav_item($key, $navlink, $class, $level);
Scott committed
605 606
		}

607 608
		$this->clear_context('nav_key');
		$this->clear_context('nav_index');
Scott committed
609

610 611
		$this->output('</ul>');
	}
Scott committed
612

613 614 615
	public function nav_clear($navtype)
	{
		$this->output(
Scott committed
616
			'<div class="qa-nav-' . $navtype . '-clear">',
617 618 619
			'</div>'
		);
	}
Scott committed
620

Scott committed
621
	public function nav_item($key, $navlink, $class, $level = null)
622 623 624 625 626
	{
		$suffix = strtr($key, array( // map special character in navigation key
			'$' => '',
			'/' => '-',
		));
Scott committed
627

Scott committed
628 629
		$this->output('<li class="qa-' . $class . '-item' . (@$navlink['opposite'] ? '-opp' : '') .
			(@$navlink['state'] ? (' qa-' . $class . '-' . $navlink['state']) : '') . ' qa-' . $class . '-' . $suffix . '">');
630 631
		$this->nav_link($navlink, $class);

Scott committed
632 633 634 635
		$subnav = isset($navlink['subnav']) ? $navlink['subnav'] : array();
		if (is_array($subnav) && count($subnav) > 0) {
			$this->nav_list($subnav, $class, 1 + $level);
		}
Scott committed
636

637 638
		$this->output('</li>');
	}
Scott committed
639

640 641 642
	public function nav_link($navlink, $class)
	{
		if (isset($navlink['url'])) {
Scott committed
643
			$this->output(
Scott committed
644 645 646 647 648
				'<a href="' . $navlink['url'] . '" class="qa-' . $class . '-link' .
				(@$navlink['selected'] ? (' qa-' . $class . '-selected') : '') .
				(@$navlink['favorited'] ? (' qa-' . $class . '-favorited') : '') .
				'"' . (strlen(@$navlink['popup']) ? (' title="' . $navlink['popup'] . '"') : '') .
				(isset($navlink['target']) ? (' target="' . $navlink['target'] . '"') : '') . '>' . $navlink['label'] .
649
				'</a>'
Scott committed
650
			);
Scott committed
651
		} else {
652
			$this->output(
Scott committed
653 654 655 656
				'<span class="qa-' . $class . '-nolink' . (@$navlink['selected'] ? (' qa-' . $class . '-selected') : '') .
				(@$navlink['favorited'] ? (' qa-' . $class . '-favorited') : '') . '"' .
				(strlen(@$navlink['popup']) ? (' title="' . $navlink['popup'] . '"') : '') .
				'>' . $navlink['label'] . '</span>'
657
			);
Scott committed
658 659
		}

660
		if (strlen(@$navlink['note']))
Scott committed
661
			$this->output('<span class="qa-' . $class . '-note">' . $navlink['note'] . '</span>');
662
	}
Scott committed
663

664 665 666 667
	public function logged_in()
	{
		$this->output_split(@$this->content['loggedin'], 'qa-logged-in', 'div');
	}
Scott committed
668

669 670 671 672 673 674 675
	public function header_clear()
	{
		$this->output(
			'<div class="qa-header-clear">',
			'</div>'
		);
	}
Scott committed
676

677 678 679 680 681 682 683 684 685 686 687 688
	public function sidepanel()
	{
		$this->output('<div class="qa-sidepanel">');
		$this->widgets('side', 'top');
		$this->sidebar();
		$this->widgets('side', 'high');
		$this->widgets('side', 'low');
		$this->output_raw(@$this->content['sidepanel']);
		$this->feed();
		$this->widgets('side', 'bottom');
		$this->output('</div>', '');
	}
Scott committed
689

690 691 692
	public function sidebar()
	{
		$sidebar = @$this->content['sidebar'];
Scott committed
693

694 695 696 697 698 699
		if (!empty($sidebar)) {
			$this->output('<div class="qa-sidebar">');
			$this->output_raw($sidebar);
			$this->output('</div>', '');
		}
	}
Scott committed
700

701 702 703 704 705 706
	public function feed()
	{
		$feed = @$this->content['feed'];

		if (!empty($feed)) {
			$this->output('<div class="qa-feed">');
Scott committed
707
			$this->output('<a href="' . $feed['url'] . '" class="qa-feed-link">' . @$feed['label'] . '</a>');
708
			$this->output('</div>');
Scott committed
709
		}
710
	}
Scott committed
711

712 713 714
	public function main()
	{
		$content = $this->content;
715
		$hidden = !empty($content['hidden']) ? ' qa-main-hidden' : '';
Scott committed
716
		$extratags = isset($this->content['main_tags']) ? $this->content['main_tags'] : '';
Scott committed
717

Scott committed
718
		$this->output('<div class="qa-main' . $hidden . '"' . $extratags . '>');
Scott committed
719

720
		$this->widgets('main', 'top');
Scott committed
721

722
		$this->page_title_error();
Scott committed
723

724
		$this->widgets('main', 'high');
Scott committed
725

726
		$this->main_parts($content);
Scott committed
727

728
		$this->widgets('main', 'low');
Scott committed
729

730 731
		$this->page_links();
		$this->suggest_next();
Scott committed
732

733
		$this->widgets('main', 'bottom');
Scott committed
734

735 736
		$this->output('</div> <!-- END qa-main -->', '');
	}
Scott committed
737

738 739 740 741 742 743 744
	public function page_title_error()
	{
		if (isset($this->content['title'])) {
			$favorite = isset($this->content['favorite']) ? $this->content['favorite'] : null;

			if (isset($favorite))
				$this->output('<form ' . $favorite['form_tags'] . '>');
Scott committed
745

746
			$this->output('<div class="qa-main-heading">');
747
			$this->favorite();
748
			$this->output('<h1>');
749 750
			$this->title();
			$this->output('</h1>');
751
			$this->output('</div>');
752 753 754 755 756 757

			if (isset($favorite)) {
				$formhidden = isset($favorite['form_hidden']) ? $favorite['form_hidden'] : null;
				$this->form_hidden_elements($formhidden);
				$this->output('</form>');
			}
Scott committed
758
		}
759 760 761

		if (isset($this->content['success']))
			$this->success($this->content['success']);
762 763 764
		if (isset($this->content['error']))
			$this->error($this->content['error']);
	}
Scott committed
765

766 767 768 769 770 771 772 773
	public function favorite()
	{
		$favorite = isset($this->content['favorite']) ? $this->content['favorite'] : null;
		if (isset($favorite)) {
			$favoritetags = isset($favorite['favorite_tags']) ? $favorite['favorite_tags'] : '';
			$this->output('<span class="qa-favoriting" ' . $favoritetags . '>');
			$this->favorite_inner_html($favorite);
			$this->output('</span>');
Scott committed
774
		}
775
	}
Scott committed
776

777 778 779 780 781 782 783 784
	public function title()
	{
		$q_view = @$this->content['q_view'];

		// link title where appropriate
		$url = isset($q_view['url']) ? $q_view['url'] : false;

		if (isset($this->content['title'])) {
Scott committed
785
			$this->output(
Scott committed
786
				$url ? '<a href="' . $url . '">' : '',
787 788
				$this->content['title'],
				$url ? '</a>' : ''
Scott committed
789 790 791
			);
		}

792
		// add closed note in title
793
		if (!empty($q_view['closed']['state']))
Scott committed
794
			$this->output(' [' . $q_view['closed']['state'] . ']');
795
	}
Scott committed
796

797 798 799 800 801
	public function favorite_inner_html($favorite)
	{
		$this->favorite_button(@$favorite['favorite_add_tags'], 'qa-favorite');
		$this->favorite_button(@$favorite['favorite_remove_tags'], 'qa-unfavorite');
	}
Scott committed
802

803 804 805
	public function favorite_button($tags, $class)
	{
		if (isset($tags))
Scott committed
806
			$this->output('<input ' . $tags . ' type="submit" value="" class="' . $class . '-button"/> ');
807
	}
Scott committed
808

809 810 811 812 813 814 815 816 817 818
	public function error($error)
	{
		if (strlen($error)) {
			$this->output(
				'<div class="qa-error">',
				$error,
				'</div>'
			);
		}
	}
Scott committed
819

820 821 822 823 824 825 826 827 828 829 830
	public function success($message)
	{
		if (strlen($message)) {
			$this->output(
				'<div class="qa-success">',
				$message,
				'</div>'
			);
		}
	}

831 832 833 834 835
	public function main_parts($content)
	{
		foreach ($content as $key => $part) {
			$this->set_context('part', $key);
			$this->main_part($key, $part);
Scott committed
836 837
		}

838 839
		$this->clear_context('part');
	}
Scott committed
840

841 842
	public function main_part($key, $part)
	{
Scott committed
843 844
		$isRanking = strpos($key, 'ranking') === 0;

845 846 847 848
		$partdiv = (
			strpos($key, 'custom') === 0 ||
			strpos($key, 'form') === 0 ||
			strpos($key, 'q_list') === 0 ||
Scott committed
849
			(strpos($key, 'q_view') === 0 && !isset($this->content['form_q_edit'])) ||
850 851
			strpos($key, 'a_form') === 0 ||
			strpos($key, 'a_list') === 0 ||
Scott committed
852
			$isRanking ||
853 854 855
			strpos($key, 'message_list') === 0 ||
			strpos($key, 'nav_list') === 0
		);
Scott committed
856

Scott committed
857 858 859 860 861
		if ($partdiv) {
			$class = 'qa-part-' . strtr($key, '_', '-');
			if ($isRanking)
				$class .= ' qa-ranking-' . $part['type'] . '-' . (isset($part['sort']) ? $part['sort'] : 'points');
			// help target CSS to page parts
Scott committed
862
			$this->output('<div class="' . $class . '">');
Scott committed
863
		}
Scott committed
864

865 866
		if (strpos($key, 'custom') === 0)
			$this->output_raw($part);
Scott committed
867

868 869
		elseif (strpos($key, 'form') === 0)
			$this->form($part);
Scott committed
870

871 872
		elseif (strpos($key, 'q_list') === 0)
			$this->q_list_and_form($part);
Scott committed
873

874 875
		elseif (strpos($key, 'q_view') === 0)
			$this->q_view($part);
Scott committed
876

877 878
		elseif (strpos($key, 'a_form') === 0)
			$this->a_form($part);
Scott committed
879

880 881
		elseif (strpos($key, 'a_list') === 0)
			$this->a_list($part);
Scott committed
882

883 884
		elseif (strpos($key, 'ranking') === 0)
			$this->ranking($part);
Scott committed
885

886 887
		elseif (strpos($key, 'message_list') === 0)
			$this->message_list_and_form($part);
Scott committed
888

889 890 891 892
		elseif (strpos($key, 'nav_list') === 0) {
			$this->part_title($part);
			$this->nav_list($part['nav'], $part['type'], 1);
		}
Scott committed
893

894 895 896
		if ($partdiv)
			$this->output('</div>');
	}
Scott committed
897

898 899 900
	public function footer()
	{
		$this->output('<div class="qa-footer">');
Scott committed
901

902 903 904
		$this->nav('footer');
		$this->attribution();
		$this->footer_clear();
Scott committed
905

906 907
		$this->output('</div> <!-- END qa-footer -->', '');
	}
Scott committed
908

909 910 911
	public function attribution()
	{
		// Hi there. I'd really appreciate you displaying this link on your Q2A site. Thank you - Gideon
Scott committed
912

913 914 915 916 917 918
		$this->output(
			'<div class="qa-attribution">',
			'Powered by <a href="http://www.question2answer.org/">Question2Answer</a>',
			'</div>'
		);
	}
Scott committed
919

920 921 922 923 924 925 926
	public function footer_clear()
	{
		$this->output(
			'<div class="qa-footer-clear">',
			'</div>'
		);
	}
Scott committed
927

928 929 930 931
	public function section($title)
	{
		$this->part_title(array('title' => $title));
	}
Scott committed
932

933 934 935
	public function part_title($part)
	{
		if (strlen(@$part['title']) || strlen(@$part['title_tags']))
Scott committed
936
			$this->output('<h2' . rtrim(' ' . @$part['title_tags']) . '>' . @$part['title'] . '</h2>');
937
	}
Scott committed
938

939 940 941 942 943
	public function part_footer($part)
	{
		if (isset($part['footer']))
			$this->output($part['footer']);
	}
Scott committed
944

945 946 947 948
	public function form($form)
	{
		if (!empty($form)) {
			$this->part_title($form);
Scott committed
949

950
			if (isset($form['tags']))
Scott committed
951
				$this->output('<form ' . $form['tags'] . '>');
Scott committed
952

953
			$this->form_body($form);
Scott committed
954

955 956
			if (isset($form['tags']))
				$this->output('</form>');
Scott committed
957
		}
958
	}
Scott committed
959

960 961
	public function form_columns($form)
	{
Scott committed
962
		if (isset($form['ok']) || !empty($form['fields']))
Scott committed
963
			$columns = ($form['style'] == 'wide') ? 2 : 1;
964 965
		else
			$columns = 0;
Scott committed
966

967 968
		return $columns;
	}
Scott committed
969

970 971 972 973
	public function form_spacer($form, $columns)
	{
		$this->output(
			'<tr>',
Scott committed
974
			'<td colspan="' . $columns . '" class="qa-form-' . $form['style'] . '-spacer">',
975 976 977 978 979
			'&nbsp;',
			'</td>',
			'</tr>'
		);
	}
Scott committed
980

981 982 983 984
	public function form_body($form)
	{
		if (@$form['boxed'])
			$this->output('<div class="qa-form-table-boxed">');
Scott committed
985

986
		$columns = $this->form_columns($form);
Scott committed
987

988
		if ($columns)
Scott committed
989
			$this->output('<table class="qa-form-' . $form['style'] . '-table">');
Scott committed
990

991 992 993
		$this->form_ok($form, $columns);
		$this->form_fields($form, $columns);
		$this->form_buttons($form, $columns);
Scott committed
994

995 996
		if ($columns)
			$this->output('</table>');
Scott committed
997

998
		$this->form_hidden($form);
Scott committed
999

1000 1001 1002
		if (@$form['boxed'])
			$this->output('</div>');
	}
Scott committed
1003

1004 1005 1006
	public function form_ok($form, $columns)
	{
		if (!empty($form['ok'])) {
Scott committed
1007 1008
			$this->output(
				'<tr>',
Scott committed
1009
				'<td colspan="' . $columns . '" class="qa-form-' . $form['style'] . '-ok">',
1010
				$form['ok'],
Scott committed
1011 1012 1013 1014
				'</td>',
				'</tr>'
			);
		}
1015
	}
Scott committed
1016

Scott committed
1017 1018 1019
	/**
	 * Reorder the fields of $form according to the $keys array which contains the field keys in their new order. Call
	 * before any fields are output. See the docs for qa_array_reorder() in util/sort.php for the other parameters.
Scott committed
1020 1021 1022 1023
	 * @param $form
	 * @param $keys
	 * @param mixed $beforekey
	 * @param bool $reorderrelative
Scott committed
1024
	 */
Scott committed
1025
	public function form_reorder_fields(&$form, $keys, $beforekey = null, $reorderrelative = true)
1026
	{
Scott committed
1027
		require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
1028

1029 1030 1031
		if (is_array($form['fields']))
			qa_array_reorder($form['fields'], $keys, $beforekey, $reorderrelative);
	}
Scott committed
1032

1033 1034 1035 1036 1037
	public function form_fields($form, $columns)
	{
		if (!empty($form['fields'])) {
			foreach ($form['fields'] as $key => $field) {
				$this->set_context('field_key', $key);
Scott committed
1038

1039 1040 1041 1042
				if (@$field['type'] == 'blank')
					$this->form_spacer($form, $columns);
				else
					$this->form_field_rows($form, $columns, $field);
Scott committed
1043 1044
			}

1045
			$this->clear_context('field_key');
Scott committed
1046
		}
1047
	}
Scott committed
1048

1049 1050 1051
	public function form_field_rows($form, $columns, $field)
	{
		$style = $form['style'];
Scott committed
1052

1053 1054 1055
		if (isset($field['style'])) { // field has different style to most of form
			$style = $field['style'];
			$colspan = $columns;
Scott committed
1056
			$columns = ($style == 'wide') ? 2 : 1;
Scott committed
1057
		} else
1058
			$colspan = null;
Scott committed
1059

1060 1061 1062 1063
		$prefixed = (@$field['type'] == 'checkbox') && ($columns == 1) && !empty($field['label']);
		$suffixed = (@$field['type'] == 'select' || @$field['type'] == 'number') && $columns == 1 && !empty($field['label']) && !@$field['loose'];
		$skipdata = @$field['tight'];
		$tworows = ($columns == 1) && (!empty($field['label'])) && (!$skipdata) &&
Scott committed
1064
			((!($prefixed || $suffixed)) || (!empty($field['error'])) || (!empty($field['note'])));
Scott committed
1065

1066 1067
		if (isset($field['id'])) {
			if ($columns == 1)
Scott committed
1068
				$this->output('<tbody id="' . $field['id'] . '">', '<tr>');
Scott committed
1069
			else
Scott committed
1070 1071
				$this->output('<tr id="' . $field['id'] . '">');
		} else
1072
			$this->output('<tr>');
Scott committed
1073

1074 1075
		if ($columns > 1 || !empty($field['label']))
			$this->form_label($field, $style, $columns, $prefixed, $suffixed, $colspan);
Scott committed
1076

1077 1078 1079 1080 1081 1082
		if ($tworows) {
			$this->output(
				'</tr>',
				'<tr>'
			);
		}
Scott committed
1083

1084
		if (!$skipdata)
Scott committed
1085
			$this->form_data($field, $style, $columns, !($prefixed || $suffixed), $colspan);
Scott committed
1086

1087
		$this->output('</tr>');
Scott committed
1088

1089 1090 1091
		if ($columns == 1 && isset($field['id']))
			$this->output('</tbody>');
	}
Scott committed
1092

1093 1094 1095
	public function form_label($field, $style, $columns, $prefixed, $suffixed, $colspan)
	{
		$extratags = '';
Scott committed
1096

1097 1098
		if ($columns > 1 && (@$field['type'] == 'select-radio' || @$field['rows'] > 1))
			$extratags .= ' style="vertical-align:top;"';
Scott committed
1099

1100
		if (isset($colspan))
Scott committed
1101
			$extratags .= ' colspan="' . $colspan . '"';
Scott committed
1102

Scott committed
1103
		$this->output('<td class="qa-form-' . $style . '-label"' . $extratags . '>');
Scott committed
1104

1105 1106 1107 1108
		if ($prefixed) {
			$this->output('<label>');
			$this->form_field($field, $style);
		}
Scott committed
1109

1110
		$this->output(@$field['label']);
Scott committed
1111

1112 1113
		if ($prefixed)
			$this->output('</label>');
Scott committed
1114

1115 1116 1117
		if ($suffixed) {
			$this->output('&nbsp;');
			$this->form_field($field, $style);
Scott committed
1118 1119
		}

1120 1121
		$this->output('</td>');
	}
Scott committed
1122

1123 1124 1125 1126
	public function form_data($field, $style, $columns, $showfield, $colspan)
	{
		if ($showfield || (!empty($field['error'])) || (!empty($field['note']))) {
			$this->output(
Scott committed
1127
				'<td class="qa-form-' . $style . '-data"' . (isset($colspan) ? (' colspan="' . $colspan . '"') : '') . '>'
1128
			);
Scott committed
1129

1130 1131
			if ($showfield)
				$this->form_field($field, $style);
Scott committed
1132

1133 1134
			if (!empty($field['error'])) {
				if (@$field['note_force'])
Scott committed
1135 1136
					$this->form_note($field, $style, $columns);

1137
				$this->form_error($field, $style, $columns);
Scott committed
1138
			} elseif (!empty($field['note']))
1139
				$this->form_note($field, $style, $columns);
Scott committed
1140

1141 1142 1143
			$this->output('</td>');
		}
	}
Scott committed
1144

1145 1146 1147
	public function form_field($field, $style)
	{
		$this->form_prefix($field, $style);
Scott committed
1148

1149
		$this->output_raw(@$field['html_prefix']);
Scott committed
1150

1151 1152 1153 1154
		switch (@$field['type']) {
			case 'checkbox':
				$this->form_checkbox($field, $style);
				break;
Scott committed
1155

1156 1157 1158
			case 'static':
				$this->form_static($field, $style);
				break;
Scott committed
1159

1160 1161 1162
			case 'password':
				$this->form_password($field, $style);
				break;
Scott committed
1163

1164 1165 1166
			case 'number':
				$this->form_number($field, $style);
				break;
Scott committed
1167

1168 1169 1170 1171
			case 'file':
				$this->form_file($field, $style);
				break;

1172 1173 1174
			case 'select':
				$this->form_select($field, $style);
				break;
Scott committed
1175

1176 1177 1178
			case 'select-radio':
				$this->form_select_radio($field, $style);
				break;
Scott committed
1179

1180 1181 1182
			case 'image':
				$this->form_image($field, $style);
				break;
Scott committed
1183

1184 1185 1186
			case 'custom':
				$this->output_raw(@$field['html']);
				break;
Scott committed
1187

1188 1189 1190 1191 1192 1193
			default:
				if (@$field['type'] == 'textarea' || @$field['rows'] > 1)
					$this->form_text_multi_row($field, $style);
				else
					$this->form_text_single_row($field, $style);
				break;
Scott committed
1194 1195
		}

1196
		$this->output_raw(@$field['html_suffix']);
Scott committed
1197

1198 1199
		$this->form_suffix($field, $style);
	}
Scott committed
1200

Scott committed
1201 1202 1203
	/**
	 * Reorder the buttons of $form according to the $keys array which contains the button keys in their new order. Call
	 * before any buttons are output. See the docs for qa_array_reorder() in util/sort.php for the other parameters.
Scott committed
1204 1205 1206 1207
	 * @param $form
	 * @param $keys
	 * @param mixed $beforekey
	 * @param bool $reorderrelative
Scott committed
1208
	 */
Scott committed
1209
	public function form_reorder_buttons(&$form, $keys, $beforekey = null, $reorderrelative = true)
1210
	{
Scott committed
1211
		require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
1212

1213 1214 1215
		if (is_array($form['buttons']))
			qa_array_reorder($form['buttons'], $keys, $beforekey, $reorderrelative);
	}
Scott committed
1216

1217 1218 1219 1220 1221 1222 1223 1224
	public function form_buttons($form, $columns)
	{
		if (!empty($form['buttons'])) {
			$style = @$form['style'];

			if ($columns) {
				$this->output(
					'<tr>',
Scott committed
1225
					'<td colspan="' . $columns . '" class="qa-form-' . $style . '-buttons">'
1226 1227
				);
			}
Scott committed
1228

1229 1230
			foreach ($form['buttons'] as $key => $button) {
				$this->set_context('button_key', $key);
Scott committed
1231

1232 1233 1234 1235 1236
				if (empty($button))
					$this->form_button_spacer($style);
				else {
					$this->form_button_data($button, $key, $style);
					$this->form_button_note($button, $style);
Scott committed
1237 1238 1239
				}
			}

1240
			$this->clear_context('button_key');
Scott committed
1241

1242
			if ($columns) {
Scott committed
1243
				$this->output(
1244 1245
					'</td>',
					'</tr>'
Scott committed
1246 1247 1248
				);
			}
		}
1249
	}
Scott committed
1250

1251 1252
	public function form_button_data($button, $key, $style)
	{
Scott committed
1253
		$baseclass = 'qa-form-' . $style . '-button qa-form-' . $style . '-button-' . $key;
Scott committed
1254

Scott committed
1255 1256
		$this->output('<input' . rtrim(' ' . @$button['tags']) . ' value="' . @$button['label'] . '" title="' . @$button['popup'] . '" type="submit"' .
			(isset($style) ? (' class="' . $baseclass . '"') : '') . '/>');
1257
	}
Scott committed
1258

1259 1260 1261 1262
	public function form_button_note($button, $style)
	{
		if (!empty($button['note'])) {
			$this->output(
Scott committed
1263
				'<span class="qa-form-' . $style . '-note">',
1264 1265 1266 1267
				$button['note'],
				'</span>',
				'<br/>'
			);
Scott committed
1268
		}
1269
	}
Scott committed
1270

1271 1272
	public function form_button_spacer($style)
	{
Scott committed
1273
		$this->output('<span class="qa-form-' . $style . '-buttons-spacer">&nbsp;</span>');
1274
	}
Scott committed
1275

1276 1277 1278 1279
	public function form_hidden($form)
	{
		$this->form_hidden_elements(@$form['hidden']);
	}
Scott committed
1280

1281 1282
	public function form_hidden_elements($hidden)
	{
1283 1284 1285 1286 1287 1288
		if (empty($hidden))
			return;

		foreach ($hidden as $name => $value) {
			if (is_array($value)) {
				// new method of outputting tags
Scott committed
1289 1290
				$this->output('<input ' . @$value['tags'] . ' type="hidden" value="' . @$value['value'] . '"/>');
			} else {
1291
				// old method
Scott committed
1292
				$this->output('<input name="' . $name . '" type="hidden" value="' . $value . '"/>');
1293
			}
Scott committed
1294
		}
1295
	}
Scott committed
1296

1297 1298 1299
	public function form_prefix($field, $style)
	{
		if (!empty($field['prefix']))
Scott committed
1300
			$this->output('<span class="qa-form-' . $style . '-prefix">' . $field['prefix'] . '</span>');
1301
	}
Scott committed
1302

1303 1304 1305
	public function form_suffix($field, $style)
	{
		if (!empty($field['suffix']))
Scott committed
1306
			$this->output('<span class="qa-form-' . $style . '-suffix">' . $field['suffix'] . '</span>');
1307
	}
Scott committed
1308

1309 1310
	public function form_checkbox($field, $style)
	{
Scott committed
1311
		$this->output('<input ' . @$field['tags'] . ' type="checkbox" value="1"' . (@$field['value'] ? ' checked' : '') . ' class="qa-form-' . $style . '-checkbox"/>');
1312
	}
Scott committed
1313

1314 1315
	public function form_static($field, $style)
	{
Scott committed
1316
		$this->output('<span class="qa-form-' . $style . '-static">' . @$field['value'] . '</span>');
1317
	}
Scott committed
1318

1319 1320
	public function form_password($field, $style)
	{
Scott committed
1321
		$this->output('<input ' . @$field['tags'] . ' type="password" value="' . @$field['value'] . '" class="qa-form-' . $style . '-text"/>');
1322
	}
Scott committed
1323

1324 1325
	public function form_number($field, $style)
	{
Scott committed
1326
		$this->output('<input ' . @$field['tags'] . ' type="text" value="' . @$field['value'] . '" class="qa-form-' . $style . '-number"/>');
1327
	}
Scott committed
1328

1329 1330
	public function form_file($field, $style)
	{
Scott committed
1331
		$this->output('<input ' . @$field['tags'] . ' type="file" class="qa-form-' . $style . '-file"/>');
1332 1333
	}

1334 1335 1336 1337 1338 1339
	/**
	 * Output a <select> element. The $field array may contain the following keys:
	 *   options: (required) a key-value array containing all the options in the select.
	 *   tags: any attributes to be added to the select.
	 *   value: the selected value from the 'options' parameter.
	 *   match_by: whether to match the 'value' (default) or 'key' of each option to determine if it is to be selected.
Scott committed
1340 1341
	 * @param $field
	 * @param $style
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	 */
	public function form_select($field, $style)
	{
		$this->output('<select ' . (isset($field['tags']) ? $field['tags'] : '') . ' class="qa-form-' . $style . '-select">');

		// Only match by key if it is explicitly specified. Otherwise, for backwards compatibility, match by value
		$matchbykey = isset($field['match_by']) && $field['match_by'] === 'key';

		foreach ($field['options'] as $key => $value) {
			$selected = isset($field['value']) && (
				($matchbykey && $key === $field['value']) ||
				(!$matchbykey && $value === $field['value'])
			);
			$this->output('<option value="' . $key . '"' . ($selected ? ' selected' : '') . '>' . $value . '</option>');
Scott committed
1356 1357
		}

1358 1359
		$this->output('</select>');
	}
Scott committed
1360

1361 1362 1363
	public function form_select_radio($field, $style)
	{
		$radios = 0;
Scott committed
1364

1365 1366 1367
		foreach ($field['options'] as $tag => $value) {
			if ($radios++)
				$this->output('<br/>');
Scott committed
1368

Scott committed
1369
			$this->output('<input ' . @$field['tags'] . ' type="radio" value="' . $tag . '"' . (($value == @$field['value']) ? ' checked' : '') . ' class="qa-form-' . $style . '-radio"/> ' . $value);
Scott committed
1370
		}
1371
	}
Scott committed
1372

1373 1374
	public function form_image($field, $style)
	{
Scott committed
1375
		$this->output('<div class="qa-form-' . $style . '-image">' . @$field['html'] . '</div>');
1376
	}
Scott committed
1377

1378 1379
	public function form_text_single_row($field, $style)
	{
Scott committed
1380
		$this->output('<input ' . @$field['tags'] . ' type="text" value="' . @$field['value'] . '" class="qa-form-' . $style . '-text"/>');
1381
	}
Scott committed
1382

1383 1384
	public function form_text_multi_row($field, $style)
	{
Scott committed
1385
		$this->output('<textarea ' . @$field['tags'] . ' rows="' . (int)$field['rows'] . '" cols="40" class="qa-form-' . $style . '-text">' . @$field['value'] . '</textarea>');
1386
	}
Scott committed
1387

1388 1389 1390
	public function form_error($field, $style, $columns)
	{
		$tag = ($columns > 1) ? 'span' : 'div';
Scott committed
1391

Scott committed
1392
		$this->output('<' . $tag . ' class="qa-form-' . $style . '-error">' . $field['error'] . '</' . $tag . '>');
1393
	}
Scott committed
1394

1395 1396 1397
	public function form_note($field, $style, $columns)
	{
		$tag = ($columns > 1) ? 'span' : 'div';
Scott committed
1398

Scott committed
1399
		$this->output('<' . $tag . ' class="qa-form-' . $style . '-note">' . @$field['note'] . '</' . $tag . '>');
1400
	}
Scott committed
1401

1402 1403 1404
	public function ranking($ranking)
	{
		$this->part_title($ranking);
Scott committed
1405

1406 1407
		if (!isset($ranking['type']))
			$ranking['type'] = 'items';
Scott committed
1408
		$class = 'qa-top-' . $ranking['type'];
Scott committed
1409

1410 1411 1412
		if (!$this->ranking_block_layout) {
			// old, less semantic table layout
			$this->ranking_table($ranking, $class);
Scott committed
1413
		} else {
1414 1415
			// new block layout
			foreach ($ranking['items'] as $item) {
Scott committed
1416
				$this->output('<span class="qa-ranking-item ' . $class . '-item">');
1417 1418
				$this->ranking_item($item, $class);
				$this->output('</span>');
Scott committed
1419
			}
1420
		}
Scott committed
1421

1422 1423
		$this->part_footer($ranking);
	}
Scott committed
1424

Scott committed
1425
	public function ranking_item($item, $class, $spacer = false) // $spacer is deprecated
1426 1427 1428 1429 1430
	{
		if (!$this->ranking_block_layout) {
			// old table layout
			$this->ranking_table_item($item, $class, $spacer);
			return;
Scott committed
1431 1432
		}

1433 1434
		if (isset($item['count']))
			$this->ranking_count($item, $class);
Scott committed
1435

1436 1437
		if (isset($item['avatar']))
			$this->avatar($item, $class);
Scott committed
1438

1439
		$this->ranking_label($item, $class);
Scott committed
1440

1441 1442 1443
		if (isset($item['score']))
			$this->ranking_score($item, $class);
	}
Scott committed
1444

1445 1446
	public function ranking_cell($content, $class)
	{
Scott committed
1447 1448
		$tag = $this->ranking_block_layout ? 'span' : 'td';
		$this->output('<' . $tag . ' class="' . $class . '">' . $content . '</' . $tag . '>');
1449
	}
Scott committed
1450

1451 1452
	public function ranking_count($item, $class)
	{
Scott committed
1453
		$this->ranking_cell($item['count'] . ' &#215;', $class . '-count');
1454
	}
Scott committed
1455

1456 1457
	public function ranking_label($item, $class)
	{
Scott committed
1458
		$this->ranking_cell($item['label'], $class . '-label');
1459
	}
Scott committed
1460

1461 1462
	public function ranking_score($item, $class)
	{
Scott committed
1463
		$this->ranking_cell($item['score'], $class . '-score');
1464
	}
Scott committed
1465

1466 1467 1468 1469
	/**
	 * @deprecated Table-based layout of users/tags is deprecated from 1.7 onwards and may be
	 * removed in a future version. Themes can switch to the new layout by setting the member
	 * variable $ranking_block_layout to false.
Scott committed
1470 1471
	 * @param $ranking
	 * @param $class
1472 1473 1474 1475 1476 1477
	 */
	public function ranking_table($ranking, $class)
	{
		$rows = min($ranking['rows'], count($ranking['items']));

		if ($rows > 0) {
Scott committed
1478
			$this->output('<table class="' . $class . '-table">');
1479 1480 1481 1482 1483 1484 1485 1486
			$columns = ceil(count($ranking['items']) / $rows);

			for ($row = 0; $row < $rows; $row++) {
				$this->set_context('ranking_row', $row);
				$this->output('<tr>');

				for ($column = 0; $column < $columns; $column++) {
					$this->set_context('ranking_column', $column);
Scott committed
1487
					$this->ranking_table_item(@$ranking['items'][$column * $rows + $row], $class, $column > 0);
Scott committed
1488
				}
1489 1490 1491

				$this->clear_context('ranking_column');
				$this->output('</tr>');
Scott committed
1492
			}
1493 1494
			$this->clear_context('ranking_row');
			$this->output('</table>');
Scott committed
1495
		}
1496
	}
Scott committed
1497

1498 1499
	/**
	 * @deprecated See ranking_table above.
Scott committed
1500 1501 1502
	 * @param $item
	 * @param $class
	 * @param $spacer
1503 1504 1505 1506 1507
	 */
	public function ranking_table_item($item, $class, $spacer)
	{
		if ($spacer)
			$this->ranking_spacer($class);
Scott committed
1508

1509 1510 1511
		if (empty($item)) {
			$this->ranking_spacer($class);
			$this->ranking_spacer($class);
Scott committed
1512

1513 1514 1515
		} else {
			if (isset($item['count']))
				$this->ranking_count($item, $class);
Scott committed
1516

1517
			if (isset($item['avatar']))
Scott committed
1518
				$item['label'] = $item['avatar'] . ' ' . $item['label'];
Scott committed
1519

1520
			$this->ranking_label($item, $class);
Scott committed
1521

1522 1523
			if (isset($item['score']))
				$this->ranking_score($item, $class);
Scott committed
1524
		}
1525
	}
Scott committed
1526

1527 1528
	/**
	 * @deprecated See ranking_table above.
Scott committed
1529
	 * @param $class
1530 1531 1532
	 */
	public function ranking_spacer($class)
	{
Scott committed
1533
		$this->output('<td class="' . $class . '-spacer">&nbsp;</td>');
1534
	}
Scott committed
1535 1536


1537 1538 1539 1540
	public function message_list_and_form($list)
	{
		if (!empty($list)) {
			$this->part_title($list);
Scott committed
1541

1542
			$this->error(@$list['error']);
Scott committed
1543

1544
			if (!empty($list['form'])) {
Scott committed
1545
				$this->output('<form ' . $list['form']['tags'] . '>');
1546 1547 1548
				unset($list['form']['tags']); // we already output the tags before the messages
				$this->message_list_form($list);
			}
Scott committed
1549

1550
			$this->message_list($list);
Scott committed
1551

1552 1553
			if (!empty($list['form'])) {
				$this->output('</form>');
Scott committed
1554 1555
			}
		}
1556
	}
Scott committed
1557

1558 1559 1560 1561 1562 1563
	public function message_list_form($list)
	{
		if (!empty($list['form'])) {
			$this->output('<div class="qa-message-list-form">');
			$this->form($list['form']);
			$this->output('</div>');
Scott committed
1564
		}
1565
	}
Scott committed
1566

1567 1568 1569
	public function message_list($list)
	{
		if (isset($list['messages'])) {
Scott committed
1570
			$this->output('<div class="qa-message-list" ' . @$list['tags'] . '>');
Scott committed
1571

Scott committed
1572
			foreach ($list['messages'] as $message) {
1573
				$this->message_item($message);
Scott committed
1574
			}
Scott committed
1575

1576
			$this->output('</div> <!-- END qa-message-list -->', '');
Scott committed
1577
		}
1578
	}
Scott committed
1579

1580 1581
	public function message_item($message)
	{
Scott committed
1582
		$this->output('<div class="qa-message-item" ' . @$message['tags'] . '>');
1583 1584 1585 1586 1587
		$this->message_content($message);
		$this->post_avatar_meta($message, 'qa-message');
		$this->message_buttons($message);
		$this->output('</div> <!-- END qa-message-item -->', '');
	}
Scott committed
1588

1589 1590 1591 1592 1593 1594
	public function message_content($message)
	{
		if (!empty($message['content'])) {
			$this->output('<div class="qa-message-content">');
			$this->output_raw($message['content']);
			$this->output('</div>');
Scott committed
1595
		}
1596
	}
Scott committed
1597

1598 1599 1600 1601 1602 1603
	public function message_buttons($item)
	{
		if (!empty($item['form'])) {
			$this->output('<div class="qa-message-buttons">');
			$this->form($item['form']);
			$this->output('</div>');
Scott committed
1604
		}
1605
	}
Scott committed
1606

1607 1608 1609
	public function list_vote_disabled($items)
	{
		$disabled = false;
Scott committed
1610

1611 1612
		if (count($items)) {
			$disabled = true;
Scott committed
1613

1614 1615 1616
			foreach ($items as $item) {
				if (@$item['vote_on_page'] != 'disabled')
					$disabled = false;
Scott committed
1617 1618 1619
			}
		}

1620 1621
		return $disabled;
	}
Scott committed
1622

1623 1624 1625 1626
	public function q_list_and_form($q_list)
	{
		if (empty($q_list))
			return;
Scott committed
1627

1628
		$this->part_title($q_list);
Scott committed
1629

1630
		if (!empty($q_list['form']))
Scott committed
1631
			$this->output('<form ' . $q_list['form']['tags'] . '>');
Scott committed
1632

1633
		$this->q_list($q_list);
Scott committed
1634

1635 1636 1637 1638
		if (!empty($q_list['form'])) {
			unset($q_list['form']['tags']); // we already output the tags before the qs
			$this->q_list_form($q_list);
			$this->output('</form>');
Scott committed
1639 1640
		}

1641 1642
		$this->part_footer($q_list);
	}
Scott committed
1643

1644 1645 1646 1647 1648 1649
	public function q_list_form($q_list)
	{
		if (!empty($q_list['form'])) {
			$this->output('<div class="qa-q-list-form">');
			$this->form($q_list['form']);
			$this->output('</div>');
Scott committed
1650
		}
1651
	}
Scott committed
1652

1653 1654 1655
	public function q_list($q_list)
	{
		if (isset($q_list['qs'])) {
Scott committed
1656
			$this->output('<div class="qa-q-list' . ($this->list_vote_disabled($q_list['qs']) ? ' qa-q-list-vote-disabled' : '') . '">', '');
1657 1658
			$this->q_list_items($q_list['qs']);
			$this->output('</div> <!-- END qa-q-list -->', '');
Scott committed
1659
		}
1660
	}
Scott committed
1661

1662 1663
	public function q_list_items($q_items)
	{
Scott committed
1664
		foreach ($q_items as $q_item) {
1665
			$this->q_list_item($q_item);
Scott committed
1666
		}
1667
	}
Scott committed
1668

1669 1670
	public function q_list_item($q_item)
	{
Scott committed
1671
		$this->output('<div class="qa-q-list-item' . rtrim(' ' . @$q_item['classes']) . '" ' . @$q_item['tags'] . '>');
Scott committed
1672

1673 1674 1675
		$this->q_item_stats($q_item);
		$this->q_item_main($q_item);
		$this->q_item_clear();
Scott committed
1676

1677 1678
		$this->output('</div> <!-- END qa-q-list-item -->', '');
	}
Scott committed
1679

1680 1681 1682
	public function q_item_stats($q_item)
	{
		$this->output('<div class="qa-q-item-stats">');
Scott committed
1683

1684 1685
		$this->voting($q_item);
		$this->a_count($q_item);
Scott committed
1686

1687 1688 1689 1690 1691 1692
		$this->output('</div>');
	}

	public function q_item_main($q_item)
	{
		$this->output('<div class="qa-q-item-main">');
Scott committed
1693

1694 1695 1696
		$this->view_count($q_item);
		$this->q_item_title($q_item);
		$this->q_item_content($q_item);
Scott committed
1697

1698 1699 1700
		$this->post_avatar_meta($q_item, 'qa-q-item');
		$this->post_tags($q_item, 'qa-q-item');
		$this->q_item_buttons($q_item);
Scott committed
1701

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
		$this->output('</div>');
	}

	public function q_item_clear()
	{
		$this->output(
			'<div class="qa-q-item-clear">',
			'</div>'
		);
	}

	public function q_item_title($q_item)
	{
		$this->output(
			'<div class="qa-q-item-title">',
Scott committed
1717
			'<a href="' . $q_item['url'] . '">' . $q_item['title'] . '</a>',
1718
			// add closed note in title
Scott committed
1719
			empty($q_item['closed']['state']) ? '' : ' [' . $q_item['closed']['state'] . ']',
1720 1721 1722 1723 1724 1725 1726 1727 1728
			'</div>'
		);
	}

	public function q_item_content($q_item)
	{
		if (!empty($q_item['content'])) {
			$this->output('<div class="qa-q-item-content">');
			$this->output_raw($q_item['content']);
Scott committed
1729 1730
			$this->output('</div>');
		}
1731
	}
Scott committed
1732

1733 1734 1735 1736 1737 1738
	public function q_item_buttons($q_item)
	{
		if (!empty($q_item['form'])) {
			$this->output('<div class="qa-q-item-buttons">');
			$this->form($q_item['form']);
			$this->output('</div>');
Scott committed
1739
		}
1740
	}
Scott committed
1741

1742 1743 1744
	public function voting($post)
	{
		if (isset($post['vote_view'])) {
Scott committed
1745
			$this->output('<div class="qa-voting ' . (($post['vote_view'] == 'updown') ? 'qa-voting-updown' : 'qa-voting-net') . '" ' . @$post['vote_tags'] . '>');
1746 1747
			$this->voting_inner_html($post);
			$this->output('</div>');
Scott committed
1748
		}
1749
	}
Scott committed
1750

1751 1752 1753 1754 1755 1756
	public function voting_inner_html($post)
	{
		$this->vote_buttons($post);
		$this->vote_count($post);
		$this->vote_clear();
	}
Scott committed
1757

1758 1759
	public function vote_buttons($post)
	{
Scott committed
1760
		$this->output('<div class="qa-vote-buttons ' . (($post['vote_view'] == 'updown') ? 'qa-vote-buttons-updown' : 'qa-vote-buttons-net') . '">');
Scott committed
1761

Scott committed
1762
		switch (@$post['vote_state']) {
1763 1764 1765
			case 'voted_up':
				$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-one-button qa-voted-up');
				break;
Scott committed
1766

1767 1768 1769
			case 'voted_up_disabled':
				$this->post_disabled_button($post, 'vote_up_tags', '+', 'qa-vote-one-button qa-vote-up');
				break;
Scott committed
1770

1771 1772 1773
			case 'voted_down':
				$this->post_hover_button($post, 'vote_down_tags', '&ndash;', 'qa-vote-one-button qa-voted-down');
				break;
Scott committed
1774

1775 1776 1777
			case 'voted_down_disabled':
				$this->post_disabled_button($post, 'vote_down_tags', '&ndash;', 'qa-vote-one-button qa-vote-down');
				break;
Scott committed
1778

1779 1780 1781 1782
			case 'up_only':
				$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-first-button qa-vote-up');
				$this->post_disabled_button($post, 'vote_down_tags', '', 'qa-vote-second-button qa-vote-down');
				break;
Scott committed
1783

1784 1785 1786 1787
			case 'enabled':
				$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-first-button qa-vote-up');
				$this->post_hover_button($post, 'vote_down_tags', '&ndash;', 'qa-vote-second-button qa-vote-down');
				break;
Scott committed
1788

1789 1790 1791 1792 1793
			default:
				$this->post_disabled_button($post, 'vote_up_tags', '', 'qa-vote-first-button qa-vote-up');
				$this->post_disabled_button($post, 'vote_down_tags', '', 'qa-vote-second-button qa-vote-down');
				break;
		}
Scott committed
1794

1795 1796
		$this->output('</div>');
	}
Scott committed
1797

1798 1799 1800 1801
	public function vote_count($post)
	{
		// You can also use $post['upvotes_raw'], $post['downvotes_raw'], $post['netvotes_raw'] to get
		// raw integer vote counts, for graphing or showing in other non-textual ways
Scott committed
1802

Scott committed
1803
		$this->output('<div class="qa-vote-count ' . (($post['vote_view'] == 'updown') ? 'qa-vote-count-updown' : 'qa-vote-count-net') . '"' . @$post['vote_count_tags'] . '>');
1804 1805 1806 1807

		if ($post['vote_view'] == 'updown') {
			$this->output_split($post['upvotes_view'], 'qa-upvote-count');
			$this->output_split($post['downvotes_view'], 'qa-downvote-count');
1808
		} else {
1809
			$this->output_split($post['netvotes_view'], 'qa-netvote-count');
1810
		}
Scott committed
1811

1812 1813
		$this->output('</div>');
	}
Scott committed
1814

1815 1816 1817 1818 1819 1820 1821
	public function vote_clear()
	{
		$this->output(
			'<div class="qa-vote-clear">',
			'</div>'
		);
	}
Scott committed
1822

1823 1824 1825
	public function a_count($post)
	{
		// You can also use $post['answers_raw'] to get a raw integer count of answers
Scott committed
1826

1827 1828 1829
		$this->output_split(@$post['answers'], 'qa-a-count', 'span', 'span',
			@$post['answer_selected'] ? 'qa-a-count-selected' : (@$post['answers_raw'] ? null : 'qa-a-count-zero'));
	}
Scott committed
1830

1831 1832 1833 1834 1835 1836 1837
	public function view_count($post)
	{
		// You can also use $post['views_raw'] to get a raw integer count of views

		$this->output_split(@$post['views'], 'qa-view-count');
	}

Scott committed
1838
	public function avatar($item, $class, $prefix = null)
1839 1840 1841 1842
	{
		if (isset($item['avatar'])) {
			if (isset($prefix))
				$this->output($prefix);
Scott committed
1843 1844

			$this->output(
Scott committed
1845
				'<span class="' . $class . '-avatar">',
1846 1847
				$item['avatar'],
				'</span>'
Scott committed
1848 1849
			);
		}
1850
	}
Scott committed
1851

1852 1853 1854
	public function a_selection($post)
	{
		$this->output('<div class="qa-a-selection">');
Scott committed
1855

1856 1857 1858 1859 1860 1861
		if (isset($post['select_tags']))
			$this->post_hover_button($post, 'select_tags', '', 'qa-a-select');
		elseif (isset($post['unselect_tags']))
			$this->post_hover_button($post, 'unselect_tags', '', 'qa-a-unselect');
		elseif ($post['selected'])
			$this->output('<div class="qa-a-selected">&nbsp;</div>');
Scott committed
1862

1863
		if (isset($post['select_text']))
Scott committed
1864
			$this->output('<div class="qa-a-selected-text">' . @$post['select_text'] . '</div>');
Scott committed
1865

1866 1867
		$this->output('</div>');
	}
Scott committed
1868

1869 1870 1871
	public function post_hover_button($post, $element, $value, $class)
	{
		if (isset($post[$element]))
Scott committed
1872
			$this->output('<input ' . $post[$element] . ' type="submit" value="' . $value . '" class="' . $class . '-button"/> ');
1873
	}
Scott committed
1874

1875 1876 1877
	public function post_disabled_button($post, $element, $value, $class)
	{
		if (isset($post[$element]))
Scott committed
1878
			$this->output('<input ' . $post[$element] . ' type="submit" value="' . $value . '" class="' . $class . '-disabled" disabled="disabled"/> ');
1879
	}
Scott committed
1880

Scott committed
1881
	public function post_avatar_meta($post, $class, $avatarprefix = null, $metaprefix = null, $metaseparator = '<br/>')
1882
	{
Scott committed
1883
		$this->output('<span class="' . $class . '-avatar-meta">');
1884 1885 1886 1887 1888 1889 1890
		$this->avatar($post, $class, $avatarprefix);
		$this->post_meta($post, $class, $metaprefix, $metaseparator);
		$this->output('</span>');
	}

	/**
	 * @deprecated Deprecated from 1.7; please use avatar() instead.
Scott committed
1891 1892 1893
	 * @param $post
	 * @param $class
	 * @param string $prefix
1894
	 */
Scott committed
1895
	public function post_avatar($post, $class, $prefix = null)
1896 1897 1898
	{
		$this->avatar($post, $class, $prefix);
	}
Scott committed
1899

Scott committed
1900
	public function post_meta($post, $class, $prefix = null, $separator = '<br/>')
1901
	{
Scott committed
1902
		$this->output('<span class="' . $class . '-meta">');
Scott committed
1903

1904 1905
		if (isset($prefix))
			$this->output($prefix);
Scott committed
1906

1907
		$order = explode('^', @$post['meta_order']);
Scott committed
1908

1909 1910 1911 1912 1913
		foreach ($order as $element) {
			switch ($element) {
				case 'what':
					$this->post_meta_what($post, $class);
					break;
Scott committed
1914

1915 1916 1917
				case 'when':
					$this->post_meta_when($post, $class);
					break;
Scott committed
1918

1919 1920 1921
				case 'where':
					$this->post_meta_where($post, $class);
					break;
Scott committed
1922

1923 1924 1925 1926
				case 'who':
					$this->post_meta_who($post, $class);
					break;
			}
Scott committed
1927 1928
		}

1929
		$this->post_meta_flags($post, $class);
Scott committed
1930

1931 1932
		if (!empty($post['what_2'])) {
			$this->output($separator);
Scott committed
1933 1934 1935 1936

			foreach ($order as $element) {
				switch ($element) {
					case 'what':
Scott committed
1937
						$this->output('<span class="' . $class . '-what">' . $post['what_2'] . '</span>');
Scott committed
1938 1939 1940
						break;

					case 'when':
Scott committed
1941
						$this->output_split(@$post['when_2'], $class . '-when');
Scott committed
1942 1943 1944
						break;

					case 'who':
Scott committed
1945
						$this->output_split(@$post['who_2'], $class . '-who');
Scott committed
1946 1947 1948 1949 1950
						break;
				}
			}
		}

1951 1952
		$this->output('</span>');
	}
Scott committed
1953

1954 1955 1956
	public function post_meta_what($post, $class)
	{
		if (isset($post['what'])) {
Scott committed
1957
			$classes = $class . '-what';
1958
			if (@$post['what_your'])
Scott committed
1959
				$classes .= ' ' . $class . '-what-your';
Scott committed
1960

1961
			if (isset($post['what_url']))
Scott committed
1962
				$this->output('<a href="' . $post['what_url'] . '" class="' . $classes . '">' . $post['what'] . '</a>');
1963
			else
Scott committed
1964
				$this->output('<span class="' . $classes . '">' . $post['what'] . '</span>');
Scott committed
1965
		}
1966
	}
Scott committed
1967

1968 1969
	public function post_meta_when($post, $class)
	{
Scott committed
1970
		$this->output_split(@$post['when'], $class . '-when');
1971
	}
Scott committed
1972

1973 1974
	public function post_meta_where($post, $class)
	{
Scott committed
1975
		$this->output_split(@$post['where'], $class . '-where');
1976
	}
Scott committed
1977

1978 1979 1980
	public function post_meta_who($post, $class)
	{
		if (isset($post['who'])) {
Scott committed
1981
			$this->output('<span class="' . $class . '-who">');
Scott committed
1982

1983
			if (strlen(@$post['who']['prefix']))
Scott committed
1984
				$this->output('<span class="' . $class . '-who-pad">' . $post['who']['prefix'] . '</span>');
Scott committed
1985

1986
			if (isset($post['who']['data']))
Scott committed
1987
				$this->output('<span class="' . $class . '-who-data">' . $post['who']['data'] . '</span>');
Scott committed
1988

1989
			if (isset($post['who']['title']))
Scott committed
1990
				$this->output('<span class="' . $class . '-who-title">' . $post['who']['title'] . '</span>');
Scott committed
1991

1992
			// You can also use $post['level'] to get the author's privilege level (as a string)
Scott committed
1993

1994
			if (isset($post['who']['points'])) {
Scott committed
1995
				$post['who']['points']['prefix'] = '(' . $post['who']['points']['prefix'];
1996
				$post['who']['points']['suffix'] .= ')';
Scott committed
1997
				$this->output_split($post['who']['points'], $class . '-who-points');
Scott committed
1998 1999
			}

2000
			if (strlen(@$post['who']['suffix']))
Scott committed
2001
				$this->output('<span class="' . $class . '-who-pad">' . $post['who']['suffix'] . '</span>');
Scott committed
2002

2003
			$this->output('</span>');
Scott committed
2004
		}
2005
	}
Scott committed
2006

2007 2008
	public function post_meta_flags($post, $class)
	{
Scott committed
2009
		$this->output_split(@$post['flags'], $class . '-flags');
2010
	}
Scott committed
2011

2012 2013 2014
	public function post_tags($post, $class)
	{
		if (!empty($post['q_tags'])) {
Scott committed
2015
			$this->output('<div class="' . $class . '-tags">');
2016 2017
			$this->post_tag_list($post, $class);
			$this->output('</div>');
Scott committed
2018
		}
2019
	}
Scott committed
2020

2021 2022
	public function post_tag_list($post, $class)
	{
Scott committed
2023
		$this->output('<ul class="' . $class . '-tag-list">');
Scott committed
2024

Scott committed
2025
		foreach ($post['q_tags'] as $taghtml) {
2026
			$this->post_tag_item($taghtml, $class);
Scott committed
2027
		}
Scott committed
2028

2029 2030
		$this->output('</ul>');
	}
Scott committed
2031

2032 2033
	public function post_tag_item($taghtml, $class)
	{
Scott committed
2034
		$this->output('<li class="' . $class . '-tag-item">' . $taghtml . '</li>');
2035
	}
Scott committed
2036

2037 2038 2039
	public function page_links()
	{
		$page_links = @$this->content['page_links'];
Scott committed
2040

2041 2042
		if (!empty($page_links)) {
			$this->output('<div class="qa-page-links">');
Scott committed
2043

2044 2045 2046
			$this->page_links_label(@$page_links['label']);
			$this->page_links_list(@$page_links['items']);
			$this->page_links_clear();
Scott committed
2047

2048
			$this->output('</div>');
Scott committed
2049
		}
2050
	}
Scott committed
2051

2052 2053 2054
	public function page_links_label($label)
	{
		if (!empty($label))
Scott committed
2055
			$this->output('<span class="qa-page-links-label">' . $label . '</span>');
2056
	}
Scott committed
2057

2058 2059 2060 2061
	public function page_links_list($page_items)
	{
		if (!empty($page_items)) {
			$this->output('<ul class="qa-page-links-list">');
Scott committed
2062

2063
			$index = 0;
Scott committed
2064

2065 2066 2067
			foreach ($page_items as $page_link) {
				$this->set_context('page_index', $index++);
				$this->page_links_item($page_link);
Scott committed
2068

2069 2070
				if ($page_link['ellipsis'])
					$this->page_links_item(array('type' => 'ellipsis'));
Scott committed
2071 2072
			}

2073
			$this->clear_context('page_index');
Scott committed
2074

2075
			$this->output('</ul>');
Scott committed
2076
		}
2077
	}
Scott committed
2078

2079 2080 2081 2082 2083 2084
	public function page_links_item($page_link)
	{
		$this->output('<li class="qa-page-links-item">');
		$this->page_link_content($page_link);
		$this->output('</li>');
	}
Scott committed
2085

2086 2087 2088 2089
	public function page_link_content($page_link)
	{
		$label = @$page_link['label'];
		$url = @$page_link['url'];
Scott committed
2090

2091 2092
		switch ($page_link['type']) {
			case 'this':
Scott committed
2093
				$this->output('<span class="qa-page-selected">' . $label . '</span>');
2094
				break;
Scott committed
2095

2096
			case 'prev':
Scott committed
2097
				$this->output('<a href="' . $url . '" class="qa-page-prev">&laquo; ' . $label . '</a>');
2098
				break;
Scott committed
2099

2100
			case 'next':
Scott committed
2101
				$this->output('<a href="' . $url . '" class="qa-page-next">' . $label . ' &raquo;</a>');
2102
				break;
Scott committed
2103

2104 2105 2106 2107 2108
			case 'ellipsis':
				$this->output('<span class="qa-page-ellipsis">...</span>');
				break;

			default:
Scott committed
2109
				$this->output('<a href="' . $url . '" class="qa-page-link">' . $label . '</a>');
2110
				break;
Scott committed
2111
		}
2112
	}
Scott committed
2113

2114 2115 2116 2117 2118 2119 2120
	public function page_links_clear()
	{
		$this->output(
			'<div class="qa-page-links-clear">',
			'</div>'
		);
	}
Scott committed
2121

2122 2123 2124
	public function suggest_next()
	{
		$suggest = @$this->content['suggest_next'];
Scott committed
2125

2126 2127 2128
		if (!empty($suggest)) {
			$this->output('<div class="qa-suggest-next">');
			$this->output($suggest);
Scott committed
2129 2130
			$this->output('</div>');
		}
2131
	}
Scott committed
2132

2133 2134 2135
	public function q_view($q_view)
	{
		if (!empty($q_view)) {
Scott committed
2136
			$this->output('<div class="qa-q-view' . (@$q_view['hidden'] ? ' qa-q-view-hidden' : '') . rtrim(' ' . @$q_view['classes']) . '"' . rtrim(' ' . @$q_view['tags']) . '>');
Scott committed
2137

2138 2139 2140
			if (isset($q_view['main_form_tags'])) {
				$this->output('<form ' . $q_view['main_form_tags'] . '>'); // form for question voting buttons
			}
2141 2142

			$this->q_view_stats($q_view);
Scott committed
2143 2144

			if (isset($q_view['main_form_tags'])) {
2145
				$this->form_hidden_elements(@$q_view['voting_form_hidden']);
Scott committed
2146 2147 2148
				$this->output('</form>');
			}

2149 2150
			$this->q_view_main($q_view);
			$this->q_view_clear();
Scott committed
2151

2152
			$this->output('</div> <!-- END qa-q-view -->', '');
Scott committed
2153
		}
2154
	}
Scott committed
2155

2156 2157 2158
	public function q_view_stats($q_view)
	{
		$this->output('<div class="qa-q-view-stats">');
Scott committed
2159

2160 2161
		$this->voting($q_view);
		$this->a_count($q_view);
Scott committed
2162

2163 2164
		$this->output('</div>');
	}
Scott committed
2165

2166 2167 2168
	public function q_view_main($q_view)
	{
		$this->output('<div class="qa-q-view-main">');
Scott committed
2169

2170
		if (isset($q_view['main_form_tags'])) {
Scott committed
2171
			$this->output('<form ' . $q_view['main_form_tags'] . '>'); // form for buttons on question
2172
		}
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185

		$this->view_count($q_view);
		$this->q_view_content($q_view);
		$this->q_view_extra($q_view);
		$this->q_view_follows($q_view);
		$this->q_view_closed($q_view);
		$this->post_tags($q_view, 'qa-q-view');
		$this->post_avatar_meta($q_view, 'qa-q-view');
		$this->q_view_buttons($q_view);

		if (isset($q_view['main_form_tags'])) {
			$this->form_hidden_elements(@$q_view['buttons_form_hidden']);
			$this->output('</form>');
Scott committed
2186 2187
		}

2188
		$this->c_list(@$q_view['c_list'], 'qa-q-view');
2189 2190 2191 2192 2193 2194 2195
		$this->c_form(@$q_view['c_form']);

		$this->output('</div> <!-- END qa-q-view-main -->');
	}

	public function q_view_content($q_view)
	{
Scott committed
2196 2197
		$content = isset($q_view['content']) ? $q_view['content'] : '';

Scott committed
2198
		$this->output('<div class="qa-q-view-content qa-post-content">');
Scott committed
2199 2200
		$this->output_raw($content);
		$this->output('</div>');
2201
	}
Scott committed
2202

2203 2204 2205
	public function q_view_follows($q_view)
	{
		if (!empty($q_view['follows']))
Scott committed
2206
			$this->output(
2207 2208
				'<div class="qa-q-view-follows">',
				$q_view['follows']['label'],
Scott committed
2209
				'<a href="' . $q_view['follows']['url'] . '" class="qa-q-view-follows-link">' . $q_view['follows']['title'] . '</a>',
Scott committed
2210 2211
				'</div>'
			);
2212
	}
Scott committed
2213

2214 2215 2216 2217
	public function q_view_closed($q_view)
	{
		if (!empty($q_view['closed'])) {
			$haslink = isset($q_view['closed']['url']);
Scott committed
2218

2219 2220 2221
			$this->output(
				'<div class="qa-q-view-closed">',
				$q_view['closed']['label'],
Scott committed
2222
				($haslink ? ('<a href="' . $q_view['closed']['url'] . '"') : '<span') . ' class="qa-q-view-closed-content">',
2223 2224 2225 2226
				$q_view['closed']['content'],
				$haslink ? '</a>' : '</span>',
				'</div>'
			);
Scott committed
2227
		}
2228
	}
Scott committed
2229

2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	public function q_view_extra($q_view)
	{
		if (!empty($q_view['extra'])) {
			$this->output(
				'<div class="qa-q-view-extra">',
				$q_view['extra']['label'],
				'<span class="qa-q-view-extra-content">',
				$q_view['extra']['content'],
				'</span>',
				'</div>'
			);
Scott committed
2241
		}
2242
	}
Scott committed
2243

2244 2245 2246 2247 2248 2249
	public function q_view_buttons($q_view)
	{
		if (!empty($q_view['form'])) {
			$this->output('<div class="qa-q-view-buttons">');
			$this->form($q_view['form']);
			$this->output('</div>');
Scott committed
2250
		}
2251
	}
Scott committed
2252

2253 2254 2255 2256 2257 2258 2259
	public function q_view_clear()
	{
		$this->output(
			'<div class="qa-q-view-clear">',
			'</div>'
		);
	}
Scott committed
2260

2261 2262
	public function a_form($a_form)
	{
Scott committed
2263 2264
		$this->output('<div class="qa-a-form"' . (isset($a_form['id']) ? (' id="' . $a_form['id'] . '"') : '') .
			(@$a_form['collapse'] ? ' style="display:none;"' : '') . '>');
Scott committed
2265

2266 2267
		$this->form($a_form);
		$this->c_list(@$a_form['c_list'], 'qa-a-item');
Scott committed
2268

2269 2270
		$this->output('</div> <!-- END qa-a-form -->', '');
	}
Scott committed
2271

2272 2273 2274 2275
	public function a_list($a_list)
	{
		if (!empty($a_list)) {
			$this->part_title($a_list);
Scott committed
2276

Scott committed
2277
			$this->output('<div class="qa-a-list' . ($this->list_vote_disabled($a_list['as']) ? ' qa-a-list-vote-disabled' : '') . '" ' . @$a_list['tags'] . '>', '');
2278 2279
			$this->a_list_items($a_list['as']);
			$this->output('</div> <!-- END qa-a-list -->', '');
Scott committed
2280
		}
2281
	}
Scott committed
2282

2283 2284
	public function a_list_items($a_items)
	{
Scott committed
2285
		foreach ($a_items as $a_item) {
2286
			$this->a_list_item($a_item);
Scott committed
2287
		}
2288
	}
Scott committed
2289

2290 2291
	public function a_list_item($a_item)
	{
Scott committed
2292
		$extraclass = @$a_item['classes'] . ($a_item['hidden'] ? ' qa-a-list-item-hidden' : ($a_item['selected'] ? ' qa-a-list-item-selected' : ''));
Scott committed
2293

Scott committed
2294
		$this->output('<div class="qa-a-list-item ' . $extraclass . '" ' . @$a_item['tags'] . '>');
Scott committed
2295

2296 2297 2298
		if (isset($a_item['main_form_tags'])) {
			$this->output('<form ' . $a_item['main_form_tags'] . '>'); // form for answer voting buttons
		}
Scott committed
2299

2300
		$this->voting($a_item);
Scott committed
2301

2302 2303 2304 2305
		if (isset($a_item['main_form_tags'])) {
			$this->form_hidden_elements(@$a_item['voting_form_hidden']);
			$this->output('</form>');
		}
Scott committed
2306

2307 2308
		$this->a_item_main($a_item);
		$this->a_item_clear();
Scott committed
2309

2310 2311
		$this->output('</div> <!-- END qa-a-list-item -->', '');
	}
Scott committed
2312

2313 2314 2315
	public function a_item_main($a_item)
	{
		$this->output('<div class="qa-a-item-main">');
Scott committed
2316

2317
		if (isset($a_item['main_form_tags'])) {
Scott committed
2318
			$this->output('<form ' . $a_item['main_form_tags'] . '>'); // form for buttons on answer
2319
		}
Scott committed
2320

2321 2322 2323 2324
		if ($a_item['hidden'])
			$this->output('<div class="qa-a-item-hidden">');
		elseif ($a_item['selected'])
			$this->output('<div class="qa-a-item-selected">');
Scott committed
2325

2326 2327 2328 2329
		$this->a_selection($a_item);
		$this->error(@$a_item['error']);
		$this->a_item_content($a_item);
		$this->post_avatar_meta($a_item, 'qa-a-item');
Scott committed
2330

2331 2332
		if ($a_item['hidden'] || $a_item['selected'])
			$this->output('</div>');
Scott committed
2333

2334
		$this->a_item_buttons($a_item);
Scott committed
2335

2336 2337 2338
		if (isset($a_item['main_form_tags'])) {
			$this->form_hidden_elements(@$a_item['buttons_form_hidden']);
			$this->output('</form>');
Scott committed
2339 2340
		}

2341
		$this->c_list(@$a_item['c_list'], 'qa-a-item');
2342
		$this->c_form(@$a_item['c_form']);
Scott committed
2343

2344 2345
		$this->output('</div> <!-- END qa-a-item-main -->');
	}
Scott committed
2346

2347 2348 2349 2350 2351 2352 2353
	public function a_item_clear()
	{
		$this->output(
			'<div class="qa-a-item-clear">',
			'</div>'
		);
	}
Scott committed
2354

2355 2356
	public function a_item_content($a_item)
	{
Scott committed
2357 2358 2359 2360
		if (!isset($a_item['content'])) {
			$a_item['content'] = '';
		}

Scott committed
2361
		$this->output('<div class="qa-a-item-content qa-post-content">');
2362 2363 2364
		$this->output_raw($a_item['content']);
		$this->output('</div>');
	}
Scott committed
2365

2366 2367 2368 2369 2370 2371
	public function a_item_buttons($a_item)
	{
		if (!empty($a_item['form'])) {
			$this->output('<div class="qa-a-item-buttons">');
			$this->form($a_item['form']);
			$this->output('</div>');
Scott committed
2372
		}
2373
	}
Scott committed
2374

2375 2376
	public function c_form($c_form)
	{
Scott committed
2377 2378
		$this->output('<div class="qa-c-form"' . (isset($c_form['id']) ? (' id="' . $c_form['id'] . '"') : '') .
			(@$c_form['collapse'] ? ' style="display:none;"' : '') . '>');
Scott committed
2379

2380
		$this->form($c_form);
Scott committed
2381

2382 2383
		$this->output('</div> <!-- END qa-c-form -->', '');
	}
Scott committed
2384

2385 2386 2387
	public function c_list($c_list, $class)
	{
		if (!empty($c_list)) {
Scott committed
2388
			$this->output('', '<div class="' . $class . '-c-list"' . (@$c_list['hidden'] ? ' style="display:none;"' : '') . ' ' . @$c_list['tags'] . '>');
2389 2390
			$this->c_list_items($c_list['cs']);
			$this->output('</div> <!-- END qa-c-list -->', '');
Scott committed
2391
		}
2392
	}
Scott committed
2393

2394 2395
	public function c_list_items($c_items)
	{
Scott committed
2396
		foreach ($c_items as $c_item) {
2397
			$this->c_list_item($c_item);
Scott committed
2398
		}
2399
	}
Scott committed
2400

2401 2402
	public function c_list_item($c_item)
	{
Scott committed
2403
		$extraclass = @$c_item['classes'] . (@$c_item['hidden'] ? ' qa-c-item-hidden' : '');
Scott committed
2404

Scott committed
2405
		$this->output('<div class="qa-c-list-item ' . $extraclass . '" ' . @$c_item['tags'] . '>');
Scott committed
2406

2407 2408 2409 2410
		if (isset($c_item['vote_view']) && isset($c_item['main_form_tags'])) {
			// form for comment voting buttons
			$this->output('<form ' . $c_item['main_form_tags'] . '>');
			$this->voting($c_item);
2411 2412 2413 2414
			$this->form_hidden_elements(@$c_item['voting_form_hidden']);
			$this->output('</form>');
		}

2415 2416
		$this->c_item_main($c_item);
		$this->c_item_clear();
Scott committed
2417

2418 2419
		$this->output('</div> <!-- END qa-c-item -->');
	}
Scott committed
2420

2421 2422
	public function c_item_main($c_item)
	{
2423 2424 2425 2426
		if (isset($c_item['main_form_tags'])) {
			$this->output('<form ' . $c_item['main_form_tags'] . '>'); // form for buttons on comment
		}

2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
		$this->error(@$c_item['error']);

		if (isset($c_item['expand_tags']))
			$this->c_item_expand($c_item);
		elseif (isset($c_item['url']))
			$this->c_item_link($c_item);
		else
			$this->c_item_content($c_item);

		$this->output('<div class="qa-c-item-footer">');
		$this->post_avatar_meta($c_item, 'qa-c-item');
		$this->c_item_buttons($c_item);
		$this->output('</div>');
2440 2441 2442 2443 2444

		if (isset($c_item['main_form_tags'])) {
			$this->form_hidden_elements(@$c_item['buttons_form_hidden']);
			$this->output('</form>');
		}
2445
	}
Scott committed
2446

2447 2448 2449
	public function c_item_link($c_item)
	{
		$this->output(
Scott committed
2450
			'<a href="' . $c_item['url'] . '" class="qa-c-item-link">' . $c_item['title'] . '</a>'
2451 2452
		);
	}
Scott committed
2453

2454 2455 2456
	public function c_item_expand($c_item)
	{
		$this->output(
Scott committed
2457
			'<a href="' . $c_item['url'] . '" ' . $c_item['expand_tags'] . ' class="qa-c-item-expand">' . $c_item['title'] . '</a>'
2458 2459
		);
	}
Scott committed
2460

2461 2462
	public function c_item_content($c_item)
	{
Scott committed
2463 2464 2465 2466
		if (!isset($c_item['content'])) {
			$c_item['content'] = '';
		}

Scott committed
2467
		$this->output('<div class="qa-c-item-content qa-post-content">');
2468 2469 2470
		$this->output_raw($c_item['content']);
		$this->output('</div>');
	}
Scott committed
2471

2472 2473 2474 2475 2476
	public function c_item_buttons($c_item)
	{
		if (!empty($c_item['form'])) {
			$this->output('<div class="qa-c-item-buttons">');
			$this->form($c_item['form']);
Scott committed
2477 2478 2479 2480
			$this->output('</div>');
		}
	}

2481 2482 2483 2484 2485 2486 2487 2488 2489
	public function c_item_clear()
	{
		$this->output(
			'<div class="qa-c-item-clear">',
			'</div>'
		);
	}


Scott committed
2490 2491
	/**
	 * Generic method to output a basic list of question links.
Scott committed
2492 2493
	 * @param array $q_list
	 * @param string $attrs
Scott committed
2494
	 */
Scott committed
2495
	public function q_title_list($q_list, $attrs = null)
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
	{
		$this->output('<ul class="qa-q-title-list">');
		foreach ($q_list as $q) {
			$this->output(
				'<li class="qa-q-title-item">',
				'<a href="' . qa_q_path_html($q['postid'], $q['title']) . '" ' . $attrs . '>' . qa_html($q['title']) . '</a>',
				'</li>'
			);
		}
		$this->output('</ul>');
	}
Scott committed
2507

Scott committed
2508 2509
	/**
	 * Output block of similar questions when asking.
Scott committed
2510 2511
	 * @param array $q_list
	 * @param string $pretext
Scott committed
2512
	 */
Scott committed
2513
	public function q_ask_similar($q_list, $pretext = '')
2514 2515 2516 2517 2518 2519 2520
	{
		if (!count($q_list))
			return;

		$this->output('<div class="qa-ask-similar">');

		if (strlen($pretext) > 0)
Scott committed
2521
			$this->output('<p class="qa-ask-similar-title">' . $pretext . '</p>');
2522 2523 2524 2525 2526
		$this->q_title_list($q_list, 'target="_blank"');

		$this->output('</div>');
	}
}