Location: PHPKode > projects > JaxBlog > JaxBlog/app/common/builder-user.php
<?php

// config.php MUST have already been brought in 
// via require_once() prior to calling this script.

require_once('database.php');
require_once('refolder.php');

function getPageInfo($page_id) {
	return databaseGetRow('SELECT title, parent_id, visible_stories, require_login FROM Pages WHERE id = ?', array($page_id));
}

function buildUserPage($page_id, $story_id, $header, $onload, $content, $breadcrumb) {

	// Rule of thumb: only pass in values that change from 
	// page to page (assuming we're getting some reuse to occur.
	
	// Read in the html template, which has a series of replacement 
	// tokens delimited by brackets [].  
	$template = FILE_TEMPLATE_PAGE;
	if (isset($page_id) && $page_id == 1) $template = FILE_TEMPLATE_MAIN;
	$html = templateRefolderCSSAndJavascript($template);
	
	// Build the title
	$title = TITLE_HOMEPAGE;
	if (isset($page_id)) {
		$title .= ' - ' . databaseGetValue('SELECT title FROM Pages WHERE id = ?', array($page_id));
	} else if (isset($story_id)) {
		$title .= ' - ' . databaseGetValue('SELECT title FROM Stories WHERE id = ?', array($story_id));
	} else {
		$title .= ' - Archived Stories';
	}
	
	// Build an array of replacements for the tokens, start with panes that show on each page.
	$replacements = buildPanes($page_id);
	
	$replacements['Header']  .= $header;
	$replacements['Onload']  .= $onload;
	$replacements['Title']   .= $title;
	$replacements['Content'] .= $breadcrumb . $content;
	
	// Break the template into chunks, each chunk is either html
	// or the delimited token.
	$i = 0;
	$chunks = preg_split("/\[([A-Za-z0-9\s\/]*)\]/", $html, -1, PREG_SPLIT_DELIM_CAPTURE);
	$html = '';
	foreach ($chunks as $chunk) {
		if ($i++ % 2 == 1) {
			if (isset($replacements[$chunk])) {
				$html .= $replacements[$chunk] . "\n";
			} else {
				// Do nothing, likely pane is disabled.
			}
		} else {
			$html .= $chunk . "\n";
		}
	}
	
	return $html;
}

function buildPanes($page_id) {
	
	$panes = array();
	$panes['Onload']  = '';
	$panes['Header']  = "<meta name='Generator' content='JaxBlog' />\n";
	$panes['Title']   = '';
	$panes['Content'] = '';
	
	$rows = databaseGetRows("SELECT id, name, display, page_id, story_id, file_id, content FROM Panes WHERE disabled <> 'Y' ORDER BY id", array());
	// need to add page_id to panes table
	
	foreach ($rows as $row) {
		$id       = $row['id'];
		$css      = '';
		$content  = '';
		$shell_id = '';
		
		switch ($row['display']) {
			case '1':   // Main Navigation Bar
				$css = 'navbar_page';
				$content = buildNavbarPages($page_id, '');
				break;
			case '2':   // Navigation Bar Pages
				$css = 'navbar_page';
				$content = buildNavbarPages($row['page_id'], '');
				break;
			case '3':   // Navigation Bar Stories
				$css = 'navbar_story';
				$content = buildNavbarStories($row['page_id'], '');
				break;
			case '4':   // Single Story
				$css = 'story';
				if ($row['story_id'] == null) {
					$content = '';
				} else {
					$story = databaseGetRow('SELECT title, ' . databaseGetDate('stamp') . ' AS stamp, content FROM Stories WHERE id = ?', array($row['story_id']));
					$content = buildStoryFromDBRow($story, "", 'sub_story');
				}
				break;
			case '5':   // Slideshow
				$css = 'slideshow';
				$shell_id = 'pane_' . $css . '_' . $id;
				$panes['Onload']    .= "jaxSlideshowRegister('../user_slideshow/user_slideshow.php?request=xml&pane_id=" . $row['id'] . "',  '$shell_id', true);\n";
				$content = '';
				break;
			case '6':   // Video Links
				$css = 'video';
				$shell_id = 'pane_' . $css . '_' . $id;
				$content = "\n" . ' this feature not yet implemented ' . "\n";
				break;
			case '7':   // Calendar
				$css = 'calendar';
				$shell_id = 'pane_' . $css . '_' . $id;
				$panes['Onload']    .= "jaxCalendarRegister('../user_events/user_events.php?request=xml', '$shell_id');\n";
				$content = '';
				break;
			case '8':   // Scoreboard
				$css = 'scoreboard';
				$shell_id = 'pane_' . $css . '_' . $id;
				$panes['Onload'] .= "jaxScoreboardOnClickForwardToUrl('../user_scoreboard/user_scoreboard_list.php');\n";
				$panes['Onload'] .= "jaxScoreboardRegister('../user_scoreboard/user_scoreboard.php?request=xml', '$shell_id');\n";
				$content = '';
				break;
			case '9':   // Search
				break;
			case '10':  // Login
				$css = 'login';
				$shell_id = 'pane_' . $css . '_' . $id;
				$panes['Onload']    .= "jaxLoginRegister('../user_login/user_login.php?request=xml', '$shell_id');\n";
				$content = '';
				break;
			case '100': // Raw HTML
				$css = 'raw';
				$content = $row['content'];
				break;
		}
		
		$pane = 'pane_' . $css;
		
		if ($shell_id != '') {
			// Panes with a shell id get an enclosing 'shell' of header, content, and footer.
			$content = 
				"<div class='pane_container " . $pane . "'>\n" . 
				"\t<div class='pane_header " . $pane . "_header'></div>\n" .
				"\t<div class='pane_content " . $pane . "_content' id='" . $shell_id . "'>\n" .
				$content . 
				"\t</div>\n" .
				"\t<div class='pane_footer " . $pane . "_footer'></div>\n" .
				"</div>\n";
		} else {
			// Otherwise the pane is a simple div plus content.
			if ($content != '') {
				$content = 
					"<div class='" . $pane . "' id='" . $pane . "_" . $id . "'>\n" .
					 $content . 
					 "\n</div>\n";
			}
		}
		
		$panes[$row['name']] = $content;
	}

	return $panes;
}

function buildNavbarPages($page_id, $tabs = '') {
	
	// If there's no page id, we're digging around archives.  Give the user a home link.
	if (!isset($page_id)) {
		$link = "<a class=\"navbar_link\" href=\"main.php?page_id=1\">Home</a>";
		$html = $tabs . "\t\t\t\t<div class=\"navbar_child_div\">" . $link . "</div>\n";
		return $html;
	}
	
	// Get database record for current page.
	$row = getPageInfo($page_id);
	$title = $row['title'];
	$parent_id = $row['parent_id'];
	$html = "\n";
	
	// If the parent exists, dig out parent information and create a link for it.
	if ($parent_id > 0) {
		$sql = 'SELECT id, parent_id, title FROM Pages WHERE id = ?';
		$row = databaseGetRow($sql, array($parent_id));
		if ($row) {
			$parent_id = $row['parent_id'];
			$link = "<a class=\"navbar_link\" href=\"main.php?page_id=" . $row['id'] . "\">" . $row['title'] . "</a>";
			$html = $tabs . "\t<div class=\"navbar_div\">$link</div>\n" . $html . "\n";
		}
	}
	
	// Write the current page div, no link.
	$html .= "\t\t\t\t<div class=\"navbar_current_div\">$title</div>\n";
	
	// Write out child links.
	$sql = 'SELECT id, title FROM Pages WHERE parent_id = ? ORDER BY sortorder';
	$rows = databaseGetRows($sql, array($page_id));
	foreach ($rows as $row) {
		$link = "<a class=\"navbar_link\" href=\"main.php?page_id=" . $row['id'] . "\">" . $row['title'] . "</a>";
		$html .= $tabs . "\t\t\t\t<div class=\"navbar_child_div\">" . $link . "</div>\n";
	}

	$html .= $tabs;
	return $html;
}

function buildNavbarStories($page_id, $tabs = '') {
	
	$title = databaseGetValue('SELECT title FROM Pages WHERE id = ?', array($page_id));
	$html  = "\t\t\t\t<div class=\"navbar_current_div\">Stories for page $title</div>\n";
	
	// Write out story links.
	$sql  = 'SELECT s.id, s.title FROM Stories s, PageToStory ps WHERE ps.page_id = ? AND s.id = ps.story_id ORDER BY stamp DESC';
	$rows = databaseGetRows($sql, array($page_id));
	foreach ($rows as $row) {
		$link = "<a class=\"navbar_link\" href=\"main.php?story_id=" . $row['id'] . "\">" . $row['title'] . "</a>";
		$html .= $tabs . "\t\t\t\t<div class=\"navbar_child_div\">" . $link . "</div>\n";
	}

	$html .= $tabs;
	return $html;
}

function buildStoryFromDBRow($db_row, $tabs, $css_prefix = 'story') {
	$subtitle = '';
	if (FLAG_TIMESTAMP == 'Y') {
		$subtitle = databaseFormatToReadableDate($db_row['stamp']);
	}
	return buildStory($db_row['title'], $subtitle, $db_row['content'], $tabs, $css_prefix);
}

function buildStory($title, $subtitle, $content, $tabs, $css_class) {
	
	$html = '';
	$html .= $tabs . "\t" .   '<div class="'  . $css_class . '">' . "\n";
	$html .= $tabs . "\t\t" . '<div class="'  . $css_class . '_title">' . $title . "\n";
	$html .= $tabs . "\t\t" . '<span class="' . $css_class . '_subtitle">' . $subtitle . '</span></div>' . "\n";
	$html .= $tabs . "\t\t" . '<div class="'  . $css_class . '_content">' . $content . '</div>' . "\n";
	$html .= $tabs . "\t\t" . '<div class="'  . $css_class . '_footer"></div>' . "\n";
	$html .= $tabs . "\t" .   '</div>' . "\n";
	
	return $html;
}

?>
Return current item: JaxBlog