-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProcessMigrations.module
228 lines (192 loc) · 6.16 KB
/
ProcessMigrations.module
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
<?php
/**
* Migrations
*
*
* @author Benjamin Milde
*
* ProcessWire 2.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class ProcessMigrations extends Process {
protected $migrations = null;
/**
* @return Migrations
*/
protected function m() {
if(!is_null($this->migrations)) return $this->migrations;
$this->migrations = $this->wire('modules')->get('Migrations');
return $this->migrations;
}
private function arrayToLinks($links)
{
array_walk($links, function(&$i, $k){
$i = "<a href='$i' target='_blank'>$k</a>";
});
return $links;
}
private function arrayToList($list)
{
return array_reduce($list, function($c, $item){
return $c . "<li>$item</li>";
}, '<ul>') . '</ul>';
}
public function ___execute() {
$table = $this->modules->get("MarkupAdminDataTable");
$table->setSortable(false);
$table->setEncodeEntities(false);
$table->headerRow(array(
$this->_("Filename"),
$this->_("Description"),
$this->_("Action"),
$this->_("Status"),
));
try {
$files = $this->m()->getMigrations();
} catch (WireException $e) {
$files = array();
$this->error($e->getMessage());
}
if(!count($files)){
// Onboarding
$button = $this->createNewButton();
$button->addClass('head_button_clone');
$button->addClass('space_after');
$out = "<p>" . $this->_('It seems like the module couldn\'t find any migrations. Create a new migrations to see it listed here.') . "</p>";
$out .= $button->render();
$out .= "<p>" . $this->_('If this doesn\'t work please make sure the /site/migrations/ folder does exist and is readable by ProcessWire.') . "</p>";
$out .= "<p>" . $this->_('Getting help:') . "</p>";
$links = array(
$this->_('Forum Support Topic') => 'https://processwire.com/talk/topic/13045-migrations/',
$this->_('GitHub') => 'https://github.com/LostKobrakai/Migrations'
);
$out .= $this->arrayToList($this->arrayToLinks($links));
return $out;
}
$iconMigrate = "<i class='fa fa-play' title='Migrate'></i>";
$iconRollback = "<i class='fa fa-history' title='Rollback'></i>";
foreach ($files as $file) {
/** @var Migrationfile $file */
$data = array();
$data[] = $file->shortname . ' <span style="color: #ddd">' . substr($file->type, 0, 1) . '</span>';
$data[] = nl2br($file->description);
if ($file->migrated) {
$data[$iconRollback] = "revoke/?id=" . md5($file->filename);
$data[] = '<span style="color: limegreen">Migrated</span>';
} else {
$data[$iconMigrate] = "execute/?id=" . md5($file->filename);
$data[] = '<span style="color: #ddd">-</span>';
}
$table->row($data);
}
$button = $this->wire('modules')->get('InputfieldButton');
$button->attr('href', 'migrate/');
$button->value = $this->_('Migrate Latest');
$button->addClass('head_button_clone');
$button->icon = 'play';
$button2 = $this->createNewButton();
return $table->render().$button->render().$button2->render();
}
protected function createNewButton()
{
$button = $this->wire('modules')->get('InputfieldButton');
$button->attr('href', 'create/');
$button->value = $this->_('Create New');
$button->addClass('ui-priority-secondary');
$button->icon = 'plus-circle';
return $button;
}
public function ___executeExecute() {
$file = $this->evaluateID();
try{
$this->m()->migrate($file);
} catch (Exception $e) {
$this->message("Could not migrate $file. Got error " . get_class($e) . " with " . $e->getMessage());
return false;
}
$this->session->redirect($this->page->url);
}
public function ___executeRevoke() {
$file = $this->evaluateID();
try{
$this->m()->rollback($file);
} catch (Exception $e) {
$this->message("Could not rollback $file. Got error " . get_class($e) . " with " . $e->getMessage());
return false;
}
$this->session->redirect($this->page->url);
}
protected function evaluateID() {
if(!$this->input->get->id) $this->session->redirect($this->page->url);
$id = $this->input->get->text("id");
$file = null;
$files = $this->m()->getMigrations();
foreach ($files as $f) {
/** @var Migrationfile $f */
if(md5($f->filename) === $id)
return $f->filename;
}
$this->error("Couldn't find file!");
$this->message($id);
$this->message($files);
$this->session->redirect($this->page->url);
}
public function ___executeMigrate()
{
($files = $this->m()->migrate('*', true))
? $this->message("Ran migration on $files->count latest migrations.")
: $this->message('Nothing to migrate');
$this->session->redirect($this->page->url);
}
public function ___executeCreate()
{
$types = array(
'default' => $this->_('Default Migration'),
'field' => $this->_('Field Migration'),
'template' => $this->_('Template Migration'),
);
if($this->input->post->create){
$type = $this->input->post->text('type');
$desc = $this->input->post->textarea('desc');
if(!in_array($type, array_keys($types))) $this->session->redirect($this->page->url);
try{
$file = $this->m()->createNew($type, array(
'description' => $desc
));
$this->message("Created new migration " . basename($file));
} catch(WireException $e) {
$this->error($e->getMessage());
}
$this->session->redirect($this->page->url);
}
$form = $this->wire('modules')->get('InputfieldForm');
$form->attr('action', '.');
$type = $this->wire('modules')->get('InputfieldSelect');
$type->attr('id+name', 'type');
$type->required = true;
$type->label = $this->_('Type');
$type->description = $this->_('Type of migration.');
foreach ($types as $key => $label) {
$type->addOption($key, $label);
}
$type->columnWidth = 20;
$form->add($type);
$desc = $this->wire('modules')->get('InputfieldTextarea');
$desc->attr('id+name', 'desc');
$desc->label = $this->_('Description');
$desc->description = $this->_('Description for the migration. (optional)');
$desc->columnWidth = 80;
$form->add($desc);
$submit = $this->wire('modules')->get('InputfieldSubmit');
$submit->attr('id+name', 'create');
$submit->value = $this->_('Create');
$submit->icon = 'plus-circle';
$form->add($submit);
return $form->render();
}
}