1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace midcom\datamanager\storage\container; |
7
|
|
|
|
8
|
|
|
use midcom_core_dbaobject; |
9
|
|
|
use midcom\datamanager\schema; |
10
|
|
|
use midcom\datamanager\storage\transientnode; |
11
|
|
|
use midcom\datamanager\storage\node; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Experimental storage baseclass |
15
|
|
|
*/ |
16
|
|
|
class dbacontainer extends container |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var midcom_core_dbaobject |
21
|
|
|
*/ |
22
|
|
|
private $object; |
23
|
|
|
|
24
|
3 |
|
public function __construct(schema $schema, midcom_core_dbaobject $object, array $defaults) |
25
|
|
|
{ |
26
|
3 |
|
$this->object = $object; |
27
|
3 |
|
$this->schema = $schema; |
28
|
|
|
|
29
|
3 |
|
foreach ($this->schema->get_fields() as $name => $config) { |
30
|
3 |
|
if (array_key_exists($name, $defaults)) { |
31
|
|
|
$config['default'] = $defaults[$name]; |
32
|
|
|
} |
33
|
3 |
|
$config['name'] = $name; |
34
|
3 |
|
$field = $this->prepare_field($config); |
35
|
3 |
|
if ( isset($config['default']) |
36
|
3 |
|
&& !$this->object->id) { |
37
|
|
|
$field->set_value($config['default']); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$this->fields[$name] = $field; |
41
|
|
|
} |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function get_value() |
48
|
|
|
{ |
49
|
|
|
return $this->object; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function set_value($object) |
56
|
|
|
{ |
57
|
|
|
$this->object = $object; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @param array $config |
63
|
|
|
* @return node |
64
|
|
|
*/ |
65
|
3 |
|
private function prepare_field(array $config) |
66
|
|
|
{ |
67
|
3 |
|
if ( empty($config['storage']['location']) |
68
|
|
|
// This line is needed because a parameter default is set by the schema parser and then ignored |
69
|
|
|
// by the type. The things we do for backwards compatibility... |
70
|
3 |
|
|| $config['storage']['location'] === 'parameter') { |
71
|
3 |
|
if (class_exists('midcom\datamanager\storage\\' . $config['type'])) { |
72
|
1 |
|
$classname = 'midcom\datamanager\storage\\' . $config['type']; |
73
|
2 |
|
} elseif (strtolower($config['storage']['location']) === 'parameter') { |
74
|
2 |
|
$classname = 'midcom\datamanager\storage\parameter'; |
75
|
|
|
} else { |
76
|
3 |
|
return new transientnode($config); |
77
|
|
|
} |
78
|
|
|
} elseif (strtolower($config['storage']['location']) === 'metadata') { |
79
|
|
|
$classname = 'midcom\datamanager\storage\metadata'; |
80
|
|
|
} elseif (strtolower($config['storage']['location']) === 'privilege') { |
81
|
|
|
$classname = 'midcom\datamanager\storage\privilege'; |
82
|
|
|
} else { |
83
|
3 |
|
$classname = 'midcom\datamanager\storage\property'; |
84
|
|
|
} |
85
|
|
|
return new $classname($this->object, $config); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
public function save() |
89
|
1 |
|
{ |
90
|
1 |
|
if ($this->object->id) { |
91
|
1 |
|
$stat = $this->object->update(); |
92
|
|
|
} elseif ($stat = $this->object->create()) { |
93
|
2 |
|
$this->object->set_parameter('midcom.helper.datamanager2', 'schema_name', $this->schema->get_name()); |
94
|
|
|
} |
95
|
|
|
if (!$stat) { |
96
|
|
|
if (\midcom_connection::get_error() === MGD_ERR_ACCESS_DENIED) { |
97
|
|
|
throw new \midcom_error_forbidden('Failed to save: ' . \midcom_connection::get_error_string()); |
98
|
|
|
} |
99
|
|
|
throw new \midcom_error('Failed to save: ' . \midcom_connection::get_error_string()); |
100
|
2 |
|
} |
101
|
2 |
|
|
102
|
|
|
foreach ($this->fields as $node) { |
103
|
2 |
|
$node->save(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|