function ym_firesale_ppp_packs()
{
    global $wpdb;
    // do all packs!
    foreach (ym_get_bundles() as $pack) {
        $pack_id = $pack->id;
        // get base data from the table
        $sql = 'SELECT original_cost FROM ' . $wpdb->ym_app_ppp_pack . '
			WHERE pack_id = ' . $pack_id;
        $fire_original_cost = $wpdb->get_var($sql);
        $base_cost = number_format($pack->cost, 2);
        $fire_original_cost = $fire_original_cost ? $fire_original_cost : 0;
        $is_currently_firesale = $fire_original_cost ? TRUE : FALSE;
        if ($ym_firesale_id = ym_firesale_exists($pack_id, YM_APP_TYPE_PACK)) {
            // a fire sale exists
            $tier_data = ym_firesale_get_current_tier($ym_firesale_id);
            $tier_data = $tier_data[0];
            if (!$fire_original_cost) {
                // starting a firesale
                $sql = 'INSERT INTO ' . $wpdb->ym_app_ppp_pack . '(pack_id, original_cost) VALUES (' . $pack_id . ', ' . $fire_original_cost . ')';
                $wpdb->query($sql);
                ym_firesale_log(array('doing' => 'StartingPricingModel', 'packId' => $pack_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                ym_fire_sale_start($tier_data->fire_id);
            }
            // cost update
            if ($base_price != $tier_data->fire_price) {
                if ($tier_data) {
                    // packs stores in pence/cents base unit
                    $pack->cost = str_replace('.', '', $tier_data->fire_price);
                    ym_firesale_log(array('doing' => 'TierChange', 'packId' => $pack_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                    ym_firesale_tier_log($tier_data->fire_tier_id);
                    ym_fire_tier_start($tier_data->fire_tier_id);
                } else {
                    // no tier
                    $pack->cost = str_replace('.', '', $fire_original_cost);
                    $sql = 'DELETE FROM ' . $wpdb->ym_app_ppp_pack . ' WHERE pack_id = ' . $pack_id;
                    $wpdb->query($sql);
                    ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                }
            }
        } else {
            if ($is_currently_firesale == 1) {
                // firesale needs resetting
                $pack->cost = $fire_original_cost;
                ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price));
            }
        }
        // update the ppp pack
        //		$all_packs[$key] = $pack;
        $pack->cost = number_format($pack->cost, 2, '.', '') * 100;
        //store in pence
        $sql = 'UPDATE ' . $wpdb->prefix . 'ym_post_pack SET cost = ' . $pack->cost . ' WHERE id = ' . $pack->id;
        $wpdb->query($sql);
    }
}
/**
Content Index/Cart Style pages
Running in non cart mode
*/
function ym_get_all_bundle_buttons($args)
{
    if (!is_user_logged_in()) {
        global $ym_res;
        return $ym_res->msg_header . ym_filter_message($ym_res->all_bundles_not_logged_in) . $ym_res->msg_footer;
    }
    $hide_purchased = isset($args['hide_purchased']) ? $args['hide_purchased'] : TRUE;
    $max = isset($args['max']) ? $args['max'] : FALSE;
    $bundleoffset = isset($_REQUEST['bundleoffset']) ? $_REQUEST['bundleoffset'] : 0;
    //	[ym_buy_bundle]
    $supported_args = array('gateways', 'hidecoupon', 'list_contents', 'hide_purchased');
    $arg_string = '';
    foreach ($supported_args as $arg) {
        if (isset($args[$arg])) {
            $arg_string .= $arg . '=' . $args[$arg] . ' ';
        }
    }
    $content = '';
    global $ym_user;
    $user_id = $ym_user->ID;
    // rinse and repeat
    $bundles = ym_get_bundles();
    $count = 0;
    $bundleoffsetcount = 0;
    foreach ($bundles as $bundle) {
        if ($hide_purchased && ym_has_purchased_bundle($bundle->id, $user_id)) {
            // skip if purchased
            continue;
        }
        $bundleoffsetcount++;
        if ($bundleoffset && $bundleoffsetcount <= $bundleoffset) {
            continue;
        }
        $content .= '[ym_buy_bundle bundle_id="' . $bundle->id . '" ' . $arg_string . ']' . "\n";
        $count++;
        if ($max && $count >= $max) {
            // exit it max reached
            break;
        }
    }
    $total = count($bundles);
    // pagniate?
    if ($max) {
        // paginate
        $content .= '<p style="overflow: hidden;">';
        if ($bundleoffset) {
            $url = get_permalink();
            if (FALSE === strpos($url, '?')) {
                $url .= '?';
            } else {
                $url .= '&';
            }
            $url .= 'bundleoffset=' . ($bundleoffset - $max);
            $content .= '<a href="' . $url . '">' . __('Back', 'ym') . '</a> ';
        }
        //		if ($count == $max) {
        if ($count + $bundleoffset < $total) {
            $url = get_permalink();
            if (FALSE === strpos($url, '?')) {
                $url .= '?';
            } else {
                $url .= '&';
            }
            $url .= 'bundleoffset=' . ($bundleoffset + $max);
            $content .= ' <a href="' . $url . '" class="ym_forward_link">' . __('Forward', 'ym') . '</a>';
        }
    }
    // and spit
    return do_shortcode($content);
}
/**
// Updates the account type for posts and pages
*/
function ym_account_save($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || defined('DOING_AJAX') && DOING_AJAX || isset($_REQUEST['bulk_edit'])) {
        return;
    }
    // nonce needed
    /**
    // Use nonce for verification
      wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
      // in widgets 
    	//if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
    */
    $ignores = array('auto-draft', 'inherit', 'trash');
    // allow save on types from http://codex.wordpress.org/Function_Reference/get_post_status
    if (in_array(get_post_status($post_id), $ignores)) {
        return;
    }
    //we add the account types within this if because it should always be set.
    //the only occurrence where it wouldnt be set is in a scheduled post.
    $opt = '';
    if (ym_post('post_account_types')) {
        foreach ((array) ym_post('post_account_types') as $type) {
            $opt .= $type . ';';
        }
        $opt = rtrim($opt, ';');
    }
    update_post_meta($post_id, '_ym_account_type', $opt);
    update_post_meta($post_id, '_ym_post_purchasable', ym_post('post_purchasable'));
    if (ym_post('post_purchasable')) {
        $purchase_expiry = ym_post('post_purchasable_expiry');
        $purchase_limit = ym_post('post_purchasable_limit');
        if ($purchase_expiry != '') {
            $purchase_expiry = strtotime($purchase_expiry);
        }
        $purchase_duration = (int) ym_post('post_purchasable_duration');
        update_post_meta($post_id, '_ym_post_purchasable_featured', ym_post('post_purchasable_featured'));
        update_post_meta($post_id, '_ym_post_purchasable_index', ym_post('ppp_index'));
        update_post_meta($post_id, '_ym_post_purchasable_cost', ym_post('post_purchasable_cost'));
        update_post_meta($post_id, '_ym_post_purchasable_expiry', $purchase_expiry);
        update_post_meta($post_id, '_ym_post_purchasable_limit', $purchase_limit);
        update_post_meta($post_id, '_ym_post_purchasable_duration', $purchase_duration);
        $gateways = ym_spawn_gateways();
        foreach ($gateways as $gateway) {
            if (method_exists($gateway, 'additional_pack_fields')) {
                $fields = $gateway->additional_pack_fields();
                foreach ($fields as $field) {
                    update_post_meta($post_id, '_ym_post_purchasable_' . $field['name'], ym_post($field['name']));
                }
            }
        }
        // bundle
        global $wpdb;
        $data = ym_post('ym_bundle', array());
        $bundles = ym_get_bundles();
        foreach ($bundles as $bundle) {
            $id = $bundle->id;
            if (in_array($id, $data)) {
                // add
                ym_add_post_to_bundle($post_id, $id);
            } else {
                // remove
                ym_remove_post_from_bundle($post_id, $id);
            }
        }
    }
    if (ym_post('available_from')) {
        $string = array();
        foreach (ym_post('available_from') as $key => $type) {
            $string[] = $key . '=' . $type;
        }
        $string = implode(';', $string);
        update_post_meta($post_id, '_ym_account_min_duration', $string);
    }
}
ym_bundle_form($bundle, __('Create Bundle', 'ym'));
echo ym_end_box();
/**
Gifting
*/
echo ym_start_box(__('Gift a Bundle', 'ym'));
global $wpdb, $ym_formgen;
echo '<form action="" method="post">';
echo '<table style="width: 100%;"><tr><td>' . __('Select a User', 'ym') . '</td><td>' . __('Select a Bundle', 'ym') . '</td></tr><tr><td>';
$user_sql = 'SELECT DISTINCT(ID) AS value, user_login AS label
			FROM ' . $wpdb->users . ' u
			ORDER BY user_login';
$ym_formgen->render_combo_from_query('user_to_gift', $user_sql);
echo '</td><td>';
echo '<select name="bundle_to_gift">';
$bundles = ym_get_bundles();
foreach ($bundles as $bundle) {
    echo '<option value="' . $bundle->id . '">' . $bundle->name . '</option>';
}
echo '</select>';
echo '</td><td>';
echo '<input type="submit" class="button-secondary" value="' . __('Gift Bundle', 'ym') . '" />';
echo '</td></tr></table>';
echo '</form>';
echo ym_end_box();
echo ym_start_box(__('Bundle Purchases Made', 'ym'));
echo '<table style="width: 100%;" class="form-table widefat">
	<tr>
		<th>' . __('Member', 'ym') . '</th>
		<th>' . __('(ID) Name', 'ym') . '</th>
		<th>' . __('Purchase Expiry', 'ym') . '</th>