<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 5/5/2015	ELL
// 
	include ("check_session.php");
		
	$style_sheet = $_REQUEST['style_sheet'];
	$class_table = $style_sheet . "_classes";

	if (isset($_REQUEST['class_sort_method']) && ($_REQUEST['class_sort_method'] != ""))	{
		$class_sort_method = $_REQUEST['class_sort_method'];
		$_SESSION['class_sort_method'] = $class_sort_method;
	}
	elseif (isset($_SESSION['class_sort_method']))
		$class_sort_method = $_SESSION['class_sort_method'];
	else
		$class_sort_method = 0;
		
	if ($class_sort_method == 1)	{
		$sort = "`class`";
		$sort2 = "`class`";
	}
	else	{
		$sort = "`sort_order`";
		$sort2 = "`subclass_sort_order`, `sort_order`";
	}
			
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `custom` = 1 ORDER BY $sort";
	if ($result = $bwb_dbh->query($query))
	{
		$row_delim = "";
		$classes_custom = "";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$classes_custom .= $row_delim . json_encode($row);
			$row_delim = "@@@@@";
		}
	}
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` ORDER BY $sort";
	if ($result = $bwb_dbh->query($query))
	{
		$row_delim = "";
		$classes_sort_1 = "";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$classes_sort_1 .= $row_delim . json_encode($row);
			$row_delim = "@@@@@";
		}
	}
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` ORDER BY $sort2";
	if ($result = $bwb_dbh->query($query))
	{
		$row_delim = "";
		$classes_sort_2 = "";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$classes_sort_2 .= $row_delim . json_encode($row);
			$row_delim = "@@@@@";
		}
	}
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` ORDER BY `sort_order`";
	if ($result = $bwb_dbh->query($query))
	{
		$row_delim = "";
		$classes_sort_order = "";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$classes_sort_order .= $row_delim . json_encode($row);
			$row_delim = "@@@@@";
		}
	}
		
		$list_delim = ":::::";
		echo $classes_sort_1 . $list_delim . $classes_sort_2 . $list_delim . $classes_sort_order . $list_delim . $classes_custom;
?>