|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class DatabasebackupLogDetailForm extends GridFieldDetailForm |
|
4
|
|
|
{ |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
class DatabasebackupLogDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest |
|
8
|
|
|
{ |
|
9
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
10
|
|
|
"ItemEditForm" => "ADMIN", |
|
11
|
|
|
"doDownload" => "ADMIN", |
|
12
|
|
|
'doRestoreDatabaseBackup' => "ADMIN" |
|
13
|
|
|
); |
|
14
|
|
|
|
|
15
|
|
|
public function ItemEditForm() |
|
16
|
|
|
{ |
|
17
|
|
|
$form = parent::ItemEditForm(); |
|
18
|
|
|
$actions = $this->record->getCMSActions(); |
|
19
|
|
|
$oldActions = $form->Actions(); |
|
20
|
|
|
foreach ($actions as $action) { |
|
21
|
|
|
$oldActions->push($action); |
|
22
|
|
|
} |
|
23
|
|
|
$form->setActions($oldActions); |
|
|
|
|
|
|
24
|
|
|
return $form; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* run the action (separate method) and send the right message back |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
public function doRestoreDatabaseBackup($data, $form) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
if (!$this->record->ID) { |
|
34
|
|
|
return new SS_HTTPResponse("Please pass an ID in the form content", 400); |
|
35
|
|
|
} |
|
36
|
|
|
$databaseToRestore = DatabasebackupLog::get()->byID($this->record->ID); |
|
37
|
|
|
if (!$databaseToRestore) { |
|
38
|
|
|
return new SS_HTTPResponse("backup #$id not found", 400); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
$outcome = $this->restoreDatabaseBackup($databaseToRestore); |
|
|
|
|
|
|
41
|
|
|
if ($outcome) { |
|
42
|
|
|
$message = _t('Databasebackup.DB_RESTORED', 'Database Restored, please reload the entire site to continue...'); |
|
43
|
|
|
$form->sessionMessage($message, 'good', false); |
|
44
|
|
|
return $this->getToplevelController()->redirectBack(); |
|
45
|
|
|
} else { |
|
46
|
|
|
$message = _t('Databasebackup.DB_NOT_RESTORED', 'Database * NOT * Restored'); |
|
47
|
|
|
} |
|
48
|
|
|
$toplevelController = $this->getToplevelController(); |
|
49
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
|
50
|
|
|
$backForm = $toplevelController->getEditForm(); |
|
51
|
|
|
$backForm->sessionMessage($message, 'good', false); |
|
|
|
|
|
|
52
|
|
|
} else { |
|
53
|
|
|
$form->sessionMessage($message, 'good', false); |
|
54
|
|
|
} |
|
55
|
|
|
//when an item is deleted, redirect to the parent controller |
|
56
|
|
|
$controller = $this->getToplevelController(); |
|
57
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
|
58
|
|
|
return $controller->redirect($this->getBacklink(), 302); //redirect back to admin section |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* downloads the file |
|
64
|
|
|
* @param Array $data |
|
65
|
|
|
* @param Form $form |
|
66
|
|
|
*/ |
|
67
|
|
|
public function doDownload($data, $form) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->record->ID) { |
|
70
|
|
|
return new SS_HTTPResponse("Please pass an ID in the form content", 400); |
|
71
|
|
|
} |
|
72
|
|
|
$databaseToRestore = DatabasebackupLog::get()->byID($this->record->ID); |
|
73
|
|
|
if (!$databaseToRestore) { |
|
74
|
|
|
return new SS_HTTPResponse("backup #$id not found", 400); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
if (!file_exists($databaseToRestore->FullLocation)) { |
|
77
|
|
|
return new SS_HTTPResponse("file #$id not found", 400); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* copies back up files up one ... |
|
83
|
|
|
* returns true on success, false on failure |
|
84
|
|
|
* |
|
85
|
|
|
* @param DatabasebackupLog $databaseToRestore |
|
86
|
|
|
* |
|
87
|
|
|
* @return Boolean |
|
88
|
|
|
*/ |
|
89
|
|
|
private function restoreDatabaseBackup(DatabasebackupLog $databaseToRestore) |
|
90
|
|
|
{ |
|
91
|
|
|
if (Permission::check("ADMIN")) { |
|
92
|
|
|
Config::inst()->update("DatabasebackupLog", "max_db_copies", Config::inst()->get("DatabasebackupLog", "max_db_copies") + 1); |
|
93
|
|
|
//firstly make a backup of the current state ... |
|
94
|
|
|
$obj = new DatabasebackupLog(); |
|
95
|
|
|
$obj->Title = _t("DatabaseBackup.RESTORE_NOTE", "Additional backup before doing a database restore."); |
|
|
|
|
|
|
96
|
|
|
$obj->write(); |
|
97
|
|
|
//make sure it still exists ... |
|
98
|
|
|
$databaseToRestore = DatabasebackupLog::get()->byID($databaseToRestore->ID); |
|
99
|
|
|
if ($databaseToRestore) { |
|
100
|
|
|
return $databaseToRestore->restoreDatabaseBackup(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|