1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Admin; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\LeftAndMain; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Dev\SapphireInfo; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\Security\Member; |
12
|
|
|
use SilverStripe\Security\Permission; |
13
|
|
|
use SilverStripe\Security\Security; |
14
|
|
|
use SilverStripe\View\ArrayData; |
15
|
|
|
use SilverStripe\View\SSViewer; |
16
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Allows workflow definitions to be exported from one SilverStripe install, ready for import into another. |
20
|
|
|
* |
21
|
|
|
* YAML is used for export as it's native to SilverStripe's config system and we're using {@link WorkflowTemplate} |
22
|
|
|
* for some of the import-specific heavy lifting, which is already heavily predicated on YAML. |
23
|
|
|
* |
24
|
|
|
* @todo |
25
|
|
|
* - If workflow-def is created badly, the "update template definition" logic, sometimes doesn't work |
26
|
|
|
* |
27
|
|
|
* @author [email protected] |
28
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
29
|
|
|
* @package advancedworkflow |
30
|
|
|
*/ |
31
|
|
|
class WorkflowDefinitionExporter |
32
|
|
|
{ |
33
|
|
|
use Configurable; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The base filename of the file to the exported |
37
|
|
|
* |
38
|
|
|
* @config |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private static $export_filename_prefix = 'workflow-definition-export'; |
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var Member |
45
|
|
|
*/ |
46
|
|
|
protected $member; |
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @var WorkflowDefinition |
50
|
|
|
*/ |
51
|
|
|
protected $workflowDefinition; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @param number $definitionID |
56
|
|
|
* @return void |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function __construct($definitionID) |
59
|
|
|
{ |
60
|
|
|
$this->setMember(Security::getCurrentUser()); |
|
|
|
|
61
|
|
|
$this->workflowDefinition = DataObject::get_by_id(WorkflowDefinition::class, $definitionID); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @param Member $member |
67
|
|
|
*/ |
68
|
|
|
public function setMember($member) |
69
|
|
|
{ |
70
|
|
|
$this->member = $member; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return WorkflowDefinition |
75
|
|
|
*/ |
76
|
|
|
public function getDefinition() |
77
|
|
|
{ |
78
|
|
|
return $this->workflowDefinition; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Runs the export |
83
|
|
|
* |
84
|
|
|
* @return string $template |
85
|
|
|
*/ |
86
|
|
|
public function export() |
87
|
|
|
{ |
88
|
|
|
// Disable any access to use of WorkflowExport if user has no SecurityAdmin access |
89
|
|
|
if (!Permission::check('CMS_ACCESS_SecurityAdmin')) { |
90
|
|
|
throw Exception(_t('SilverStripe\\ErrorPage\\ErrorPage.CODE_403', '403 - Forbidden'), 403); |
91
|
|
|
} |
92
|
|
|
$def = $this->getDefinition(); |
93
|
|
|
$templateData = new ArrayData(array( |
94
|
|
|
'ExportMetaData' => $this->ExportMetaData(), |
95
|
|
|
'ExportActions' => $def->Actions(), |
|
|
|
|
96
|
|
|
'ExportUsers' => $def->Users(), |
|
|
|
|
97
|
|
|
'ExportGroups' => $def->Groups() |
|
|
|
|
98
|
|
|
)); |
99
|
|
|
return $this->format($templateData); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Format the exported data as YAML. |
104
|
|
|
* |
105
|
|
|
* @param ArrayData $templateData |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function format($templateData) |
109
|
|
|
{ |
110
|
|
|
$viewer = SSViewer::execute_template(['type' => 'Includes', 'WorkflowDefinitionExport'], $templateData); |
|
|
|
|
111
|
|
|
// Temporary until we find the source of the replacement in SSViewer |
112
|
|
|
$processed = str_replace('&', '&', $viewer); |
113
|
|
|
// Clean-up newline "gaps" that SSViewer leaves behind from the placement of template control structures |
114
|
|
|
return preg_replace("#^\R+|^[\t\s]*\R+#m", '', $processed); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the size of the current export in bytes. |
119
|
|
|
* Used for pushing data to the browser to prompt for download |
120
|
|
|
* |
121
|
|
|
* @param string $str |
122
|
|
|
* @return number $bytes |
123
|
|
|
*/ |
124
|
|
|
public function getExportSize($str) |
125
|
|
|
{ |
126
|
|
|
return mb_strlen($str, 'UTF-8'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Generate template vars for metadata |
131
|
|
|
* |
132
|
|
|
* @return ArrayData |
133
|
|
|
*/ |
134
|
|
|
public function ExportMetaData() |
135
|
|
|
{ |
136
|
|
|
$def = $this->getDefinition(); |
137
|
|
|
return new ArrayData(array( |
138
|
|
|
'ExportHost' => preg_replace("#http(s)?://#", '', Director::protocolAndHost()), |
139
|
|
|
'ExportDate' => date('d/m/Y H-i-s'), |
140
|
|
|
'ExportUser' => $this->member->FirstName . ' ' . $this->member->Surname, |
141
|
|
|
'ExportVersionFramework' => $this->ssVersion(), |
142
|
|
|
'ExportWorkflowDefName' => $this->processTitle($def->Title), |
|
|
|
|
143
|
|
|
'ExportRemindDays' => $def->RemindDays, |
|
|
|
|
144
|
|
|
'ExportSort' => $def->Sort |
|
|
|
|
145
|
|
|
)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Try different ways of obtaining the current SilverStripe version for YAML output. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
private function ssVersion() |
154
|
|
|
{ |
155
|
|
|
// Remove colons so they don't screw with YAML parsing |
156
|
|
|
$versionSapphire = str_replace(':', '', singleton(SapphireInfo::class)->Version()); |
157
|
|
|
$versionLeftMain = str_replace(':', '', singleton(LeftAndMain::class)->CMSVersion()); |
158
|
|
|
if ($versionSapphire != _t('SilverStripe\\Admin\\LeftAndMain.VersionUnknown', 'Unknown')) { |
159
|
|
|
return $versionSapphire; |
160
|
|
|
} |
161
|
|
|
return $versionLeftMain; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function processTitle($title) |
165
|
|
|
{ |
166
|
|
|
// If an import is exported and re-imported, the new export date is appended to Title, |
167
|
|
|
// making for a very long title |
168
|
|
|
return preg_replace("#\s[\d]+\/[\d]+\/[\d]+\s[\d]+-[\d]+-[\d]+(\s[\d]+)?#", '', $title); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Prompt the client for file download. |
173
|
|
|
* We're "overriding" SS_HTTPRequest::send_file() for more robust cross-browser support |
174
|
|
|
* |
175
|
|
|
* @param array $filedata |
176
|
|
|
* @return HTTPResponse $response |
177
|
|
|
*/ |
178
|
|
|
public function sendFile($filedata) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$response = new HTTPResponse($filedata['body']); |
181
|
|
|
if (preg_match("#MSIE\s(6|7|8)?\.0#", $_SERVER['HTTP_USER_AGENT'])) { |
182
|
|
|
// IE headers |
183
|
|
|
$response->addHeader("Cache-Control", "public"); |
184
|
|
|
$response->addHeader("Content-Disposition", "attachment; filename=\"" . basename($filedata['name']) . "\""); |
185
|
|
|
$response->addHeader("Content-Type", "application/force-download"); |
186
|
|
|
$response->addHeader("Content-Type", "application/octet-stream"); |
187
|
|
|
$response->addHeader("Content-Type", "application/download"); |
188
|
|
|
$response->addHeader("Content-Type", $filedata['mime']); |
189
|
|
|
$response->addHeader("Content-Description", "File Transfer"); |
190
|
|
|
$response->addHeader("Content-Length", $filedata['size']); |
191
|
|
|
} else { |
192
|
|
|
// Everyone else |
193
|
|
|
$response->addHeader( |
194
|
|
|
"Content-Type", |
195
|
|
|
$filedata['mime'] . "; name=\"" . addslashes($filedata['name']) . "\"" |
196
|
|
|
); |
197
|
|
|
$response->addHeader("Content-disposition", "attachment; filename=" . addslashes($filedata['name'])); |
198
|
|
|
$response->addHeader("Content-Length", $filedata['size']); |
199
|
|
|
} |
200
|
|
|
return $response; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.