diff --git a/commerce_flat_rate.i18n.inc b/commerce_flat_rate.i18n.inc index 1cc407a..ec062b5 100644 --- a/commerce_flat_rate.i18n.inc +++ b/commerce_flat_rate.i18n.inc @@ -50,3 +50,20 @@ function commerce_flat_rate_i18n_string_objects($type) { ->fetchAllAssoc('name', PDO::FETCH_ASSOC); } } + +/** + * Process i18n string editing form submissions. + * + * Once a new translation has been added, clear the related Flat-Rate service + * from cache. + * + * @see commerce_flat_rate_i18n_string_locale_translate_edit_form_submit() + */ +function commerce_flat_rate_i18n_string_locale_translate_edit_form_submit($form, &$form_state) { + $i18n_string = $form_state['values']['i18n_string']; + if (is_object($i18n_string) && 'commerce_flat_rate' == $i18n_string->textgroup) { + // Remove the Flat Rate service associated to the current translation from + // the cache, it will be rebuild as soon as needed. + commerce_flat_rate_cache_delete($i18n_string->objectid); + } +} diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index a74b90a..8eeaa8c 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -142,11 +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:service:' . $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. @@ -157,65 +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; - $untranslated = array( - 'title' => $service['title'], - 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], - 'description' => $service['description'], - ); + $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); + } - if (module_exists('i18n_string')) { - $service = i18n_object('commerce_flat_rate', $service)->translate($language->language); + $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 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, - ); // Add the base rate, sort order, and default status to the service's // description on the admin overview page. @@ -230,6 +316,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { $services[$name]['description'] .= '
' . $extra_description . '
'; } + } } @@ -333,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(); @@ -414,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 5224ca5..997528f 100644 --- a/includes/commerce_flat_rate.admin.inc +++ b/includes/commerce_flat_rate.admin.inc @@ -248,6 +248,9 @@ function commerce_flat_rate_service_form_submit($form, &$form_state) { 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']); } } @@ -337,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))));