tmgmt_test.plugin.source.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Contains the test source plugin.
  5. */
  6. class TMGMTTestSourcePluginController extends TMGMTDefaultSourcePluginController {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public function getUri(TMGMTJobItem $job_item) {
  11. // Provide logic which allows to test for source which is either accessible
  12. // or not accessible to anonymous user. This is may then be used to test if
  13. // the source url is attached to the job comment sent to a translation
  14. // service.
  15. $path = 'node';
  16. if ($job_item->item_type == 'test_not_accessible') {
  17. $path = 'admin';
  18. }
  19. return array('path' => $path, 'options' => array());
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getLabel(TMGMTJobItem $job_item) {
  25. $label = $this->pluginType . ':' . $job_item->item_type . ':' . $job_item->item_id;
  26. // We need to test if job and job item labels get properly truncated,
  27. // therefore in case the job item type is "test_with_long_label" we append
  28. // further text to the existing label.
  29. if ($job_item->item_type == 'test_with_long_label') {
  30. $label .= 'Some very long and boring label that definitely exceeds hundred and twenty eight characters which is the maximum character count for the job item label.';
  31. }
  32. return $label;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getData(TMGMTJobItem $job_item) {
  38. // Allow tests to set custom source data.
  39. $source = variable_get('tmgmt_test_source_data', array(
  40. 'dummy' => array(
  41. 'deep_nesting' => array(
  42. '#text' => 'Text for job item with type @type and id @id.',
  43. '#label' => 'Label for job item with type @type and id @id.',
  44. ),
  45. ),
  46. ));
  47. $variables = array(
  48. '@type' => $job_item->item_type,
  49. '@id' => $job_item->item_id,
  50. );
  51. $this->replacePlaceholders($source, $variables);
  52. return $source;
  53. }
  54. /**
  55. * Will replace placeholders in the #text offsets.
  56. *
  57. * @param array $data
  58. * Data structures where to replace placeholders.
  59. * @param $variables
  60. * Key value pairs.
  61. */
  62. protected function replacePlaceholders(&$data, $variables) {
  63. foreach (element_children($data) as $key) {
  64. if (isset($data[$key]['#text'])) {
  65. $data[$key]['#text'] = format_string($data[$key]['#text'], $variables);
  66. }
  67. else {
  68. $this->replacePlaceholders($data[$key], $variables);
  69. }
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function saveTranslation(TMGMTJobItem $job_item) {
  76. // Set a variable that can be checked later for a given job item.
  77. variable_set('tmgmt_test_saved_translation_' . $job_item->item_type . '_' . $job_item->item_id, TRUE);
  78. $job_item->accepted();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getExistingLangCodes(TMGMTJobItem $job_item) {
  84. return array_keys(language_list());
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getSourceLangCode(TMGMTJobItem $job_item) {
  90. $source_languages = variable_get('tmgmt_test_source_languages', array());
  91. if (isset($source_languages[$job_item->tjiid])) {
  92. return $source_languages[$job_item->tjiid];
  93. }
  94. return 'en';
  95. }
  96. }