1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Translate Element Type. |
7
|
|
|
* |
8
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2016, Bob Olde Hampsink |
10
|
|
|
* @license MIT |
11
|
|
|
* |
12
|
|
|
* @link http://github.com/boboldehampsink |
13
|
|
|
*/ |
14
|
|
|
class TranslateElementType extends BaseElementType |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Return element type name. |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function getName() |
22
|
|
|
{ |
23
|
|
|
return Craft::t('Translations'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Return true so we have a locale select menu. |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function isLocalized() |
32
|
|
|
{ |
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return true so we have a status select menu. |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function hasStatuses() |
42
|
|
|
{ |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Define statuses. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getStatuses() |
52
|
|
|
{ |
53
|
|
|
return array( |
54
|
|
|
TranslateModel::DONE => Craft::t('Done'), |
55
|
|
|
TranslateModel::PENDING => Craft::t('Pending'), |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Define available table column names. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function defineAvailableTableAttributes() |
65
|
|
|
{ |
66
|
|
|
return array( |
67
|
|
|
'original' => array('label' => Craft::t('Original')), |
68
|
|
|
'field' => array('label' => Craft::t('Translation')), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the default table attributes. |
74
|
|
|
* |
75
|
|
|
* @param string $source |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getDefaultTableAttributes($source = null) |
80
|
|
|
{ |
81
|
|
|
return array('original', 'field'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Don't encode the attribute html. |
86
|
|
|
* |
87
|
|
|
* @param BaseElementModel $element |
88
|
|
|
* @param string $attribute |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getTableAttributeHtml(BaseElementModel $element, $attribute) |
93
|
|
|
{ |
94
|
|
|
return $element->$attribute; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Define criteria. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function defineCriteriaAttributes() |
103
|
|
|
{ |
104
|
|
|
return array( |
105
|
|
|
'original' => AttributeType::String, |
106
|
|
|
'translation' => AttributeType::String, |
107
|
|
|
'source' => AttributeType::String, |
108
|
|
|
'file' => AttributeType::String, |
109
|
|
|
'status' => array(AttributeType::String, 'default' => TranslateModel::DONE), |
110
|
|
|
'locale' => array(AttributeType::String, 'default' => 'en_us'), |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Cancel the elements query. |
116
|
|
|
* |
117
|
|
|
* @param DbCommand $query |
118
|
|
|
* @param ElementCriteriaModel $criteria |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria) |
123
|
|
|
{ |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create element from row. |
129
|
|
|
* |
130
|
|
|
* @param array $row |
131
|
|
|
* |
132
|
|
|
* @return TranslateModel |
133
|
|
|
*/ |
134
|
|
|
public function populateElementModel($row) |
135
|
|
|
{ |
136
|
|
|
return TranslateModel::populateModel($row); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Define the sources. |
141
|
|
|
* |
142
|
|
|
* @param string $context |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getSources($context = null) |
147
|
|
|
{ |
148
|
|
|
// Get plugin sources |
149
|
|
|
$pluginSources = array(); |
150
|
|
|
$plugins = craft()->plugins->getPlugins(); |
151
|
|
|
foreach ($plugins as $path => $plugin) { |
152
|
|
|
$pluginSources['plugins:'.$path] = array( |
153
|
|
|
'label' => $plugin->classHandle, |
154
|
|
|
'criteria' => array( |
155
|
|
|
'source' => craft()->path->getPluginsPath().$path, |
156
|
|
|
), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Get template sources |
161
|
|
|
$templateSources = array(); |
162
|
|
|
$templates = IOHelper::getFolderContents(craft()->path->getSiteTemplatesPath(), false); |
163
|
|
|
foreach ($templates as $template) { |
164
|
|
|
|
165
|
|
|
// Get path/name of template files and folders |
166
|
|
|
if (preg_match('/(.*)\/(.*?)(\.(html|twig|js|json|atom|rss)|\/)$/', $template, $matches)) { |
167
|
|
|
|
168
|
|
|
// If matches, get template name |
169
|
|
|
$path = $matches[2]; |
170
|
|
|
|
171
|
|
|
// Add template source |
172
|
|
|
$templateSources['templates:'.$path] = array( |
173
|
|
|
'label' => $path, |
174
|
|
|
'criteria' => array( |
175
|
|
|
'source' => $template, |
176
|
|
|
), |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Get default sources |
182
|
|
|
$sources = array( |
183
|
|
|
'*' => array( |
184
|
|
|
'label' => Craft::t('All translations'), |
185
|
|
|
'criteria' => array( |
186
|
|
|
'source' => array( |
187
|
|
|
craft()->path->getPluginsPath(), |
188
|
|
|
craft()->path->getSiteTemplatesPath(), |
189
|
|
|
), |
190
|
|
|
), |
191
|
|
|
), |
192
|
|
|
array('heading' => Craft::t('Default')), |
193
|
|
|
'plugins' => array( |
194
|
|
|
'label' => Craft::t('Plugins'), |
195
|
|
|
'criteria' => array( |
196
|
|
|
'source' => craft()->path->getPluginsPath(), |
197
|
|
|
), |
198
|
|
|
'nested' => $pluginSources, |
199
|
|
|
), |
200
|
|
|
'templates' => array( |
201
|
|
|
'label' => Craft::t('Templates'), |
202
|
|
|
'criteria' => array( |
203
|
|
|
'source' => craft()->path->getSiteTemplatesPath(), |
204
|
|
|
), |
205
|
|
|
'nested' => $templateSources, |
206
|
|
|
), |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
// Get sources by hook |
210
|
|
|
$plugins = craft()->plugins->call('registerTranslateSources'); |
211
|
|
|
if (count($plugins)) { |
212
|
|
|
$sources[] = array('heading' => Craft::t('Custom')); |
213
|
|
|
foreach ($plugins as $plugin) { |
214
|
|
|
|
215
|
|
|
// Add as own source |
216
|
|
|
$sources = array_merge($sources, $plugin); |
217
|
|
|
|
218
|
|
|
// Add to "All translations" |
219
|
|
|
foreach ($plugin as $key => $values) { |
220
|
|
|
$sources['*']['criteria']['source'][] = $values['criteria']['source']; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// Return sources |
226
|
|
|
return $sources; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the html. |
231
|
|
|
* |
232
|
|
|
* @param array $criteria |
233
|
|
|
* @param array $disabledElementIds |
234
|
|
|
* @param array $viewState |
235
|
|
|
* @param string $sourceKey |
236
|
|
|
* @param string $context |
237
|
|
|
* @param bool $includeContainer |
238
|
|
|
* @param bool $showCheckboxes |
239
|
|
|
* |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
public function getIndexHtml($criteria, $disabledElementIds, $viewState, $sourceKey, $context, $includeContainer, $showCheckboxes) |
243
|
|
|
{ |
244
|
|
|
$variables = array( |
245
|
|
|
'viewMode' => $viewState['mode'], |
246
|
|
|
'context' => $context, |
247
|
|
|
'elementType' => new ElementTypeVariable($this), |
248
|
|
|
'disabledElementIds' => $disabledElementIds, |
249
|
|
|
'attributes' => $this->getTableAttributesForSource($sourceKey), |
250
|
|
|
'elements' => craft()->translate->get($criteria), |
251
|
|
|
'showCheckboxes' => $showCheckboxes, |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
// Inject some custom js also |
255
|
|
|
craft()->templates->includeJs("$('table.fullwidth thead th').css('width', '50%');"); |
256
|
|
|
craft()->templates->includeJs("$('.buttons.hidden').removeClass('hidden');"); |
257
|
|
|
|
258
|
|
|
$template = '_elements/'.$viewState['mode'].'view/'.($includeContainer ? 'container' : 'elements'); |
259
|
|
|
|
260
|
|
|
return craft()->templates->render($template, $variables); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|