qa-theme.php 17.2 KB
Newer Older
Scott committed
1 2
<?php
/*
3 4 5
	Snow Theme for Question2Answer Package
	Copyright (C) 2014 Q2A Market <http://www.q2amarket.com>

Scott committed
6 7 8
	File:           qa-theme.php
	Version:        Snow 1.4
	Description:    Q2A theme class
9 10 11 12 13 14 15 16 17 18

	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 3 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.
Scott committed
19
*/
Scott committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * Snow theme extends
 *
 * Extends the core theme class <code>qa_html_theme_base</code>
 *
 * @package qa_html_theme_base
 * @subpackage qa_html_theme
 * @category Theme
 * @since Snow 1.0
 * @version 1.4
 * @author Q2A Market <http://www.q2amarket.com>
 * @copyright (c) 2014, Q2A Market
 * @license http://www.gnu.org/copyleft/gpl.html
 */
class qa_html_theme extends qa_html_theme_base
{
37 38
	protected $theme = 'snowflat';

39 40 41
	// use local font files instead of Google Fonts
	private $localfonts = true;

pupi1985 committed
42
	// theme subdirectories
43 44
	private $js_dir = 'js';
	private $icon_url = 'images/icons';
45 46

	private $fixed_topbar = false;
47
	private $welcome_widget_class = 'wet-asphalt';
48
	private $ask_search_box_class = 'turquoise';
49
	// Size of the user avatar in the navigation bar
50
	private $nav_bar_avatar_size = 52;
51

Scott committed
52 53 54 55 56 57 58
	/**
	 * Adding aditional meta for responsive design
	 *
	 * @since Snow 1.4
	 */
	public function head_metas()
	{
Scott committed
59
		$this->output('<meta name="viewport" content="width=device-width, initial-scale=1"/>');
60
		parent::head_metas();
Scott committed
61 62 63 64 65 66 67 68 69
	}

	/**
	 * Adding theme stylesheets
	 *
	 * @since Snow 1.4
	 */
	public function head_css()
	{
70
		// add RTL CSS file
71
		if ($this->isRTL)
Scott committed
72
			$this->content['css_src'][] = $this->rooturl . 'qa-styles-rtl.css?' . QA_VERSION;
73

74 75 76 77 78
		// add Ubuntu font CSS file from Google Fonts
		if ($this->localfonts)
			$this->content['css_src'][] = $this->rooturl . 'fonts/ubuntu.css?' . QA_VERSION;
		else
			$this->content['css_src'][] = '//fonts.googleapis.com/css?family=Ubuntu:400,700,400italic,700italic';
Scott committed
79

80
		parent::head_css();
Scott committed
81 82

		// output some dynamic CSS inline
83
		$this->head_inline_css();
Scott committed
84 85 86 87 88 89 90 91 92
	}

	/**
	 * Adding theme javascripts
	 *
	 * @since Snow 1.4
	 */
	public function head_script()
	{
93
		$jsUrl = $this->rooturl . $this->js_dir . '/snow-core.js?' . QA_VERSION;
Scott committed
94 95
		$this->content['script'][] = '<script src="' . $jsUrl . '"></script>';

96
		parent::head_script();
Scott committed
97 98 99 100 101 102 103 104 105
	}

	/**
	 * Adding point count for logged in user
	 *
	 * @since Snow 1.4
	 */
	public function logged_in()
	{
106
		parent::logged_in();
107 108 109 110
		if (qa_is_logged_in()) {
			$userpoints = qa_get_logged_in_points();
			$pointshtml = $userpoints == 1
				? qa_lang_html_sub('main/1_point', '1', '1')
Scott committed
111
				: qa_html(qa_format_number($userpoints))
112 113 114
			;
			$this->output('<div class="qam-logged-in-points">' . $pointshtml . '</div>');
		}
Scott committed
115 116 117
	}

	/**
118
	 * Adding body class dynamically. Override needed to add class on admin/approve-users page
Scott committed
119 120 121 122 123 124
	 *
	 * @since Snow 1.4
	 */
	public function body_tags()
	{
		$class = 'qa-template-' . qa_html($this->template);
125
		$class .= empty($this->theme) ? '' : ' qa-theme-' . qa_html($this->theme);
Scott committed
126 127

		if (isset($this->content['categoryids'])) {
Scott committed
128
			foreach ($this->content['categoryids'] as $categoryid) {
Scott committed
129
				$class .= ' qa-category-' . qa_html($categoryid);
Scott committed
130
			}
Scott committed
131 132
		}

Scott committed
133
		// add class if admin/approve-users page
134
		if ($this->template === 'admin' && qa_request_part(1) === 'approve')
Scott committed
135 136
			$class .= ' qam-approve-users';

137 138
		if ($this->fixed_topbar)
			$class .= ' qam-body-fixed';
Scott committed
139 140 141 142 143

		$this->output('class="' . $class . ' qa-body-js-off"');
	}

	/**
144
	 * Login form for user dropdown menu.
Scott committed
145 146 147 148 149 150 151 152
	 *
	 * @since Snow 1.4
	 */
	public function nav_user_search()
	{
		// outputs login form if user not logged in
		$this->output('<div class="qam-account-items-wrapper">');

Scott committed
153
		$this->qam_user_account();
Scott committed
154 155 156 157

		$this->output('<div class="qam-account-items clearfix">');

		if (!qa_is_logged_in()) {
158 159
			if (isset($this->content['navigation']['user']['login']) && !QA_FINAL_EXTERNAL_USERS) {
				$login = $this->content['navigation']['user']['login'];
Scott committed
160
				$this->output(
161
					'<form action="' . $login['url'] . '" method="post">',
162
						'<input type="text" name="emailhandle" dir="auto" placeholder="' . trim(qa_lang_html(qa_opt('allow_login_email_only') ? 'users/email_label' : 'users/email_handle_label'), ':') . '"/>',
Scott committed
163 164
						'<input type="password" name="password" dir="auto" placeholder="' . trim(qa_lang_html('users/password_label'), ':') . '"/>',
						'<div><input type="checkbox" name="remember" id="qam-rememberme" value="1"/>',
165
						'<label for="qam-rememberme">' . qa_lang_html('users/remember') . '</label></div>',
Scott committed
166 167
						'<input type="hidden" name="code" value="' . qa_html(qa_get_form_security_code('login')) . '"/>',
						'<input type="submit" value="' . $login['label'] . '" class="qa-form-tall-button qa-form-tall-button-login" name="dologin"/>',
168
					'</form>'
Scott committed
169
				);
170 171 172

				// remove regular navigation link to log in page
				unset($this->content['navigation']['user']['login']);
Scott committed
173 174 175
			}
		}

176
		$this->nav('user');
Scott committed
177 178 179 180 181
		$this->output('</div> <!-- END qam-account-items -->');
		$this->output('</div> <!-- END qam-account-items-wrapper -->');
	}

	/**
182
	 * Modify markup for topbar.
Scott committed
183 184 185 186 187 188 189
	 *
	 * @since Snow 1.4
	 */
	public function nav_main_sub()
	{
		$this->output('<div class="qam-main-nav-wrapper clearfix">');
		$this->output('<div class="sb-toggle-left qam-menu-toggle"><i class="icon-th-list"></i></div>');
Scott committed
190
		$this->nav_user_search();
Scott committed
191 192 193 194 195 196 197
		$this->logo();
		$this->nav('main');
		$this->output('</div> <!-- END qam-main-nav-wrapper -->');
		$this->nav('sub');
	}

	/**
198
	 * Remove the '-' from the note for the category page (notes).
Scott committed
199 200 201 202 203 204 205
	 *
	 * @since Snow 1.4
	 * @param type $navlink
	 * @param type $class
	 */
	public function nav_link($navlink, $class)
	{
206
		if (isset($navlink['note']) && !empty($navlink['note'])) {
207
			$search = array(' - <', '> - ');
Scott committed
208
			$replace = array(' <', '> ');
209
			$navlink['note'] = str_replace($search, $replace, $navlink['note']);
Scott committed
210
		}
211
		parent::nav_link($navlink, $class);
Scott committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	}

	/**
	 * Rearranges the layout:
	 * - Swaps the <tt>main()</tt> and <tt>sidepanel()</tt> functions
	 * - Moves the header and footer functions outside qa-body-wrapper
	 * - Keeps top/high and low/bottom widgets separated
	 *
	 * @since Snow 1.4
	 */
	public function body_content()
	{
		$this->body_prefix();
		$this->notices();

		$this->widgets('full', 'top');
		$this->header();

		$this->output('<div class="qa-body-wrapper">', '');
		$this->widgets('full', 'high');

		$this->output('<div class="qa-main-wrapper">', '');
		$this->main();
		$this->sidepanel();
		$this->output('</div> <!-- END main-wrapper -->');

		$this->widgets('full', 'low');
		$this->output('</div> <!-- END body-wrapper -->');

		$this->footer();

		$this->body_suffix();
	}

	/**
	 * Header in full width top bar
	 *
	 * @since Snow 1.4
	 */
	public function header()
	{
253
		$class = $this->fixed_topbar ? ' fixed' : '';
Scott committed
254

Scott committed
255 256
		$this->output('<div id="qam-topbar" class="clearfix' . $class . '">');

Scott committed
257
		$this->nav_main_sub();
258
		$this->output('</div> <!-- END qam-topbar -->');
Scott committed
259

260
		$this->output($this->ask_button());
Scott committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
		$this->qam_search('the-top', 'the-top-search');
	}

	/**
	 * Footer in full width bottom bar
	 *
	 * @since Snow 1.4
	 */
	public function footer()
	{
		$this->output('<div class="qam-footer-box">');

		$this->output('<div class="qam-footer-row">');
		$this->widgets('full', 'bottom');
		$this->output('</div> <!-- END qam-footer-row -->');

277
		parent::footer();
278
		$this->output('</div> <!-- END qam-footer-box -->');
Scott committed
279 280 281 282 283 284 285 286 287
	}

	/**
	 * Overridden to customize layout and styling
	 *
	 * @since Snow 1.4
	 */
	public function sidepanel()
	{
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		// remove sidebar for user profile pages
		if ($this->template == 'user')
			return;

		$this->output('<div id="qam-sidepanel-toggle"><i class="icon-left-open-big"></i></div>');
		$this->output('<div class="qa-sidepanel" id="qam-sidepanel-mobile">');
		$this->qam_search();
		$this->widgets('side', 'top');
		$this->sidebar();
		$this->widgets('side', 'high');
		$this->widgets('side', 'low');
		if (isset($this->content['sidepanel']))
			$this->output_raw($this->content['sidepanel']);
		$this->feed();
		$this->widgets('side', 'bottom');
		$this->output('</div>', '');
Scott committed
304 305 306
	}

	/**
307
	 * Allow alternate sidebar color.
Scott committed
308 309 310 311 312
	 *
	 * @since Snow 1.4
	 */
	public function sidebar()
	{
313 314 315
		if (isset($this->content['sidebar'])) {
			$sidebar = $this->content['sidebar'];
			if (!empty($sidebar)) {
316
				$this->output('<div class="qa-sidebar ' . $this->welcome_widget_class . '">');
317 318 319
				$this->output_raw($sidebar);
				$this->output('</div>', '');
			}
Scott committed
320 321 322 323
		}
	}

	/**
324
	 * Add close icon
Scott committed
325 326 327 328 329 330
	 *
	 * @since Snow 1.4
	 * @param array $q_item
	 */
	public function q_item_title($q_item)
	{
pupi1985 committed
331
		$closedText = qa_lang('main/closed');
332 333
		$imgHtml = empty($q_item['closed'])
			? ''
Scott committed
334
			: '<img src="' . $this->rooturl . $this->icon_url . '/closed-q-list.png" class="qam-q-list-close-icon" alt="' . $closedText . '" title="' . $closedText . '"/>';
335

Scott committed
336
		$this->output(
337 338 339 340 341
			'<div class="qa-q-item-title">',
			// add closed note in title
			$imgHtml,
			'<a href="' . $q_item['url'] . '">' . $q_item['title'] . '</a>',
			'</div>'
Scott committed
342 343 344 345
		);
	}

	/**
346
	 * Add RSS feeds icon and closed icon for closed questions
Scott committed
347 348 349 350 351
	 *
	 * @since Snow 1.4
	 */
	public function title()
	{
352
		$q_view = isset($this->content['q_view']) ? $this->content['q_view'] : null;
Scott committed
353

Scott committed
354
		// RSS feed link in title
355 356 357 358
		if (isset($this->content['feed']['url'])) {
			$feed = $this->content['feed'];
			$label = isset($feed['label']) ? $feed['label'] : '';
			$this->output('<a href="' . $feed['url'] . '" title="' . $label . '"><i class="icon-rss qam-title-rss"></i></a>');
Scott committed
359 360
		}

Scott committed
361 362 363 364
		// link title where appropriate
		$url = isset($q_view['url']) ? $q_view['url'] : false;

		// add closed image
pupi1985 committed
365
		$closedText = qa_lang('main/closed');
366 367
		$imgHtml = empty($q_view['closed'])
			? ''
Scott committed
368
			: '<img src="' . $this->rooturl . $this->icon_url . '/closed-q-view.png" class="qam-q-view-close-icon" alt="' . $closedText . '" width="24" height="24" title="' . $closedText . '"/>';
369

Scott committed
370 371
		if (isset($this->content['title'])) {
			$this->output(
372 373 374 375
				$imgHtml,
				$url ? '<a href="' . $url . '">' : '',
				$this->content['title'],
				$url ? '</a>' : ''
Scott committed
376 377 378 379 380
			);
		}
	}

	/**
381
	 * Add view counter to question list
Scott committed
382 383 384 385 386
	 *
	 * @since Snow 1.4
	 * @param array $q_item
	 */
	public function q_item_stats($q_item)
387
	{
Scott committed
388 389 390 391
		$this->output('<div class="qa-q-item-stats">');

		$this->voting($q_item);
		$this->a_count($q_item);
392
		parent::view_count($q_item);
Scott committed
393 394 395 396 397 398 399 400 401 402

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

	/**
	 * Prevent display view counter on usual place
	 *
	 * @since Snow 1.4
	 * @param type $q_item
	 */
403
	public function view_count($q_item) {}
Scott committed
404 405

	/**
406
	 * Add view counter to question view
Scott committed
407 408 409 410 411 412 413 414 415 416
	 *
	 * @since Snow 1.4
	 * @param type $q_view
	 */
	public function q_view_stats($q_view)
	{
		$this->output('<div class="qa-q-view-stats">');

		$this->voting($q_view);
		$this->a_count($q_view);
417
		parent::view_count($q_view);
Scott committed
418 419 420 421 422

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

	/**
423
	 * Modify user whometa, move to top
Scott committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	 *
	 * @since Snow 1.4
	 * @param type $q_view
	 */
	public function q_view_main($q_view)
	{
		$this->output('<div class="qa-q-view-main">');

		if (isset($q_view['main_form_tags']))
			$this->output('<form ' . $q_view['main_form_tags'] . '>'); // form for buttons on question

		$this->post_avatar_meta($q_view, 'qa-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->q_view_buttons($q_view);
443
		$this->c_list(isset($q_view['c_list']) ? $q_view['c_list'] : null, 'qa-q-view');
Scott committed
444 445

		if (isset($q_view['main_form_tags'])) {
446 447
			if (isset($q_view['buttons_form_hidden']))
				$this->form_hidden_elements($q_view['buttons_form_hidden']);
Scott committed
448 449 450
			$this->output('</form>');
		}

451
		$this->c_form(isset($q_view['c_form']) ? $q_view['c_form'] : null);
Scott committed
452 453 454 455 456

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

	/**
457
	 * Move user whometa to top in answer
Scott committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471
	 *
	 * @since Snow 1.4
	 * @param type $a_item
	 */
	public function a_item_main($a_item)
	{
		$this->output('<div class="qa-a-item-main">');

		$this->post_avatar_meta($a_item, 'qa-a-item');

		if (isset($a_item['main_form_tags']))
			$this->output('<form ' . $a_item['main_form_tags'] . '>'); // form for buttons on answer

		if ($a_item['hidden'])
pupi1985 committed
472
			$answerState = 'hidden';
Scott committed
473
		elseif ($a_item['selected'])
pupi1985 committed
474 475 476 477 478 479
			$answerState = 'selected';
		else
			$answerState = null;

		if (isset($answerState))
			$this->output('<div class="qa-a-item-' . $answerState . '">');
Scott committed
480 481

		$this->a_selection($a_item);
482 483
		if (isset($a_item['error']))
			$this->error($a_item['error']);
Scott committed
484 485
		$this->a_item_content($a_item);

pupi1985 committed
486
		if (isset($answerState))
Scott committed
487 488 489 490
			$this->output('</div>');

		$this->a_item_buttons($a_item);

491 492
		if (isset($a_item['c_list']))
			$this->c_list($a_item['c_list'], 'qa-a-item');
Scott committed
493 494

		if (isset($a_item['main_form_tags'])) {
495 496
			if (isset($a_item['buttons_form_hidden']))
				$this->form_hidden_elements($a_item['buttons_form_hidden']);
Scott committed
497 498 499
			$this->output('</form>');
		}

500
		$this->c_form(isset($a_item['c_form']) ? $a_item['c_form'] : null);
Scott committed
501 502 503 504 505

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

	/**
506
	 * Move user whometa to top in comment
Scott committed
507 508 509 510 511 512 513 514
	 *
	 * @since Snow 1.4
	 * @param type $c_item
	 */
	public function c_item_main($c_item)
	{
		$this->post_avatar_meta($c_item, 'qa-c-item');

515 516
		if (isset($c_item['error']))
			$this->error($c_item['error']);
Scott committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

		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->c_item_buttons($c_item);
		$this->output('</div>');
	}

	/**
	 * Q2A Market attribution.
	 * I'd really appreciate you displaying this link on your Q2A site. Thank you - Jatin
	 *
	 * @since Snow 1.4
	 */
	public function attribution()
	{
		// floated right
		$this->output(
			'<div class="qa-attribution">',
			'Snow Theme by <a href="http://www.q2amarket.com">Q2A Market</a>',
			'</div>'
		);
544
		parent::attribution();
Scott committed
545 546
	}

Scott committed
547 548 549 550 551 552 553 554 555 556
	/**
	 * User account navigation item. This will return based on login information.
	 * If user is logged in, it will populate user avatar and account links.
	 * If user is guest, it will populate login form and registration link.
	 *
	 * @since Snow 1.4
	 */
	private function qam_user_account()
	{
		if (qa_is_logged_in()) {
557
			// get logged-in user avatar
Scott committed
558 559 560
			$handle = qa_get_logged_in_user_field('handle');
			$toggleClass = 'qam-logged-in';

561
			if (QA_FINAL_EXTERNAL_USERS)
562
				$tobar_avatar = qa_get_external_avatar_html(qa_get_logged_in_user_field('userid'), $this->nav_bar_avatar_size, true);
Scott committed
563 564 565 566 567 568 569 570
			else {
				$tobar_avatar = qa_get_user_avatar_html(
					qa_get_logged_in_user_field('flags'),
					qa_get_logged_in_user_field('email'),
					$handle,
					qa_get_logged_in_user_field('avatarblobid'),
					qa_get_logged_in_user_field('avatarwidth'),
					qa_get_logged_in_user_field('avatarheight'),
571
					$this->nav_bar_avatar_size,
Scott committed
572 573 574 575
					false
				);
			}

576 577 578
			$avatar = strip_tags($tobar_avatar, '<img>');
			if (!empty($avatar))
				$handle = '';
579
		}
580 581
		else {
			// display login icon and label
Scott committed
582 583
			$handle = $this->content['navigation']['user']['login']['label'];
			$toggleClass = 'qam-logged-out';
584
			$avatar = '<i class="icon-key qam-auth-key"></i>';
Scott committed
585 586 587
		}

		// finally output avatar with div tag
588
		$handleBlock = empty($handle) ? '' : '<div class="qam-account-handle">' . qa_html($handle) . '</div>';
Scott committed
589 590
		$this->output(
			'<div id="qam-account-toggle" class="' . $toggleClass . '">',
591 592
			$avatar,
			$handleBlock,
Scott committed
593 594 595 596
			'</div>'
		);
	}

Scott committed
597
	/**
598
	 * Add search-box wrapper with extra class for color scheme
Scott committed
599 600 601 602
	 *
	 * @since Snow 1.4
	 * @version 1.0
	 */
pupi1985 committed
603
	private function qam_search($addon_class = null, $ids = null)
Scott committed
604
	{
pupi1985 committed
605
		$id = isset($ids) ? ' id="' . $ids . '"' : '';
Scott committed
606

pupi1985 committed
607
		$this->output('<div class="qam-search ' . $this->ask_search_box_class . ' ' . $addon_class . '"' . $id . '>');
608
		$this->search();
Scott committed
609 610 611 612 613 614 615 616 617 618 619 620 621
		$this->output('</div>');
	}


	/**
	 * Dynamic <code>CSS</code> based on options and other interaction with Q2A.
	 *
	 * @since Snow 1.4
	 * @version 1.0
	 * @return string The CSS code
	 */
	private function head_inline_css()
	{
Scott committed
622 623
		$css = array('<style>');

624
		if (!qa_is_logged_in())
Scott committed
625
			$css[] = '.qa-nav-user { margin: 0 !important; }';
Scott committed
626 627

		if (qa_request_part(1) !== qa_get_logged_in_handle()) {
Scott committed
628 629 630 631 632 633 634
			$css[] = '@media (max-width: 979px) {';
			$css[] = ' body.qa-template-user.fixed, body[class*="qa-template-user-"].fixed { padding-top: 118px !important; }';
			$css[] = ' body.qa-template-users.fixed { padding-top: 95px !important; }';
			$css[] = '}';
			$css[] = '@media (min-width: 980px) {';
			$css[] = ' body.qa-template-users.fixed { padding-top: 105px !important;}';
			$css[] = '}';
Scott committed
635
		}
636

Scott committed
637 638 639
		$css[] = '</style>';

		$this->output_array($css);
Scott committed
640 641
	}

642 643 644 645 646 647 648 649 650 651
	/**
	 * Custom ask button for medium and small screen
	 *
	 * @access private
	 * @since Snow 1.4
	 * @version 1.0
	 * @return string Ask button html markup
	 */
	private function ask_button()
	{
652 653 654 655 656 657 658 659 660
		return
			'<div class="qam-ask-search-box">' .
			'<div class="qam-ask-mobile">' .
			'<a href="' . qa_path('ask', null, qa_path_to_root()) . '" class="' . $this->ask_search_box_class . '">' .
			qa_lang_html('main/nav_ask') .
			'</a>' .
			'</div>' .
			'<div class="qam-search-mobile ' . $this->ask_search_box_class . '" id="qam-search-mobile">' .
			'</div>' .
661
			'</div>';
662 663
	}

Scott committed
664
}