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 399
	public function body_hidden()
	{
		$indent = $this->isRTL ? '9999px' : '-9999px';
Scott committed
400
		$this->output('<div style="position:absolute; left:' . $indent . '; top:-9999px;">');
401 402 403
		$this->waiting_template();
		$this->output('</div>');
	}
Scott committed
404

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

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

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

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

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

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

441 442 443 444 445 446 447 448
		$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
449

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

576 577 578 579 580 581 582 583 584
			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
585 586
			}

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

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

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

600
		$index = 0;
Scott committed
601

602 603 604 605
		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
606 607
		}

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

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

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

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

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

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

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

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

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

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

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

678 679 680 681 682 683 684 685 686 687 688 689
	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
690

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

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

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

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

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

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

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

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

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

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

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

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

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

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

739 740 741 742 743 744 745
	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
746

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

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

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

767 768 769 770 771 772 773 774
	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
775
		}
776
	}
Scott committed
777

778 779 780 781 782 783 784 785
	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
786
			$this->output(
Scott committed
787
				$url ? '<a href="' . $url . '">' : '',
788 789
				$this->content['title'],
				$url ? '</a>' : ''
Scott committed
790 791 792
			);
		}

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

798 799 800 801 802
	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
803

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

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

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

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

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

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

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

Scott committed
858 859 860 861 862
		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
863
			$this->output('<div class="' . $class . '">');
Scott committed
864
		}
Scott committed
865

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

968 969
		return $columns;
	}
Scott committed
970

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

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

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

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

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

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

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

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

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

Scott committed
1018 1019 1020
	/**
	 * 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
1021 1022 1023 1024
	 * @param $form
	 * @param $keys
	 * @param mixed $beforekey
	 * @param bool $reorderrelative
Scott committed
1025
	 */
Scott committed
1026
	public function form_reorder_fields(&$form, $keys, $beforekey = null, $reorderrelative = true)
1027
	{
Scott committed
1028
		require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
1029

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

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

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

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

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

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

1061 1062 1063 1064
		$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
1065
			((!($prefixed || $suffixed)) || (!empty($field['error'])) || (!empty($field['note'])));
Scott committed
1066

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1189 1190 1191 1192 1193 1194
			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
1195 1196
		}

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

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

Scott committed
1202 1203 1204
	/**
	 * 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
1205 1206 1207 1208
	 * @param $form
	 * @param $keys
	 * @param mixed $beforekey
	 * @param bool $reorderrelative
Scott committed
1209
	 */
Scott committed
1210
	public function form_reorder_buttons(&$form, $keys, $beforekey = null, $reorderrelative = true)
1211
	{
Scott committed
1212
		require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
1213

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1335 1336 1337 1338 1339 1340
	/**
	 * 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
1341 1342
	 * @param $field
	 * @param $style
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
	 */
	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
1357 1358
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1467 1468 1469 1470
	/**
	 * @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
1471 1472
	 * @param $ranking
	 * @param $class
1473 1474 1475 1476 1477 1478
	 */
	public function ranking_table($ranking, $class)
	{
		$rows = min($ranking['rows'], count($ranking['items']));

		if ($rows > 0) {
Scott committed
1479
			$this->output('<table class="' . $class . '-table">');
1480 1481 1482 1483 1484 1485 1486 1487
			$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
1488
					$this->ranking_table_item(@$ranking['items'][$column * $rows + $row], $class, $column > 0);
Scott committed
1489
				}
1490 1491 1492

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

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

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

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

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

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

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

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


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

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

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

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

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

1559 1560 1561 1562 1563 1564
	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
1565
		}
1566
	}
Scott committed
1567

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

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

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

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

1590 1591 1592 1593 1594 1595
	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
1596
		}
1597
	}
Scott committed
1598

1599 1600 1601 1602 1603 1604
	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
1605
		}
1606
	}
Scott committed
1607

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

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

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

1621 1622
		return $disabled;
	}
Scott committed
1623

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

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

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

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

1636 1637 1638 1639
		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
1640 1641
		}

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

1645 1646 1647 1648 1649 1650
	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
1651
		}
1652
	}
Scott committed
1653

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

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

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

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

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

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

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

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

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

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

1699 1700 1701
		$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
1702

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
		$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
1718
			'<a href="' . $q_item['url'] . '">' . $q_item['title'] . '</a>',
1719
			// add closed note in title
Scott committed
1720
			empty($q_item['closed']['state']) ? '' : ' [' . $q_item['closed']['state'] . ']',
1721 1722 1723 1724 1725 1726 1727 1728 1729
			'</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
1730 1731
			$this->output('</div>');
		}
1732
	}
Scott committed
1733

1734 1735 1736 1737 1738 1739
	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
1740
		}
1741
	}
Scott committed
1742

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

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

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

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

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

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

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

1780 1781 1782 1783
			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
1784

1785 1786 1787 1788
			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
1789

1790 1791 1792 1793 1794
			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
1795

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

1799 1800 1801 1802
	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
1803

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

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

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

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

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

1828 1829 1830
		$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
1831

1832 1833 1834 1835 1836 1837 1838
	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
1839
	public function avatar($item, $class, $prefix = null)
1840 1841 1842 1843
	{
		if (isset($item['avatar'])) {
			if (isset($prefix))
				$this->output($prefix);
Scott committed
1844 1845

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

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

1857 1858 1859 1860 1861 1862
		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
1863

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

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

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

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

Scott committed
1882
	public function post_avatar_meta($post, $class, $avatarprefix = null, $metaprefix = null, $metaseparator = '<br/>')
1883
	{
Scott committed
1884
		$this->output('<span class="' . $class . '-avatar-meta">');
1885 1886 1887 1888 1889 1890 1891
		$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
1892 1893 1894
	 * @param $post
	 * @param $class
	 * @param string $prefix
1895
	 */
Scott committed
1896
	public function post_avatar($post, $class, $prefix = null)
1897 1898 1899
	{
		$this->avatar($post, $class, $prefix);
	}
Scott committed
1900

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2064
			$index = 0;
Scott committed
2065

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

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

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

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

2080 2081 2082 2083 2084 2085
	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
2086

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		$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
2187 2188
		}

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

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

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

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

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

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

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

2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
	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
2242
		}
2243
	}
Scott committed
2244

2245 2246 2247 2248 2249 2250
	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
2251
		}
2252
	}
Scott committed
2253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2322 2323 2324 2325
		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
2326

2327 2328 2329 2330
		$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
2331

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

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

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

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

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

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

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

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

2367 2368 2369 2370 2371 2372
	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
2373
		}
2374
	}
Scott committed
2375

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

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

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

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

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

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

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

2408 2409 2410 2411
		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);
2412 2413 2414 2415
			$this->form_hidden_elements(@$c_item['voting_form_hidden']);
			$this->output('</form>');
		}

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

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

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

2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
		$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>');
2441 2442 2443 2444 2445

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

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

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

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

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

2473 2474 2475 2476 2477
	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
2478 2479 2480 2481
			$this->output('</div>');
		}
	}

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


Scott committed
2491 2492
	/**
	 * Generic method to output a basic list of question links.
Scott committed
2493 2494
	 * @param array $q_list
	 * @param string $attrs
Scott committed
2495
	 */
Scott committed
2496
	public function q_title_list($q_list, $attrs = null)
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
	{
		$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
2508

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

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

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

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