Projet

Général

Profil

Paste
Télécharger (4,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_i18n / rules_i18n.module @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules i18n integration.
6
 */
7

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function rules_i18n_rules_ui_menu_alter(&$items, $base_path, $base_count) {
12

    
13
  $items[$base_path . '/manage/%rules_config/edit'] = array(
14
    'title' => 'Edit',
15
    'type' => MENU_DEFAULT_LOCAL_TASK,
16
    'weight' => -100,
17
  );
18

    
19
  // For reaction-rules i18n generates the menu items, for the others we provide
20
  // further i18n menu items for all other base paths.
21

    
22
  if ($base_path != 'admin/config/workflow/rules/reaction') {
23

    
24
    $items[$base_path . '/manage/%rules_config/translate'] = array(
25
      'title' => 'Translate',
26
      'page callback' => 'i18n_page_translate_localize',
27
      'page arguments' => array('rules_config', $base_count + 1),
28
      'access callback' => 'i18n_object_translate_access',
29
      'access arguments' => array('rules_config', $base_count + 1),
30
      'type' => MENU_LOCAL_TASK,
31
      'file' => 'i18n.pages.inc',
32
      'file path' => drupal_get_path('module', 'i18n'),
33
      'weight' => 10,
34
    );
35

    
36
    $items[$base_path . '/manage/%rules_config/translate/%i18n_language'] = array(
37
      'title' => 'Translate',
38
      'page callback' => 'i18n_page_translate_localize',
39
      'page arguments' => array('rules_config', $base_count + 1, $base_count + 3),
40
      'access callback' => 'i18n_object_translate_access',
41
      'access arguments' => array('rules_config', $base_count),
42
      'type' => MENU_CALLBACK,
43
      'file' => 'i18n.pages.inc',
44
      'file path' => drupal_get_path('module', 'i18n'),
45
      'weight' => 10,
46
    );
47
  }
48
}
49

    
50
/**
51
 * Implements hook_entity_info_alter().
52
 */
53
function rules_i18n_entity_info_alter(&$info) {
54
  // Enable i18n support via the entity API.
55
  $info['rules_config']['i18n controller class'] = 'RulesI18nStringController';
56
}
57

    
58
/**
59
 * Implements hook_rules_config_insert().
60
 */
61
function rules_i18n_rules_config_insert($rules_config) {
62
  // Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
63
  // @see rules_i18n_rules_config_defaults_rebuild()
64
  if (!empty($rules_config->is_rebuild)) {
65
    return;
66
  }
67

    
68
  i18n_string_object_update('rules_config', $rules_config);
69
}
70

    
71
/**
72
 * Implements hook_rules_config_update().
73
 */
74
function rules_i18n_rules_config_update($rules_config, $original = NULL) {
75
  // Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
76
  // @see rules_i18n_rules_config_defaults_rebuild()
77
  if (!empty($rules_config->is_rebuild)) {
78
    return;
79
  }
80
  $original = $original ? $original : $rules_config->original;
81

    
82
  // Account for name changes.
83
  if ($original->name != $rules_config->name) {
84
    i18n_string_update_context("rules:rules_config:{$original->name}:*", "rules:rules_config:{$rules_config->name}:*");
85
  }
86

    
87
  // We need to remove the strings of any disappeared properties, i.e. strings
88
  // from translatable parameters of deleted actions.
89

    
90
  // i18n_object() uses a static cache per config, so bypass it to wrap the
91
  // original entity.
92
  $object_key = i18n_object_key('rules_config', $original);
93
  $old_i18n_object = new RulesI18nStringObjectWrapper('rules_config', $object_key, $original);
94
  $old_strings = $old_i18n_object->get_strings(array('empty' => TRUE));
95

    
96
  // Note: For the strings to have updated values, the updated entity needs to
97
  // be handled last due to i18n's cache.
98
  $strings = i18n_object('rules_config', $rules_config)->get_strings(array('empty' => TRUE));
99

    
100
  foreach (array_diff_key($old_strings, $strings) as $name => $string) {
101
    $string->remove(array('empty' => TRUE));
102
  }
103
  // Now update the remaining strings.
104
  foreach ($strings as $string) {
105
    $string->update(array('empty' => TRUE, 'update' => TRUE));
106
  }
107
}
108

    
109
/**
110
 * Implements hook_rules_config_delete().
111
 */
112
function rules_i18n_rules_config_delete($rules_config) {
113
  // Only react on real delete, not revert.
114
  if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
115
    i18n_string_object_remove('rules_config', $rules_config);
116
  }
117
}
118

    
119
/**
120
 * Implements hook_rules_config_defaults_rebuild().
121
 */
122
function rules_i18n_rules_config_defaults_rebuild($rules_configs, $originals) {
123
  // Once all defaults have been rebuilt, update all i18n strings at once. That
124
  // way we build the rules cache once the rebuild is complete and avoid
125
  // rebuilding caches for each updated rule.
126
  foreach ($rules_configs as $name => $rule_config) {
127
    if (empty($originals[$name])) {
128
      rules_i18n_rules_config_insert($rule_config);
129
    }
130
    else {
131
      rules_i18n_rules_config_update($rule_config, $originals[$name]);
132
    }
133
  }
134
}