1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Utility class to facilitate a simple YML-import via the standard CMS ImportForm() logic. |
4
|
|
|
* |
5
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
6
|
|
|
* @package advancedworkflow |
7
|
|
|
*/ |
8
|
|
|
class WorkflowBulkLoader extends BulkLoader { |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @inheritDoc |
12
|
|
|
*/ |
13
|
|
|
public function preview($filepath) { |
14
|
|
|
return $this->processAll($filepath, true); |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $filepath |
19
|
|
|
* @param boolean $preview |
20
|
|
|
*/ |
21
|
|
|
protected function processAll($filepath, $preview = false) { |
22
|
|
|
$results = new BulkLoader_Result(); |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
$yml = singleton('WorkflowDefinitionImporter')->parseYAMLImport($filepath); |
26
|
|
|
$this->processRecord($yml, $this->columnMap, $results, $preview); |
27
|
|
|
return $results; |
28
|
|
|
} catch(ValidationException $e) { |
29
|
|
|
return new BulkLoader_Result(); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $record |
35
|
|
|
* @param array $columnMap |
36
|
|
|
* @param BulkLoader_Result $results |
37
|
|
|
* @param boolean $preview |
38
|
|
|
* @return number |
39
|
|
|
*/ |
40
|
|
|
protected function processRecord($record, $columnMap, &$results, $preview = false) { |
41
|
|
|
$posted = Controller::curr()->getRequest()->postVars(); |
42
|
|
|
$default = WorkflowDefinitionExporter::$export_filename_prefix.'0.yml'; |
43
|
|
|
$filename = (isset($posted['_CsvFile']['name']) ? $posted['_CsvFile']['name'] : $default); |
44
|
|
|
|
45
|
|
|
// @todo is this the best way to extract records (nested array keys)?? |
46
|
|
|
$struct = $record['Injector']['ExportedWorkflow']; |
47
|
|
|
$name = $struct['constructor'][0]; |
48
|
|
|
$import = $this->createImport($name, $filename, $record); |
49
|
|
|
|
50
|
|
|
$template = Injector::inst()->createWithArgs('WorkflowTemplate', $struct['constructor']); |
51
|
|
|
$template->setStructure($struct['properties']['structure']); |
52
|
|
|
|
53
|
|
|
$def = WorkflowDefinition::create(); |
54
|
|
|
$def->workflowService = singleton('WorkflowService'); |
55
|
|
|
$def->Template = $template->getName(); |
|
|
|
|
56
|
|
|
$obj = $def->workflowService->defineFromTemplate($def, $def->Template); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$results->addCreated($obj, ''); |
59
|
|
|
$objID = $obj->ID; |
60
|
|
|
|
61
|
|
|
// Update the import |
62
|
|
|
$import->DefinitionID = $objID; |
|
|
|
|
63
|
|
|
$import->write(); |
64
|
|
|
|
65
|
|
|
$obj->destroy(); |
66
|
|
|
unset($obj); |
67
|
|
|
|
68
|
|
|
return $objID; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create the ImportedWorkflowTemplate record for the uploaded YML file. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $filename |
76
|
|
|
* @param array $record |
77
|
|
|
* @return ImportedWorkflowTemplate $import |
78
|
|
|
*/ |
79
|
|
|
protected function createImport($name, $filename, $record) { |
80
|
|
|
// This is needed to feed WorkflowService#getNamedTemplate() |
81
|
|
|
$import = ImportedWorkflowTemplate::create(); |
82
|
|
|
$import->Name = $name; |
|
|
|
|
83
|
|
|
$import->Filename = $filename; |
|
|
|
|
84
|
|
|
$import->Content = serialize($record); |
|
|
|
|
85
|
|
|
$import->write(); |
86
|
|
|
|
87
|
|
|
return $import; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.