-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmgmt_zanata.utils.inc
123 lines (112 loc) · 3.06 KB
/
tmgmt_zanata.utils.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @file
* Program Utilities.
*/
/**
* To build TMGMT job item data in more consumable format.
*/
class TmgmtZanataJobItemDataBuilder {
private $node_contents;
/**
* Create a new job item data builder.
*
* @param TMGMTJobItem $job_item
* A single document in this job.
*/
public function __construct(TMGMTJobItem $job_item) {
$flattened_source = tmgmt_flatten_data($job_item->getData());
$this->node_contents = $this->processNodeContents($flattened_source);
}
public function getNodeContents() {
return $this->node_contents;
}
/**
* Map translatable content keys to their formats.
*
* Creates mapping between translatable content keys
* in the order, formats to their values
* example: body][0][format => body][0][value.
*
* @param array $src_msg_keys
* Keys from tmgmt_flatten_data.
*
* @return array
* format to value mappings
*/
private function mapFormatsToValues(array $src_msg_keys) {
$values = array();
$formats = array();
$others = array();
foreach ($src_msg_keys as $key) {
if (strstr($key, 'value')) {
array_push($values, $key);
}
elseif (strstr($key, 'format')) {
array_push($formats, $key);
}
else {
array_push($others, $key);
}
}
$result = array();
foreach ($values as $value) {
foreach ($formats as $format) {
if (strstr($format, rtrim($value, 'value'))) {
$result[$format] = $value;
}
}
}
return array_merge($result, $others);
}
/**
* Process TMGMT flatten data and make it more descriptive.
*
* @param array $flattened_source
* Job item data flattened with tmgmt's helper method.
*
* @return array
* TmgmtZanataNodeFieldContent elements
*/
private function processNodeContents(array $flattened_source) {
$node_contents = array();
foreach ($this->mapFormatsToValues(array_keys($flattened_source)) as $format => $value) {
if (is_int($format)) {
array_push($node_contents, new TmgmtZanataNodeFieldContent($value, $flattened_source[$value]));
}
else {
array_push($node_contents, new TmgmtZanataNodeFieldContent(
$value, $flattened_source[$value], $flattened_source[$format])
);
}
}
return $node_contents;
}
}
/**
* Class TmgmtZanataNodeFieldContent.
*/
class TmgmtZanataNodeFieldContent {
public $key;
public $label;
public $data;
public $isHtml;
public $isTitle;
/**
* Create a new Node Field Content.
*
* @param string $source
* The key for the field.
* @param array $data
* Data containing label and text.
* @param array $format
* Format that describes whether the field has html content.
*/
public function __construct($source, array $data, $format = array('#text' => 'plain_text')) {
$this->key = $source;
$this->label = $data['#label'];
$this->data = $data['#text'];
$this->isHtml = strstr($format['#text'], 'html');
$this->isTitle = $source === 'node_title';
}
}