-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuottawa_lang_editor.french_cleanup.inc
250 lines (230 loc) · 8.4 KB
/
uottawa_lang_editor.french_cleanup.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* @file
* Holds the UOttawaLanguageEditor_FrenchCleanup class, which is responsible
* for cleaning up references in the French side of a dual-language form.
*/
/**
* Provides a static interface for cleaning up various properties of
* the French version of form elements.
*/
class UOttawaLanguageEditor_FrenchCleanup {
/**
* Stores tokens to check javascript against
*
* Note that JS token searching is done using
* strpos($value, $token) !== false
*
* Format is $token => $replacement
*
* @var array
*/
private $js_tokens = array();
/**
* Stores tokens to check classes against
*
* Class checking is absolute ($token == $value)
*
* Format is $token => $replacement
*
* @var array
*/
private $class_tokens = array();
/**
* Stores tokens to check states against
*
* State checking is absolute ($token == $value)
*
* Format is $token => $replacement
*
* @var array
*/
private $state_tokens = array();
/**
* Stores tokens to check validation methods against
*
* Validation method checking is absolute ($token == $value)
*
* Format is $token => $replacement
*
* @var array
*/
private $validation_tokens = array();
/**
* Static instance of this class (singleton pattern)
* @var UOttawaLanguageEditor_FrenchCleanup
*/
private static $instance = NULL;
/**
* Obtains the static instance of this class (singleton pattern)
* @return UOttawaLanguageEditor_FrenchCleanup
*/
public static function i() {
if (self::$instance == NULL) {
self::$instance = new UOttawaLanguageEditor_FrenchCleanup();
}
return self::$instance;
}
/**
* Private constructor (singleton pattern)
*/
private function __construct() {
$this->init_js_tokens();
$this->init_class_tokens();
$this->init_state_tokens();
$this->init_validation_tokens();
}
/**
* Initializes the tokens to check validation input against
*
* You should add additional tokens to match validation elements here, in
* the format:
* validation_function_name => replacement_validation_function_name
*/
private function init_validation_tokens() {
// this validation method causes problems, so we've replaced it completely
$this->validation_tokens["path_form_element_validate"] = "uottawa_lang_editor_french_path_form_element_validate";
}
/**
* Initializes the tokens to check javascript files against
*
* You should add additional tokens to match validation elements here, in
* the format:
* identifying_js_file_fragment => path_to_replacement_js_file
*/
private function init_js_tokens() {
$lang_editor_path = drupal_get_path("module", "uottawa_lang_editor");
$this->js_tokens["/node/node.js"] = $lang_editor_path . "/node.js";
$this->js_tokens["/path/path.js"] = $lang_editor_path . "/path.js";
$this->js_tokens["/pathauto/pathauto.js"] = $lang_editor_path . "/pathauto.js";
$this->js_tokens["/menu/menu.js"] = $lang_editor_path . "/menu.js";
$this->js_tokens["/metatag/metatag.vertical-tabs.js"] = $lang_editor_path . "/metatag.vertical-tabs.js";
}
/**
* Initializes the tokens to check classes against
*
* You should add additional tokens to match classes here, in the format:
* class_name => replacement_class_name
*/
private function init_class_tokens() {
$this->class_tokens["node-form-revision-information"] = "node-form-revision-information-french";
$this->class_tokens["node-form-author"] = "node-form-author-french";
$this->class_tokens["node-form-options"] = "node-form-options-french";
$this->class_tokens["path-form"] = "path-form-french";
$this->class_tokens["menu-link-form"] = "menu-link-form-french";
$this->class_tokens["metatags-form"] = "metatags-form-french";
}
/**
* Initializes the tokens to check state values against
*
* You should add additional tokens to match state values here, in the
* format:
* original_jquery => replacement_query
*/
private function init_state_tokens() {
$this->state_tokens['textarea[name="log"]'] = 'textarea[name="log_french"]';
$this->state_tokens['input[name="path[pathauto]"]'] = 'input[name="path_french[pathauto_french]"]';
$this->state_tokens['input[name="menu[enabled]"]'] = 'input[name="menu_french[enabled_french]"]';
}
/**
* Checks a condition based on two operands and the $mode
* @param mixed $left The first operand
* @param mixed $right The second operand
* @param string $mode The identifying mode to compare $left and $right with
* @return boolean True if they match, otherwise false
*/
private function check_condition($left, $right, $mode = "equals") {
switch ($mode) {
case "contains":
return strpos($left, $right) !== FALSE;
default:
case "equals":
return $left == $right;
}
}
/**
* Cleans up $data by replacing the tokens in $tokenArray with their
* replacements
*
* @param array $tokenArray An associative array of replacement values
* @param array $data The data to find replacements for
* @param string $mode The comparison mode to be used
* @param boolean $targetValues If true, checks for the tokens in the array
* values of $data. Otherwise, checks for the tokens in the keys of $data.
* @return array The cleaned up version of $data
*/
private function cleanup_tokens(&$tokenArray, $data, $mode = "equals", $targetValues = TRUE) {
if (!is_array($data)) {
$data = array($data);
}
$new_data = array();
foreach ($data as $key => $val) {
$found = FALSE;
foreach ($tokenArray as $token => $replacement) {
// we only want to replace things once
if (!$found) {
// if we're looking for values
if ($targetValues) {
// we don't want to process arrays of values
if (!is_array($val)) {
// check the condition
if ($this->check_condition($val, $token, $mode)) {
// do the replacement
$found = TRUE;
$new_data[$key] = $replacement;
}
}
// if we're looking for keys
}
else {
// check the condition
if ($this->check_condition($key, $token, $mode)) {
// do the replacement
$found = TRUE;
$new_data[$replacement] = $val;
}
}
}
}
// if we didn't do a replacement, keep the original
if (!$found) {
$new_data[$key] = $val;
}
}
// return
return $new_data;
}
/**
* Cleans up validation information for a french element
* @param array $data The array of validation elements
* @return array Cleaned up version
*/
public function cleanup_validation_tokens($data) {
return $this->cleanup_tokens($this->validation_tokens, $data, "equals");
}
/**
* Cleans up state information for a french element
* @param array $data The array of state elements (note this is the list of
* actual jQuery => condition elements)
* @return array Cleaned up version
*/
public function cleanup_state_tokens($data) {
return $this->cleanup_tokens($this->state_tokens, $data, "equals", FALSE);
}
/**
* Cleans up js file information for a french element
* @param array $data The array of js files
* @return array Cleaned up version
*/
public function cleanup_js_tokens($data) {
return $this->cleanup_tokens($this->js_tokens, $data, "contains");
}
/**
* Cleans up class information for a french element
* @param array $data The array of classes
* @return array Cleaned up version
*/
public function cleanup_class_tokens($data) {
return $this->cleanup_tokens($this->class_tokens, $data, "equals");
}
}