function sunshine_flat_rate_options($options)
{
    $options[] = array('name' => 'Flat Rate', 'type' => 'title', 'desc' => '');
    $options[] = array('name' => __('Enable Flat Rate Shipping', 'sunshine'), 'id' => 'flat_rate_active', 'type' => 'checkbox', 'options' => array(1));
    $options[] = array('name' => __('Name', 'sunshine'), 'id' => 'flat_rate_name', 'type' => 'text');
    $options[] = array('name' => __('Flat Rate Shipping Cost', 'sunshine') . ' (' . sunshine_currency_symbol() . ')', 'id' => 'flat_rate_cost', 'type' => 'text', 'css' => 'width: 50px;');
    $options[] = array('name' => __('Taxable', 'sunshine'), 'id' => 'flat_rate_taxable', 'type' => 'checkbox', 'options' => array(1));
    return $options;
}
function sunshine_pickup_options($options)
{
    $options[] = array('name' => __('Pickup', 'sunshine'), 'type' => 'title', 'desc' => '');
    $options[] = array('name' => __('Enable Pickup Shipping', 'sunshine'), 'id' => 'pickup_active', 'type' => 'checkbox', 'options' => array(1));
    $options[] = array('name' => __('Name', 'sunshine'), 'id' => 'pickup_name', 'type' => 'text');
    $options[] = array('name' => __('Pickup Shipping Cost', 'sunshine') . ' (' . sunshine_currency_symbol() . ')', 'id' => 'pickup_cost', 'type' => 'text', 'css' => 'width: 50px;');
    $options[] = array('name' => __('Pickup Instructions', 'sunshine'), 'id' => 'pickup_instructions', 'type' => 'textarea');
    $options[] = array('name' => __('Taxable', 'sunshine'), 'id' => 'pickup_taxable', 'type' => 'checkbox', 'options' => array(1));
    return $options;
}
function sunshine_local_options($options)
{
    $options[] = array('name' => 'Local Delivery', 'type' => 'title', 'desc' => '');
    $options[] = array('name' => __('Enable Local Delivery Shipping', 'sunshine'), 'id' => 'local_active', 'type' => 'checkbox', 'options' => array(1));
    $options[] = array('name' => __('Name', 'sunshine'), 'id' => 'local_name', 'type' => 'text');
    $options[] = array('name' => __('Local Delivery Shipping Cost', 'sunshine') . ' (' . sunshine_currency_symbol() . ')', 'id' => 'local_cost', 'type' => 'text', 'css' => 'width: 50px;');
    $options[] = array('name' => __('Allowed zip/post codes', 'sunshine'), 'id' => 'local_zipcodes', 'type' => 'textarea', 'tip' => __('What zip/post codes is this allowed for? Separate each zip/post code with a comma.', 'sunshine'));
    $options[] = array('name' => __('Taxable', 'sunshine'), 'id' => 'local_taxable', 'type' => 'checkbox', 'options' => array(1));
    return $options;
}
function sunshine_money_format($value, $echo = true)
{
    global $sunshine;
    if (!$value) {
        $value = 0;
    }
    /*
    if ($sunshine->options['show_price_including_tax'])
    	$value = $value + max(0, $value * ($sunshine->options['tax_rate']/100));
    */
    $value = str_replace(',', '.', $value);
    if (is_numeric($value)) {
        $formatted_value = number_format($value, stripslashes($sunshine->options['currency_decimals']), stripslashes($sunshine->options['currency_decimal_separator']), stripslashes($sunshine->options['currency_thousands_separator']));
        $currency_symbol = sunshine_currency_symbol();
        switch (stripslashes($sunshine->options['currency_symbol_position'])) {
            case 'left':
                $format = '%1$s%2$s';
                break;
            case 'right':
                $format = '%2$s%1$s';
                break;
            case 'left_space':
                $format = '%1$s %2$s';
                break;
            case 'right_space':
                $format = '%2$s %1$s';
                break;
        }
        $formatted_value = sprintf($format, $currency_symbol, $formatted_value);
        if ($echo) {
            echo $formatted_value;
        } else {
            return $formatted_value;
        }
    }
    return $value;
}
function sunshine_admin_user_cart($user)
{
    if (current_user_can('sunshine_manage_options')) {
        $items = SunshineUser::get_user_meta_by_id($user->ID, 'cart', false);
        $orders = get_posts('post_type=sunshine-order&meta_key=_sunshine_customer_id&meta_value=' . $user->ID . '&nopaging=1');
        ?>
		<h3><?php 
        _e('Orders', 'sunshine');
        ?>
</h3>
		<?php 
        if ($orders) {
            echo '<ul>';
            foreach ($orders as $order) {
                $order_data = unserialize(get_post_meta($order->ID, '_sunshine_order_data', true));
                $total = sunshine_money_format($order_data['total'], false);
                echo '<li><a href="post.php?post=124&action=edit">' . $order->post_title . '</a>, ' . date(get_option('date_format'), strtotime($order->post_date)) . ' &mdash; ' . $total . '</li>';
            }
        } else {
            echo '<li><em>' . __('User has no orders yet', 'sunshine') . '</em></li>';
        }
        ?>
		<h3><?php 
        _e('Address Information', 'sunshine');
        ?>
</h3>
		<table class="form-table">
	 	<tr>
	 		<th><?php 
        _e('Billing Information', 'sunshine');
        ?>
</th>
	 		<td>
				<?php 
        $address = SunshineUser::get_user_meta_by_id($user->ID, 'address');
        $address2 = SunshineUser::get_user_meta_by_id($user->ID, 'address2');
        $city = SunshineUser::get_user_meta_by_id($user->ID, 'city');
        $state = SunshineUser::get_user_meta_by_id($user->ID, 'state');
        $zip = SunshineUser::get_user_meta_by_id($user->ID, 'zip');
        $country = SunshineUser::get_user_meta_by_id($user->ID, 'country');
        if ($address) {
            echo $address . '<br />';
            if ($address2) {
                echo $address2 . '<br />';
            }
            echo $city . ', ' . $state . ' ' . $zip . '<br />' . $country;
        } else {
            echo '<em>' . __('No current billing address', 'sunshine') . '</em>';
        }
        ?>
			</td>
	 	</tr>
	 	<tr>
	 		<th><?php 
        _e('Shipping Information', 'sunshine');
        ?>
</th>
	 		<td>
				<?php 
        $address = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_address');
        $address2 = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_address2');
        $city = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_city');
        $state = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_state');
        $zip = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_zip');
        $country = SunshineUser::get_user_meta_by_id($user->ID, 'shipping_country');
        if ($address) {
            echo $address . '<br />';
            if ($address2) {
                echo $address2 . '<br />';
            }
            echo $city . ', ' . $state . ' ' . $zip . '<br />' . $country;
        } else {
            echo '<em>' . __('No current shipping address', 'sunshine') . '</em>';
        }
        ?>
			</td>
	 	</tr>
	 	</table>
	 	<h3 id="sunshine-credits"><?php 
        _e('Sunshine Gallery Credits for Purchases', 'sunshine');
        ?>
</h3>
		<table class="form-table">
	 	<tr>
	 		<th><label for="sunshine_credits"><?php 
        _e('Credits', 'sunshine');
        ?>
</label></th>
	 		<td>
				<?php 
        $currency_symbol = sunshine_currency_symbol();
        $currency_symbol_format = sunshine_currency_symbol_format();
        $text_field = '<input type="text" name="sunshine_credits" id="sunshine_credits" value="' . esc_attr(SunshineUser::get_user_meta_by_id($user->ID, 'credits')) . '" />';
        echo sprintf($currency_symbol_format, $currency_symbol, $text_field);
        ?>
			</td>
	 	</tr>
	 	</table>
	 	<h3 id="sunshine-cart"><?php 
        _e('Sunshine Items in Cart', 'sunshine');
        ?>
</h3>
		<?php 
        if ($items) {
            ?>
			<table id="sunshine-cart-items" width="100%">
			<tr>
				<th class="image"><?php 
            _e('Image', 'sunshine');
            ?>
</th>
				<th class="name"><?php 
            _e('Product', 'sunshine');
            ?>
</th>
				<th class="qty"><?php 
            _e('Quantity', 'sunshine');
            ?>
</th>
				<th class="price"><?php 
            _e('Item Price', 'sunshine');
            ?>
</th>
			</tr>
			<?php 
            foreach ($items as $item) {
                ?>
				<tr class="item">
					<td class="image">
						<?php 
                $thumb = wp_get_attachment_image_src($item['image_id'], 'thumbnail');
                $image_html = '<a href="' . get_permalink($item['image_id']) . '"><img src="' . $thumb[0] . '" alt="" class="image-thumb" /></a>';
                echo apply_filters('sunshine_cart_image_html', $image_html, $item, $thumb);
                ?>
					</td>
					<td class="name">
						<?php 
                $product = get_post($item['product_id']);
                $cat = wp_get_post_terms($item['product_id'], 'sunshine-product-category');
                ?>
						<strong><span class="sunshine-item-cat"><?php 
                echo apply_filters('sunshine_cart_item_category', isset($cat[0]->name) ? $cat[0]->name : '', $item);
                ?>
</span> - <span class="sunshine-item-name"><?php 
                echo apply_filters('sunshine_cart_item_name', $product->post_title, $item);
                ?>
</span></strong><br />
						<div class="sunshine-item-comments"><?php 
                echo apply_filters('sunshine_cart_item_comments', $item['comments'], $item);
                ?>
</div>
					</td>
					<td class="qty">
						<?php 
                echo $item['qty'];
                ?>
					</td>
					<td class="price">
						<?php 
                sunshine_money_format($item['price']);
                ?>
					</td>
				</tr>
			<?php 
            }
            ?>
			</table>
		<?php 
        } else {
            ?>
			<p><?php 
            _e('No items in cart', 'sunshine');
            ?>
</p>
		<?php 
        }
        ?>
	<?php 
    }
}
function sunshine_product_quick_edit($column_name, $post_type)
{
    switch ($post_type) {
        case 'sunshine-product':
            switch ($column_name) {
                case 'price':
                    ?>
				<fieldset class="inline-edit-col-right">
                  	<div class="inline-edit-col">
						<?php 
                    $currency_symbol = sunshine_currency_symbol();
                    $currency_symbol_format = sunshine_currency_symbol_format();
                    $price_levels = get_terms('sunshine-product-price-level', array('hide_empty' => false));
                    $price_levels_count = count($price_levels);
                    if ($price_levels_count > 1) {
                        foreach ($price_levels as $price_level) {
                            echo '<label><span class="title">' . $price_level->name . '</span>';
                            $text_field = '<input type="text" name="sunshine_product_price_' . $price_level->term_id . '" value="" />';
                            echo sprintf($currency_symbol_format, $currency_symbol, $text_field);
                            echo '</label>';
                        }
                    } elseif ($price_levels_count == 0) {
                        echo __('No price levels setup', 'sunshine');
                    } else {
                        echo '<label><span class="title">' . __('Price', 'sunshine') . '</span>';
                        $text_field = '<input type="text" name="sunshine_product_price_' . $price_levels[0]->term_id . '" value="" />';
                        echo sprintf($currency_symbol_format, $currency_symbol, $text_field);
                    }
                    ?>
						<label>
	                       <span class="title"><?php 
                    _e('Taxable', 'sunshine');
                    ?>
</span>
	                       <input type="checkbox" name="sunshine_product_taxable" value="1" />
	                    </label>
						<label>
	                       <span class="title"><?php 
                    _e('Extra Shipping Fee', 'sunshine');
                    ?>
</span>
							<?php 
                    $text_field = '<input type="text" name="sunshine_product_shipping" value="" />';
                    echo sprintf($currency_symbol_format, $currency_symbol, $text_field);
                    ?>
	                    </label>
                  	</div>
               	</fieldset>
				<?php 
                    break;
            }
            break;
    }
}
function sunshine_bulk_add_products()
{
    ?>
	<div class="wrap sunshine">
		<h2><?php 
    _e('Bulk Add Products', 'sunshine');
    ?>
</h2>

		<?php 
    if (isset($_POST['sunshine_bulk_add_products']) && $_POST['sunshine_bulk_add_products'] == 1) {
        $added = 0;
        for ($i = 0; $i < count($_POST['name']); $i++) {
            $name = sanitize_text_field($_POST['name'][$i]);
            $category = sanitize_text_field($_POST['category'][$i]);
            $taxable = intval($_POST['taxable'][$i]);
            if ($name != '') {
                $current_user = wp_get_current_user();
                $product_id = wp_insert_post(array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => $current_user->ID, 'post_title' => $name, 'post_status' => 'publish', 'post_type' => 'sunshine-product'));
                wp_set_object_terms($product_id, $category, 'sunshine-product-category');
                add_post_meta($product_id, 'sunshine_product_taxable', $taxable);
                $price_levels = get_terms('sunshine-product-price-level', array('hide_empty' => false));
                foreach ($price_levels as $price_level) {
                    $price = $_POST['price_' . $price_level->term_id][$i];
                    if ($price) {
                        update_post_meta($product_id, 'sunshine_product_price_' . $price_level->term_id, $price);
                    }
                }
                do_action('sunshine_bulk_add_product', $product_id, $_POST, $i);
                $added++;
            }
        }
        echo '<div id="message" class="updated"><p>Bulk import complete! ' . $added . ' products added. <a href="edit.php?post_type=sunshine-product">View products</a></p></div>';
    }
    ?>

		<form method="post">
			<input type="hidden" name="sunshine_bulk_add_products" value="1" />
		<?php 
    $categories = get_terms('sunshine-product-category', array('hide_empty' => false));
    $price_levels = get_terms('sunshine-product-price-level', array('hide_empty' => false));
    $currency_symbol_format = sunshine_currency_symbol_format();
    $currency_symbol = sunshine_currency_symbol();
    ?>
		<table id="sunshine-bulk-add-products">
			<tr>
				<th><?php 
    _e('Name', 'sunshine');
    ?>
:</th>
				<td><input type="text" name="name[]" /></td>
				<th><?php 
    _e('Category', 'sunshine');
    ?>
:</th>
				<td>
					<select name="category[]">
						<option value=""></option>
					<?php 
    foreach ($categories as $category) {
        echo '<option value="' . $category->slug . '">' . $category->name . '</option>';
    }
    ?>
					</select>
				</td>
				<th><?php 
    _e('Price Level', 'sunshine');
    ?>
:</th>
				<td>
					<?php 
    foreach ($price_levels as $price_level) {
        $text_field = '<input type="text" name="price_' . $price_level->term_id . '[]" size="6" />';
        echo $price_level->name . ': ' . sprintf($currency_symbol_format, $currency_symbol, $text_field) . '<br />';
    }
    ?>
				</td>
				<th><?php 
    _e('Taxable', 'sunshine');
    ?>
:</th>
				<td><input type="checkbox" value="1" name="taxable[]" /></td>
				<?php 
    do_action('sunshine_bulk_add_product_item');
    ?>
				<td><a href="#" class="sunshine-remove-product"><?php 
    _e('Remove', 'sunshine');
    ?>
</a></td>
			</tr>
		</table>
		<a href="#" id="sunshine-add-product"><?php 
    _e('Add another product', 'sunshine');
    ?>
</a>

		<p><strong><?php 
    _e('Before you add these, please double check everything is accurate!', 'sunshine');
    ?>
</strong></p>

		<p><input type="submit" value="<?php 
    _e('Add these products', 'sunshine');
    ?>
" class="button-primary" /></p>

		</form>

		<script type="text/javascript">
		jQuery(document).ready(function() {
			jQuery("#sunshine-add-product").live('click', function() {
			    jQuery('#sunshine-bulk-add-products tr:first').clone(true,true).appendTo('#sunshine-bulk-add-products');
			    jQuery('#sunshine-bulk-add-products tr:last input, #sunshine-bulk-add-products tr:last select').val('');
			    jQuery('#sunshine-bulk-add-products tr:last input[type=checkbox]').removeAttr('checked');
			    return false;
			});
			jQuery(".sunshine-remove-product").live('click', function(event) {
				jQuery(this).parent().parent().remove();
				return false;
			});
		});
		</script>

	</div>
<?php 
}