/**
 * General settings for Crowdfunding
 *
 * @since Astoundify Crowdfunding 0.9
 * 
 * @param $settings
 * @return $settings
 */
function atcf_settings_general($settings)
{
    $settings['atcf_settings'] = array('id' => 'atcf_settings', 'name' => '<strong>' . __('Astoundify Crowdfunding Settings', 'atcf') . '</strong>', 'desc' => __('Configuration related to crowdfunding.', 'atcf'), 'type' => 'header');
    $settings['atcf_automatic_process'] = array('id' => 'atcf_automatic_process', 'name' => __('Automatically Start Payment Processing', 'atcf'), 'desc' => __('When a campaign is complete and meets the criteria, payments will automatically start processing.', 'atcf'), 'type' => 'checkbox', 'std' => 1);
    $settings['atcf_to_process'] = array('id' => 'atcf_to_process', 'name' => __('Batch Process', 'atcf'), 'desc' => __('The number of payments per campaign to process each hour.', 'atcf'), 'type' => 'text', 'size' => 'small', 'std' => 20);
    $settings['atcf_settings_custom_pledge'] = array('id' => 'atcf_settings_custom_pledge', 'name' => __('Custom Pledging', 'atcf'), 'desc' => __('Allow arbitrary amounts to be pledged.', 'atcf'), 'type' => 'checkbox', 'std' => 1);
    $settings['atcf_settings_campaign_minimum'] = array('id' => 'atcf_campaign_length_min', 'name' => __('Minimum Campaign Length', 'atcf'), 'desc' => __('The minimum days a campaign can run for.', 'atcf'), 'type' => 'text', 'size' => 'small', 'std' => 14);
    $settings['atcf_settings_campaign_maximum'] = array('id' => 'atcf_campaign_length_max', 'name' => __('Maximum Campaign Length', 'atcf'), 'desc' => __('The maximum days a campaign can run for.', 'atcf'), 'type' => 'text', 'size' => 'small', 'std' => 42);
    $types = atcf_campaign_types();
    $_types = array();
    foreach ($types as $key => $type) {
        $_types[$key] = $type['title'] . ' &mdash; <small>' . $type['description'] . '</small>';
    }
    $settings['atcf_settings_campaign_types'] = array('id' => 'atcf_campaign_types', 'name' => __('Campaign Types', 'atcf'), 'desc' => __('Select which campaign types are allowed.', 'atcf'), 'type' => 'multicheck', 'options' => $_types);
    $settings['atcf_settings_require_account'] = array('id' => 'atcf_settings_require_account', 'name' => __('Require Account', 'atcf'), 'desc' => __('Require users to be logged in to submit a campaign.', 'atcf'), 'type' => 'checkbox');
    return $settings;
}
/**
 * @missing
 *
 * @since Astoundify Crowdfunding unknown
 */
function atcf_campaign_types_active()
{
    global $edd_options;
    $types = atcf_campaign_types();
    $active = isset($edd_options['atcf_campaign_types']) ? $edd_options['atcf_campaign_types'] : null;
    if (!$active) {
        $keys = array();
        foreach ($types as $key => $type) {
            $keys[$key] = $type['title'] . ' &mdash; <small>' . $type['description'] . '</small>';
        }
        return $keys;
    }
    return $active;
}
/**
 * Campaign Configuration
 *
 * Hook into EDD Download Information and add a bit more stuff.
 * These are all things that can be updated while the campaign runs/before
 * being published.
 *
 * @since Astoundify Crowdfunding 0.1-alpha
 *
 * @return void
 */
function _atcf_metabox_campaign_info()
{
    global $post, $edd_options, $wp_locale;
    /** Verification Field */
    wp_nonce_field('cf', 'cf-save');
    $campaign = atcf_get_campaign($post);
    $end_date = $campaign->end_date();
    if (!$end_date && !$campaign->is_endless()) {
        $min = isset($edd_options['atcf_campaign_length_min']) ? $edd_options['atcf_campaign_length_min'] : 14;
        $max = isset($edd_options['atcf_campaign_length_max']) ? $edd_options['atcf_campaign_length_max'] : 48;
        $start = apply_filters('atcf_shortcode_submit_field_length_start', round(($min + $max) / 2));
        $end_date = date('Y-m-d h:i:s', time() + $start * 86400);
    }
    $jj = mysql2date('d', $end_date);
    $mm = mysql2date('m', $end_date);
    $aa = mysql2date('Y', $end_date);
    $hh = mysql2date('H', $end_date);
    $mn = mysql2date('i', $end_date);
    $ss = mysql2date('s', $end_date);
    do_action('atcf_metabox_campaign_info_before', $campaign);
    $types = atcf_campaign_types();
    ?>
	<p>
		<label for="_campaign_featured">
			<input type="checkbox" name="_campaign_featured" id="_campaign_featured" value="1" <?php 
    checked(1, $campaign->featured());
    ?>
 />
			<?php 
    _e('Featured campaign', 'atcf');
    ?>
		</label>
	</p>

	<p>
		<label for="_campaign_physical">
			<input type="checkbox" name="_campaign_physical" id="_campaign_physical" value="1" <?php 
    checked(1, $campaign->needs_shipping());
    ?>
 />
			<?php 
    _e('Collect shipping information on checkout', 'atcf');
    ?>
		</label>
	</p>

	<p>
		<strong><?php 
    _e('Funding Type:', 'atcf');
    ?>
</strong>
	</p>

	<p>
		<?php 
    foreach (atcf_campaign_types_active() as $key => $desc) {
        ?>
		<label for="campaign_type[<?php 
        echo esc_attr($key);
        ?>
]"><input type="radio" name="campaign_type" id="campaign_type[<?php 
        echo esc_attr($key);
        ?>
]" value="<?php 
        echo esc_attr($key);
        ?>
" <?php 
        checked($key, $campaign->type());
        ?>
 /> <strong><?php 
        echo $types[$key]['title'];
        ?>
</strong> &mdash; <?php 
        echo $types[$key]['description'];
        ?>
</label><br />
		<?php 
    }
    ?>
	</p>

	<p>
		<?php 
    $goal_args = array('name' => 'campaign_goal', 'value' => esc_attr(edd_format_amount(edd_sanitize_amount($campaign->goal(false)))), 'class' => 'edd-price-field');
    if ('before' == edd_get_option('currency_position', 'before')) {
        echo edd_currency_filter('');
        echo EDD()->html->text($goal_args);
    } else {
        echo EDD()->html->text($goal_args);
        echo edd_currency_filter('');
    }
    ?>
	</p>
	<p>
		<label for="campaign_location"><strong><?php 
    _e('Location:', 'atcf');
    ?>
</strong></label><br />
		<input type="text" name="campaign_location" id="campaign_location" value="<?php 
    echo esc_attr($campaign->location());
    ?>
" class="regular-text" />
	</p>

	<p>
		<label for="campaign_author"><strong><?php 
    _e('Author:', 'atcf');
    ?>
</strong></label><br />
		<input type="text" name="campaign_author" id="campaign_author" value="<?php 
    echo esc_attr($campaign->author());
    ?>
" class="regular-text" />
	</p>

	<p>
		<label for="campaign_email"><strong><?php 
    _e('Contact Email:', 'atcf');
    ?>
</strong></label><br />
		<input type="text" name="campaign_contact_email" id="campaign_contact_email" value="<?php 
    echo esc_attr($campaign->contact_email());
    ?>
" class="regular-text" />
	</p>

	<style>#end-aa { width: 3.4em } #end-jj, #end-hh, #end-mn { width: 2em; }</style>

	<p>
		<strong><?php 
    _e('End Date:', 'atcf');
    ?>
</strong><br />

		<select id="end-mm" name="end-mm">
			<?php 
    for ($i = 1; $i < 13; $i = $i + 1) {
        $monthnum = zeroise($i, 2);
        ?>
				<option value="<?php 
        echo $monthnum;
        ?>
" <?php 
        selected($monthnum, $mm);
        ?>
>
				<?php 
        printf('%1$s-%2$s', $monthnum, $wp_locale->get_month_abbrev($wp_locale->get_month($i)));
        ?>
				</option>
			<?php 
    }
    ?>
		</select>

		<input type="text" id="end-jj" name="end-jj" value="<?php 
    echo esc_attr($jj);
    ?>
" size="2" maxlength="2" autocomplete="off" />,
		<input type="text" id="end-aa" name="end-aa" value="<?php 
    echo esc_attr($aa);
    ?>
" size="4" maxlength="4" autocomplete="off" /> @
		<input type="text" id="end-hh" name="end-hh" value="<?php 
    echo esc_attr($hh);
    ?>
" size="2" maxlength="2" autocomplete="off" /> :
		<input type="text" id="end-mn" name="end-mn" value="<?php 
    echo esc_attr($mn);
    ?>
" size="2" maxlength="2" autocomplete="off" />
		<input type="hidden" id="end-ss" name="end-ss" value="<?php 
    echo esc_attr($ss);
    ?>
" />
		<input type="hidden" id="campaign_end_date" name="campaign_end_date" value="1" />
	</p>

	<p>
		<label for="campaign_endless">
			<input type="checkbox" name="campaign_endless" id="campaign_endless" value="1" <?php 
    checked(1, $campaign->is_endless());
    ?>
> <?php 
    printf(__('This %s never ends', 'atcf'), strtolower(edd_get_label_singular()));
    ?>
		</label>
	</p>
<?php 
    do_action('atcf_metabox_campaign_info_after', $campaign);
}