Esempio n. 1
0
/**
 * Creates/Updates currency
 *
 * @param array $currency_data Currency information
 * @param int $currency_id Currency id
 * @param string $lang_code 2-letter language code
 * @return int Currency id
 */
function fn_update_currency($currency_data, $currency_id, $lang_code = DESCR_SL)
{
    /**
     * Updates currency data before updating
     *
     * @param array  $currency_data Currency information
     * @param int    $currency_id   Currency id
     * @param string $lang_code     2-letter language code
     */
    fn_set_hook('update_currency_pre', $currency_data, $currency_id, $lang_code);
    $currency_data['currency_code'] = strtoupper($currency_data['currency_code']);
    $currency_data['coefficient'] = !empty($currency_data['is_primary']) || !isset($currency_data['coefficient']) ? 1 : $currency_data['coefficient'];
    $currency_data['symbol'] = empty($currency_data['symbol']) ? '' : SecurityHelper::sanitizeHtml($currency_data['symbol']);
    if (empty($currency_data['coefficient']) || floatval($currency_data['coefficient']) <= 0) {
        fn_set_notification('W', __('warning'), __('currency_rate_greater_than_null'));
        return false;
    }
    $is_exists = db_get_field("SELECT COUNT(*) FROM ?:currencies WHERE currency_code = ?s AND currency_id <> ?i", $currency_data['currency_code'], $currency_id);
    if (!empty($is_exists)) {
        fn_set_notification('E', __('error'), __('error_currency_exists', array('[code]' => $currency_data['currency_code'])));
        return false;
    }
    if (isset($currency_data['decimals']) && $currency_data['decimals'] > 2) {
        fn_set_notification('W', __('warning'), __('notice_too_many_decimals', array('[DECIMALS]' => $currency_data['decimals'], '[CURRENCY]' => $currency_data['currency_code'])));
    }
    if (!empty($currency_data['is_primary'])) {
        db_query("UPDATE ?:currencies SET is_primary = 'N' WHERE is_primary = 'Y'");
    }
    if (empty($currency_id)) {
        $currency_id = db_query("INSERT INTO ?:currencies ?e", $currency_data);
        fn_create_description('currency_descriptions', 'currency_code', $currency_data['currency_code'], $currency_data);
    } else {
        $old_currency_code = db_get_field("SELECT currency_code FROM ?:currencies WHERE currency_id = ?i", $currency_id);
        db_query("UPDATE ?:currencies SET ?u WHERE currency_id = ?i", $currency_data, $currency_id);
        db_query('UPDATE ?:currency_descriptions SET ?u WHERE currency_code = ?s AND lang_code = ?s', $currency_data, $old_currency_code, $lang_code);
    }
    /**
     * Changes result of currency saving
     *
     * @param array  $currency_data Currency information
     * @param int    $currency_id   Currency id
     * @param string $lang_code     2-letter language code
     * @param int Currency id
     */
    fn_set_hook('update_currency_post', $currency_data, $currency_id, $lang_code, $currency_id);
    return $currency_id;
}