1 | <?php |
||
15 | class WorkflowDefinitionExporter { |
||
|
|||
16 | |||
17 | /** |
||
18 | * The base filename of the file to the exported |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | public static $export_filename_prefix = 'workflow-definition-export'; |
||
23 | /** |
||
24 | * |
||
25 | * @var \Member |
||
26 | */ |
||
27 | protected $member; |
||
28 | /** |
||
29 | * |
||
30 | * @var \WorkflowDefinition |
||
31 | */ |
||
32 | protected $workflowDefinition; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @param number $definitionID |
||
37 | * @return void |
||
38 | */ |
||
39 | public function __construct($definitionID) { |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * @param \Member $member |
||
47 | */ |
||
48 | public function setMember($member) { |
||
51 | |||
52 | /** |
||
53 | * @return \WorkflowDefinition |
||
54 | */ |
||
55 | public function getDefinition() { |
||
58 | |||
59 | /** |
||
60 | * Runs the export |
||
61 | * |
||
62 | * @return string $template |
||
63 | */ |
||
64 | public function export() { |
||
65 | // Disable any access to use of WorkflowExport if user has no SecurityAdmin access |
||
66 | if(!Permission::check('CMS_ACCESS_SecurityAdmin')) { |
||
67 | throw Exception(_t('ErrorPage.403'), 403); |
||
68 | } |
||
69 | $def = $this->getDefinition(); |
||
70 | $templateData = new ArrayData(array( |
||
71 | 'ExportMetaData' => $this->ExportMetaData(), |
||
72 | 'ExportActions' => $def->Actions(), |
||
73 | 'ExportUsers' => $def->Users(), |
||
74 | 'ExportGroups' => $def->Groups() |
||
75 | )); |
||
76 | return $this->format($templateData); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Format the exported data as YAML. |
||
81 | * |
||
82 | * @param \ArrayData $templateData |
||
83 | * @return void |
||
84 | */ |
||
85 | public function format($templateData) { |
||
86 | $viewer = SSViewer::execute_template('WorkflowDefinitionExport', $templateData); |
||
87 | // Temporary until we find the source of the replacement in SSViewer |
||
88 | $processed = str_replace('&', '&', $viewer); |
||
89 | // Clean-up newline "gaps" that SSViewer leaves behind from the placement of template control structures |
||
90 | return preg_replace("#^\R+|^[\t\s]*\R+#m", '', $processed); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns the size of the current export in bytes. |
||
95 | * Used for pushing data to the browser to prompt for download |
||
96 | * |
||
97 | * @param string $str |
||
98 | * @return number $bytes |
||
99 | */ |
||
100 | public function getExportSize($str) { |
||
103 | |||
104 | /** |
||
105 | * Generate template vars for metadata |
||
106 | * |
||
107 | * @return ArrayData |
||
108 | */ |
||
109 | public function ExportMetaData() { |
||
110 | $def = $this->getDefinition(); |
||
111 | return new ArrayData(array( |
||
112 | 'ExportHost' => preg_replace("#http(s)?://#", '', Director::protocolAndHost()), |
||
113 | 'ExportDate' => date('d/m/Y H-i-s'), |
||
114 | 'ExportUser' => $this->member->FirstName.' '.$this->member->Surname, |
||
115 | 'ExportVersionFramework' => $this->ssVersion(), |
||
116 | 'ExportWorkflowDefName' => $this->processTitle($def->Title), |
||
117 | 'ExportRemindDays' => $def->RemindDays, |
||
118 | 'ExportSort' => $def->Sort |
||
119 | )); |
||
120 | } |
||
121 | |||
122 | /* |
||
123 | * Try different ways of obtaining the current SilverStripe version for YAML output. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | private function ssVersion() { |
||
128 | // Remove colons so they don't screw with YAML parsing |
||
129 | $versionSapphire = str_replace(':', '', singleton('SapphireInfo')->Version()); |
||
130 | $versionLeftMain = str_replace(':', '', singleton('LeftAndMain')->CMSVersion()); |
||
131 | if($versionSapphire != _t('LeftAndMain.VersionUnknown')) { |
||
132 | return $versionSapphire; |
||
133 | } |
||
134 | return $versionLeftMain; |
||
135 | } |
||
136 | |||
137 | private function processTitle($title) { |
||
141 | |||
142 | /** |
||
143 | * Prompt the client for file download. |
||
144 | * We're "overriding" SS_HTTPRequest::send_file() for more robust cross-browser support |
||
145 | * |
||
146 | * @param array $filedata |
||
147 | * @return \SS_HTTPResponse $response |
||
148 | */ |
||
149 | public function sendFile($filedata) { |
||
170 | |||
171 | } |
||
172 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.