diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 2142b1b..5a11d9b 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -142,10 +142,86 @@ function commerce_flat_rate_commerce_shipping_method_info() { } /** + * Helper function to get the base cache id for a Flat Rate services. + * + * @param string $name + * The Flat Rate service name + * @param boolean $name + * If the wildcard Id should be returned, works only with i18n enabled + * @return string + */ +function commerce_flat_rate_cache_get_id($name, $wildcard = FALSE) { + $use_translation = module_exists('i18n_string'); + $base_cache_id = 'commerce_flat_rate:services:' . $name; + + if ($use_translation) { + if ($wildcard) { + // Widcard is meaningful only when dealing with translations. + $base_cache_id .= ':*'; + } + else { + // If we're using translations, let's cache each translation separately. + global $language; + $base_cache_id .= ':' . $language->language; + } + } + return $base_cache_id; +} + +/** + * Helper function to retrieve a Flat Rate services from cache. + * + * @param $name + * @return array|null + */ +function commerce_flat_rate_cache_get($name) { + $id = commerce_flat_rate_cache_get_id($name); + $cache = cache_get($id); + if ($cache) { + return $cache->data; + } + return NULL; +} + +/** + * Helper function to store a Flat Rate service from cache. + * + * @param string $name + * The Flat Rate service name + * @param array $data + * The Flat Rate service data + * @return mixed + * The stored cache item, from cache_set() + */ +function commerce_flat_rate_cache_set($name, $data) { + $id = commerce_flat_rate_cache_get_id($name); + return cache_set($id, $data, 'cache', CACHE_TEMPORARY); +} + +/** + * Helper function to remove a Flat Rate services from cache. + * @param $name + * The Flat Rate service name + */ +function commerce_flat_rate_cache_delete($name) { + // Avoiding to handle empty service names + if (empty($name)) { + return; + } + // Get the Service ID to be deleted. We're asking for a wildcard suffix in + // case multi-language deletion if needed. + $id = commerce_flat_rate_cache_get_id($name, TRUE); + cache_clear_all($id, 'cache', TRUE); +} + + +/** * Implements hook_commerce_shipping_service_info(). */ function commerce_flat_rate_commerce_shipping_service_info() { + global $language; $services = array(); + $use_translations = module_exists('i18n_string'); // Get the default service name directly to avoid a recursive loop. It's ok in // this function if the service no longer exists. @@ -156,55 +232,76 @@ function commerce_flat_rate_commerce_shipping_service_info() { if (!empty($result)) { foreach ($result as $name => $service) { - // Create a base rate price array for the service - $base_rate = array( - 'amount' => $service['amount'], - 'currency_code' => $service['currency_code'], - 'data' => array(), - ); - // Unserialize the data array for the service. - $data = !empty($service['data']) ? unserialize($service['data']) : array(); + // First try to fetch the service from our cache. + $cached_service = commerce_flat_rate_cache_get($name); + if ($cached_service) { + $services[$name] = $cached_service; + } + else { + // Create a base rate price array for the service + $base_rate = array( + 'amount' => $service['amount'], + 'currency_code' => $service['currency_code'], + 'data' => array(), + ); - $price_component_type = 'flat_rate_' . $name; + // Unserialize the data array for the service. + $data = !empty($service['data']) ? unserialize($service['data']) : array(); - // If the data array specifies an included tax, include it now. - if (module_exists('commerce_tax') && !empty($data['include_tax']) && $tax_rate = commerce_tax_rate_load($data['include_tax'])) { - // Reverse apply the tax. - $tax_amount = $base_rate['amount'] - ($base_rate['amount'] / (1 + $tax_rate['rate'])); - $tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount); + $price_component_type = 'flat_rate_' . $name; - // Add a base price to the data array. - $component = array( - 'amount' => $base_rate['amount'] - $tax_amount, - 'currency_code' => $base_rate['currency_code'], - 'data' => array(), - ); + // If the data array specifies an included tax, include it now. + if (module_exists('commerce_tax') && !empty($data['include_tax']) && $tax_rate = commerce_tax_rate_load($data['include_tax'])) { + // Reverse apply the tax. + $tax_amount = $base_rate['amount'] - ($base_rate['amount'] / (1 + $tax_rate['rate'])); + $tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount); - $base_rate['data'] = commerce_price_component_add($base_rate, $price_component_type, $component, TRUE, FALSE); + // Add a base price to the data array. + $component = array( + 'amount' => $base_rate['amount'] - $tax_amount, + 'currency_code' => $base_rate['currency_code'], + 'data' => array(), + ); - // Add the tax to the data array. - $component['amount'] = $tax_amount; - $component['data']['tax_rate'] = $tax_rate; + $base_rate['data'] = commerce_price_component_add($base_rate, $price_component_type, $component, TRUE, FALSE); - $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); - } + // Add the tax to the data array. + $component['amount'] = $tax_amount; + $component['data']['tax_rate'] = $tax_rate; - // Add the full service array to our return value. - $services[$name] = array( - 'title' => $service['title'], - 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], - 'description' => $service['description'], - 'shipping_method' => 'flat_rate', - 'rules_component' => !empty($service['rules_component']), - 'price_component' => $price_component_type, - 'callbacks' => array( - 'rate' => 'commerce_flat_rate_service_rate_order', - ), - 'base_rate' => $base_rate, - 'weight' => $service['weight'], - 'data' => $data, - ); + $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); + } + + $untranslated = array( + 'title' => $service['title'], + 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], + 'description' => $service['description'], + ); + + if ($use_translations) { + $service = i18n_object('commerce_flat_rate', $service)->translate($language->language); + } + // Add the full service array to our return value. + $services[$name] = array( + 'title' => $service['title'], + 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], + 'description' => $service['description'], + 'shipping_method' => 'flat_rate', + 'rules_component' => !empty($service['rules_component']), + 'price_component' => $price_component_type, + 'callbacks' => array( + 'rate' => 'commerce_flat_rate_service_rate_order', + ), + 'base_rate' => $base_rate, + 'weight' => $service['weight'], + 'data' => $data, + 'untranslated_strings' => $untranslated, + ); + + // Save the newly created service to our cache. + commerce_flat_rate_cache_set($name, $services[$name]); + } // Add the base rate, sort order, and default status to the service's // description on the admin overview page. @@ -219,6 +316,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { $services[$name]['description'] .= '
' . $extra_description . '
'; } + } } @@ -322,6 +420,9 @@ function commerce_flat_rate_service_delete($name, $skip_reset = FALSE) { rules_config_delete(array('commerce_shipping_service_' . $name)); + // Delete the service from our cache. + commerce_flat_rate_cache_delete($name); + // Clear the necessary caches and rebuild the menu items. if (!$skip_reset) { commerce_shipping_services_reset(); @@ -403,3 +504,12 @@ function commerce_flat_rate_form_commerce_checkout_form_alter(&$form, &$form_sta } } } + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function commerce_flat_rate_form_i18n_string_locale_translate_edit_form_alter(&$form, &$form_state) { + // Inject our submit function to be able to clear each service cache as soon + // as any of its properties got translated. + $form['#submit'][] = 'commerce_flat_rate_i18n_string_locale_translate_edit_form_submit'; +} diff --git a/includes/commerce_flat_rate.admin.inc b/includes/commerce_flat_rate.admin.inc index c61e605..997528f 100644 --- a/includes/commerce_flat_rate.admin.inc +++ b/includes/commerce_flat_rate.admin.inc @@ -15,6 +15,16 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) // Store the initial shipping service in the form state. $form_state['shipping_service'] = $shipping_service; + $defaults = array( + 'untranslated_strings' => array( + 'title' => '', + 'display_title' => '', + 'description' => '' + ) + ); + + $shipping_service += $defaults; + $form['flat_rate'] = array( '#tree' => TRUE, ); @@ -22,7 +32,7 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) $form['flat_rate']['title'] = array( '#type' => 'textfield', '#title' => t('Title'), - '#default_value' => $shipping_service['title'], + '#default_value' => $shipping_service['untranslated_strings']['title'], '#description' => t('The administrative title of this flat rate. It is recommended that this title begin with a capital letter and contain only letters, numbers, and spaces.'), '#required' => TRUE, '#size' => 32, @@ -53,8 +63,8 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) $form['flat_rate']['display_title'] = array( '#type' => 'textfield', - '#title' => t('Display title'), - '#default_value' => $shipping_service['display_title'], + '#title' => t('Display title', array(), array('context' => 'title for display purposes')), + '#default_value' => $shipping_service['untranslated_strings']['display_title'], '#description' => t('The front end display title of this flat rate shown to customers. Leave blank to default to the Title from above.'), '#size' => 32, ); @@ -63,7 +73,7 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) '#type' => 'textarea', '#title' => t('Description'), '#description' => t('Describe this flat rate if necessary. The text will be displayed in the flat rate services overview table.'), - '#default_value' => $shipping_service['description'], + '#default_value' => $shipping_service['untranslated_strings']['description'], '#rows' => 3, ); @@ -233,6 +243,14 @@ function commerce_flat_rate_service_form_submit($form, &$form_state) { else { drupal_set_message(t('Flat rate service saved.')); $form_state['redirect'] = 'admin/commerce/config/shipping/services/flat-rate'; + + // Update string translations. + if (module_exists('i18n_string')) { + i18n_string_object_update('commerce_flat_rate', $shipping_service); + } + + // Delete the service from our cache, it will be created as soon as needed. + commerce_flat_rate_cache_delete($shipping_service['name']); } } @@ -322,6 +340,7 @@ function commerce_flat_rate_service_make_default_form($form, &$form_state, $name function commerce_flat_rate_service_make_default_form_submit($form, &$form_state) { $name = $form_state['values']['service_name']; commerce_flat_rate_set_default_service($name); + menu_rebuild(); drupal_set_message(t('%title is now the default flat rate shipping service.', array('%title' => commerce_shipping_service_get_title($name))));