|
1
|
|
|
<?php |
|
2
|
|
|
namespace Itstructure\Mult\Classes; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Database\Migrations\Migration; |
|
5
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
6
|
|
|
use Illuminate\Support\Facades\Schema; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MultilingualMigration |
|
10
|
|
|
* |
|
11
|
|
|
* @package Itstructure\Mult\Classes |
|
12
|
|
|
*/ |
|
13
|
|
|
class MultilingualMigration extends Migration |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Table name to contain the project languages. |
|
17
|
|
|
*/ |
|
18
|
|
|
const LANGUAGE_TABLE_NAME = 'languages'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Primary key name for any table in a system. |
|
22
|
|
|
*/ |
|
23
|
|
|
const PRIMARY_KEY_NAME = 'id'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Creates table with timestamp fields: created_at and updated_at. |
|
27
|
|
|
* @param string $tableName |
|
28
|
|
|
* @param callable $columnsCallback |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function createTableWithTimestamps(string $tableName, callable $columnsCallback): void |
|
32
|
|
|
{ |
|
33
|
|
|
Schema::create($tableName, function (Blueprint $table) use ($columnsCallback) { |
|
34
|
|
|
$columnsCallback($table); |
|
35
|
|
|
$table->timestamps(); |
|
36
|
|
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates two tables: main(primary) table and translate table. |
|
41
|
|
|
* For example: |
|
42
|
|
|
* pages: |
|
43
|
|
|
* - id |
|
44
|
|
|
* - created_at |
|
45
|
|
|
* - updated_at |
|
46
|
|
|
* |
|
47
|
|
|
* pages_languages: |
|
48
|
|
|
* - pages_id |
|
49
|
|
|
* - languages_id |
|
50
|
|
|
* - title |
|
51
|
|
|
* - text |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $tableName - table name which needs to be translated. |
|
54
|
|
|
* @param callable $multilingualColumnsCallback - callback for multilingual fields. |
|
55
|
|
|
* @param callable|null $primaryColumnsCallback - callback for main(primary) fields. |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function createMultilingualTable(string $tableName, callable $multilingualColumnsCallback, callable $primaryColumnsCallback = null): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->createTableWithTimestamps($tableName, function (Blueprint $table) use ($primaryColumnsCallback) { |
|
62
|
|
|
$table->bigIncrements(self::PRIMARY_KEY_NAME)->primaryKey(); |
|
63
|
|
|
if (is_callable($primaryColumnsCallback)) { |
|
64
|
|
|
$primaryColumnsCallback($table); |
|
65
|
|
|
} |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
$keyToPrimaryTable = $this->keyToPrimaryTable($tableName); |
|
69
|
|
|
$keyToLanguageTable = self::keyToLanguageTable(); |
|
70
|
|
|
|
|
71
|
|
|
$translateTableName = $this->translateTableName($tableName); |
|
72
|
|
|
|
|
73
|
|
|
$this->createTableWithTimestamps($translateTableName, function (Blueprint $table) use ($keyToPrimaryTable, $keyToLanguageTable, $multilingualColumnsCallback, $tableName) { |
|
74
|
|
|
$table->unsignedBigInteger($keyToPrimaryTable); |
|
75
|
|
|
$table->unsignedBigInteger($keyToLanguageTable); |
|
76
|
|
|
$table->primary([$keyToPrimaryTable, $keyToLanguageTable]); |
|
77
|
|
|
$multilingualColumnsCallback($table); |
|
78
|
|
|
|
|
79
|
|
|
$table->foreign($keyToPrimaryTable)->references(self::PRIMARY_KEY_NAME)->on($tableName)->onDelete('cascade'); |
|
80
|
|
|
$table->foreign($keyToLanguageTable)->references(self::PRIMARY_KEY_NAME)->on(self::LANGUAGE_TABLE_NAME)->onDelete('cascade'); |
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Drop main table with translate table. |
|
86
|
|
|
* @param string $tableName - main table name. |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function dropMultilingualTable(string $tableName): void |
|
90
|
|
|
{ |
|
91
|
|
|
Schema::dropIfExists($this->translateTableName($tableName)); |
|
92
|
|
|
Schema::dropIfExists($tableName); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns key name for link translate table with languages table. |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function keyToLanguageTable(): string |
|
100
|
|
|
{ |
|
101
|
|
|
return self::LANGUAGE_TABLE_NAME . '_id'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns table name for translates. |
|
106
|
|
|
* @param string $tableName - main(primary) table name. |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
private function translateTableName(string $tableName): string |
|
110
|
|
|
{ |
|
111
|
|
|
return $tableName . '_' . self::LANGUAGE_TABLE_NAME; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns key name for link translate table with main table. |
|
116
|
|
|
* @param string $tableName - main table name. |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
private function keyToPrimaryTable(string $tableName): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $tableName . '_id'; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|