<?php
/**
 * Alkivia Chameleon.
 * Settings Admin.
 *
 * @version		$Rev: 66 $
 * @author		Jordi Canals
 * @package		Themes
 * @subpackage	Chameleon
 * @link		http://alkivia.org/chameleon
 * @license		http://www.gnu.org/licenses/gpl.html GNU General Public License v3

	Copyright 2009 Jordi Canals <alkivia@jcanals.net>

    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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// File cannot be called directly
if (isset($_SERVER['SCRIPT_FILENAME']) && 'settings.php' == basename($_SERVER['SCRIPT_FILENAME'])) {
	die ('Please do not load this page directly. Thanks!');
}

include_once ( dirname(__FILE__) .'/framework/formating.php' );

global $cham_theme_data, $cham_settings; //  = get_option('chameleon_settings');
$cham_styles = akv_dir_content(dirname(__FILE__) . '/styles', 'extensions=css&tree=0&with_ext=0');

$uploads = wp_upload_dir();
$dirname = $uploads['basedir'] . '/alkivia';
$dir_url = $uploads['baseurl'] . '/alkivia';

if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
	check_admin_referer('chameleon-theme-settings');

	if ( isset($_POST['action']) && 'update' == $_POST['action'] ) {
		$cham_settings = stripslashes_deep($_POST['cham_settings']);
		update_option('chameleon_settings', $cham_settings);
		akv_admin_notify();

		// Upload the header file
		include_once ( CHAM_ABSPATH . '/framework/upload.php');
		$handle = new akvUpload($_FILES['header-file'], 'chameleon');
			if ( $handle->uploaded ) {
				$handle->image_resize	= true;
				$handle->image_ratio_y	= true;
				$handle->image_x = 972;

				$handle->file_overwrite		= true;
				$handle->file_auto_rename	= false;
				$handle->file_new_name_body	= 'theme_head';
				$handle->image_convert		= 'jpg';

				$handle->Process($dirname);
				if ( ! $handle->processed ) {
					akv_admin_notify(__('Error', 'chameleon') . ': ' . $handle->error);
				}

				// Process a miniature for this image. This is a banner width image
				$handle->image_resize	= true;
				$handle->image_ratio_y	= true;
				$handle->image_x = 468;

				$handle->file_overwrite		= true;
				$handle->file_auto_rename	= false;
				$handle->file_new_name_body	= 'theme_head_thumb';
				$handle->image_convert		= 'jpg';

				$handle->Process($dirname);
				if ( ! $handle->processed ) {
					akv_admin_notify(__('Error creating thumbnail', 'chameleon') . ': ' . $handle->error);
				}

			}

	} else { // Missing action
		wp_die('Bad form received.', 'chameleon');
	}
}
?>

<div class="wrap">
	<div id="icon-chameleon-admin" class="icon32"></div>
	<h2><?php _e('Chameleon Theme Settings', 'chameleon') ?></h2>

	<table id="akmin">
	<tr>
		<td class="content">
			<form method="post" enctype="multipart/form-data" action="themes.php?page=chameleon">
			<?php wp_nonce_field('chameleon-theme-settings'); ?>
			<fieldset>

			<dl>
				<dt><?php _e('Header Image', 'chameleon'); ?></dt>
				<dd>
					<table width="100%" class="form-table">
					<?php if ( file_exists( $dirname . '/theme_head_thumb.jpg') ) { ?>
					<tr>
						<th scope="row"><?php _e('Current header image:', 'chameleon') ?></th>
						<td><img src="<?php echo $dir_url . '/theme_head_thumb.jpg?' . rand(); ?>" /></td>
					</tr>
					<?php } ?>
					<tr>
						<th scope="row"><label for="header-file"><?php _e('Upload new image:', 'chameleon') ?></label></th>
						<td>
							<input type="file" size="40" name="header-file" value="" /><br />
							<span class="setting-description"><?php _e('The uploaded image will be proportionally resized to 970 pixels wide', 'chameleon')?></span><br />
							<input type="checkbox" name="cham_settings[back_image]" value="1" <?php checked(1, $cham_settings['back_image']);?> /> <?php _e('Use as foreground image. Hide site name and description.', 'chameleon')?><br />
							<span class="setting-description"><?php _e('Using it as background, will display blog name and description over this image.', 'chameleon')?></span>
						</td>
					</tr>
					</table>
				</dd>
			</dl>

			<dl>
				<dt><?php _e('Styles', 'chameleon')?></dt>
				<dd>
					<table width="100%" class="form-table">
					<tr>
						<th scope="row"><?php _e('Style palette:', 'chameleon') ?></th>
						<td>
							<select name="cham_settings[style]" tabindex="1">
							<?php foreach ($cham_styles as $theme_sheet) {
								echo "<option value='{$theme_sheet}'";
								selected($theme_sheet, $cham_settings['style']);
								echo "> " . ucfirst($theme_sheet) . " </option>";
							} ?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e('Show favicon?', 'chameleon') ?></th>
						<td>
							<input type="radio" name="cham_settings[favicon]" value="1"<?php checked('1', $cham_settings['favicon']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
							<input type="radio" name="cham_settings[favicon]" value="0"<?php checked('0', $cham_settings['favicon']); ?> /> <?php _e('No', 'chameleon'); ?>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e('Users can switch styles?', 'chameleon') ?></th>
						<td>
							<input type="radio" name="cham_settings[switch_styles]" value="1"<?php checked('1', $cham_settings['switch_styles']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
							<input type="radio" name="cham_settings[switch_styles]" value="0"<?php checked('0', $cham_settings['switch_styles']); ?> /> <?php _e('No', 'chameleon'); ?>
						</td>
					</tr>
					</table>
				</dd>
			</dl>

			<dl>
				<dt><?php _e('Menu Links', 'chameleon'); ?></dt>
				<dd>
					<table width="100%" class="form-table">


			<tr>
				<th scope="row"><?php _e('Menu Home Link?', 'chameleon') ?></th>
				<td>
					<input type="radio" name="cham_settings[home_link]" value="1"<?php checked('1', $cham_settings['home_link']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
					<input type="radio" name="cham_settings[home_link]" value="0"<?php checked('0', $cham_settings['home_link']); ?> /> <?php _e('No', 'chameleon'); ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php _e('Menu Login/Logout Link?', 'chameleon') ?></th>
				<td>
					<input type="radio" name="cham_settings[login_link]" value="1"<?php checked('1', $cham_settings['login_link']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
					<input type="radio" name="cham_settings[login_link]" value="0"<?php checked('0', $cham_settings['login_link']); ?> /> <?php _e('No', 'chameleon'); ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php _e('Menu Admin/Register Link?', 'chameleon') ?></th>
				<td>
					<input type="radio" name="cham_settings[admin_link]" value="1"<?php checked('1', $cham_settings['admin_link']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
					<input type="radio" name="cham_settings[admin_link]" value="0"<?php checked('0', $cham_settings['admin_link']); ?> /> <?php _e('No', 'chameleon'); ?>
				</td>
			</tr>
			</table>
				</dd>
			</dl>

			<dl>
				<dt><?php _e('Header and Footer', 'chameleon'); ?></dt>
				<dd>
					<table width="100%" class="form-table">
					<tr>
						<th scope="row"><?php _e('Footer Text:', 'chameleon') ?></th>
						<td>
							<textarea rows="4" cols="80" name="cham_settings[footer]"><?php echo $cham_settings['footer']; ?></textarea>
						</td>
					</tr>
					</table>
				</dd>
			</dl>

			<dl>
				<dt><?php _e('Other Options', 'chameleon'); ?></dt>
				<dd>
					<table width="100%" class="form-table">
					<tr>
						<th scope="row"><?php _e('Enable author pages?', 'chameleon') ?></th>
						<td>
							<input type="radio" name="cham_settings[author]" value="1"<?php checked('1', $cham_settings['author']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
							<input type="radio" name="cham_settings[author]" value="0"<?php checked('0', $cham_settings['author']); ?> /> <?php _e('No', 'chameleon'); ?>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e('Enable self pings?', 'chameleon') ?></th>
						<td>
							<input type="radio" name="cham_settings[self_ping]" value="1"<?php checked('1', $cham_settings['self_ping']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
							<input type="radio" name="cham_settings[self_ping]" value="0"<?php checked('0', $cham_settings['self_ping']); ?> /> <?php _e('No', 'chameleon'); ?>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e('Comments on pages?', 'chameleon') ?></th>
						<td>
							<input type="radio" name="cham_settings[page_comments]" value="1"<?php checked('1', $cham_settings['page_comments']); ?> /> <?php _e('Yes', 'chameleon'); ?> &nbsp;&nbsp;
							<input type="radio" name="cham_settings[page_comments]" value="0"<?php checked('0', $cham_settings['page_comments']); ?> /> <?php _e('No', 'chameleon'); ?>
						</td>
					</tr>
					</table>
				</dd>
			</dl>

			<p class="submit">
				<input type="hidden" name="action" value="update" />
				<input type="submit" name="Submit" value="<?php _e('Save Changes', 'chameleon') ?>" class="button-primary" />
			</p>
		</fieldset>
		</form>

		<p class="footer"><a href="<?php echo $cham_theme_data['URI']; ?>"><?php echo $cham_theme_data['Name'] . ' ' . $cham_theme_data['Version']?></a> &nbsp;
			&copy; Copyright 2009 <?php echo $cham_theme_data['Author']?></p>
		</td>
		<td class="sidebar">
			<dl>
				<dt>Alkivia Chameleon</dt>
				<dd>
					<ul>
						<li><a href="http://alkivia.org/chameleon" class="theme-home"><?php _e('Theme Homepage', 'chameleon'); ?></a></li>
						<li><a href="http://wordpress.org/tags/alkivia-chameleon?forum_id=5" class="support-forum"><?php _e('Support Forum', 'chameleon'); ?></a></li>
						<li><a href="http://alkivia.org" class="ak-home"><?php _e('Author Homepage', 'chameleon')?></a></li>
						<li><a href="http://alkivia.org/donate" class="donate"><?php _e('Help donating', 'chameleon')?></a></li>
					</ul>
				</dd>
			</dl>
		</td>
	</tr>
	</table>
</div>
