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