Projet

Général

Profil

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

root / drupal7 / sites / all / modules / panels / i18n_panels / i18n_panels.module @ 5a7e6170

1
<?php
2

    
3
/**
4
 * @file
5
 * Internationalization (i18n) submodule: Panels translation.
6
 */
7

    
8
/**
9
 * Fetch the i18n_settings of the content type if there are any.
10
 *
11
 * @param stdClass $pane
12
 *   The pane to deal with.
13
 *
14
 * @return array|false
15
 *   Settings or FALSE if none are present.
16
 */
17
function i18n_panels_get_i18n_settings($pane) {
18
  ctools_include('content');
19
  $content_type = ctools_get_content_type($pane->type);
20
  if (isset($content_type['i18n_settings'])) {
21
    if (is_string($content_type['i18n_settings']) && function_exists($content_type['i18n_settings'])) {
22
      $content_type['i18n_settings'] = $content_type['i18n_settings']($pane->configuration);
23
    }
24
  }
25
  // Provide the override title string as translation for all panes that have
26
  // this setting enabled.
27
  if (isset($pane->configuration['override_title']) && $pane->configuration['override_title']) {
28
    if (isset($content_type['i18n_settings']) && is_array($content_type['i18n_settings'])) {
29
      $content_type['i18n_settings'][] = 'override_title_text';
30
    }
31
    else {
32
      $content_type['i18n_settings'] = array('override_title_text');
33
    }
34
  }
35
  return isset($content_type['i18n_settings']) ? $content_type['i18n_settings'] : FALSE;
36
}
37

    
38
/**
39
 * Returns the translation object of the pane.
40
 *
41
 * @param stdClass $pane
42
 *   The pane to deal with.
43
 *
44
 * @return stdClass|FALSE
45
 *   Returns FALSE if no translation is necessary.
46
 */
47
function i18n_panels_get_i18n_translation_object($pane) {
48
  $translation_object = array();
49

    
50
  // Handle content type specific i18n settings.
51
  if ($i18n_settings = i18n_panels_get_i18n_settings($pane)) {
52
    // Register translatable settings.
53
    foreach ($i18n_settings as $i18n_setting => $settings) {
54
      if (!is_array($settings)) {
55
        $i18n_setting = $settings;
56
        $settings = array('format' => 'plain_text');
57
      }
58
      $translation_object[$i18n_setting] = NULL;
59
      $key_exists = FALSE;
60
      // Ensure a nested setting is "unpacked".
61
      $config_value = drupal_array_get_nested_value($pane->configuration, explode('|', $i18n_setting), $key_exists);
62
      // If we reached the end of the nested setting use the value as source.
63
      if ($key_exists) {
64
        $translation_object[$i18n_setting] = array(
65
          'string' => $config_value,
66
          'format' => $settings['format'],
67
        );
68
        $translation_object['panels_i18n_settings'][$i18n_setting] = $settings;
69
      }
70
    }
71
  }
72

    
73
  // Check if this pane has a custom title enabled.
74
  if (!empty($pane->configuration['override_title'])) {
75
    $translation_object['title']['string'] = $pane->configuration['override_title_text'];
76
  }
77
  if (!empty($translation_object)) {
78
    return (object) $translation_object;
79
  }
80
  return FALSE;
81
}
82

    
83
/**
84
 * Implements hook_panels_pane_insert().
85
 *
86
 * @param stdClass $pane
87
 *   The pane to deal with.
88
 */
89
function i18n_panels_panels_pane_insert($pane) {
90
  i18n_panels_panels_pane_update($pane);
91
}
92

    
93
/**
94
 * Implements hook_panels_pane_update().
95
 *
96
 * @param stdClass $pane
97
 *   The pane to deal with.
98
 */
99
function i18n_panels_panels_pane_update($pane) {
100
  if ($translation_object = i18n_panels_get_i18n_translation_object($pane)) {
101
    $translation_object->uuid = $pane->uuid;
102
    $status = i18n_string_object_update('pane_configuration', $translation_object);
103
  }
104
}
105

    
106
/**
107
 * Implements hook_panels_pane_delete().
108
 *
109
 * @param array $pids
110
 *   Array with the panel ids to delete.
111
 */
112
function i18n_panels_panels_pane_delete($pids) {
113
  if (!empty($pids)) {
114
    // Fetch the uuids from the db.
115
    $uuids = db_select('panels_pane')
116
      ->fields('panels_pane', array('uuid'))
117
      ->condition('pid', $pids)
118
      ->execute()
119
      ->fetchCol();
120
    foreach ($uuids as $uuid) {
121
      // Create dummy pane with uuid as property.
122
      $pane = (object) array('uuid' => $uuid);
123
      i18n_string_object_remove('pane_configuration', $pane);
124
    }
125
  }
126
}
127

    
128
/**
129
 * Implements hook_panels_pane_prerender().
130
 *
131
 * @param stdClass $pane
132
 *   The pane to deal with.
133
 */
134
function i18n_panels_panels_pane_prerender($pane) {
135
  // Check if this pane has translations.
136
  if (isset($pane->uuid) && $translation_object = i18n_panels_get_i18n_translation_object($pane)) {
137
    $translation_object->uuid = $pane->uuid;
138
    // Send to translation.
139
    $translation_object = i18n_string_object_translate('pane_configuration', $translation_object);
140
    unset($translation_object->uuid, $translation_object->i18n_settings);
141
    foreach ($translation_object as $i18n_setting => $translated_setting) {
142
      if ($i18n_setting != 'panels_i18n_settings') {
143
        if (is_array($translated_setting)) {
144
          $translated_setting = $translated_setting['string'];
145
        }
146
        drupal_array_set_nested_value($pane->configuration, explode('|', $i18n_setting), $translated_setting);
147
      }
148
    }
149
  }
150
}
151

    
152
/**
153
 * Implements hook_panels_display_save().
154
 *
155
 * @param panels_display $display
156
 *   The display to deal with.
157
 */
158
function i18n_panels_panels_display_save($display) {
159
  $status = i18n_string_object_update('display_configuration', $display);
160
}
161

    
162
/**
163
 * Implements hook_panels_display_delete().
164
 *
165
 * @param int $did
166
 *   Id of the display to delete.
167
 */
168
function i18n_panels_panels_delete_display($did) {
169
  // Fetch uuid to delete the translations.
170
  $uuid = db_select('panels_display')
171
    ->fields('panels_display', array('uuid'))
172
    ->condition('did', $did)
173
    ->execute()
174
    ->fetchColumn();
175
  // Build a dummy display.
176
  $display = (object) array('uuid' => $uuid);
177

    
178
  // Check if this display was just saved in the db.
179
  if (!_18n_panels_is_exported_panels_display($display)) {
180
    // If the display was just saved in the db remove all translations.
181
    i18n_string_object_remove('display_configuration', $display);
182
    // Remove related pane translations too.
183
    $pids = db_select('panels_pane')
184
      ->fields('panels_pane', array('pid'))
185
      ->condition('did', $did)
186
      ->execute()
187
      ->fetchCol();
188
    i18n_panels_panels_pane_delete($pids);
189
  }
190
  else {
191
    // If the display is exported leave the translated strings but give the user
192
    // a hint how to clean up.
193
    drupal_set_message(
194
      t(
195
        'The reverted panels display(s) were exported, please run a <a href="!link">string refresh</a> to update the translatable strings.',
196
        array('!link' => url('admin/config/regional/translate/i18n_string'))
197
      ),
198
      'warning',
199
      FALSE
200
    );
201
  }
202
}
203

    
204
/**
205
 * Implements hook_panels_pre_render().
206
 *
207
 * This function must not rely on the passed $renderer parameter. The parameter
208
 * could be empty because this function is reused in i18n_ctools_render_alter().
209
 * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
210
 *
211
 * @see i18n_ctools_render_alter()
212
 *
213
 * @param panels_display $display
214
 *   The display to deal with.
215
 * @param panels_renderer_standard $renderer
216
 *   The renderer to deal with.
217
 */
218
function i18n_panels_panels_pre_render(&$display, $renderer) {
219
  // Avoid double translations.
220
  if (!isset($display->i18n_panels_title_translated)) {
221
    $translation = i18n_string_object_translate('display_configuration', $display);
222
    if (is_array($translation->title)) {
223
      $display->title = $translation->title['string'];
224
    }
225
    else {
226
      $display->title = $translation->title;
227
    }
228
    $display->i18n_panels_title_translated = TRUE;
229
  }
230
}
231

    
232
/**
233
 * Implements hook_ctools_render_alter().
234
 *
235
 * Under some circumstances the title of the panel page is set before
236
 * hook_panels_pre_render() is fired. Such cases can be handled with this hook.
237
 * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
238
 */
239
function i18n_ctools_render_alter(&$info, $page, $context) {
240
  // @todo Find a better way to detect a panels page.
241
  if ($page === TRUE && !empty($info['content']['#display']) && $info['content']['#display'] instanceof panels_display) {
242
    i18n_panels_panels_pre_render($info['content']['#display'], NULL);
243
    // Set the info title. This is used to set the page title.
244
    $info['title'] = $info['content']['#display']->get_title();
245
  }
246
}
247

    
248

    
249
/**
250
 * Implements hook_ctools_plugin_post_alter().
251
 *
252
 * Register some translatable configuration settings for plugins.
253
 *
254
 */
255
function i18n_panels_ctools_plugin_post_alter(&$plugin, $plugin_type_info) {
256
  if ($plugin_type_info['type'] == 'content_types') {
257
    // Modify custom content.
258
    if ($plugin['name'] == 'custom') {
259
      // Register callback to get the translatable settings.
260
      $plugin['i18n_settings'] = 'ctools_custom_content_type_i18n_settings';
261
    }
262
  }
263
}
264

    
265
/**
266
 * Callback to provide the translatable settings appropriate to the config.
267
 *
268
 * @param array $conf
269
 *   Content type configuration.
270
 *
271
 * @return array
272
 *   i18n_settings configuration.
273
 */
274
function ctools_custom_content_type_i18n_settings($conf) {
275
  return array(
276
    'title',
277
    'body' => array('format' => $conf['format']),
278
  );
279
}
280

    
281
/**
282
 * Implements hook_i18n_string_list_TEXTGROUP_alter().
283
 *
284
 * Necessary to support the dynamic translatable settings defined by ctools
285
 * content types.
286
 */
287
function i18n_panels_i18n_string_list_panels_alter(&$strings, $type = NULL, $object = NULL) {
288
  if (isset($object->panels_i18n_settings)) {
289
    foreach ($object->panels_i18n_settings as $i18n_setting => $settings) {
290
      if (isset($object->{$i18n_setting})) {
291
        $strings['panels'][$type][$object->uuid][$i18n_setting] = $object->{$i18n_setting};
292
      }
293
    }
294
  }
295
}
296

    
297
/**
298
 * Implements hook_i18n_string_list().
299
 *
300
 * @todo Figure out a generic solution to fetch exported displays.
301
 */
302
function i18n_panels_i18n_string_list($group) {
303
  $strings = array();
304
  if ($group == 'panels') {
305

    
306
    // Fetch all available displays.
307
    $displays = _18n_panels_fetch_all_panel_displays();
308

    
309
    foreach ($displays as $display) {
310
      if (empty($display->uuid)) {
311
        drupal_set_message(t('The display %display has no uuid, please resave or re-export it.', array('%display' => $display->did)), 'warning');
312
        continue;
313
      }
314
      // Avoid duplicated runs _18n_panels_fetch_all_panel_displays() probably
315
      // returns the same display twice, one for the db based and one for the
316
      // exported one.
317
      if (isset($strings['panels']['display_configuration'][$display->uuid])) {
318
        continue;
319
      }
320
      $strings['panels']['display_configuration'][$display->uuid]['title']['string'] = $display->title;
321
      foreach ($display->content as $pane) {
322
        if (empty($pane->uuid)) {
323
          // Fetch exported uuid and validate it.
324
          $uuid = str_replace('new-', '', $pane->pid);
325
          if (!panels_uuid_is_valid($uuid)) {
326
            drupal_set_message(t('The pane %pane has no uuid, please resave or re-export it.', array('%pane' => $pane->pid)), 'warning');
327
            continue;
328
          }
329
          $pane->uuid = $uuid;
330
        }
331
        if ($translation_object = i18n_panels_get_i18n_translation_object($pane)) {
332
          // Split up all strings and add them to the list.
333
          $pane_strings = (array) $translation_object;
334
          unset($pane_strings['panels_i18n_settings']);
335
          foreach ($pane_strings as $key => $pane_string) {
336
            $strings['panels']['pane_configuration'][$pane->uuid][$key] = $pane_string;
337
          }
338
        }
339
      }
340
    }
341
  }
342
  return $strings;
343
}
344

    
345
/**
346
 * Checks if the give display is exported or only stored in the db.
347
 *
348
 * @return boolean
349
 *   TRUE if the display is available from code.
350
 */
351
function _18n_panels_is_exported_panels_display($display) {
352
  if (isset($display->uuid)) {
353
    $displays = _18n_panels_fetch_all_panel_displays();
354
    return isset($displays['exported-' . $display->uuid]);
355
  }
356
  return FALSE;
357
}
358

    
359
/**
360
 * Returns a list of really all available panel displays.
361
 *
362
 * The list is statically cached. Use the parameter $reset to refresh the list
363
 * during the same request.
364
 * Probably returns the same display twice - once with the db based and once
365
 * the exported one.
366
 *
367
 * @todo I bet there are better ways to solve this mess.
368
 *
369
 * @param boolean $reset
370
 *   Reset the static cache.
371
 *
372
 * @return array
373
 *   List of all panel displays.
374
 */
375
function _18n_panels_fetch_all_panel_displays($reset = FALSE) {
376
  $displays = &drupal_static(__FUNCTION__, array());
377
  if (!empty($displays) && !$reset) {
378
    return $displays;
379
  }
380

    
381
  // Fetch db based displays.
382
  $dids = db_select('panels_display')->fields('panels_display', array('did'))->execute()->fetchCol();
383
  $displays = panels_load_displays($dids);
384

    
385
  // Fetch exported displays.
386
  ctools_include('export');
387
  foreach (ctools_export_crud_load_all('panels_display') as $panels_display) {
388
    if (!empty($panels_display->uuid)) {
389
      $displays['exported-' . $panels_display->uuid] = $panels_display;
390
    }
391
  }
392

    
393
  // Fetch mini panels.
394
  $mini_panels = ctools_export_crud_load_all('panels_mini');
395
  foreach ($mini_panels as $pane) {
396
    if (!empty($pane->display->uuid)) {
397
      $displays['exported-' . $pane->display->uuid] = $pane->display;
398
    }
399
  }
400

    
401
  // Fetch in page manager embedded displays.
402
  if (module_exists('page_manager')) {
403
    module_load_include('inc', 'page_manager', 'page_manager.admin');
404
    $tasks = page_manager_get_tasks_by_type('page');
405
    $pages = array('operations' => array(), 'tasks' => array());
406
    page_manager_get_pages($tasks, $pages);
407

    
408
    foreach ($pages['tasks'] as $task) {
409
      $page = page_manager_cache_load($task);
410
      $task_info = page_manager_get_task_subtasks($page->task);
411
      foreach ($page->handler_info as $id => $info) {
412
        $page_manager_handler = $page->handlers[$id];
413
        if ($page_manager_handler->handler == 'panel_context') {
414

    
415
          // @todo Is there really no better way to check this?
416
          $is_exported = ($page_manager_handler->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE) || (isset($page->subtask['storage']) && $page->subtask['storage'] == t('Overridden')));
417

    
418
          if (!empty($page_manager_handler->conf['display'])) {
419
            $panels_display = $page_manager_handler->conf['display'];
420
            $displays['exported-' . $panels_display->uuid] = $panels_display;
421
          }
422
          elseif ($is_exported && isset($page_manager_handler->conf['did'])) {
423
            $panels_display = panels_load_display($page_manager_handler->conf['did']);
424
            if (isset($panels_display->uuid)) {
425
              $displays['exported-' . $panels_display->uuid] = $panels_display;
426
            }
427
          }
428
        }
429
      }
430
    }
431
  }
432

    
433
  // Fetch panelizer displays.
434
  if (module_exists('panelizer')) {
435
    // Fetch all default handlers.
436
    $panelizer_defaults = ctools_export_crud_load_all('panelizer_defaults');
437
    foreach ($panelizer_defaults as $panelizer_default) {
438
      $displays['exported-' . $panelizer_default->display->uuid] = $panelizer_default->display;
439
    }
440
  }
441
  return $displays;
442
}