flack /
openpsa
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
||||||
| 4 | */ |
||||||
| 5 | |||||||
| 6 | namespace midcom\datamanager; |
||||||
| 7 | |||||||
| 8 | use Symfony\Component\Form\FormFactoryInterface; |
||||||
| 9 | use Symfony\Component\Form\Form; |
||||||
| 10 | use midcom_core_dbaobject; |
||||||
| 11 | use midcom_core_context; |
||||||
| 12 | use midcom; |
||||||
| 13 | use midcom\datamanager\extension\transformer\multipleTransformer; |
||||||
| 14 | use midcom\datamanager\storage\recreateable; |
||||||
| 15 | use midcom\datamanager\extension\type\schemaType; |
||||||
| 16 | use midcom\datamanager\extension\type\toolbarType; |
||||||
| 17 | use midcom\datamanager\storage\container\container; |
||||||
| 18 | use Symfony\Component\Form\FormBuilderInterface; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * Experimental datamanager class |
||||||
| 22 | */ |
||||||
| 23 | class datamanager |
||||||
| 24 | { |
||||||
| 25 | private schemadb $schemadb; |
||||||
| 26 | |||||||
| 27 | private ?schema $schema = null; |
||||||
| 28 | |||||||
| 29 | private ?container $storage = null; |
||||||
| 30 | |||||||
| 31 | private array $defaults = []; |
||||||
| 32 | |||||||
| 33 | private ?renderer $renderer = null; |
||||||
| 34 | |||||||
| 35 | private ?Form $form = null; |
||||||
| 36 | |||||||
| 37 | 177 | public function __construct(schemadb $schemadb) |
|||||
| 38 | { |
||||||
| 39 | 177 | $this->schemadb = $schemadb; |
|||||
| 40 | } |
||||||
| 41 | |||||||
| 42 | 154 | private function get_factory() : FormFactoryInterface |
|||||
| 43 | { |
||||||
| 44 | 154 | return midcom::get()->getContainer()->get('form.factory'); |
|||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | 81 | public static function from_schemadb(string $path) : self |
|||||
| 48 | { |
||||||
| 49 | 81 | return new static(schemadb::from_path($path)); |
|||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | 44 | public function set_defaults(array $defaults) : self |
|||||
| 53 | { |
||||||
| 54 | 44 | $this->defaults = $defaults; |
|||||
| 55 | 44 | return $this; |
|||||
| 56 | } |
||||||
| 57 | |||||||
| 58 | 172 | public function set_storage(?midcom_core_dbaobject $storage = null, ?string $schemaname = null) : self |
|||||
| 59 | { |
||||||
| 60 | 172 | if ( $schemaname === null |
|||||
| 61 | 172 | && !empty($storage->id)) { |
|||||
| 62 | 74 | $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | 172 | $this->set_schema($schemaname); |
|||||
| 66 | |||||||
| 67 | 172 | $defaults = array_merge($this->schema->get_defaults(), $this->defaults); |
|||||
| 68 | 172 | if ($storage === null) { |
|||||
| 69 | 15 | $this->storage = new storage\container\nullcontainer($this->schema, $defaults); |
|||||
| 70 | } else { |
||||||
| 71 | 157 | $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults); |
|||||
| 72 | } |
||||||
| 73 | |||||||
| 74 | 172 | if ($this->form !== null) { |
|||||
| 75 | 16 | if ($this->form->isSubmitted()) { |
|||||
| 76 | 13 | $this->form = null; |
|||||
| 77 | } else { |
||||||
| 78 | 3 | $this->form->setData($this->storage); |
|||||
| 79 | } |
||||||
| 80 | } |
||||||
| 81 | |||||||
| 82 | 172 | return $this; |
|||||
| 83 | } |
||||||
| 84 | |||||||
| 85 | 172 | private function set_schema(?string $name) |
|||||
| 86 | { |
||||||
| 87 | 172 | if ($name && !$this->schemadb->has($name)) { |
|||||
| 88 | debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO); |
||||||
| 89 | $name = null; |
||||||
| 90 | } |
||||||
| 91 | |||||||
| 92 | 172 | $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first(); |
|||||
| 93 | 172 | if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) { |
|||||
| 94 | $this->form = null; |
||||||
| 95 | } |
||||||
| 96 | 172 | $this->schema = $schema; |
|||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | 160 | public function get_schema(?string $name = null) : schema |
|||||
| 100 | { |
||||||
| 101 | 160 | if ($name) { |
|||||
| 102 | 1 | return $this->schemadb->get($name); |
|||||
| 103 | } |
||||||
| 104 | 160 | if ($this->schema === null) { |
|||||
| 105 | 17 | $this->set_schema($name); |
|||||
| 106 | } |
||||||
| 107 | 160 | return $this->schema; |
|||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | 158 | public function get_storage() : container |
|||||
| 111 | { |
||||||
| 112 | 158 | if (!$this->storage) { |
|||||
| 113 | 14 | $this->set_storage(null); |
|||||
| 114 | } |
||||||
| 115 | 158 | return $this->storage; |
|||||
| 116 | } |
||||||
| 117 | |||||||
| 118 | 150 | public function get_renderer($template = null, bool $skip_empty = false) : renderer |
|||||
| 119 | { |
||||||
| 120 | 150 | if ($this->renderer === null) { |
|||||
| 121 | 150 | $this->renderer = new renderer(new engine); |
|||||
| 122 | 150 | $this->renderer->set_l10n($this->get_schema()->get_l10n()); |
|||||
|
0 ignored issues
–
show
The method
set_l10n() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 123 | } |
||||||
| 124 | 150 | if ($template) { |
|||||
| 125 | 150 | if (is_string($template)) { |
|||||
| 126 | 150 | $templates = $this->get_schema()->get('templates'); |
|||||
| 127 | 150 | if (!array_key_exists($template, $templates)) { |
|||||
| 128 | throw new \midcom_error('Template ' . $template . ' not found in config'); |
||||||
| 129 | } |
||||||
| 130 | 150 | $template = new $templates[$template]($this->renderer, $skip_empty); |
|||||
| 131 | } |
||||||
| 132 | 150 | $view = $this->get_form()->createView(); |
|||||
| 133 | 150 | $this->renderer->set_template($view, $template); |
|||||
| 134 | } |
||||||
| 135 | 150 | return $this->renderer; |
|||||
| 136 | } |
||||||
| 137 | |||||||
| 138 | 108 | public function get_controller(?string $name = null) : controller |
|||||
| 139 | { |
||||||
| 140 | 108 | return new controller($this, $name); |
|||||
| 141 | } |
||||||
| 142 | |||||||
| 143 | 154 | public function get_form(?string $name = null, bool $reset = false) : Form |
|||||
| 144 | { |
||||||
| 145 | 154 | if ($reset) { |
|||||
| 146 | $this->form = null; |
||||||
| 147 | } |
||||||
| 148 | |||||||
| 149 | 154 | if ( $this->form === null |
|||||
| 150 | 154 | || ($name && $this->form->getName() != $name)) { |
|||||
| 151 | 152 | $this->build_form($this->get_builder($name)); |
|||||
| 152 | } |
||||||
| 153 | 154 | return $this->form; |
|||||
| 154 | } |
||||||
| 155 | |||||||
| 156 | /** |
||||||
| 157 | * Get FormBuilder for manipulating the form before it gets locked by setData(). |
||||||
| 158 | * The builder instance needs to be passed back to build_form() for the modifications |
||||||
| 159 | * to have a consistent effect. |
||||||
| 160 | */ |
||||||
| 161 | 154 | public function get_builder(?string $name = null) : FormBuilderInterface |
|||||
| 162 | { |
||||||
| 163 | 154 | $config = [ |
|||||
| 164 | 154 | 'schema' => $this->get_schema() |
|||||
| 165 | 154 | ]; |
|||||
| 166 | 154 | $builder = $this->get_factory()->createNamedBuilder($this->get_name($name), schemaType::class, options: $config); |
|||||
| 167 | 154 | $builder->add('form_toolbar', toolbarType::class, [ |
|||||
| 168 | 154 | 'operations' => $this->schema->get('operations'), |
|||||
|
0 ignored issues
–
show
The method
get() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 169 | 154 | 'index_method' => 'noindex' |
|||||
| 170 | 154 | ]); |
|||||
| 171 | 154 | return $builder; |
|||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | 154 | public function build_form(FormBuilderInterface $builder) : self |
|||||
| 175 | { |
||||||
| 176 | 154 | $this->form = $builder->getForm() |
|||||
| 177 | 154 | ->setData($this->get_storage()); |
|||||
| 178 | |||||||
| 179 | 154 | return $this; |
|||||
| 180 | } |
||||||
| 181 | |||||||
| 182 | 154 | private function get_name(?string $name) : string |
|||||
| 183 | { |
||||||
| 184 | 154 | if (!$name && $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT)) { |
|||||
| 185 | // Replace the dots in the component name with underscores |
||||||
| 186 | 147 | $name = midcom::get()->componentloader->path_to_prefix($name); |
|||||
| 187 | } |
||||||
| 188 | 154 | return $name ?: 'midcom_helper_datamanager2'; |
|||||
| 189 | } |
||||||
| 190 | |||||||
| 191 | 10 | public function get_content_raw() : array |
|||||
| 192 | { |
||||||
| 193 | 10 | $ret = []; |
|||||
| 194 | |||||||
| 195 | 10 | foreach ($this->storage as $field => $value) |
|||||
| 196 | { |
||||||
| 197 | 10 | $ret[$field] = $value->get_value(); |
|||||
| 198 | 10 | $config = $this->schema->get_field($field); |
|||||
|
0 ignored issues
–
show
It seems like
$field can also be of type null; however, parameter $name of midcom\datamanager\schema::get_field() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 199 | 10 | if (!empty($config['type_config']['allow_multiple'])) { |
|||||
| 200 | 1 | $transformer = new multipleTransformer($config); |
|||||
| 201 | 1 | $ret[$field] = $transformer->transform($ret[$field]); |
|||||
| 202 | } |
||||||
| 203 | } |
||||||
| 204 | |||||||
| 205 | 10 | return $ret; |
|||||
| 206 | } |
||||||
| 207 | |||||||
| 208 | 4 | public function get_content_csv() : array |
|||||
| 209 | { |
||||||
| 210 | 4 | return $this->render('csv'); |
|||||
| 211 | } |
||||||
| 212 | |||||||
| 213 | 40 | public function get_content_html() : array |
|||||
| 214 | { |
||||||
| 215 | 40 | return $this->render('view'); |
|||||
| 216 | } |
||||||
| 217 | |||||||
| 218 | 46 | public function render(string $type) : array |
|||||
| 219 | { |
||||||
| 220 | 46 | $ret = []; |
|||||
| 221 | |||||||
| 222 | 46 | $renderer = $this->get_renderer($type); |
|||||
| 223 | 46 | foreach ($renderer->get_view()->children as $name => $value) { |
|||||
| 224 | 46 | if ($name == 'form_toolbar') { |
|||||
| 225 | 46 | continue; |
|||||
| 226 | } |
||||||
| 227 | 46 | $ret[$name] = $renderer->widget($value); |
|||||
| 228 | } |
||||||
| 229 | 46 | return $ret; |
|||||
| 230 | } |
||||||
| 231 | |||||||
| 232 | 13 | public function display_view(bool $skip_empty = false) |
|||||
| 233 | { |
||||||
| 234 | 13 | $renderer = $this->get_renderer('view', $skip_empty); |
|||||
| 235 | 13 | echo $renderer->block($renderer->get_view(), 'form'); |
|||||
| 236 | } |
||||||
| 237 | |||||||
| 238 | public function recreate() : bool |
||||||
| 239 | { |
||||||
| 240 | $ret = true; |
||||||
| 241 | foreach ($this->storage as $field) { |
||||||
| 242 | if ( $field instanceof recreateable |
||||||
| 243 | && !$field->recreate()) { |
||||||
| 244 | $ret = false; |
||||||
| 245 | } |
||||||
| 246 | } |
||||||
| 247 | return $ret; |
||||||
| 248 | } |
||||||
| 249 | } |
||||||
| 250 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.