/**
 * Smarty {oos_address_label} function plugin
 *
 * Type:     function
 * Name:     oos_address_label
 * Version:  1.0
 * -------------------------------------------------------------
 */

function smarty_function_oos_address_label($params, &$smarty)
{

    $customers_id = '';
    $address_id = 1;
    $html = true;
    $boln = '';
    $eoln = '<br>';

    MyOOS_CoreApi::requireOnce('lib/smarty/libs/plugins/shared.escape_special_chars.php');
    MyOOS_CoreApi::requireOnce('lib/smarty-plugins/myoos/function.oos_address_format.php');

    foreach($params as $_key => $_val) {
      $$_key = smarty_function_escape_special_chars($_val);
    }

    $dbconn =& oosDBGetConn();
    $oostable =& oosDBGetTables();

    $address_result = $dbconn->Execute("SELECT entry_firstname AS firstname, entry_lastname AS lastname, entry_company AS company, entry_street_address AS street_address, entry_suburb AS suburb, entry_city AS city, entry_postcode AS postcode, entry_state AS state, entry_zone_id AS zone_id, entry_country_id AS country_id FROM " . $oostable['address_book'] . " WHERE customers_id = '" . (int)$customers_id . "' AND address_book_id = '" . (int)$address_id . "'");
    $address = $address_result->fields;

    $format_id = oos_get_address_format_id($address['country_id']);


    return smarty_function_oos_address_format(array('address_format_id' => $format_id,
                                                    'address'   => $address,
                                                    'html'      => $html),
                                                  $smarty);


}
        oos_redirect(oos_href_link($aModules['checkout'], $aFilename['checkout_shipping'], '', 'SSL'));
    }
}
if ($process == '0') {
    $address_booktable = $oostable['address_book'];
    $sql = "SELECT COUNT(*) AS total\n            FROM {$address_booktable}\n            WHERE customers_id = '" . intval($_SESSION['customer_id']) . "'\n              AND address_book_id != '" . intval($_SESSION['sendto']) . "'";
    $addresses_count_result = $dbconn->Execute($sql);
    $addresses_count = $addresses_count_result->fields['total'];
    if ($addresses_count > 0) {
        $radio_buttons = 0;
        $address_booktable = $oostable['address_book'];
        $sql = "SELECT address_book_id, entry_firstname AS firstname, entry_lastname AS lastname,\n                       entry_company AS company, entry_street_address AS street_address,\n                       entry_suburb AS suburb, entry_city AS city, entry_postcode AS postcode,\n                       entry_state AS state, entry_zone_id AS zone_id, entry_country_id AS country_id\n                FROM {$address_booktable}\n                WHERE customers_id = '" . intval($_SESSION['customer_id']) . "'";
        $addresses_result = $dbconn->Execute($sql);
        $addresses_array = array();
        while ($addresses = $addresses_result->fields) {
            $format_id = oos_get_address_format_id($address['country_id']);
            $addresses_array[] = array('format_id' => $format_id, 'radio_buttons' => $radio_buttons, 'firstname' => $addresses['firstname'], 'lastname' => $addresses['lastname'], 'address_book_id' => $addresses['address_book_id'], 'address' => oos_address_format($format_id, $addresses, true, ' ', ', '));
            $radio_buttons++;
            // Move that ADOdb pointer!
            $addresses_result->MoveNext();
        }
    }
}
// if no shipping destination address was selected, use their own address as default
if (!isset($_SESSION['sendto'])) {
    $_SESSION['sendto'] = $_SESSION['customer_default_address_id'];
}
if (!isset($process)) {
    $process = '0';
}
// links breadcrumb
 /**
  * Return a formatted address
  *
  * @param $customers_id
  * @param $address_id
  * @param $html
  * @param $boln
  * @param $eoln
  * @param $address
  * @param $html
  * @param $boln
  * @param $eoln
  */
  function oos_address_label($customers_id, $address_id = 1, $html = false, $boln = '', $eoln = "\n") {

    // Get database information
    $dbconn =& oosDBGetConn();
    $oostable =& oosDBGetTables();

    $address_booktable = $oostable['address_book'];
    $query = "SELECT entry_firstname AS firstname, entry_lastname AS lastname, entry_company AS company,
                     entry_street_address AS street_address, entry_suburb AS suburb, entry_city AS city,
                     entry_postcode AS postcode, entry_state AS state, entry_zone_id AS zone_id,
                     entry_country_id AS country_id
              FROM  $address_booktable
              WHERE  customers_id = '" . intval($customers_id) . "' AND
                     address_book_id = '" . intval($address_id) . "'";
    $address = $dbconn->GetRow($query);

    $format_id = oos_get_address_format_id($address['country_id']);

    return oos_address_format($format_id, $address, $html, $boln, $eoln);
  }