<?php

/*

___Canvas Core Functions________________________________________

Core functions for generating the Canvas GUI
and manipulating layout contents.

________________________________________________________________

*/



require_once('canvas-globals.php');
require_once('canvas-install.php');
require_once('canvas-xml.php');
require_once('canvas-import_functions.php');
require_once('canvas-admin.php');
require_once('Ink/ink.php');

define(MODULE_TEMPLATE_SIZE_SMALL, -1);
define(MODULE_TEMPLATE_SIZE_MEDIUM, -2);
define(MODULE_TEMPLATE_SIZE_LARGE, -3);
define(MODULE_TEMPLATE_SIZE_FULL, -4);



function canvas_build_canvas($curr_page) {
	global $current_zones, $wpdb, $canvas;
	if(isset($_GET["template"])) $template = ABSPATH.'wp-content/themes/'.$_GET["template"];
		else $template = TEMPLATEPATH;
 	if(file_exists($template . "/canvas.php")) {
			
			if(get_option('canvas_auto_publish') != 'true') echo '<div id="publish"><a href="javascript:void(0)"><img src="'.CANVASURI.'images/publish.gif" title="Publish Your Changes" alt="Publish Changes" id="publish_image" /></a></div>';
				else echo '<div id="publish" class="publish_nobutton"></div>';
			echo
<<<EOF
			<a id="publish" href="?page=FlutterEditCanvas&defaultmods=1">Load all Default Modules</a>
			<a id="publish" href="?page=FlutterEditCanvas&resetmods=1" onclick="return confirmBeforeReset();">Reset Modules</a>
			<script type="text/javascript" language="javascript">
				function confirmBeforeReset()
				{
					return confirm("You're about put all your modules back in the shelf. Are you sure you want to do this?");
				}
			</script>
EOF;
		
			include($template . "/canvas.php");

			//Put modules whose canvas no more exists back to the shelf
			$currDBModules = $wpdb->get_results("SELECT block_id, zone FROM ".$canvas->main." WHERE zone <> 'shelf' AND theme = '".get_option('template')."' AND page='$curr_page'"); 
			$updated = false;
			foreach ($currDBModules as $currDBModule){
				$found = false;
				foreach($current_zones as $current_zone){
					if ($currDBModule->zone == $current_zone) $found = true;
				}
				if (!$found){
					$updated = true;
					$wpdb->query("UPDATE ".$canvas->main." SET zone = 'shelf' WHERE block_id = '".$currDBModule->block_id."'");
				}
			}
			if ($updated) {
				wp_redirect("?page=FlutterEditCanvas");
				return;
			}
			
			echo '<br />'; 
		} else { canvas_no_theme(); }
}

function canvas_check_theme() {
	global $wpdb, $canvas, $table_prefix;
	$table = $table_prefix . 'ink';
	$theme = get_option('template');

	$filename = get_template_directory().'/canvas.php';

	// Install Canvas
	if(!$wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE theme = '".get_option('template')."'")){ 
		//canvas_install();
		if(file_exists($filename)) {
			if(!get_option('canvas_first_install')) update_option('canvas_first_install', time());
			if($wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE theme = '".get_option('template')."'") && !$wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE zone != 'shelf' AND theme = '".get_option('template')."'")) {
				$xmlfile = get_template_directory().'/canvas.xml';
				if(file_exists($xmlfile)) canvas_import_layout($xmlfile);
			}
		}
	}

	// Install Ink
	if (!$wpdb->get_results("SELECT * FROM ".$table." WHERE theme = '$theme'")) {
		if(file_exists($filename)) {
			$file_data = implode('', file($filename));
			if(preg_match("|Ink Elements:(.*)|i", $file_data, $ink_string)) $ink_string = trim($ink_string[1]);
			if(function_exists('ink_elements')) ink_elements($ink_string);
		}
	}

}

function canvas_create_draggable($block_id, $zone, $position, $module_name, $module_title, $content, $type, $group, $template_size, $template_name) {

	// Get plugin sizes
	$moduleTemplatesFolder = dirname(__FILE__)."/modules/".$module_name."/templates";
	$moduleTemplateFolder = dirname(__FILE__)."/modules/".$module_name."/templates/default";

	if ($zone=="shelf") $template_name="";


	$module_title_min = $module_title;
	if (strlen($module_title) > 10 ){	
		$module_title_min = substr($module_title,0,10);//."...";
	}

	$class = 'container';
	if($type == 'plugin') $class = 'container-plugin';
	$class .= ' '.strtolower($group);
	echo '<div class="'.$class.'" id="canvas_'.$zone.'_'.$position.'_'.$block_id.'" title="'.strip_tags($module_title." - ".$content).'">'."\n";
	echo '<h6 class="handle"><span>'.$module_title_min.'</span></h6>'."\n";
	$module_size_id = 'canvas_'.$zone.'_'.$position.'_'.$block_id.'_module_size';
	$module_name_id = 'canvas_'.$zone.'_'.$position.'_'.$block_id.'_module_name';
	$module_template_name_id = 'canvas_'.$zone.'_'.$position.'_'.$block_id.'_module_template_name';
//echo($template_size);
	

	echo 
<<<EOF
	<!--<div id="module_size_select" class="module_size_select">
	
	<select name="$module_size_id" id="$module_size_id" style="height:18px;font-size:11px;" onchange="Canvas.changePublishImg();">
		$otherSizesStr
	</select>
	<select name="$module_template_name_id" id="$module_template_name_id" style="height:18px;font-size:11px;"  onchange="Canvas.changePublishImg();">
		$templatesNamesStr
	</select>
	</div>--->
	<input type="hidden" name="$module_size_id" id="$module_size_id" value="$template_size" />
	<input type="hidden" name="$module_template_name_id" id="$module_template_name_id" value="$template_name" />	
	<input id="$module_name_id" type="hidden" value="$module_name"/>
EOF;
	echo canvas_create_plugin_form($zone, $position, $block_id, $type)."\n";
	echo '<div class="content">'.$content."</div>\n";

 	echo '</div>'."\n";
}

function canvas_create_management_block($block,$style='',$delete_button='true') {
	if ($block->is_original) $delete_button='false';

	if($block->uri != '') $link = '<a href="'.$block->uri.'">'.$block->author.'</a>';
		else $link = $block->author;
	if($block->type == 'plugin') $class = ' class="plugin '.strtolower($block->block_group).'"';
		else $class = ' class="'.strtolower($block->block_group).'"';
	echo "\t".'<li id="block_'.$block->block_id.'"'.$class.$style.'>';
	if($delete_button == 'true') echo '<a class="delete_block" id="'.$block->block_id.'" href="javascript:void(0)"><img src="'.CANVASURI.'images/delete.png" alt="Delete" title="Delete block"/></a>';
	echo '<a class="duplicate_block" id="'.$block->block_id.'" href="javascript:void(0)"><img src="'.CANVASURI.'images/duplicate.png" alt="Duplicate" title="Duplicate block"/></a>';
	echo '<a class="block_info" id="'.$block->block_id.'" href="javascript:void(0)"><img src="'.CANVASURI.'images/block_info.png" alt="Info" title="Block information"/></a>';
	if ($block->is_original)
		echo '<h3><span id="title_'.$block->block_id.'">'.$block->module_title.'</span> by '.$link.'.</h3><p>'.wptexturize($block->description).'</p>';
	else
		echo '<h3><span class="title" id="title_'.$block->block_id.'">'.$block->module_title.'</span> by '.$link.'.</h3><p>'.wptexturize($block->description).'</p>';
	echo '<div class="info" id="info_'.$block->block_id.'" style="display: none;"><div>'.
			'<strong>Module Folder:</strong> modules/'.$block->module_name.
			'<br/><strong>Module ID:</strong> '.$block->block_id.
			'<br/><strong>Path:</strong> '.$block->path.
			'<br/><strong>Zone:</strong> '.$block->zone.
			'<br/><strong>Position:</strong> '.$block->position.
			'<br/><strong>Author URI:</strong> '.$block->uri.'</div></div>';
	echo '</li>'."\n";
}

function canvas_create_plugin_form($zone,$position,$block_id,$block_type) {
	if($block_type == 'plugin'){
		echo '<a href="'.CANVASURI.'canvas-plugin_form.php?zone='.$zone.'&position='.$position.'&block_id='.$block_id.'" class="lbOn"><img class="lbLink" src="'.CANVASURI.'images/plugin_options.gif" title="Module Settings" alt="Module Settings"/></a>';
		echo '<a href="'.CANVASURI.'canvas-plugin_info.php?block_id='.$block_id.'" class="lbOn"><img class="lbLinkInfo" src="'.CANVASURI.'images/block_info.png" title="Module Information" alt="Module Information"/></a>';
	}
}



function canvas_database_plugin($matches, $type = 'select', $variable_value, $where_cond = '') {
	global $wpdb, $canvas;
	$ALLOW_TABLES = array("sitecategories");

	$content = '';
	if(!isset($matches[3])) $matches[3] = $matches[2];
	if ($where_cond != '') {
		$where_cond = str_replace('%prefix%', $wpdb->prefix, $where_cond);
		$where_cond = " WHERE " . $where_cond;
	}


	// We need to find out whether the table name requires user prefix or global prefix
	$req_table_prefix = $wpdb->prefix;
	if (!$wpdb->get_var("SHOW TABLES LIKE '".$req_table_prefix.$matches[1]."'") == $req_table_prefix.$matches[1]){
		if (in_array($matches[1], $ALLOW_TABLES))
			$req_table_prefix = (isset($wpdb->base_prefix)?$wpdb->base_prefix:$wpdb->prefix);
	}
	

	if($wpdb->get_var("SHOW TABLES LIKE '".$req_table_prefix.$matches[1]."'") == $req_table_prefix.$matches[1]) {
		//echo("<br/>SELECT ".$matches[2].", ".$matches[3]." FROM ".$req_table_prefix.$matches[1]."  TBL ".$where_cond);
		if($data = $wpdb->get_results("SELECT ".$matches[2].", ".$matches[3]." FROM ".$req_table_prefix.$matches[1]."  TBL ".$where_cond)) {
			foreach($data as $row) {
				if($type == 'select') {
					$content .= '<option value="'.$row->$matches[3].'" ';
					if ($row->$matches[3] == $variable_value) $content .= ' selected="selected" ';
					$content .= '>&nbsp;'.$row->$matches[2]."</option>\n";
				} elseif($type == 'radio') {
					$content .= '<label><input type="radio" value="'.$row->$matches[3].'" ';
					if ($row->$matches[3] == $variable_value) $content .= ' checked="checked" ';
					$content .= '/>&nbsp;'.$row->$matches[2].'</label>';
				}
			}
		}
	}
	
	return $content;
}


/**
*	
*	@param $zone_name (String) The zone name
*	@param $template_name (String) The name of the template to grab. The default value is "default".
*	@param $template_size (Integer) Grabs the specified version of the template. The default value is MODULE_TEMPLATE_SIZE_SMALL. The value of $template_size could be 
*		- MODULE_TEMPLATE_SIZE_SMALL, MODULE_TEMPLATE_SIZE_MEDIUM, MODULE_TEMPLATE_SIZE_LARGE, or MODULE_TEMPLATE_SIZE_FULL. 
*		- The maximum width of the module that can be placed in this canvas.
*	@param $allow_other_sizes (Boolean) If you tried to put a version of a module larger the zone version, and this value is set to True, it will just give a warning. Otherwise, it will give error. The default value is true.
*	@param $default_module (String) This will bring up a certain module by default.
*	@param $allow_other_modules (Boolean) If set to True, then the admin can put another module in the space. Otherwise, you shouldn’t be able to move the module. The default value is true.
*	@param $styles (String) A string that will be printed in the style attribute of the canvas div.  
**/

function canvas_define_zone($zone_name, $template_name = "default", $template_size = MODULE_TEMPLATE_SIZE_SMALL, $allow_other_sizes = TRUE, $default_module = '', $allow_other_modules = TRUE, $styles='') {
	$parent='';
	global $zones, $parents, $current_zones;
	$current_zones[] = $zone_name;
	if($_GET["template"] != '') $theme = $_GET["template"];
		else $theme = get_option('template');
	$parents[] = $parent;
	$zones[] = $zone_name;
	echo '<div class="canvas_droppable_zone '.$zone_name.'" id="'.$zone_name.'"';
	if($styles != '') echo ' style="'.$styles.'"';
	echo '>'."\n";
	get_canvas_draggables($zone_name, $theme);
	echo '<input type="hidden" id="'.$zone_name.'_template_name" value="'.$template_name.'" />';
	echo '<input type="hidden" id="'.$zone_name.'_template_size" value="'.$template_size.'" />';
	echo '<input type="hidden" id="'.$zone_name.'_allow_other_sizes" value="'.$allow_other_sizes.'" />';
	echo '<input type="hidden" id="'.$zone_name.'_allow_other_modules" value="'.$allow_other_modules.'" />';
	echo '<input type="hidden" id="'.$zone_name.'_default_module" value="'.$default_module.'" />';
	echo '</div>'."\n";
}

function canvas_execute_function($function_name, $block_id) {
	global $wpdb, $canvas;
	if($var_data = $wpdb->get_results("SELECT variable_name, value, type FROM ".$canvas->variables." WHERE parent = '$block_id'")) {
		$url_string = '';
		$vars = array();
		if (!empty($var_data)) {
		  foreach($var_data as $var) {
			$url_string .= '&'.$var->variable_name.'='.$var->value;
			//if ($function_name)
			//	$vars[str_replace($function_name.'_', '', $var->variable_name)] = $var->value;
			//else
			switch($var->type){
				case 'fileupload':
					$vars[$var->variable_name] = FLUTTER_URI.'files_flutter/'.$var->value;
					break;
				default:
					$vars[$var->variable_name] = $var->value;
			}
		  }
		

		  $url_string = str_replace($function_name.'_', '', $url_string);
		  $url_string .= '&block_id='.$block_id;
		}
		$url_string = substr($url_string, 1);
		if(is_callable($function_name)) $function_name($url_string, $vars);
	}
	return $vars;
}

function canvas_getNodeByAttr($attr, $nodeID, $xmldoc) {
	if ($xmldoc->hasChildNodes()) {
		$children = $xmldoc->childNodes;
		foreach($children as $node) {
			if($node->hasAttributes()) {
				$attributes = $node->attributes;
				foreach ($attributes as $attribute) {
					if(($attribute->name == $attr) && (stripos($attribute->value, $nodeID)) !== false) {
						return $node;
					} else {
						if(canvas_getNodeByAttr($attr,$nodeID,$node))
							return canvas_getNodeByAttr($attr,$nodeID,$node);
					}
				}
			} else {
				if(canvas_getNodeByAttr($attr,$nodeID,$node))
					return canvas_getNodeByAttr($attr,$nodeID,$node);
			}
		}
	} else {
		return null;
	}
}

function canvas_head() {
	global $installed, $canvas, $wpdb;
	$load_default_modules = 0;

	if($_GET["resetmods"]){
		$wpdb->query("UPDATE ".$canvas->main." SET zone = 'shelf', template_name='', template_size=''");
		$wpdb->query("UPDATE ".$canvas->variables." SET default_value = '__RESETDEFAULT__'");
		echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=?page=FlutterEditCanvas&resetmodsmsg=1">';
		//wp_redirect("?page=FlutterEditCanvas&resetmodsmsg=1");
		die();
	}

	if ($wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE theme = '".get_option('template')."' AND zone <>'shelf'")) {
		$load_default_modules = 0;
	}
	else{
		$load_default_modules = 1;
	}
	if(isset($_GET["defaultmods"])) $load_default_modules = 1; 

	if($_GET["page"] == 'FlutterEditCanvas' || $_GET["page"] == 'FlutterInk') : 


					?>
<link rel="stylesheet" href="<?php echo CANVASURI; ?>stylesheet.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php echo CANVASURI; ?>lightbox.css" type="text/css" media="screen" />
		<?php if(!isset($_GET["template"])) : ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_directory') ?>/canvas.css" type="text/css" media="screen" />
		<?php else : ?>
<link rel="stylesheet" href="<?php echo get_bloginfo('wpurl').'/wp-content/themes/'.$_GET["template"]; ?>/canvas.css" type="text/css" media="screen" />
		<?php endif; ?>
<script language="JavaScript" type="text/javascript" > 
	JS_CANVASURI="<?php echo CANVASURI; ?>";

	// The following piece of code checks whether 'www' is included in teh original URL or not,
	// The AJAX js must be called from exactl same URL, otherwise the browser will prevent it 
	// for security reason (calling a URL from another place).
	var originalHost = location.host;
	if (originalHost.substring(0,4) == "www."){
		if (JS_CANVASURI.indexOf("://www.") == -1)
			alert("The registered URL in wordpress does not have a 'www', please remove 'www' from the URL.");
	}
	else {
		if (JS_CANVASURI.indexOf("://www.") > -1)
			alert("The registered URL in wordpress has a 'www', please add 'www' to the URL.");
	}


	LOAD_DEFAULT_MODULES=<?php echo $load_default_modules; ?>;
</script>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/prototype.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/scriptaculous/scriptaculous.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/lightbox.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/canvas-ajax.js.php"></script>
	<?php endif;
	if($_GET["content"] == 'manage') : ?>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/canvas-management.js"></script>
	<?php endif;
	if($_GET["content"] == 'options') : ?>
<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/canvas-options.js"></script>
	<?php endif;
}

function canvas_include_block($filename) {
	include($filename);
}

function canvas_interrupt() {
	if(file_exists(get_template_directory().'/canvas.php'))
		ob_start();
}

function canvas_no_theme() {
	$theme = current_theme_info();
	echo '<div class="wrap"><h2>Theme Not Supported</h2><p>The theme you are currently using ('.$theme->title.' by '.$theme->author.') doesn\'t seem to support Canvas. At this point in the game, Canvas only supports themes that are built to run with the Canvas layout and styling system. To use Canvas, you\'ll have to either <a href="themes.php">select</a> a different theme from your collection.</p>';
	$themes = get_themes();
	$theme_names = array_keys($themes);
	natcasesort($theme_names);
	$canvas_themes = array();
	foreach($theme_names as $index => $theme_name) {
		$stylesheet_dir = $themes[$theme_name]['Stylesheet Dir'];
		if(file_exists(ABSPATH . $stylesheet_dir . '/canvas.php')) {
			$canvas_themes[$index]['title'] = $themes[$theme_name]['Title'];
			$canvas_themes[$index]['template'] = $themes[$theme_name]['Template'];
			$canvas_themes[$index]['directory'] = $stylesheet_dir;
		}
	}
	if(!empty($canvas_themes)) {
		echo '<p>Although your current theme isn\'t compatible, you do have some Canvas compatible themes available. Select a theme from the listing below to edit an inactive theme.</p>'."\n";
		foreach($canvas_themes as $theme) {
			$link = 'themes.php?page=Main.php&amp;template='.$theme['template'];
			echo '<h3><p><a href="'.$link.'">&raquo; '.$theme['title'].'</a></p></h3>'."\n";
		}
	}
	echo '</div>'."\n";
}

function canvas_show_zone_options($zone, $theme) {
	$content_position = canvas_zone_handler($zone);
	
	if(true) {return; $display = ' style="display: none;"'; $toggle_style = ' style="background-color: #cfcfcf;"'; }
		else { $display = ''; $toggle_style = ''; }
	$content = '<h8 id="togglezoneoption_'.$zone.'"'.$toggle_style.'></h8>'."\n";
	$content .= '<span class="canvas_zone_options" id="options_'.$zone.'"'.$display.'>'."\n";
	$content .= '<select class="zone_content_options" id="zoneoption_'.$zone.'" name="zoneoption_'.$zone.'">'."\n";
	$content .= '<option value="before">Insert blocks before original content.'."\n";
	$content .= '<option value="append">Insert blocks after original content.'."\n";
	$content .= '<option value="replace">Replace original content.'."\n";
	$content .= '</select>'."\n";
	$content .= '<img src="'.CANVASURI.'images/replace_up.png" title="Replace original content" onclick="Canvas.zoneHandler(\''.$zone.'\', \'replace\')" />';
	$content .= '<img src="'.CANVASURI.'images/append_up.png" title="Insert after original content" onclick="Canvas.zoneHandler(\''.$zone.'\', \'append\')" />';
	$content .= '<img src="'.CANVASURI.'images/before_up.png" title="Insert before original content" onclick="Canvas.zoneHandler(\''.$zone.'\', \'before\')" />';
	$content .= '</span>'."\n";
	if($content_position != '') $content = str_replace($content_position.'_up', $content_position.'_down', $content);
	$content = str_replace('value="'.$content_position.'"', 'value="'.$content_position.'" selected="selected"', $content);

	echo $content;
}

function mod_var($var_name){
	global $mod_vars; 
	//return $mod_vars[$function_name][$var_name];
	return $mod_vars[$var_name];
}

/**
*	Used inside any theme file in order to display a canvas whenever this page is displayed.
*	
**/
function canvas_zone($zone_name) {
	global $wpdb, $canvas;
	if($zone_data = $wpdb->get_results("SELECT module_id, block_id, path, type, position, zone, template_name, template_size FROM ".$canvas->main." WHERE zone = '$zone_name' AND theme = '".get_option('template')."' ORDER BY position")) {
		foreach($zone_data as $zone) {

			// Get module name
			include_once('RCCWP_CustomWriteModule.php');
			$customWriteModule = RCCWP_CustomWriteModule::Get($zone->module_id);
			$zone->module_name = $customWriteModule->name;

			$dir = get_module_template_folder($zone->module_name, $zone->template_name, $zone->template_size);
			global $mod_vars;
			$mod_vars = canvas_execute_function($zone->path, $zone->block_id);
			if (!file_exists($dir.'default.php')) return;
			include($dir.'default.php');

		}
	}
}


/**
*	Used inside any theme file in order to grab the specified module directly without the need to define it in the canvas.php.
*	
*	@param $module_name (String) The zone name
*	@param $template_name (String) The name of the template to grab. The default value is "default".
*	@param $template_size (Integer) Grabs the specified version of the template. The default value is "small".
**/

function get_module($module_name, $template_name = "default", $template_size = MODULE_TEMPLATE_SIZE_SMALL) {
	global $wpdb, $canvas;
	
	// Load variables
	global $mod_vars;
	include_once("RCCWP_CustomWriteModule.php");
	$mod = RCCWP_CustomWriteModule::GetByName($module_name);
	$mod_vars = canvas_execute_function('', $mod->id);

	// Load template file
	$dir = get_module_template_folder($module_name, $template_name, $template_size)  ;
	if (!file_exists($dir.'default.php')) return;
	include($dir.'default.php');
}


function get_module_template_folder($module_name, $template_name, $template_size) {
	switch($template_size){
		case MODULE_TEMPLATE_SIZE_SMALL:
			$template_size = "small";
			break;
		case MODULE_TEMPLATE_SIZE_MEDIUM:
			$template_size = "medium";
			break;
		case MODULE_TEMPLATE_SIZE_LARGE:
			$template_size = "large";
			break;
		case MODULE_TEMPLATE_SIZE_FULL:
			$template_size = "full";
			break;
	}

	if (empty($template_size))
		$template_size = "small";

	if (empty($template_name))
		$template_name = "default";

	return CANVASPATH.'/modules/'.$module_name.'/templates/'.$template_name.'/'.$template_size.'/'  ;
}

function canvas_zone_handler($zone) {
	global $wpdb, $canvas;
	$position = $wpdb->get_var("SELECT value FROM ".$canvas->zone_options." WHERE option_name = 'zone_handler' AND zone = '$zone' AND theme = '".get_option('template')."'");
	if($position != '') return $position;
		else return 'before';
}

function get_canvas_draggables($zone, $theme = '') {
	include_once('RCCWP_Options.php');

	if (isset($_GET['t_page'])) 
		$curr_page = $_GET['t_page'];
	else
		$curr_page = 'home'; // Attempt to find the main page
	
	if(RCCWP_Options::Get('canvas_show_zone_name') && $zone != 'shelf') echo '<strong>'.$zone.'</strong>';
	if($zone != 'shelf') canvas_show_zone_options($zone, $theme);
	if($theme == '') $theme = get_option('template');
	global $wpdb, $canvas; 
	if($wpdb->get_var("SHOW TABLES LIKE '".$canvas->main."'") == $canvas->main)
		$results = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE theme = '$theme' AND page='$curr_page' AND zone LIKE '$zone' ORDER BY position");

    	if ($results) {
        	foreach($results as $result) {
			// Get module name
			include_once('RCCWP_CustomWriteModule.php');
			$customWriteModule = RCCWP_CustomWriteModule::Get($result->module_id);
			$result->module_name = $customWriteModule->name;
			$result->description = $customWriteModule->description;
   			if ($result->duplicate_id ==0 )
				$result->module_title = $customWriteModule->name;
			else
				$result->module_title = $wpdb->get_var("SELECT duplicate_name FROM ".$canvas->duplicates." WHERE duplicate_id = ".$result->duplicate_id);
			canvas_create_draggable($result->block_id, $zone, $result->position, $result->module_name, $result->module_title, '<p>'.$result->description.'</p>', $result->type, $result->block_group, $result->template_size, $result->template_name);
        	}
      	}
}


if(!function_exists('htmlspecialchars_decode')) {
	function htmlspecialchars_decode($str, $quote_style = ENT_COMPAT) {
		return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
	}
}





?>
