Project

General

Profile

Paste
Download (16.1 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / panels / i18n_panels / i18n_panels.module @ c06bd9a4

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
 *
210
 * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
211
 *
212
 * @see i18n_ctools_render_alter()
213
 *
214
 * @param panels_display $display
215
 *   The display to deal with.
216
 * @param panels_renderer_standard $renderer
217
 *   The renderer to deal with.
218
 */
219
function i18n_panels_panels_pre_render(&$display, $renderer) {
220
  // Avoid double translations.
221
  if (!isset($display->i18n_panels_title_translated)) {
222
    $translation = i18n_string_object_translate('display_configuration', $display);
223
    if (is_array($translation->title)) {
224
      $display->title = $translation->title['string'];
225
    }
226
    else {
227
      $display->title = $translation->title;
228
    }
229
    $display->i18n_panels_title_translated = TRUE;
230
  }
231
}
232

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

    
250

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

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

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

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

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

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

    
346
/**
347
 * Checks if the give display is exported or only stored in the db.
348
 *
349
 * @return boolean
350
 *   TRUE if the display is available from code.
351
 */
352
function _18n_panels_is_exported_panels_display($display) {
353
  if (!isset($display->uuid)) {
354
    return FALSE;
355
  }
356
  $uuid = $display->uuid;
357
  // Check exported displays.
358
  ctools_include('export');
359
  $panels_display_temp = ctools_export_crud_load('panels_display', $uuid);
360
  $panels_mini_temp = ctools_export_crud_load('panels_mini', $uuid);
361
  if (
362
    !empty($panels_display_temp)
363
    || !empty($panels_mini_temp)
364
    || (module_exists('panelizer') && ctools_export_crud_load('panelizer_defaults', $uuid))
365
  ) {
366
    return TRUE;
367
  }
368

    
369
  // Check page manager embedded displays.
370
  // @see _18n_panels_fetch_all_panel_displays()
371
  if (module_exists('page_manager')) {
372
    module_load_include('inc', 'page_manager', 'page_manager.admin');
373
    $tasks = page_manager_get_tasks_by_type('page');
374
    $pages = array('operations' => array(), 'tasks' => array());
375
    page_manager_get_pages($tasks, $pages);
376

    
377
    foreach ($pages['tasks'] as $task) {
378
      $page = page_manager_cache_load($task);
379
      foreach ($page->handler_info as $id => $info) {
380
        $page_manager_handler = $page->handlers[$id];
381
        if ($page_manager_handler->handler == 'panel_context') {
382
          $is_exported = ($page_manager_handler->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE) || (isset($page->subtask['storage']) && $page->subtask['storage'] == t('Overridden')));
383
          if (!empty($page_manager_handler->conf['display'])) {
384
            $panels_display = $page_manager_handler->conf['display'];
385
            if ($panels_display->uuid == $uuid) {
386
              return TRUE;
387
            }
388
          }
389
          elseif ($is_exported && isset($page_manager_handler->conf['did'])) {
390
            $panels_display = panels_load_display($page_manager_handler->conf['did']);
391
            if (isset($panels_display->uuid) && $panels_display->uuid == $uuid) {
392
              return TRUE;
393
            }
394
          }
395
        }
396
      }
397
    }
398
  }
399
  return FALSE;
400
}
401

    
402
/**
403
 * Returns a list of really all available panel displays.
404
 *
405
 * The list is statically cached. Use the parameter $reset to refresh the list
406
 * during the same request.
407
 * Probably returns the same display twice - once with the db based and once
408
 * the exported one.
409
 *
410
 * @todo I bet there are better ways to solve this mess.
411
 *
412
 * @param bool $reset
413
 *   Reset the static cache.
414
 *
415
 * @return array
416
 *   List of all panel displays.
417
 */
418
function _18n_panels_fetch_all_panel_displays($reset = FALSE) {
419
  $displays = &drupal_static(__FUNCTION__, array());
420
  if (!empty($displays) && !$reset) {
421
    return $displays;
422
  }
423

    
424
  // Fetch db based displays.
425
  $dids = db_select('panels_display')->fields('panels_display', array('did'))->execute()->fetchCol();
426
  $displays = panels_load_displays($dids);
427

    
428
  // Fetch exported displays.
429
  ctools_include('export');
430
  foreach (ctools_export_crud_load_all('panels_display') as $panels_display) {
431
    if (!empty($panels_display->uuid)) {
432
      $displays['exported-' . $panels_display->uuid] = $panels_display;
433
    }
434
  }
435

    
436
  // Fetch mini panels.
437
  $mini_panels = ctools_export_crud_load_all('panels_mini');
438
  foreach ($mini_panels as $pane) {
439
    if (!empty($pane->display->uuid)) {
440
      $displays['exported-' . $pane->display->uuid] = $pane->display;
441
    }
442
  }
443

    
444
  // Fetch in page manager embedded displays.
445
  if (module_exists('page_manager')) {
446
    module_load_include('inc', 'page_manager', 'page_manager.admin');
447
    $tasks = page_manager_get_tasks_by_type('page');
448
    $pages = array('operations' => array(), 'tasks' => array());
449
    page_manager_get_pages($tasks, $pages);
450

    
451
    foreach ($pages['tasks'] as $task) {
452
      $page = page_manager_cache_load($task);
453
      $task_info = page_manager_get_task_subtasks($page->task);
454
      foreach ($page->handler_info as $id => $info) {
455
        $page_manager_handler = $page->handlers[$id];
456
        if ($page_manager_handler->handler == 'panel_context') {
457

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

    
461
          if (!empty($page_manager_handler->conf['display'])) {
462
            $panels_display = $page_manager_handler->conf['display'];
463
            $displays['exported-' . $panels_display->uuid] = $panels_display;
464
          }
465
          elseif ($is_exported && isset($page_manager_handler->conf['did'])) {
466
            $panels_display = panels_load_display($page_manager_handler->conf['did']);
467
            if (isset($panels_display->uuid)) {
468
              $displays['exported-' . $panels_display->uuid] = $panels_display;
469
            }
470
          }
471
        }
472
      }
473
    }
474
  }
475

    
476
  // Fetch panelizer displays.
477
  if (module_exists('panelizer')) {
478
    // Fetch all default handlers.
479
    $panelizer_defaults = ctools_export_crud_load_all('panelizer_defaults');
480
    foreach ($panelizer_defaults as $panelizer_default) {
481
      $displays['exported-' . $panelizer_default->display->uuid] = $panelizer_default->display;
482
    }
483
  }
484
  return $displays;
485
}