This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
/
I18nWithSlugBehavior.php
358 lines (321 loc) · 9.72 KB
/
I18nWithSlugBehavior.php
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
require_once dirname(__FILE__) . '/I18nWithSlugBehaviorObjectBuilderModifier.php';
require_once dirname(__FILE__) . '/I18nWithSlugBehaviorQueryBuilderModifier.php';
require_once dirname(__FILE__) . '/I18nWithSlugBehaviorPeerBuilderModifier.php';
/**
* Allows translation of text columns through transparent one-to-many relationship
* with slug on the translated object
*
* @author Vlad Jula-Nedelcu <vlad.nedelcu@mfq.ro>
*/
class I18nWithSlugBehavior extends Behavior
{
const DEFAULT_CULTURE = 'en_EN';
// default parameters value
protected $parameters = array(
'i18n_table' => '%TABLE%_i18n',
'i18n_phpname' => '%PHPNAME%I18n',
'i18n_columns' => '',
'culture_column' => 'culture',
'default_culture' => null,
'culture_alias' => '',
'slug_column' => 'slug',
'slug_pattern' => '',
'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
'replacement' => '-',
'separator' => '-',
'permanent' => 'false'
);
protected $tableModificationOrder = 70;
protected
$objectBuilderModifier,
$queryBuilderModifier,
$peerBuilderModifier,
$i18nTable;
public function modifyDatabase()
{
foreach ($this->getDatabase()->getTables() as $table)
{
if ($table->hasBehavior('i18n_with_slug') && !$table->getBehavior('i18n_with_slug')->getParameter('default_culture'))
{
$table->getBehavior('i18n_with_slug')->addParameter(array(
'name' => 'default_culture',
'value' => $this->getParameter('default_culture')
));
}
}
}
public function modifyTable()
{
$this->addI18nTable();
$this->relateI18nTableToMainTable();
$this->addCultureColumnToI18n();
$this->addSlugColumnToI18n();
$this->moveI18nColumns();
// tell the parent table that it has a descendant
$parentBehavior = new I18nWithSlugChildBehavior();
$parentBehavior->setName('i18n_with_slug_child');
$parentBehavior->setParameters(array('slug_column' => $this->getParameter('slug_column')));
$this->i18nTable->addBehavior($parentBehavior);
$parentBehavior->getTableModifier()->modifyTable();
$parentBehavior->setTableModified(true);
}
protected function addI18nTable()
{
$table = $this->getTable();
$database = $table->getDatabase();
$i18nTableName = $this->getI18nTableName();
if ($database->hasTable($i18nTableName))
{
$this->i18nTable = $database->getTable($i18nTableName);
}
else
{
$this->i18nTable = $database->addTable(array(
'name' => $i18nTableName,
'phpName' => $this->getI18nTablePhpName(),
'package' => $table->getPackage(),
'schema' => $table->getSchema(),
'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null,
));
// every behavior adding a table should re-execute database behaviors
foreach ($database->getBehaviors() as $behavior)
{
/** @var $behavior Behavior */
$behavior->modifyDatabase();
}
}
}
protected function relateI18nTableToMainTable()
{
$table = $this->getTable();
$i18nTable = $this->i18nTable;
$pks = $this->getTable()->getPrimaryKey();
if (count($pks) > 1)
{
throw new EngineException('The i18n behavior does not support tables with composite primary keys');
}
foreach ($pks as $column)
{
if (!$i18nTable->hasColumn($column->getName()))
{
$column = clone $column;
$column->setAutoIncrement(false);
$i18nTable->addColumn($column);
}
}
if (in_array($table->getName(), $i18nTable->getForeignTableNames()))
{
return;
}
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::CASCADE);
foreach ($pks as $column)
{
$fk->addReference($column->getName(), $column->getName());
}
$i18nTable->addForeignKey($fk);
}
protected function addCultureColumnToI18n()
{
$cultureColumnName = $this->getCultureColumnName();
if (!$this->i18nTable->hasColumn($cultureColumnName))
{
$this->i18nTable->addColumn(array(
'name' => $cultureColumnName,
'type' => PropelTypes::VARCHAR,
'size' => 5,
'default' => $this->getDefaultCulture(),
'primaryKey' => 'true',
));
}
}
/**
* Moves i18n columns from the main table to the i18n table
*/
protected function moveI18nColumns()
{
$table = $this->getTable();
$i18nTable = $this->i18nTable;
foreach ($this->getI18nColumnNamesFromConfig() as $columnName)
{
if (!$i18nTable->hasColumn($columnName))
{
if (!$table->hasColumn($columnName))
{
throw new EngineException(sprintf('No column named %s found in table %s', $columnName, $table->getName()));
}
$column = $table->getColumn($columnName);
// add the column
$i18nColumn = $i18nTable->addColumn(clone $column);
// add related validators
if ($validator = $column->getValidator())
{
$i18nValidator = $i18nTable->addValidator(clone $validator);
}
// FIXME: also move FKs, and indices on this column
}
if ($table->hasColumn($columnName))
{
$table->removeColumn($columnName);
$table->removeValidatorForColumn($columnName);
}
}
}
protected function addSlugColumnToI18n()
{
if (!$this->i18nTable->hasColumn($this->getParameter('slug_column')))
{
$this->i18nTable->addColumn(array(
'name' => $this->getParameter('slug_column'),
'type' => 'VARCHAR',
'size' => 255
));
// add a unique to column
$unique = new Unique();
$unique->addColumn($this->i18nTable->getColumn($this->getParameter('slug_column')));
$unique->addColumn($this->i18nTable->getColumn($this->getParameter('culture_column')));
$this->i18nTable->addUnique($unique);
}
}
protected function getI18nTableName()
{
return $this->replaceTokens($this->getParameter('i18n_table'));
}
protected function getI18nTablePhpName()
{
return $this->replaceTokens($this->getParameter('i18n_phpname'));
}
protected function getCultureColumnName()
{
return $this->replaceTokens($this->getParameter('culture_column'));
}
protected function getI18nColumnNamesFromConfig()
{
$columnNames = explode(',', $this->getParameter('i18n_columns'));
$columnNames[] = $this->getParameter('slug_column');
foreach ($columnNames as $key => $columnName)
{
if ($columnName = trim($columnName))
{
$columnNames[$key] = $columnName;
}
else
{
unset($columnNames[$key]);
}
}
return $columnNames;
}
public function getDefaultCulture()
{
if (!$defaultCulture = $this->getParameter('default_culture'))
{
$defaultCulture = self::DEFAULT_CULTURE;
}
return $defaultCulture;
}
public function getI18nTable()
{
return $this->i18nTable;
}
public function getI18nForeignKey()
{
foreach ($this->i18nTable->getForeignKeys() as $fk)
{
if ($fk->getForeignTableName() == $this->table->getName())
{
return $fk;
}
}
}
public function getCultureColumn()
{
return $this->getI18nTable()->getColumn($this->getCultureColumnName());
}
public function getSlugColumn()
{
return $this->getI18nTable()->getColumn($this->getParameter('slug_column'));
}
public function getI18nColumns()
{
$columns = array();
$i18nTable = $this->getI18nTable();
if ($columnNames = $this->getI18nColumnNamesFromConfig())
{
// Strategy 1: use the i18n_columns parameter
foreach ($columnNames as $columnName)
{
$columns [] = $i18nTable->getColumn($columnName);
}
}
else
{
// strategy 2: use the columns of the i18n table
// warning: does not work when database behaviors add columns to all tables
// (such as timestampable behavior)
foreach ($i18nTable->getColumns() as $column)
{
if (!$column->isPrimaryKey())
{
$columns [] = $column;
}
}
}
return $columns;
}
public function replaceTokens($string)
{
$table = $this->getTable();
return strtr($string, array(
'%TABLE%' => $table->getName(),
'%PHPNAME%' => $table->getPhpName(),
));
}
public function getObjectBuilderModifier()
{
if (is_null($this->objectBuilderModifier))
{
$this->objectBuilderModifier = new I18nWithSlugBehaviorObjectBuilderModifier($this);
}
return $this->objectBuilderModifier;
}
public function getQueryBuilderModifier()
{
if (is_null($this->queryBuilderModifier))
{
$this->queryBuilderModifier = new I18nWithSlugBehaviorQueryBuilderModifier($this);
}
return $this->queryBuilderModifier;
}
public function getPeerBuilderModifier()
{
if (is_null($this->peerBuilderModifier))
{
$this->peerBuilderModifier = new I18nWithSlugBehaviorPeerBuilderModifier($this);
}
return $this->peerBuilderModifier;
}
/**
* Get the getter of the column of the behavior
*
* @return string The related getter, e.g. 'getSlug'
*/
public function getColumnGetter()
{
return 'get' . $this->i18nTable->getColumn($this->getParameter('slug_column'))->getPhpName();
}
/**
* Get the setter of the column of the behavior
*
* @return string The related setter, e.g. 'setSlug'
*/
public function getColumnSetter()
{
return 'set' . $this->i18nTable->getColumn($this->getParameter('slug_column'))->getPhpName();
}
}