<?php
/*
Plugin Name: skin-editor
Plugin URI: http://www.dennisonwolfe.com/
Description: This editor creates a unique custom folder for every new site created on wpmu.
		The custom folder is create in (\wp-content\themes\thesis_[version]\custom\skins) and it\'s name is the same as the site name.
		You can use this folder to customize the site without affecting the other sites
Version: 1.0.0
Author: Dennison+Wolfe Internet Group
Author URI: http://www.dennisonwolfe.com/
*/

/*  Copyright 2010  Dennison+Wolfe Internet Group  (email : tyler@dennisonwolfe.com)

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

include('D:\xampp\htdocs\dwdining\wp-content\info\debug.php');

//=== get path ===== debugger($wpdb->dmtable);
	$site_url = get_bloginfo('url');
	define('CUSTOM_DIR_LOC', get_bloginfo('template_url') . '/custom');
	$endIndex = strpos(dirname(__FILE__), 'wp-content', 0);
	$initialPath = substr(dirname(__FILE__), 0, $endIndex);
	$endIndex = strpos(CUSTOM_DIR_LOC, 'wp-content', 0);
	$midPath =  substr(CUSTOM_DIR_LOC, $endIndex, strlen(CUSTOM_DIR_LOC));
	$midPath = str_replace('/','\\',$midPath);
	$finalPath = $initialPath.$midPath;
	
include($initialPath.'wp-content\plugins\skin-editor\skin-functions.php');

if ( is_admin() ) {
	//plugin activation
	//add_action('activate_wp3-iis/skin-editor.php', 'wp3iis_init');
    register_activation_hook( __FILE__, 'skineditor_init' );
	//settings menu
	add_action('admin_menu', 'skineditor_tools_menu');
	//load css
	add_action('admin_print_styles', 'skineditor_load_stylesheets' );
}
else{
	//load css
	add_action('wp_head', 'skineditor_load_stylesheets');
}

/* Load css files*/
function skineditor_load_stylesheets() {
	$style_file = plugins_url('skin-editor/skin-editor.css');
	echo '<link rel="stylesheet" type="text/css" href="' . $style_file . '" />' . "\r\n";
}

/* Configuration Screen*/
function skineditor_tools_menu() {
	add_submenu_page( 'themes.php', 'Skin editor', 'Skin editor', 'manage_options', 'Skin-editor/Skin-editor.php', 'skin_forms');
	//add_submenu_page('thesis-options', "Manage Skins", "Manage Skins", 'edit_themes', basename(__FILE__), 'skins_admin');
	//call register settings function
	// add_action( 'admin_init', 'register_wp3iis_settings' );
	$plugin = plugin_basename(__FILE__); 
	add_filter( 'plugin_action_links_' . $plugin, 'skineditor_plugin_actions' );
}

/* Add Settings link to the plugins page*/
function skineditor_plugin_actions($links) {
    $settings_link = '<a href="themes.php?page=Skin-editor/Skin-editor.php">Settings</a>';

    $links = array_merge( array($settings_link), $links);

    return $links;
}

function try_create_table() {
global $wpdb;
	$wpdb->dmtable = $wpdb->base_prefix . 'sw_skin';
	$wpdb->dmtable_sub_theme = $wpdb->base_prefix . 'sw_sub_theme';
	$wpdb->dmtable_sub_theme_variation = $wpdb->base_prefix . 'sw_sub_theme_variation';
	if ( is_site_admin() ) {
		$created = 0;
		if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable}'") != $wpdb->dmtable ) {
			$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable}` (
				`id` bigint(20) NOT NULL auto_increment,
				`site_url` varchar(255) NOT NULL ,
				`domain` varchar(255) NOT NULL,
				`sub_theme` varchar(255) NOT NULL,
				`sub_theme_variation` varchar(255) ,
				`site_level_css` varchar(255),
				`active` tinyint(1) default '0',
				`folder_path` varchar(255) NOT NULL,
				`site_id` bigint(20) NOT NULL,
				PRIMARY KEY  (`id`)
			);" );			
			
			$created = 1;
		}
		if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable_sub_theme}'") != $wpdb->dmtable_sub_theme ) {
			$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable_sub_theme}` (
				`id` bigint(20) NOT NULL auto_increment,
				`sub_theme` varchar(255) NOT NULL,
				PRIMARY KEY  (`id`)
			);" );		
			
			$created = 2;
		}
		if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable_sub_theme_variation}'") != $wpdb->dmtable_sub_theme_variation ) {
			$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable_sub_theme_variation}` (
				`id` bigint(20) NOT NULL auto_increment,
				`sub_theme` varchar(255) NOT NULL,
				`sub_theme_variation` varchar(255) NOT NULL,
				PRIMARY KEY  (`id`)
			);" );		
			
			$created = 3;
		}
		
		if ( $created ) {
			
		}
	}
}

/* initialize the plugin settings*/
function skineditor_init() {	
	try_create_table();	
}


function skin_forms( ) {
	?>
	<style type="text/css">
					
	</style>
	<div id="wp3iis-admin" class="wrap">
            <?php screen_icon(); ?>
		<p>	
		<form id="skin-sub-theme">
			<table class='widefat skins'>
				<thead>
					<tr>
						<th width='120px'> Add Sub Theme: <small>for (thesis 1.7 theme)</small></th>
						<th ></th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td width='280px'>
							<input type="text" id="txtSubTheme" name="txtSubTheme" value="type here then click on Add Sub Theme" style="width:100%;"  
							onfocus="if (this.value == 'type here then click on Add Sub Theme') {this.value = '';}" 
							onblur="if (this.value == '') {this.value = 'type here then click on Add Sub Theme';}"/>
						</td>
						<td align='right'>		
							<div onclick="execute_at_server('<?php bloginfo( "wpurl" ); ?>');" onmouseover="this.style.cursor='pointer'" id="btSubTheme" name="btSubTheme" class="button" style="width:93px; margin-top:1px;">
								Add Sub Theme
							</div>
						</td>
					</tr>		
				</tbody>
			</table>
		</form>
		</p>
		<div id="info_message"></div>		
		<input type="hidden" name="action" value="saveFiles"/>		
		<p>				
		<table class='widefat skins' cellspacing= '10px'>
			<thead>
				<tr>
					<th width='120px'> Add Variations: <small>for a Sub Theme</small></th>
					<th ></th>
				</tr>
			</thead>
			<tbody>
				<tr >
					<td width='280px'>
						existing sub themes
					</td>
					<td >		
						existing variations
					</td>
				</tr>				
				<tr>
					<td width='280px'>
						<select id="ddlSubThemes" name="ddlSubThemes" class="widefat" style="width:100%;">
							<option >sub Themes</option>
							<option >sub Themes</option>
						</select>
					</td>
					<td >		
						<select id="ddlSubDomains" name="ddlSubDomains" class="widefat" >
							<option >variations</option>
							<option >variations</option>
						</select>
					</td>
				</tr>
				<tr >
					<td width='280px'>
						
					</td>
					<td >		
						
					</td>
				</tr>
				<tr>
					<td width='280px'>
						<input type="text" id="txtVariations" name="txtVariations" value="type here then click on Add Variations" style="width:100%;"  
						onfocus="if (this.value == 'type here then click on Add Variations') {this.value = '';}" 
						onblur="if (this.value == '') {this.value = 'type here then click on Add Variations';}"/>
					</td>
					<td align='right'>		
						<div onmouseover="this.style.cursor='hand'" id="btVariations" name="btVariations" class="button" style="width:93px; margin-top:1px;">
							Add Variations
						</div>
					</td>
				</tr>		
			</tbody>
		</table>									
		</p>
		<p>				
		<table class='widefat skins'>
			<thead>
				<tr>
					<th width='120px'> Activate Variation for site: </th>
					<th ></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td width='280px'>
						<input type="text" id="txtSubTheme" name="txtSubTheme" value="click" style="width:100%;"  
						onfocus="if (this.value == 'click') {this.value = '';}" 
						onblur="if (this.value == '') {this.value = 'click';}"/>
					</td>
					<td align='right'>		
						<div onmouseover="this.style.cursor='hand'" id="btSubTheme" name="btSubTheme" class="button" style="width:50px; margin-top:1px;">
							Activate
						</div>
					</td>
				</tr>		
			</tbody>
		</table>								
		</p>
	</div>
<?php
		//add data returning ajax refresh table function
	add_action('wp_ajax_skineditor_savefile', 'saveFiles', 10);
	}
?>