function cw_products_in_cart($cart, $user_info, $leave_info = false)
{
    if (empty($cart) || empty($cart['products'])) {
        return array();
    }
    return cw_products_from_scratch($cart['products'], $user_info, false, $leave_info);
}
function cw_gift_prepare_products($products)
{
    global $addons, $user_account;
    if (is_array($products)) {
        foreach ($products as $k => $product) {
            if (!empty($product['options'])) {
                $products[$k]['options'] = unserialize($product['options']);
                if (!empty($products[$k]['options']) && $addons['product_options']) {
                    $products[$k]['variant_id'] = cw_get_variant_id($products[$k]['options'], $product['product_id']);
                }
            }
            $products[$k]['amount_requested'] = $product['amount'];
            if ($product['amount'] > $product['amount_purchased']) {
                $products[$k]['amount'] = $product['amount'] - $product['amount_purchased'];
            }
        }
    }
    return cw_products_from_scratch($products, $user_account, true);
}
function cw_ps_update_cart_products(&$cart, &$products, $user_info, $offers_ids = array())
{
    global $config, $smarty, $tables, $addons;
    static $iter;
    if (APP_AREA != 'customer') {
        return false;
    }
    if (empty($products) || !is_array($products)) {
        cw_session_unregister('ps_offers_info');
        return false;
    }
    // we should track actions here as well...
    global $action;
    $action = (string) $action;
    $tracking_actions = array('add' => 1, 'update' => 1, 'delete' => 1, 'ajax_update' => 1, 'clear_cart' => 1);
    if (empty($action) || !isset($tracking_actions[$action])) {
        return -1;
        // view cart
    }
    if ($action == 'clear_cart') {
        cw_session_unregister('ps_offers_info');
        return false;
    }
    if (!isset($iter)) {
        $iter = 0;
    }
    $ps_offers_info =& cw_session_register('ps_offers_info');
    $current_product_hash = md5(cw_ps_prods_str($products));
    echo '<pre>$current_product_hash: ', $current_product_hash, '</pre>';
    //die;
    if (!empty($ps_offers_info) && is_array($ps_offers_info)) {
        if (isset($ps_offers_info['hash_offer_free']) && !empty($ps_offers_info['hash_offer_free'])) {
            if (isset($ps_offers_info['hash_offer_free'][$current_product_hash])) {
                return $ps_offers_info['hash_offer_free'][$current_product_hash];
                //3 - already processed, we should replace the cart[products] with the existing products array
                //2 - already processed
            } else {
                $iter++;
                $ps_offers_info['hash_offer_free'][$current_product_hash] = array();
                if ($iter > 1) {
                    echo '<pre>', "\t\t!!!", "{$iter} iteration: ", "ps_offers_info['processed_prods_hash']: {$ps_offers_info['processed_prods_hash']}", '</pre>';
                    echo '<pre>', "\t\tps_offers_info['product_hash']: {$ps_offers_info['product_hash']}", '</pre>';
                    //echo '<pre>', print_r($products), '</pre>';
                }
            }
        }
        /*if (isset($ps_offers_info['processed_prods_hash']) || isset($ps_offers_info['product_hash'])) {
                    $current_product_hash = md5(cw_ps_prods_str($products));
                }
        
                if (isset($ps_offers_info['processed_prods_hash'])) {
        
                    if ($ps_offers_info['processed_prods_hash'] == $current_product_hash) {
                        return 3; //already processed, we should replace the cart[products] with the existing products array
                    }
                }
        
                if (isset($ps_offers_info['product_hash'])) {
        
                    if ($ps_offers_info['product_hash'] == $current_product_hash) {
                        return 2; //already processed
                    }
                }
        
        
                if (isset($ps_offers_info['product_hash']) || isset($ps_offers_info['processed_prods_hash'])) {
                    if ($ps_offers_info['product_hash'] != $current_product_hash && $ps_offers_info['processed_prods_hash'] != $current_product_hash && $iter > 1) {
                        echo '<pre>', "\t\t!!!", "$iter iteration: ", "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
                        echo '<pre>', "ps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
                        echo '<pre>', print_r($products), '</pre>';
                    }
                }*/
        /*if (isset($ps_offers_info['hash_offer_free']) && !empty($ps_offers_info['hash_offer_free'])) {
              if (!isset($ps_offers_info['hash_offer_free'][$current_product_hash]) && $iter > 1) {
                  echo '<pre>', "\t\t!!!", "$iter iteration: ", "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
                  echo '<pre>', "\t\tps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
                  echo '<pre>', print_r($products), '</pre>';
              }
          }*/
    }
    if (empty($offers_ids) || !isset($offers_ids['new']) || !isset($offers_ids['to_delete']) || !isset($offers_ids['suitable'])) {
        return false;
    }
    //echo '<pre>', "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
    //echo '<pre>', "ps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
    //die('-');
    $offers = array();
    //if (!empty($ps_offers_info) && is_array($ps_offers_info) && isset($ps_offers_info['product_hash'])) {
    if (!empty($ps_offers_info) && isset($ps_offers_info['hash_offer_free'])) {
        if (isset($ps_offers_info['added_free_prods']) && is_array($ps_offers_info['added_free_prods']) && isset($ps_offers_info['applied_offers_free']) && is_array($ps_offers_info['applied_offers_free'])) {
            // we will not restore products for free which were deleted by a customer
            $new_offers = $offers_ids['new'];
            $offers_to_delete = $offers_ids['to_delete'];
            $suitable_offers = $offers_ids['suitable'];
            if (!empty($new_offers) && is_array($new_offers)) {
                $offers = $new_offers;
            }
            $deleted_prods_exist = false;
            if (!empty($offers_to_delete)) {
                $cart_records_to_delete = array();
                foreach ($offers_to_delete as $key => $trash) {
                    $cart_records_to_delete += $ps_offers_info['applied_offers_free'][$key];
                    //unset($ps_offers_info['applied_offers_free'][$key]); //?
                }
                if (!empty($cart_records_to_delete)) {
                    foreach ($products as $key => $cart_record) {
                        if (isset($cart_records_to_delete[$cart_record['cartid']])) {
                            unset($products[$key]);
                            $deleted_prods_exist = true;
                            if (isset($ps_offers_info['added_free_prods'][$cart_record['cartid']])) {
                                //unset($ps_offers_info['added_free_prods'][$cart_record['cartid']]); //?
                            }
                        }
                    }
                }
            }
            // offers to delelete
            // let's update the products for free in the cart; their quantities and prices should be untouched
            $free_products_exist = false;
            if (!empty($suitable_offers) && is_array($suitable_offers)) {
                $cart_records_to_update = array();
                foreach ($suitable_offers as $key => $trash) {
                    $cart_records_to_update += $ps_offers_info['applied_offers_free'][$key];
                }
                //echo '<pre>$cart_records_to_update: ', print_r($cart_records_to_update), '</pre>';
                //echo '<pre>$ps_offers_info: ', print_r($ps_offers_info), '</pre>';
                //echo '<pre>before update: ', print_r($products), '</pre>';
                $free_products_exist = false;
                if (!empty($cart_records_to_update)) {
                    foreach ($products as $key => $cart_record) {
                        if (isset($cart_records_to_update[$cart_record['cartid']])) {
                            $free_products_exist = true;
                            $products[$key] = $ps_offers_info['added_free_prods'][$cart_record['cartid']];
                        }
                    }
                }
            }
            if (!empty($new_offers) && is_array($new_offers)) {
            } else {
                if ($free_products_exist == true) {
                    $products = cw_products_from_scratch($products, $user_info, false, false);
                }
                if ($deleted_prods_exist == true || $free_products_exist == true) {
                    $ps_offers_info['product_hash'] = md5(cw_ps_prods_str($products));
                    $ps_offers_info['processed_prods_hash'] = md5(cw_ps_prods_str($cart['products']));
                    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($products))] = 2;
                    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($cart['products']))] = 3;
                    //$ps_offers_info['data_hash'] = md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info));
                    $ps_offers_info['hash'][md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info))] = 2;
                    //die('yes');
                    return true;
                } else {
                    return 4;
                    // no new offers and cart prods were not updated
                }
            }
        }
    }
    // empty($ps_offers_info)
    if (empty($offers) && !empty($offers_ids['new'])) {
        $offers = $offers_ids['new'];
    }
    if (empty($offers) || !is_array($offers)) {
        return false;
    }
    // check if the suitable products exist...
    $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
    $fields = array("{$tables['ps_bonuses']}.offer_id", 'object_id', 'quantity', "{$tables['ps_bonuses']}.offer_id as offerid", "{$tables['ps_bonuses']}.bonus_id");
    $from_tbls[] = 'ps_bonuses';
    $query_joins['ps_bonus_details'] = array('on' => "{$tables['ps_bonuses']}.bonus_id = {$tables['ps_bonus_details']}.bonus_id", 'is_inner' => 1);
    $where[] = "{$tables['ps_bonuses']}.offer_id IN ('" . implode("', '", array_keys($offers)) . "')";
    $where[] = "{$tables['ps_bonuses']}.type = '" . PS_FREE_PRODS . "'";
    $where[] = "{$tables['ps_bonus_details']}.object_type = '" . PS_OBJ_TYPE_PRODS . "'";
    $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
    $free_products = cw_query_hash($search_query, 'object_id', false);
    if (empty($free_products) || !is_array($free_products)) {
        return false;
    }
    //global $ps_offers_info;
    //$ps_offers_info = array();
    $ps_offers_info['already_applied'] = true;
    //$ps_offers_info = &cw_session_register('ps_offers_info');
    //var_dump('suitable products:', $free_products);
    //return $products;
    //die(var_dump('suitable products:', $free_products));
    global $user_account;
    cw_load('warehouse');
    foreach ($free_products as $product_id => $product_data) {
        /*
         * we will add the suitable products to the cart directly to avoid the groupping of the default products
         * and special ones
         */
        $product_status = cw_query_first_cell("SELECT status FROM {$tables['products_enabled']} as pe WHERE pe.product_id = '{$product_id}' AND pe.status = '1'");
        if ($product_status != 1) {
            continue;
        }
        $product_data['quantity'] = abs(intval($product_data['quantity']));
        if (empty($product_data['quantity'])) {
            continue;
        }
        $new_product = cw_func_call('cw_product_get', array('id' => $product_id, 'user_account' => $user_account, 'info_type' => 3));
        if ($new_product['product_type'] != constant('PRODUCT_TYPE_GENERAL')) {
            if (!empty($new_product['product_type'])) {
                continue;
            }
        }
        $amount = $product_data['quantity'];
        if (!empty($addons['egoods']) && !empty($new_product['distribution'])) {
            $amount = 1;
        }
        if ($amount < $new_product['min_amount']) {
            continue;
        }
        $possible_warehouses = cw_warehouse_get_avails_customer($product_id);
        if (empty($possible_warehouses) || !is_array($possible_warehouses)) {
            $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id);
            $possible_warehouses = array($possible_warehouse => 1);
        }
        $warehouse = array_shift(array_keys($possible_warehouses));
        //foreach($possible_warehouses as $warehouse => $tmp) {
        if (!$warehouse) {
            $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id);
        }
        if ($addons['product_options']) {
            # Get default options
            $product_options = cw_get_default_options($product_id, $amount, @$user_account['membership_id']);
            if ($product_options === false) {
                continue;
                //continue 2;
            } elseif ($product_options === true) {
                $product_options = null;
            }
            # Get the variant_id of options
            $variant_id = cw_get_variant_id($product_options, $product_id);
            if (!empty($variant_id)) {
                $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id, $variant_id);
                if (empty($warehouse)) {
                    $warehouse = $possible_warehouse;
                }
            } else {
                if (empty($warehouse)) {
                    $warehouse = $possible_warehouse;
                }
            }
        }
        if (empty($warehouse)) {
            $warehouse = $possible_warehouse;
        }
        // let's add a product the cart
        $free_price = 0.0;
        $cartid = cw_generate_cartid($products);
        $products[] = array("cartid" => $cartid, "product_id" => $product_id, "amount" => $amount, "options" => $product_options, "free_price" => @price_format(@$free_price), "salesman_doc_id" => 0, "distribution" => $new_product['distribution'], "variant_id" => $variant_id, "warehouse_customer_id" => $warehouse);
        //$amount = $amount - $result['added_amount'];
        //if ($amount <= 0) break;
        //}
        if (!isset($ps_offers_info['added_free_prods'])) {
            $ps_offers_info['added_free_prods'] = array();
        }
        $ps_offers_info['added_free_prods'][$cartid] = $products[count($products) - 1];
        $ps_offers_info['added_free_prods'][$cartid]['offer_id'] = $product_data['offerid'];
        $ps_offers_info['added_free_prods'][$cartid]['bonus_id'] = $product_data['bonus_id'];
        if (!isset($ps_offers_info['applied_offers_free'])) {
            $ps_offers_info['applied_offers_free'] = array();
        }
        if (!isset($ps_offers_info['applied_offers_free'][$product_data['offerid']])) {
            $ps_offers_info['applied_offers_free'][$product_data['offerid']] = array();
        }
        $ps_offers_info['applied_offers_free'][$product_data['offerid']][$cartid] = 1;
    }
    $products = cw_products_from_scratch($products, $user_info, false, false);
    //$ps_offers_info['product_hash'] = md5(serialize($products));
    $ps_offers_info['product_hash'] = md5(cw_ps_prods_str($products));
    $ps_offers_info['processed_prods_hash'] = md5(cw_ps_prods_str($cart['products']));
    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($products))] = 2;
    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($cart['products']))] = 3;
    //$ps_offers_info['data_hash'] = md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info));
    $ps_offers_info['hash'][md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info))] = 2;
    cw_ps_save('products', $products);
    //$ps_offers_info['processed_prods_hash'] = md5(serialize($cart['products']));
    //echo '<pre>'; print_r($ps_offers_info); echo '</pre>';
    echo '<pre>', "ps_offers_info['processed_prods_hash']: {$ps_offers_info['processed_prods_hash']}", '</pre>';
    echo '<pre>', "ps_offers_info['product_hash']: {$ps_offers_info['product_hash']}", '</pre>';
    return true;
}
        $access_status[$eventid] = "Y";
    }
    $smarty->assign('eventid', $eventid);
    $smarty->assign('event_data', $event_data);
    #
    # Get the products information
    #
    $wl_raw = cw_query("select wishlist_id, product_id, amount, amount_purchased, options from {$tables['wishlist']} where event_id='{$eventid}' AND product_id>0");
    if (is_array($wl_raw)) {
        foreach ($wl_raw as $index => $wl_product) {
            $wl_raw[$index]['options'] = unserialize($wl_product['options']);
            $wl_product['amount_requested'] = $wl_product['amount'];
            if ($wl_product['amount'] > $wl_product['amount_purchased'] && $mode == "friend_wl") {
                $wl_raw[$index]['amount'] = $wl_product['amount'] - $wl_product['amount_purchased'];
            }
        }
        $wl_products = cw_products_from_scratch($wl_raw, $user_account['membership_id'], true);
    }
    $smarty->assign('wl_products', $wl_products);
    $wl_raw = cw_query("select wishlist_id, amount, amount_purchased, object from {$tables['wishlist']} where event_id='{$eventid}' AND product_id=0");
    if (is_array($wl_raw)) {
        foreach ($wl_raw as $k => $v) {
            $object = unserialize($v['object']);
            $wl_giftcerts[] = cw_array_merge($v, $object);
        }
        $smarty->assign('wl_giftcerts', $wl_giftcerts);
    }
}
$mode = "event_details";
$smarty->assign('mode', $mode);
$smarty->assign('main', "giftreg");